FISCO-BCOS Java sdk使用加载账户私钥调用合约

文章 , 技术分享
114 0

FISCO-BCOS Java sdk使用加载账户私钥调用合约

示例合约文件,这里的是设置合约所有者的时候需要进行权限判断,get查询所有者的时候无需进行权限判断,

可以演示用不同的账户进行加载sdk进行调用合约

pragma solidity ^0.4.21;

contract GetandSet {
    address owner;
    
    constructor(){
        owner = msg.sender;
    }
    
    function set(address _a) public returns(bool) {
        require(owner == msg.sender,"owner can do it");
        owner = _a;
        return true;
    }
    function get() view public returns(address){
        return owner;
    }
}

这个是一个公用的方法可以写在service中,简化代码操作

/**
   * 加载用户账户切换业务
   */
  private TransactionResponse getTransactionResponse(String key, String funcName, List<Object> params) throws Exception {
    client = sdkUtils.sdk.getClient(Integer.valueOf("1"));
    client.getCryptoSuite().createKeyPair(key);
    CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair();
    // 过创建和使用AssembleTransactionProcessor对象来调用和查询等操作。不部署合约,那么就不需要复制binary文件
    AssembleTransactionProcessor transactionProcessor = TransactionProcessorFactory
            .createAssembleTransactionProcessor(client, cryptoKeyPair, "src/main/resources/abi/", "");
    // 使用同步方式发送交易
    TransactionResponse transactionResponse = transactionProcessor
            .sendTransactionAndGetResponseByContractLoader("GetandSet", this.address, funcName, params);
    return transactionResponse;
  }

调用方式

public TransactionResponse set(GetandSetSetInputBO input,String key) throws Exception {
    //需要用户进行传入自己的私钥进行调用,设置用户的调用权限
    return getTransactionResponse(key, "set", input.toArgs());
  }

public TransactionResponse get() throws Exception {
    //这里可以使用固定的私钥,也可以传入用户的私钥进行调用
    return getTransactionResponse("2b774babb6da3b4d6f34d4de3b6ec54e867cf5b1c60d8503f12511e36b2fdf7d", "get", Arrays.asList());
  }

完整代码

package org.example.demo.service;

@Service
@NoArgsConstructor
@Data
public class GetandSetService {
  public static final String ABI = org.example.demo.utils.IOUtil.readResourceAsString("abi/GetandSet.abi");

  public static final String BINARY = org.example.demo.utils.IOUtil.readResourceAsString("bin/ecc/GetandSet.bin");

  public static final String SM_BINARY = org.example.demo.utils.IOUtil.readResourceAsString("bin/sm/GetandSet.bin");

  @Value("${system.contract.getandSetAddress}")
  private String address;

  @Autowired
  private SdkUtils sdkUtils;

  @Autowired
  private Client client;


  AssembleTransactionProcessor txProcessor;

  @PostConstruct
  public void init() throws Exception {
    this.txProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(this.client, this.client.getCryptoSuite().getCryptoKeyPair());
  }

  /**
   * 加载用户账户切换业务
   */
  private TransactionResponse getTransactionResponse(String key, String funcName, List<Object> params) throws Exception {
    client = sdkUtils.sdk.getClient(Integer.valueOf("1"));
    client.getCryptoSuite().createKeyPair(key);
    CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair();
    // 过创建和使用AssembleTransactionProcessor对象来调用和查询等操作。不部署合约,那么就不需要复制binary文件
    AssembleTransactionProcessor transactionProcessor = TransactionProcessorFactory
            .createAssembleTransactionProcessor(client, cryptoKeyPair, "src/main/resources/abi/", "");
    // 使用同步方式发送交易
    TransactionResponse transactionResponse = transactionProcessor
            .sendTransactionAndGetResponseByContractLoader("GetandSet", this.address, funcName, params);
    return transactionResponse;
  }

  public TransactionResponse set(GetandSetSetInputBO input,String key) throws Exception {
    return getTransactionResponse(key, "set", input.toArgs());
  }


  public TransactionResponse get() throws Exception {
    return getTransactionResponse("2b774babb6da3b4d6f34d4de3b6ec54e867cf5b1c60d8503f12511e36b2fdf7d", "get", Arrays.asList());
  }

}

将client封装成一个工具类方便进行调用

package org.example.demo.utils;
@Service
public class SdkUtils {
    // 获取配置文件路径
    String configFile = SdkUtils.class.getClassLoader().getResource("config.toml").getFile();
    public BcosSDK sdk =  BcosSDK.build(configFile);
}
最后更新 2023-10-31
评论 ( 0 )
OωO
隐私评论