Search in sources :

Example 1 with ReqTrans

use of com.bsnbase.sdk.entity.req.fiscobcos.ReqTrans in project PCNGateway-Java-SDK by BSNDA.

the class TransactionService method trans.

/**
 * Invoke the smart contract in Public Key Upload Mode
 * When the off-chain business system connects to the BSN gateway, it needs to add the corresponding parameters in the request message according to the interface description. After invoking the gateway, the gateway will return the execution result of the smart contract.
 * In the transaction of public key upload mode, the private key of the on-chain transaction is generated and saved by the user, and then the client will assemble and sign the on-chain data locally, and upload the signed data to the node gateway.
 * The gateway forwards the data to the corresponding blockchain node to initiate the transaction request. In this mode, the ABI of the contract and the contract address are required to assemble the data.
 * The ABI of the contract is obtained by compiling the contract when developing the contract, and the contract address can be obtained from the details page of the participated service.
 * The method to assemble the on-chain data has been implemented in the SDK of the gateway, and it can be called directly.
 */
public static ResTrans trans(ReqTransData reqTransData) throws Exception {
    ReqTrans reqTrans = FiscoTransUtil.buildTrans(reqTransData);
    String api = Config.config.getApi() + PathUtil.FISCOBCOS_NODE_TRANS;
    BaseReqModel<ReqTrans> req = new BaseReqModel<>(reqTrans);
    req.setReqHeader(Config.config.getUserCode(), Config.config.getAppCode());
    HttpService<ReqTrans, ResTrans> httpService = new HttpService();
    BaseResModel<ResTrans> res = httpService.post(req, api, ResTrans.class);
    return res.getBody();
}
Also used : BaseReqModel(com.bsnbase.sdk.entity.base.BaseReqModel) ResTrans(com.bsnbase.sdk.entity.resp.fiscobcos.ResTrans) ReqTrans(com.bsnbase.sdk.entity.req.fiscobcos.ReqTrans) HttpService(com.bsnbase.sdk.util.common.HttpService)

Example 2 with ReqTrans

use of com.bsnbase.sdk.entity.req.fiscobcos.ReqTrans in project PCNGateway-Java-SDK by BSNDA.

the class FiscoTransUtil method buildTrans.

/**
 * 构建交易信息
 */
public static ReqTrans buildTrans(ReqTransData reqTransData) throws Exception {
    // 0:k1 1:sign
    getEncryptTypeByAlgorithmType(Config.config.getAppInfo().getAlgorithmType());
    String abi = reqTransData.getContractAbi();
    String funcName = reqTransData.getFuncName();
    int groupId = Integer.parseInt(Config.config.getAppInfo().getChannelId());
    String contractAddress = reqTransData.getContractAddress();
    List<Object> funcParam = reqTransData.getFuncParam();
    ResGetBlockHeight resGetBlockHeight = NodeService.getBlockHeight();
    if (Objects.isNull(resGetBlockHeight)) {
        throw new GlobalException(ResultInfoEnum.BLOCK_HEIGHT_ERROR);
    }
    Integer blockHeight = Integer.valueOf(resGetBlockHeight.getData());
    BigInteger blockLimit = BigInteger.valueOf(blockHeight + 100);
    String signedStr;
    String encodeTransaction = AbiUtil.transactionAssembleForMethodInvoke(abi, groupId, blockLimit, contractAddress, funcName, funcParam);
    AbiDefinition abiDefinition = AbiUtil.getFunctionAbiDefinition(funcName, abi);
    if (abiDefinition.isConstant()) {
        signedStr = encodeTransaction;
    } else {
        ECKeyPair ecKeyPair = loadKeyPair(reqTransData.getUserName(), Config.config.getAppCode(), Config.config.getMspDir());
        signedStr = AbiUtil.signMessageByEncryptType(encodeTransaction, ecKeyPair, EncryptType.getEncryptType());
    }
    ReqTrans reqTrans = new ReqTrans();
    reqTrans.setTransData(signedStr);
    reqTrans.setContractName(reqTransData.getContractName());
    return reqTrans;
}
Also used : BigInteger(java.math.BigInteger) ReqTrans(com.bsnbase.sdk.entity.req.fiscobcos.ReqTrans) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) ECKeyPair(org.fisco.bcos.web3j.crypto.ECKeyPair) BigInteger(java.math.BigInteger) ResGetBlockHeight(com.bsnbase.sdk.entity.resp.fiscobcos.ResGetBlockHeight) GlobalException(com.bsnbase.sdk.util.exception.GlobalException)

Aggregations

ReqTrans (com.bsnbase.sdk.entity.req.fiscobcos.ReqTrans)2 BaseReqModel (com.bsnbase.sdk.entity.base.BaseReqModel)1 ResGetBlockHeight (com.bsnbase.sdk.entity.resp.fiscobcos.ResGetBlockHeight)1 ResTrans (com.bsnbase.sdk.entity.resp.fiscobcos.ResTrans)1 HttpService (com.bsnbase.sdk.util.common.HttpService)1 GlobalException (com.bsnbase.sdk.util.exception.GlobalException)1 BigInteger (java.math.BigInteger)1 ECKeyPair (org.fisco.bcos.web3j.crypto.ECKeyPair)1 AbiDefinition (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)1