Search in sources :

Example 6 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class TransactionService method getAddFuncData.

/**
 * Get FuncData
 *
 * @param req
 * @return
 */
@NotNull
private static String getAddFuncData(@NotNull ReqKeyUpload req) {
    // params
    List<Type> inputs = new ArrayList<>();
    switch(req.getFuncName()) {
        case "insert":
        case "update":
            inputs.add(new Bytes32(Common.getByte32String(req.getArgs()[0])));
            inputs.add(new DynamicBytes(req.getArgs()[1].getBytes()));
            break;
        case "retrieve":
        case "remove":
            inputs.add(new Bytes32(Common.getByte32String(req.getArgs()[0])));
            break;
        case "keyAtIndex":
            inputs.add(new Uint256(Long.parseLong(req.getArgs()[0])));
            break;
        default:
    }
    if (inputs.size() == 0) {
        throw new GlobalException(ResultInfoEnum.FUNCTION_ERROR);
    }
    // Function
    Function addFunc = new Function(req.getFuncName(), inputs, Collections.emptyList());
    return FunctionEncoder.encode(addFunc);
}
Also used : Function(com.citahub.cita.abi.datatypes.Function) Type(com.citahub.cita.abi.datatypes.Type) DynamicBytes(com.citahub.cita.abi.datatypes.DynamicBytes) ArrayList(java.util.ArrayList) Uint256(com.citahub.cita.abi.datatypes.generated.Uint256) Bytes32(com.citahub.cita.abi.datatypes.generated.Bytes32) GlobalException(com.bsnbase.sdk.util.exception.GlobalException) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class TransactionService method nodeTrans.

/**
 * Invoke chaincode in Public Key Upload Mode
 * <p>
 * When the user of the public key upload mode application needs to initiate a transaction from the off-chain system to the chaincode on the chain, he/she needs to assemble the transaction message locally and call this interface to initiate the transaction.
 */
