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);
}
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();
}
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();
}
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;
}
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;
}
Aggregations