diff --git a/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/AssembleTransactionProcessor.java b/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/AssembleTransactionProcessor.java index 16e14493a..9098cc9ed 100644 --- a/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/AssembleTransactionProcessor.java +++ b/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/AssembleTransactionProcessor.java @@ -91,12 +91,21 @@ public AssembleTransactionProcessor( @Override public void deployOnly(String abi, String bin, List params) throws ABICodecException { - transactionPusher.pushOnly(createSignedConstructor(abi, bin, params)); + deployOnly(abi, bin, params, this.cryptoKeyPair); + } + + public void deployOnly(String abi, String bin, List params, CryptoKeyPair cryptoKeyPair) + throws ABICodecException { + transactionPusher.pushOnly(createSignedConstructor(abi, bin, params, cryptoKeyPair)); } @Override public TransactionReceipt deployAndGetReceipt(String data) { - String signedData = createSignedTransaction(null, data, this.cryptoKeyPair); + return deployAndGetReceipt(data, this.cryptoKeyPair); + } + + public TransactionReceipt deployAndGetReceipt(String data, CryptoKeyPair cryptoKeyPair) { + String signedData = createSignedTransaction(null, data, cryptoKeyPair); return transactionPusher.push(signedData); } @@ -145,16 +154,28 @@ public TransactionResponse deployAndGetResponse(String abi, String bin, List params, CryptoKeyPair cryptoKeyPair) + throws ABICodecException { + return deployAndGetResponse(abi, createSignedConstructor(abi, bin, params, cryptoKeyPair)); + } + @Override public TransactionResponse deployAndGetResponseWithStringParams( String abi, String bin, List params) throws ABICodecException { + return deployAndGetResponseWithStringParams(abi, bin, params, this.cryptoKeyPair); + } + + public TransactionResponse deployAndGetResponseWithStringParams( + String abi, String bin, List params, CryptoKeyPair cryptoKeyPair) + throws ABICodecException { return deployAndGetResponse( abi, bin, createSignedTransaction( null, abiCodec.encodeConstructorFromString(abi, bin, params), - this.cryptoKeyPair)); + cryptoKeyPair)); } @Override @@ -164,12 +185,30 @@ public void deployAsync( transactionPusher.pushAsync(createSignedConstructor(abi, bin, params), callback); } + public void deployAsync( + String abi, + String bin, + List params, + TransactionCallback callback, + CryptoKeyPair cryptoKeyPair) + throws ABICodecException { + transactionPusher.pushAsync( + createSignedConstructor(abi, bin, params, cryptoKeyPair), callback); + } + @Override public CompletableFuture deployAsync( String abi, String bin, List params) throws ABICodecException { return transactionPusher.pushAsync(createSignedConstructor(abi, bin, params)); } + public CompletableFuture deployAsync( + String abi, String bin, List params, CryptoKeyPair cryptoKeyPair) + throws ABICodecException { + return transactionPusher.pushAsync( + createSignedConstructor(abi, bin, params, cryptoKeyPair)); + } + /** * Deploy by bin and abi files. Should init with contractLoader. * @@ -183,21 +222,38 @@ public CompletableFuture deployAsync( @Override public TransactionResponse deployByContractLoader(String contractName, List args) throws ABICodecException, TransactionBaseException { + return deployByContractLoader(contractName, args, this.cryptoKeyPair); + } + + public TransactionResponse deployByContractLoader( + String contractName, List args, CryptoKeyPair cryptoKeyPair) + throws ABICodecException, TransactionBaseException { return deployAndGetResponse( contractLoader.getABIByContractName(contractName), contractLoader.getBinaryByContractName(contractName), - args); + args, + cryptoKeyPair); } @Override public void deployByContractLoaderAsync( String contractName, List args, TransactionCallback callback) throws ABICodecException, NoSuchTransactionFileException { + deployByContractLoaderAsync(contractName, args, callback, this.cryptoKeyPair); + } + + public void deployByContractLoaderAsync( + String contractName, + List args, + TransactionCallback callback, + CryptoKeyPair cryptoKeyPair) + throws ABICodecException, NoSuchTransactionFileException { deployAsync( contractLoader.getABIByContractName(contractName), contractLoader.getBinaryByContractName(contractName), args, - callback); + callback, + cryptoKeyPair); } @Override @@ -209,7 +265,13 @@ public void sendTransactionOnly(String signedData) { public TransactionResponse sendTransactionAndGetResponse( String to, String abi, String functionName, String data) throws TransactionBaseException, ABICodecException { - String signedData = createSignedTransaction(to, data, this.cryptoKeyPair); + return sendTransactionAndGetResponse(to, abi, functionName, data, this.cryptoKeyPair); + } + + public TransactionResponse sendTransactionAndGetResponse( + String to, String abi, String functionName, String data, CryptoKeyPair cryptoKeyPair) + throws TransactionBaseException, ABICodecException { + String signedData = createSignedTransaction(to, data, cryptoKeyPair); TransactionReceipt receipt = this.transactionPusher.push(signedData); try { return transactionDecoder.decodeReceiptWithValues(abi, functionName, receipt); @@ -224,22 +286,54 @@ public TransactionResponse sendTransactionAndGetResponse( public TransactionResponse sendTransactionAndGetResponse( String to, String abi, String functionName, List params) throws ABICodecException, TransactionBaseException { + return sendTransactionAndGetResponse(to, abi, functionName, params, this.cryptoKeyPair); + } + + public TransactionResponse sendTransactionAndGetResponse( + String to, + String abi, + String functionName, + List params, + CryptoKeyPair cryptoKeyPair) + throws ABICodecException, TransactionBaseException { String data = encodeFunction(abi, functionName, params); - return sendTransactionAndGetResponse(to, abi, functionName, data); + return sendTransactionAndGetResponse(to, abi, functionName, data, cryptoKeyPair); } @Override public TransactionResponse sendTransactionWithStringParamsAndGetResponse( String to, String abi, String functionName, List params) throws ABICodecException, TransactionBaseException { + return sendTransactionWithStringParamsAndGetResponse( + to, abi, functionName, params, this.cryptoKeyPair); + } + + public TransactionResponse sendTransactionWithStringParamsAndGetResponse( + String to, + String abi, + String functionName, + List params, + CryptoKeyPair cryptoKeyPair) + throws ABICodecException, TransactionBaseException { String data = abiCodec.encodeMethodFromString(abi, functionName, params); - return sendTransactionAndGetResponse(to, abi, functionName, data); + return sendTransactionAndGetResponse(to, abi, functionName, data, cryptoKeyPair); } @Override public TransactionReceipt sendTransactionAndGetReceiptByContractLoader( String contractName, String contractAddress, String functionName, List args) throws ABICodecException, TransactionBaseException { + return sendTransactionAndGetReceiptByContractLoader( + contractName, contractAddress, functionName, args, this.cryptoKeyPair); + } + + public TransactionReceipt sendTransactionAndGetReceiptByContractLoader( + String contractName, + String contractAddress, + String functionName, + List args, + CryptoKeyPair cryptoKeyPair) + throws ABICodecException, TransactionBaseException { String data = abiCodec.encodeMethod( contractLoader.getABIByContractName(contractName), functionName, args); @@ -253,11 +347,23 @@ public TransactionResponse sendTransactionAndGetResponseByContractLoader( String functionName, List funcParams) throws ABICodecException, TransactionBaseException { + return sendTransactionAndGetResponseByContractLoader( + contractName, contractAddress, functionName, funcParams, this.cryptoKeyPair); + } + + public TransactionResponse sendTransactionAndGetResponseByContractLoader( + String contractName, + String contractAddress, + String functionName, + List funcParams, + CryptoKeyPair cryptoKeyPair) + throws ABICodecException, TransactionBaseException { return sendTransactionAndGetResponse( contractAddress, contractLoader.getABIByContractName(contractName), functionName, - funcParams); + funcParams, + cryptoKeyPair); } @Override @@ -273,8 +379,19 @@ public void sendTransactionAsync( List params, TransactionCallback callback) throws TransactionBaseException, ABICodecException { + sendTransactionAsync(to, abi, functionName, params, callback, this.cryptoKeyPair); + } + + public void sendTransactionAsync( + String to, + String abi, + String functionName, + List params, + TransactionCallback callback, + CryptoKeyPair cryptoKeyPair) + throws TransactionBaseException, ABICodecException { String data = encodeFunction(abi, functionName, params); - sendTransactionAsync(to, data, this.cryptoKeyPair, callback); + sendTransactionAsync(to, data, cryptoKeyPair, callback); } @Override @@ -290,10 +407,22 @@ public void sendTransactionAndGetReceiptByContractLoaderAsync( List args, TransactionCallback callback) throws ABICodecException, TransactionBaseException { + sendTransactionAndGetReceiptByContractLoaderAsync( + contractName, contractAddress, functionName, args, callback, this.cryptoKeyPair); + } + + public void sendTransactionAndGetReceiptByContractLoaderAsync( + String contractName, + String contractAddress, + String functionName, + List args, + TransactionCallback callback, + CryptoKeyPair cryptoKeyPair) + throws ABICodecException, TransactionBaseException { String data = abiCodec.encodeMethod( contractLoader.getABIByContractName(contractName), functionName, args); - sendTransactionAsync(contractAddress, data, this.cryptoKeyPair, callback); + sendTransactionAsync(contractAddress, data, cryptoKeyPair, callback); } @Override @@ -355,8 +484,14 @@ public CallResponse callAndGetResponse( @Override public String createSignedConstructor(String abi, String bin, List params) throws ABICodecException { + return createSignedConstructor(abi, bin, params, this.cryptoKeyPair); + } + + public String createSignedConstructor( + String abi, String bin, List params, CryptoKeyPair cryptoKeyPair) + throws ABICodecException { return createSignedTransaction( - null, abiCodec.encodeConstructor(abi, bin, params), this.cryptoKeyPair); + null, abiCodec.encodeConstructor(abi, bin, params), cryptoKeyPair); } @Override diff --git a/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/TransactionProcessor.java b/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/TransactionProcessor.java index 9d7b4c960..fce38284c 100644 --- a/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/TransactionProcessor.java +++ b/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/TransactionProcessor.java @@ -37,7 +37,7 @@ public class TransactionProcessor implements TransactionProcessorInterface { protected static Logger log = LoggerFactory.getLogger(TransactionProcessor.class); protected final CryptoSuite cryptoSuite; - protected final CryptoKeyPair cryptoKeyPair; + protected CryptoKeyPair cryptoKeyPair; protected final Client client; protected final Integer groupId; protected final String chainId; @@ -55,29 +55,68 @@ public TransactionProcessor( this.transactionEncoder = new TransactionEncoderService(client.getCryptoSuite()); } + public CryptoKeyPair getCryptoKeyPair() { + return cryptoKeyPair; + } + + public void setCryptoKeyPair(CryptoKeyPair cryptoKeyPair) { + this.cryptoKeyPair = cryptoKeyPair; + } + @Override public TransactionReceipt sendTransactionAndGetReceipt( String to, String data, CryptoKeyPair cryptoKeyPair) { - String signedData = createSignedTransaction(to, data, cryptoKeyPair); - return this.client.sendRawTransactionAndGetReceipt(signedData); + if (cryptoKeyPair == null) { + return this.client.sendRawTransactionAndGetReceipt( + createSignedTransaction(to, data, this.cryptoKeyPair)); + } else { + return this.client.sendRawTransactionAndGetReceipt( + createSignedTransaction(to, data, cryptoKeyPair)); + } + } + + @Override + public TransactionReceipt sendTransactionAndGetReceipt(String to, String data) { + return sendTransactionAndGetReceipt(to, data, this.cryptoKeyPair); } @Override public void sendTransactionAsync( String to, String data, CryptoKeyPair cryptoKeyPair, TransactionCallback callback) { - String signedData = createSignedTransaction(to, data, cryptoKeyPair); - client.sendRawTransactionAndGetReceiptAsync(signedData, callback); + if (cryptoKeyPair == null) { + client.sendRawTransactionAndGetReceiptAsync( + createSignedTransaction(to, data, this.cryptoKeyPair), callback); + } else { + client.sendRawTransactionAndGetReceiptAsync( + createSignedTransaction(to, data, cryptoKeyPair), callback); + } + } + + @Override + public void sendTransactionAsync(String to, String data, TransactionCallback callback) { + sendTransactionAsync(to, data, this.cryptoKeyPair, callback); } @Override public byte[] sendTransactionAsyncAndGetHash( String to, String data, CryptoKeyPair cryptoKeyPair, TransactionCallback callback) { - String signedData = createSignedTransaction(to, data, cryptoKeyPair); + String signedData; + if (cryptoKeyPair == null) { + signedData = createSignedTransaction(to, data, this.cryptoKeyPair); + } else { + signedData = createSignedTransaction(to, data, cryptoKeyPair); + } client.sendRawTransactionAndGetReceiptAsync(signedData, callback); byte[] transactionHash = cryptoSuite.hash(Hex.decode(Numeric.cleanHexPrefix(signedData))); return transactionHash; } + @Override + public byte[] sendTransactionAsyncAndGetHash( + String to, String data, TransactionCallback callback) { + return sendTransactionAsyncAndGetHash(to, data, this.cryptoKeyPair, callback); + } + @Override public Call executeCall(CallRequest callRequest) { return executeCall( @@ -101,6 +140,14 @@ public String createSignedTransaction(String to, String data, CryptoKeyPair cryp new BigInteger(this.chainId), BigInteger.valueOf(this.groupId), ""); + if (cryptoKeyPair == null) { + return transactionEncoder.encodeAndSign(rawTransaction, this.cryptoKeyPair); + } return transactionEncoder.encodeAndSign(rawTransaction, cryptoKeyPair); } + + @Override + public String createSignedTransaction(String to, String data) { + return createSignedTransaction(to, data, this.cryptoKeyPair); + } } diff --git a/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/TransactionProcessorInterface.java b/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/TransactionProcessorInterface.java index 4f644a799..d47e90420 100644 --- a/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/TransactionProcessorInterface.java +++ b/sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/TransactionProcessorInterface.java @@ -38,6 +38,8 @@ public interface TransactionProcessorInterface { public TransactionReceipt sendTransactionAndGetReceipt( String to, String data, CryptoKeyPair cryptoKeyPair); + public TransactionReceipt sendTransactionAndGetReceipt(String to, String data); + /** * send transaction to fisco bcos node and get transaction receipt asynchronously. * @@ -49,8 +51,14 @@ public TransactionReceipt sendTransactionAndGetReceipt( public void sendTransactionAsync( String to, String data, CryptoKeyPair cryptoKeyPair, TransactionCallback callback); + public void sendTransactionAsync(String to, String data, TransactionCallback callback); + public byte[] sendTransactionAsyncAndGetHash( String to, String data, CryptoKeyPair cryptoKeyPair, TransactionCallback callback); + + public byte[] sendTransactionAsyncAndGetHash( + String to, String data, TransactionCallback callback); + /** * send call to fisco bcos node and receive call response. * @@ -78,4 +86,6 @@ public byte[] sendTransactionAsyncAndGetHash( * @return hexed data of signed transaction */ public String createSignedTransaction(String to, String data, CryptoKeyPair cryptoKeyPair); + + public String createSignedTransaction(String to, String data); }