public static ResKeyEscrowNo nodeTrans(@NotNull ReqKeyEscrow reqkey) throws NoSuchAlgorithmException {
    String api = Config.config.getApi() + PathUtil.FABRIC_NODE_TRANS;
    TransactionUser user = null;
    try {
        user = Config.config.getKeyStore().loadUser(reqkey.getUserName(), Config.config.getAppCode());
        user.setMspId(Config.config.getAppInfo().getMspId());
        AlgorithmTypeEnum algorithmTypeEnum = AlgorithmTypeEnum.fromAlgorithmTypeEnum(Config.config.getAppInfo().getAlgorithmType());
        if (algorithmTypeEnum == AlgorithmTypeEnum.AppAlgorithmType_SM2 && Config.config.getAppInfo().getFabricVersion().equals("2.2.1")) {
            user.setSM3(true);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new GlobalException(ResultInfoEnum.USER_CERTIFICATE_ERROR.getMsg());
    }
    // Assemble the transaction information
    TransactionRequest request = new TransactionRequest();
    request.setChannelId(Config.config.getAppInfo().getChannelId());
    request.setArgs(Common.StringBytesConvert(reqkey.getArgs()));
    request.setTransientMap(reqkey.getTransientData());
    request.setChaincodeId(reqkey.getChainCode());
    request.setFcn(reqkey.getFuncName());
    String transData = null;
    try {
        transData = getTransdata(request, user);
    } catch (Exception e) {
        e.printStackTrace();
        throw new GlobalException(ResultInfoEnum.TRANSACTION_CONVERSION_ERROR.getMsg());
    }
    ReqKeyEscrowNo keyNo = new ReqKeyEscrowNo();
    keyNo.setTransData(transData);
    BaseReqModel<ReqKeyEscrowNo> req = new BaseReqModel<ReqKeyEscrowNo>();
    req.setReqHeader(Config.config.getUserCode(), Config.config.getAppCode());
    req.setBody(keyNo);
    HttpService<ReqKeyEscrowNo, ResKeyEscrowNo> httpService = new HttpService<ReqKeyEscrowNo, ResKeyEscrowNo>();
    BaseResModel<ResKeyEscrowNo> res = httpService.post(req, api, ResKeyEscrowNo.class);
    return res.getBody();
}
Also used : ResKeyEscrowNo(com.bsnbase.sdk.entity.resp.fabric.ResKeyEscrowNo) ReqKeyEscrowNo(com.bsnbase.sdk.entity.req.fabric.ReqKeyEscrowNo) IOException(java.io.IOException) GlobalException(com.bsnbase.sdk.util.exception.GlobalException) TransactionRequest(com.bsnbase.sdk.entity.transactionHeader.TransactionRequest) IOException(java.io.IOException) GlobalException(com.bsnbase.sdk.util.exception.GlobalException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BaseReqModel(com.bsnbase.sdk.entity.base.BaseReqModel) TransactionUser(com.bsnbase.sdk.entity.transactionHeader.TransactionUser) HttpService(com.bsnbase.sdk.util.common.HttpService) AlgorithmTypeEnum(com.bsnbase.sdk.util.enums.AlgorithmTypeEnum)

Example 8 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class TransactionService method nodeTrans.

/**
 * 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.
 * -----------------------------------------------------------------------------------------------------------------
 * Example of default contract method parameters
 * FuncParam: the parameter type of the preset chaincode package is already handled in Service, just pass String.
 * <p>
 * Save data
 * FuncName:insert
 * FuncParam:{"insert1219", "123456"}
 * <p>
 * Remove data
 * FuncName:remove
 * FuncParam:{"insert1219"}
 * <p>
 * Update data
 * FuncName:update
 * FuncParam:{"insert1219", "1234567"}
 * Retrieve data
 * FuncName:retrieve
 * FuncParam:{"insert1219"}
 * <p>
 * Get key by index
 * FuncName:keyAtIndex
 * FuncParam:{"1"}
 * -----------------------------------------------------------------------------------------------------------------
 */
public static ResKeyEscrow nodeTrans(@NotNull ReqKeyUpload req) throws Exception {
    // 1 The user in Key Trust Mode
    if (Config.config.getAppInfo().getCaType() == 1) {
        throw new GlobalException(ResultInfoEnum.FUNCTION_CALL_ERROR);
    }
    String api = Config.config.getApi() + PathUtil.CITA_NODE_TRANS;
    nonce = new Random(System.currentTimeMillis());
    // Get current block height
    ResGetBlockHeight resGetBlockHeight = NodeService.getBlockHeight();
    long blockIndex = 0;
    if (resGetBlockHeight != null && !resGetBlockHeight.getData().isEmpty()) {
        blockIndex = Long.parseLong(resGetBlockHeight.getData()) + valid_until_block;
    } else {
        throw new GlobalException(ResultInfoEnum.BLOCK_HEIGHT_ERROR);
    }
    // Get FuncData
    String addFuncData = getAddFuncData(req);
    // Get the signed transaction
    String rawTx = getRawTx(req, blockIndex, addFuncData);
    // Send transaction
    ReqKeyUploadBody transBody = new ReqKeyUploadBody();
    transBody.setContractName(req.getContractName());
    transBody.setTransData(rawTx);
    BaseReqModel<ReqKeyUploadBody> baseReqModel = new BaseReqModel<>();
    baseReqModel.setReqHeader(Config.config.getUserCode(), Config.config.getAppCode());
    baseReqModel.setBody(transBody);
    HttpService<ReqKeyUploadBody, ResKeyEscrow> httpService = new HttpService<>();
    BaseResModel<ResKeyEscrow> baseRes = httpService.post(baseReqModel, api, ResKeyEscrow.class);
    return baseRes.getBody();
}
Also used : BaseReqModel(com.bsnbase.sdk.entity.base.BaseReqModel) ResKeyEscrow(com.bsnbase.sdk.entity.resp.cita.ResKeyEscrow) Random(java.util.Random) HttpService(com.bsnbase.sdk.util.common.HttpService) ResGetBlockHeight(com.bsnbase.sdk.entity.resp.cita.ResGetBlockHeight) ReqKeyUploadBody(com.bsnbase.sdk.entity.req.cita.ReqKeyUploadBody) GlobalException(com.bsnbase.sdk.util.exception.GlobalException)

Example 9 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException 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)

Example 10 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class FiscoTransUtil method getEncryptTypeByAlgorithmType.

private static EncryptType getEncryptTypeByAlgorithmType(Integer algorithmType) {
    EncryptType encryptType = null;
    AlgorithmTypeEnum algorithmTypeEnum = AlgorithmTypeEnum.fromAlgorithmTypeEnum(algorithmType);
    switch(algorithmTypeEnum) {
        case AppAlgorithmType_SM2:
            encryptType = new EncryptType(1);
            break;
        case AppAlgorithmType_K1:
            encryptType = new EncryptType(0);
            break;
        default:
    }
    if (Objects.isNull(encryptType)) {
        throw new GlobalException(ResultInfoEnum.ALGORITHM_TYPE_ERROR);
    }
    return encryptType;
}
Also used : EncryptType(org.fisco.bcos.web3j.crypto.EncryptType) AlgorithmTypeEnum(com.bsnbase.sdk.util.enums.AlgorithmTypeEnum) GlobalException(com.bsnbase.sdk.util.exception.GlobalException)

Aggregations

GlobalException (com.bsnbase.sdk.util.exception.GlobalException)15 AlgorithmTypeEnum (com.bsnbase.sdk.util.enums.AlgorithmTypeEnum)5 IOException (java.io.IOException)5 JsonException (javax.json.JsonException)3 BaseReqModel (com.bsnbase.sdk.entity.base.BaseReqModel)2 BaseResModel (com.bsnbase.sdk.entity.base.BaseResModel)2 AlgorithmTypeContext (com.bsnbase.sdk.util.algorithm.AlgorithmTypeContext)2 HttpService (com.bsnbase.sdk.util.common.HttpService)2 BigInteger (java.math.BigInteger)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 AbiDefinition (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)2 BaseResArrayModel (com.bsnbase.sdk.entity.base.BaseResArrayModel)1 ReqKeyUploadBody (com.bsnbase.sdk.entity.req.cita.ReqKeyUploadBody)1 ReqKeyEscrowNo (com.bsnbase.sdk.entity.req.fabric.ReqKeyEscrowNo)1 ReqTrans (com.bsnbase.sdk.entity.req.fiscobcos.ReqTrans)1 ResGetBlockHeight (com.bsnbase.sdk.entity.resp.cita.ResGetBlockHeight)1 ResKeyEscrow (com.bsnbase.sdk.entity.resp.cita.ResKeyEscrow)1 ResKeyEscrowNo (com.bsnbase.sdk.entity.resp.fabric.ResKeyEscrowNo)1 ResUserInfo (com.bsnbase.sdk.entity.resp.fabric.ResUserInfo)1 ResGetBlockHeight (com.bsnbase.sdk.entity.resp.fiscobcos.ResGetBlockHeight)1