From 249546cc3c4a650cd859321f8648b7452d0e3492 Mon Sep 17 00:00:00 2001 From: kyonRay Date: Fri, 10 Nov 2023 17:10:06 +0800 Subject: [PATCH] (transaction): add interfaces of new transaction manager. --- .ci/ci_check.sh | 15 +- .github/workflows/workflow.yml | 2 +- ...ransactionWithRemoteSignProcessorTest.java | 68 ++- .../bcos/sdk/v3/codec/ContractCodec.java | 39 +- .../org/fisco/bcos/sdk/v3/codec/Encoder.java | 30 + .../fisco/bcos/sdk/v3/codec/EventEncoder.java | 12 +- .../v3/codec/FunctionEncoderInterface.java | 8 +- .../sdk/v3/codec/abi/FunctionEncoder.java | 6 + .../sdk/v3/codec/scale/FunctionEncoder.java | 6 + .../sdk/v3/codec/wrapper/ABIDefinition.java | 12 +- .../codec/wrapper/ABIDefinitionFactory.java | 12 +- .../codec/wrapper/ContractABIDefinition.java | 25 +- .../sdk/v3/crypto/keypair/CryptoKeyPair.java | 13 + .../sdk/v3/model/callback/InjectFetcher.java | 5 + .../decode/TransactionDecoderService.java | 27 +- .../v2/AssembleTransactionService.java | 260 ++++++++ .../manager/v2/DefaultTransactionManager.java | 366 +++++++++++ .../v2/ProxySignTransactionManager.java | 567 ++++++++++++++++++ .../manager/v2/TransactionManager.java | 428 +++++++++++++ .../v2/TransferTransactionService.java | 33 + .../signer/TransactionJniSignerService.java | 79 +++ .../signer/TransactionSignerInterface.java | 22 +- .../signer/TransactionSignerService.java | 24 + ...ransactionSignerWithRemoteSignService.java | 27 +- .../sdk/v3/transaction/tools/Convert.java | 64 ++ 25 files changed, 2099 insertions(+), 51 deletions(-) create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/model/callback/InjectFetcher.java create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/AssembleTransactionService.java create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/DefaultTransactionManager.java create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/ProxySignTransactionManager.java create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/TransactionManager.java create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/TransferTransactionService.java create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionJniSignerService.java create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/tools/Convert.java diff --git a/.ci/ci_check.sh b/.ci/ci_check.sh index 0ccba269a..5782c1c25 100755 --- a/.ci/ci_check.sh +++ b/.ci/ci_check.sh @@ -137,7 +137,7 @@ check_standard_node() build_node "normal" "${3}" prepare_environment "${2}" ## run integration test - bash gradlew clean integrationTest --info + bash gradlew clean integrationTest --info ${4} ## clean clean_node "${1}" } @@ -170,26 +170,33 @@ LOG_INFO "------ download_binary: v3.1.0---------" download_build_chain "v3.1.0" download_binary "v3.1.0" LOG_INFO "------ check_standard_node---------" -check_standard_node +check_standard_node "true" "normal" "-A" "-x test" rm -rf ./bin LOG_INFO "------ download_binary: v3.2.0---------" download_build_chain "v3.2.0" download_binary "v3.2.0" LOG_INFO "------ check_standard_node---------" -check_standard_node "true" "sm" "-s -A" +check_standard_node "true" "normal" "-A" "-x test" rm -rf ./bin LOG_INFO "------ download_build_chain: v3.3.0---------" download_binary "v3.3.0" download_build_chain "v3.3.0" LOG_INFO "------ check_standard_node---------" -check_standard_node "true" "sm" "-s" +check_standard_node "true" "normal" "" "-x test" rm -rf ./bin LOG_INFO "------ download_build_chain: v3.4.0---------" download_binary "v3.4.0" download_build_chain "v3.4.0" +LOG_INFO "------ check_standard_node---------" +check_standard_node "true" "normal" "" "-x test" +rm -rf ./bin + +LOG_INFO "------ download_build_chain: v3.5.0---------" +download_binary "v3.5.0" +download_build_chain "v3.5.0" LOG_INFO "------ check_wasm_node---------" check_wasm_node "false" LOG_INFO "------ check_standard_node---------" diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 859f85e00..fe97cfe57 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -51,7 +51,7 @@ jobs: run: ./gradlew.bat build - name: run integration testing # FIXME: macOS WASM integration testing failed - if: runner.os != 'Windows' && runner.os != 'macOS' + if: runner.os != 'Windows' run: /bin/bash .ci/ci_check.sh build-centos: diff --git a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/AssembleTransactionWithRemoteSignProcessorTest.java b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/AssembleTransactionWithRemoteSignProcessorTest.java index 5d745ce44..aa8d45121 100644 --- a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/AssembleTransactionWithRemoteSignProcessorTest.java +++ b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/AssembleTransactionWithRemoteSignProcessorTest.java @@ -18,19 +18,24 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; + import org.apache.commons.lang3.StringUtils; +import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderJniObj; import org.fisco.bcos.sdk.v3.BcosSDK; import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.codec.ContractCodec; import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; +import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult; import org.fisco.bcos.sdk.v3.model.ConstantConfig; import org.fisco.bcos.sdk.v3.model.TransactionReceipt; +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; import org.fisco.bcos.sdk.v3.test.transaction.mock.RemoteSignCallbackMock; import org.fisco.bcos.sdk.v3.test.transaction.mock.RemoteSignProviderMock; import org.fisco.bcos.sdk.v3.transaction.manager.AssembleTransactionWithRemoteSignProcessor; import org.fisco.bcos.sdk.v3.transaction.manager.TransactionProcessorFactory; -import org.fisco.bcos.sdk.v3.transaction.model.dto.CallResponse; import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; import org.fisco.bcos.sdk.v3.transaction.signer.RemoteSignProviderInterface; +import org.fisco.bcos.sdk.v3.utils.Hex; import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; @@ -60,7 +65,8 @@ public class AssembleTransactionWithRemoteSignProcessorTest { private RemoteSignProviderInterface remoteSignProviderMock = new RemoteSignProviderMock(this.client.getCryptoSuite()); - public AssembleTransactionWithRemoteSignProcessorTest() {} + public AssembleTransactionWithRemoteSignProcessorTest() { + } @Test public void test1HelloWorldSync() throws Exception { @@ -77,32 +83,38 @@ public void test1HelloWorldSync() throws Exception { assembleTransactionWithRemoteSignProcessor .getContractLoader() .getABIByContractName("HelloWorld"); - // function1: deploy sync - TransactionResponse response = - assembleTransactionWithRemoteSignProcessor.deployByContractLoader( - "HelloWorld", new ArrayList<>()); - System.out.println("--- finish deploy with sync ---"); - Assert.assertEquals(response.getTransactionReceipt().getStatus(), 0); - Assert.assertEquals(0, response.getReturnCode()); - Assert.assertEquals(0, response.getTransactionReceipt().getStatus()); - String helloWorldAddress = response.getContractAddress(); - Assert.assertTrue( - StringUtils.isNotBlank(response.getContractAddress()) - && !StringUtils.equalsIgnoreCase( - helloWorldAddress, - "0x0000000000000000000000000000000000000000000000000000000000000000")); + String bin = assembleTransactionWithRemoteSignProcessor.getContractLoader().getBinaryByContractName("HelloWorld"); - // function2: send transaction `HelloWorld.set("test")` sync - TransactionResponse transactionResponse2 = - assembleTransactionWithRemoteSignProcessor.sendTransactionAndGetResponse( - helloWorldAddress, abi, "set", this.params); - Assert.assertEquals(0, transactionResponse2.getTransactionReceipt().getStatus()); + ContractCodec contractCodec = new ContractCodec(client.getCryptoSuite(), client.isWASM()); + + long transactionData = TransactionBuilderJniObj.createTransactionData( + "group0", + "chain0", + "", + Hex.toHexString(contractCodec.encodeConstructor(abi, bin, params)), + abi, + client.getBlockLimit().longValue()); + + String rawTxHash = TransactionBuilderJniObj.calcTransactionDataHash(client.getCryptoSuite().cryptoTypeConfig, transactionData); + + SignatureResult signatureResult = remoteSignProviderMock.requestForSign(Hex.decode(rawTxHash), this.client.getCryptoSuite().cryptoTypeConfig); + + String signedTransaction = TransactionBuilderJniObj.createSignedTransaction(transactionData, signatureResult.toString(), rawTxHash, 0); + + CompletableFuture receiptCompletableFuture = new CompletableFuture<>(); + assembleTransactionWithRemoteSignProcessor.sendTransactionAsync(signedTransaction, new TransactionCallback() { + @Override + public void onResponse(TransactionReceipt receipt) { + receiptCompletableFuture.complete(receipt); + } + }); + + TransactionReceipt transactionReceipt = receiptCompletableFuture.get(); - // function3: call, which only support sync mode. - CallResponse callResponse1 = - assembleTransactionWithRemoteSignProcessor.sendCallByContractLoader( - "HelloWorld", helloWorldAddress, "get", new ArrayList<>()); - Assert.assertEquals("test", callResponse1.getResults().get(0).getValue()); + Assert.assertEquals(transactionReceipt.getStatus(), 0); + Assert.assertFalse(transactionReceipt.getContractAddress().isEmpty()); + String abi1 = client.getABI(transactionReceipt.getContractAddress()).getABI(); + Assert.assertEquals(abi1, abi); } @Test @@ -135,8 +147,8 @@ public void test2HelloWorldAsync() throws Exception { Assert.assertTrue( StringUtils.isNotBlank(response.getContractAddress()) && !StringUtils.equalsIgnoreCase( - helloWorldAddress, - "0x0000000000000000000000000000000000000000000000000000000000000000")); + helloWorldAddress, + "0x0000000000000000000000000000000000000000000000000000000000000000")); // function2: deploy async with callback long transactionData = diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java index fd9dfec92..9c029a1e4 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java @@ -48,6 +48,10 @@ import org.fisco.bcos.sdk.v3.codec.wrapper.ContractCodecJsonWrapper; import org.fisco.bcos.sdk.v3.codec.wrapper.ContractCodecTools; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; +import org.fisco.bcos.sdk.v3.crypto.hash.Keccak256; +import org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash; +import org.fisco.bcos.sdk.v3.model.CryptoType; import org.fisco.bcos.sdk.v3.model.EventLog; import org.fisco.bcos.sdk.v3.utils.Hex; import org.fisco.bcos.sdk.v3.utils.Numeric; @@ -61,6 +65,7 @@ public class ContractCodec { private static final Logger logger = LoggerFactory.getLogger(ContractCodec.class); private final CryptoSuite cryptoSuite; + private final Hash hashImpl; private final boolean isWasm; private final ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper(); private FunctionEncoderInterface functionEncoder = null; @@ -69,25 +74,53 @@ public class ContractCodec { private final ContractCodecJsonWrapper contractCodecJsonWrapper = new ContractCodecJsonWrapper(); + @Deprecated public ContractCodec(CryptoSuite cryptoSuite, boolean isWasm) { this.cryptoSuite = cryptoSuite; + this.hashImpl = cryptoSuite.getHashImpl(); + this.isWasm = isWasm; + if (isWasm) { + this.functionEncoder = + new org.fisco.bcos.sdk.v3.codec.scale.FunctionEncoder(this.hashImpl); + this.functionReturnDecoder = new FunctionReturnDecoder(); + } else { + this.functionEncoder = + new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(this.hashImpl); + this.functionReturnDecoder = + new org.fisco.bcos.sdk.v3.codec.abi.FunctionReturnDecoder(); + } + this.abiDefinitionFactory = new ABIDefinitionFactory(hashImpl); + } + + public ContractCodec(Hash hashImpl, boolean isWasm) { + this.hashImpl = hashImpl; this.isWasm = isWasm; if (isWasm) { this.functionEncoder = - new org.fisco.bcos.sdk.v3.codec.scale.FunctionEncoder(cryptoSuite); + new org.fisco.bcos.sdk.v3.codec.scale.FunctionEncoder(this.hashImpl); this.functionReturnDecoder = new FunctionReturnDecoder(); } else { - this.functionEncoder = new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(cryptoSuite); + this.functionEncoder = + new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(this.hashImpl); this.functionReturnDecoder = new org.fisco.bcos.sdk.v3.codec.abi.FunctionReturnDecoder(); } - this.abiDefinitionFactory = new ABIDefinitionFactory(cryptoSuite); + this.abiDefinitionFactory = new ABIDefinitionFactory(this.hashImpl); + // for compatibility + if (this.hashImpl instanceof SM3Hash) { + this.cryptoSuite = new CryptoSuite(CryptoType.SM_TYPE); + } else if (this.hashImpl instanceof Keccak256) { + this.cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE); + } else { + this.cryptoSuite = null; + } } public boolean isWasm() { return isWasm; } + @Deprecated public CryptoSuite getCryptoSuite() { return this.cryptoSuite; } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/Encoder.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/Encoder.java index 0442233b6..dace1f089 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/Encoder.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/Encoder.java @@ -1,21 +1,51 @@ package org.fisco.bcos.sdk.v3.codec; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; +import org.fisco.bcos.sdk.v3.crypto.hash.Keccak256; +import org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash; +import org.fisco.bcos.sdk.v3.model.CryptoType; public class Encoder { private CryptoSuite cryptoSuite; + private Hash hashImpl; + + @Deprecated public Encoder(CryptoSuite cryptoSuite) { this.cryptoSuite = cryptoSuite; + this.hashImpl = cryptoSuite.getHashImpl(); + } + + public Encoder(Hash hash) { + // for compatibility + if (hashImpl instanceof SM3Hash) { + this.cryptoSuite = new CryptoSuite(CryptoType.SM_TYPE); + } + if (hashImpl instanceof Keccak256) { + this.cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE); + } + this.hashImpl = hash; } /** @return the cryptoSuite */ + @Deprecated public CryptoSuite getCryptoSuite() { return this.cryptoSuite; } /** @param cryptoSuite the cryptoSuite to set */ + @Deprecated public void setCryptoSuite(CryptoSuite cryptoSuite) { this.cryptoSuite = cryptoSuite; + this.hashImpl = cryptoSuite.getHashImpl(); + } + + public Hash getHashImpl() { + return hashImpl; + } + + public void setHashImpl(Hash hashImpl) { + this.hashImpl = hashImpl; } } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/EventEncoder.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/EventEncoder.java index 6542f349c..061a3dd44 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/EventEncoder.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/EventEncoder.java @@ -6,6 +6,7 @@ import org.fisco.bcos.sdk.v3.codec.datatypes.Type; import org.fisco.bcos.sdk.v3.codec.datatypes.TypeReference; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; import org.fisco.bcos.sdk.v3.utils.Numeric; /** @@ -13,10 +14,17 @@ * href="https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#events">here. */ public class EventEncoder { - private final CryptoSuite cryptoSuite; + @Deprecated private CryptoSuite cryptoSuite; + private final Hash hashImpl; + @Deprecated public EventEncoder(CryptoSuite cryptoSuite) { this.cryptoSuite = cryptoSuite; + this.hashImpl = cryptoSuite.getHashImpl(); + } + + public EventEncoder(Hash hashImpl) { + this.hashImpl = hashImpl; } public String encode(Event event) { @@ -39,7 +47,7 @@ public String buildMethodSignature( public String buildEventSignature(String methodSignature) { byte[] input = methodSignature.getBytes(); - byte[] hash = this.cryptoSuite.hash(input); + byte[] hash = this.hashImpl.hash(input); return Numeric.toHexString(hash); } } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/FunctionEncoderInterface.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/FunctionEncoderInterface.java index 9e22d5376..e6f18d52a 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/FunctionEncoderInterface.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/FunctionEncoderInterface.java @@ -6,12 +6,18 @@ import org.fisco.bcos.sdk.v3.codec.datatypes.Function; import org.fisco.bcos.sdk.v3.codec.datatypes.Type; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; public abstract class FunctionEncoderInterface extends Encoder { + @Deprecated public FunctionEncoderInterface(CryptoSuite cryptoSuite) { super(cryptoSuite); } + public FunctionEncoderInterface(Hash hash) { + super(hash); + } + public abstract byte[] encode(Function function); public static String buildMethodSignature(String methodName, List parameters) { @@ -27,7 +33,7 @@ public static String buildMethodSignature(String methodName, List paramete public byte[] buildMethodId(String methodSignature) { byte[] input = methodSignature.getBytes(); - byte[] hash = this.getCryptoSuite().hash(input); + byte[] hash = this.getHashImpl().hash(input); return Arrays.copyOfRange(hash, 0, 4); } } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/abi/FunctionEncoder.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/abi/FunctionEncoder.java index 511970a2b..f1615b326 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/abi/FunctionEncoder.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/abi/FunctionEncoder.java @@ -10,16 +10,22 @@ import org.fisco.bcos.sdk.v3.codec.datatypes.Type; import org.fisco.bcos.sdk.v3.codec.datatypes.Uint; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; /** * Ethereum Contract Application Binary Interface (ABI) encoding for functions. Further details are * available here. */ public class FunctionEncoder extends FunctionEncoderInterface { + @Deprecated public FunctionEncoder(CryptoSuite cryptoSuite) { super(cryptoSuite); } + public FunctionEncoder(Hash hash) { + super(hash); + } + @Override public byte[] encode(Function function) { List parameters = function.getInputParameters(); diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/scale/FunctionEncoder.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/scale/FunctionEncoder.java index aeeb0d130..3778c9076 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/scale/FunctionEncoder.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/scale/FunctionEncoder.java @@ -7,12 +7,18 @@ import org.fisco.bcos.sdk.v3.codec.datatypes.Function; import org.fisco.bcos.sdk.v3.codec.datatypes.Type; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; public class FunctionEncoder extends FunctionEncoderInterface { + @Deprecated public FunctionEncoder(CryptoSuite cryptoSuite) { super(cryptoSuite); } + public FunctionEncoder(Hash hash) { + super(hash); + } + @Override public byte[] encode(Function function) { List parameters = function.getInputParameters(); diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIDefinition.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIDefinition.java index 789929d59..23618b669 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIDefinition.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIDefinition.java @@ -8,6 +8,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; /** * ABIDefinition wrapper @@ -150,11 +151,20 @@ public String getMethodSignatureAsString() { * @param cryptoSuite the crypto suite used for hash calculation * @return the method id */ + @Deprecated public byte[] getMethodId(CryptoSuite cryptoSuite) { // Note: it's ok to use the abi encoder for all ABIDefinition, because only use // buildMethodId org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder encoder = - new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(cryptoSuite); + new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(cryptoSuite.getHashImpl()); + return encoder.buildMethodId(this.getMethodSignatureAsString()); + } + + public byte[] getMethodId(Hash hash) { + // Note: it's ok to use the abi encoder for all ABIDefinition, because only use + // buildMethodId + org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder encoder = + new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(hash); return encoder.buildMethodId(this.getMethodSignatureAsString()); } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIDefinitionFactory.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIDefinitionFactory.java index 3d371216c..6350da459 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIDefinitionFactory.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIDefinitionFactory.java @@ -1,6 +1,7 @@ package org.fisco.bcos.sdk.v3.codec.wrapper; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; import org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,10 +10,17 @@ public class ABIDefinitionFactory { private static final Logger logger = LoggerFactory.getLogger(ABIDefinitionFactory.class); - private CryptoSuite cryptoSuite; + @Deprecated private CryptoSuite cryptoSuite; + private Hash hashIpml; + @Deprecated public ABIDefinitionFactory(CryptoSuite cryptoSuite) { this.cryptoSuite = cryptoSuite; + this.hashIpml = cryptoSuite.getHashImpl(); + } + + public ABIDefinitionFactory(Hash hashImpl) { + this.hashIpml = hashImpl; } /** @@ -26,7 +34,7 @@ public ContractABIDefinition loadABI(String abi) { ABIDefinition[] abiDefinitions = ObjectMapperFactory.getObjectMapper().readValue(abi, ABIDefinition[].class); - ContractABIDefinition contractABIDefinition = new ContractABIDefinition(cryptoSuite); + ContractABIDefinition contractABIDefinition = new ContractABIDefinition(hashIpml); for (ABIDefinition abiDefinition : abiDefinitions) { if (abiDefinition.getType().equals("constructor")) { contractABIDefinition.setConstructor(abiDefinition); diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ContractABIDefinition.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ContractABIDefinition.java index 288418c09..a5c75e7dc 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ContractABIDefinition.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ContractABIDefinition.java @@ -6,6 +6,10 @@ import java.util.List; import java.util.Map; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; +import org.fisco.bcos.sdk.v3.crypto.hash.Keccak256; +import org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash; +import org.fisco.bcos.sdk.v3.model.CryptoType; import org.fisco.bcos.sdk.v3.utils.Hex; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,10 +25,25 @@ public class ContractABIDefinition { private Map methodIDToFunctions = new HashMap<>(); // event topic => topic private Map eventTopicToEvents = new HashMap<>(); - private final CryptoSuite cryptoSuite; + private CryptoSuite cryptoSuite; + private final Hash hashIpml; + + @Deprecated public ContractABIDefinition(CryptoSuite cryptoSuite) { this.cryptoSuite = cryptoSuite; + this.hashIpml = this.cryptoSuite.getHashImpl(); + } + + public ContractABIDefinition(Hash hashImpl) { + // for compatibility + if (hashImpl instanceof SM3Hash) { + this.cryptoSuite = new CryptoSuite(CryptoType.SM_TYPE); + } + if (hashImpl instanceof Keccak256) { + this.cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE); + } + this.hashIpml = hashImpl; } public ABIDefinition getConstructor() { @@ -79,7 +98,7 @@ public void addFunction(String name, ABIDefinition abiDefinition) { abiDefinitions.add(abiDefinition); // calculate method id and add abiDefinition to methodIdToFunctions - byte[] methodId = abiDefinition.getMethodId(this.cryptoSuite); + byte[] methodId = abiDefinition.getMethodId(this.hashIpml); this.methodIDToFunctions.put(ByteBuffer.wrap(methodId), abiDefinition); if (logger.isTraceEnabled()) { @@ -101,7 +120,7 @@ public void addEvent(String name, ABIDefinition abiDefinition) { } // calculate method id and add abiDefinition to eventTopicToEvents - byte[] methodId = abiDefinition.getMethodId(this.cryptoSuite); + byte[] methodId = abiDefinition.getMethodId(this.hashIpml); this.eventTopicToEvents.put(ByteBuffer.wrap(methodId), abiDefinition); } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/crypto/keypair/CryptoKeyPair.java b/src/main/java/org/fisco/bcos/sdk/v3/crypto/keypair/CryptoKeyPair.java index 4ac663d50..960de9ce3 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/crypto/keypair/CryptoKeyPair.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/crypto/keypair/CryptoKeyPair.java @@ -24,6 +24,7 @@ import org.fisco.bcos.sdk.v3.crypto.keystore.KeyTool; import org.fisco.bcos.sdk.v3.crypto.keystore.P12KeyStore; import org.fisco.bcos.sdk.v3.crypto.keystore.PEMKeyStore; +import org.fisco.bcos.sdk.v3.model.CryptoType; import org.fisco.bcos.sdk.v3.utils.Hex; import org.fisco.bcos.sdk.v3.utils.Numeric; import org.fisco.bcos.sdk.v3.utils.exceptions.DecoderException; @@ -84,6 +85,7 @@ public CryptoKeyPair(KeyPair keyPair) { this.hexPrivateKey = KeyTool.getHexedPrivateKey(keyPair.getPrivate()); this.hexPublicKey = KeyTool.getHexedPublicKey(keyPair.getPublic()); } + /** * Get CryptoKeyPair information from CryptoResult * @@ -145,6 +147,16 @@ public String getCurveName() { return this.curveName; } + public int getKeyType() { + switch (this.curveName) { + case SM2_CURVE_NAME: + return CryptoType.SM_TYPE; + case ECDSA_CURVE_NAME: + default: + return CryptoType.ECDSA_TYPE; + } + } + /** * Abstract function of create keyPair randomly * @@ -179,6 +191,7 @@ protected static String getPublicKeyNoPrefix(String publicKeyStr) { return Numeric.getKeyNoPrefix( UNCOMPRESSED_PUBLICKEY_FLAG_STR, publicKeyStr, PUBLIC_KEY_LENGTH_IN_HEX); } + /** * get the address according to the public key * diff --git a/src/main/java/org/fisco/bcos/sdk/v3/model/callback/InjectFetcher.java b/src/main/java/org/fisco/bcos/sdk/v3/model/callback/InjectFetcher.java new file mode 100644 index 000000000..572788aeb --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/model/callback/InjectFetcher.java @@ -0,0 +1,5 @@ +package org.fisco.bcos.sdk.v3.model.callback; + +public interface InjectFetcher { + T onFetch(); +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java index a6aa90ccf..b2790eea6 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java @@ -37,6 +37,8 @@ import org.fisco.bcos.sdk.v3.codec.wrapper.ContractABIDefinition; import org.fisco.bcos.sdk.v3.codec.wrapper.ContractCodecTools; import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.hash.Hash; +import org.fisco.bcos.sdk.v3.model.CryptoType; import org.fisco.bcos.sdk.v3.model.RetCode; import org.fisco.bcos.sdk.v3.model.TransactionReceipt; import org.fisco.bcos.sdk.v3.model.TransactionReceipt.Logs; @@ -51,6 +53,7 @@ public class TransactionDecoderService implements TransactionDecoderInterface { protected static Logger logger = LoggerFactory.getLogger(TransactionDecoderService.class); private CryptoSuite cryptoSuite; + private final Hash hashImpl; private final ContractCodec contractCodec; private final EventEncoder eventEncoder; @@ -60,11 +63,27 @@ public class TransactionDecoderService implements TransactionDecoderInterface { * @param cryptoSuite the cryptoSuite used to calculate hash and signatures * @param isWasm whether the invoked contract is a Wasm contract */ + @Deprecated public TransactionDecoderService(CryptoSuite cryptoSuite, boolean isWasm) { super(); this.cryptoSuite = cryptoSuite; - this.contractCodec = new ContractCodec(cryptoSuite, isWasm); - this.eventEncoder = new EventEncoder(cryptoSuite); + this.hashImpl = cryptoSuite.getHashImpl(); + this.contractCodec = new ContractCodec(hashImpl, isWasm); + this.eventEncoder = new EventEncoder(hashImpl); + } + + public TransactionDecoderService(Hash hashImpl, boolean isWasm) { + super(); + this.hashImpl = hashImpl; + this.contractCodec = new ContractCodec(hashImpl, isWasm); + this.eventEncoder = new EventEncoder(hashImpl); + // for compatibility + if (hashImpl instanceof org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash) { + this.cryptoSuite = new CryptoSuite(CryptoType.SM_TYPE); + } + if (hashImpl instanceof org.fisco.bcos.sdk.v3.crypto.hash.Keccak256) { + this.cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE); + } } @Override @@ -149,7 +168,7 @@ public TransactionResponse decodeReceiptStatus(TransactionReceipt receipt) { @Override public Map>> decodeEvents(String abi, List logs) { - ABIDefinitionFactory abiDefinitionFactory = new ABIDefinitionFactory(cryptoSuite); + ABIDefinitionFactory abiDefinitionFactory = new ABIDefinitionFactory(hashImpl); ContractABIDefinition contractABIDefinition = abiDefinitionFactory.loadABI(abi); Map> eventsMap = contractABIDefinition.getEvents(); Map>> result = new HashMap<>(); @@ -205,11 +224,13 @@ private String decodeMethodSign(ABIDefinition abiDefinition) { } /** @return the cryptoSuite */ + @Deprecated public CryptoSuite getCryptoSuite() { return cryptoSuite; } /** @param cryptoSuite the cryptoSuite to set */ + @Deprecated public void setCryptoSuite(CryptoSuite cryptoSuite) { this.cryptoSuite = cryptoSuite; } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/AssembleTransactionService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/AssembleTransactionService.java new file mode 100644 index 000000000..68c9e67b3 --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/AssembleTransactionService.java @@ -0,0 +1,260 @@ +package org.fisco.bcos.sdk.v3.transaction.manager.v2; + +import java.math.BigInteger; +import java.util.List; +import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.codec.ContractCodec; +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; +import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; + +/** + * AssembleTransactionService + * + *

codec(abi, method, params) -> inputData sendTx(to, inputData) -> receipt decode(abi, method, + * receipt.output, ) -> result + */ +public class AssembleTransactionService { + private TransactionManager transactionManager; + + private final ContractCodec contractCodec; + + AssembleTransactionService(Client client) { + this.contractCodec = + new ContractCodec(client.getCryptoSuite().getHashImpl(), client.isWASM()); + transactionManager = new DefaultTransactionManager(client); + } + + public void setTransactionManager(TransactionManager transactionManager) { + this.transactionManager = transactionManager; + } + + public TransactionResponse sendTransaction( + String abi, String method, List params, String to, BigInteger value) { + return null; + } + + public TransactionResponse sendTransaction( + String abi, + String method, + List params, + String to, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit) { + return null; + } + + public TransactionResponse sendTransactionWithStringParams( + String abi, + String method, + List params, + String to, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit) { + return null; + } + + public TransactionResponse sendTransactionWithStringParams( + String abi, String method, List params, String to, BigInteger value) { + return null; + } + + public TransactionResponse deployContract( + String abi, String bin, List params, BigInteger value) { + return null; + } + + public TransactionResponse deployContractWithStringParams( + String abi, String bin, List params, BigInteger value) { + return null; + } + + public TransactionResponse deployContract( + String abi, + String bin, + List params, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit) { + return null; + } + + public TransactionResponse deployContractWithStringParams( + String abi, + String bin, + List params, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit) { + return null; + } + + public TransactionResponse sendEIP1559Transaction( + String abi, + String method, + List params, + String to, + BigInteger value, + TransactionManager.EIP1559Struct eip1559Struct) { + return null; + } + + public TransactionResponse sendEIP1559TransactionWithStringParams( + String abi, + String method, + List params, + String to, + BigInteger value, + TransactionManager.EIP1559Struct eip1559Struct) { + return null; + } + + public TransactionResponse deployContractEIP1559( + String abi, + String method, + List params, + String to, + BigInteger value, + TransactionManager.EIP1559Struct eip1559Struct) { + return null; + } + + public TransactionResponse deployContractEIP1559WithStringParams( + String abi, + String method, + List params, + String to, + BigInteger value, + TransactionManager.EIP1559Struct eip1559Struct) { + return null; + } + + public String asyncSendTransaction( + String abi, + String method, + List params, + String to, + BigInteger value, + TransactionCallback callback) { + return null; + } + + public String asyncSendTransactionWithStringParams( + String abi, + String method, + List params, + String to, + BigInteger value, + TransactionCallback callback) { + return null; + } + + public String asyncSendTransaction( + String abi, + String method, + List params, + String to, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + TransactionCallback callback) { + return null; + } + + public String asyncSendTransactionWithStringParams( + String abi, + String method, + List params, + String to, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + TransactionCallback callback) { + return null; + } + + public String asyncDeployContract( + String abi, + String bin, + List params, + BigInteger value, + TransactionCallback callback) { + return null; + } + + public String asyncDeployContractWithStringParams( + String abi, + String bin, + List params, + BigInteger value, + TransactionCallback callback) { + return null; + } + + public String asyncDeployContract( + String abi, + String bin, + List params, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + TransactionCallback callback) { + return null; + } + + public String asyncDeployContractWithStringParams( + String abi, + String bin, + List params, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + TransactionCallback callback) { + return null; + } + + public String asyncSendEIP1559Transaction( + String abi, + String method, + List params, + String to, + BigInteger value, + TransactionManager.EIP1559Struct eip1559Struct, + TransactionCallback callback) { + return null; + } + + public String asyncSendEIP1559TransactionWithStringParams( + String abi, + String method, + List params, + String to, + BigInteger value, + TransactionManager.EIP1559Struct eip1559Struct, + TransactionCallback callback) { + return null; + } + + public String asyncDeployContractEIP1559( + String abi, + String bin, + List params, + String to, + BigInteger value, + TransactionManager.EIP1559Struct eip1559Struct, + TransactionCallback callback) { + return null; + } + + public String asyncDeployContractEIP1559WithStringParams( + String abi, + String bin, + List params, + String to, + BigInteger value, + TransactionManager.EIP1559Struct eip1559Struct, + TransactionCallback callback) { + return null; + } +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/DefaultTransactionManager.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/DefaultTransactionManager.java new file mode 100644 index 000000000..5b318b5f7 --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/DefaultTransactionManager.java @@ -0,0 +1,366 @@ +package org.fisco.bcos.sdk.v3.transaction.manager.v2; + +import java.math.BigInteger; +import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.client.protocol.response.Call; +import org.fisco.bcos.sdk.v3.model.TransactionReceipt; +import org.fisco.bcos.sdk.v3.model.callback.RespCallback; +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; + +/** + * Default transaction manager: + * + *

use default jni sign method use default client key pair to sign tx + */ +public class DefaultTransactionManager extends TransactionManager { + + protected DefaultTransactionManager(Client client) { + super(client); + } + + /** + * Send tx with abi field + * + * @param to to address + * @param data input data + * @param value transfer value + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransaction( + String to, String data, BigInteger value, String abi, boolean constructor) { + return null; + } + + /** + * Send tx with gasPrice and gasLimit fields + * + * @param to to address + * @param data input data + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + String abi, + boolean constructor) { + return null; + } + + /** + * Send tx with gasPrice and gasLimit fields + * + * @param to to address + * @param data input data + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor) { + return null; + } + + /** + * Send tx with abi field asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @param callback + * @return receipt + */ + @Override + protected String asyncSendTransaction( + String to, + String data, + BigInteger value, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with gasPrice and gasLimit fields asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @param callback + * @return receipt + */ + @Override + protected String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with blockLimit and nonce fields asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger blockLimit, + BigInteger nonce, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with gasPrice and gasLimit fields asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with EIP1559 + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + String abi, + boolean constructor) { + return null; + } + + /** + * Send tx with EIP1559 + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor) { + return null; + } + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send call + * + * @param to to address + * @param data input data + * @return call result + */ + @Override + protected Call sendCall(String to, String data) { + return null; + } + + /** + * Send call with signature of call data + * + * @param to to address + * @param data input data + * @param signature signature of call data + */ + @Override + protected Call sendCall(String to, String data, String signature) { + return null; + } + + /** + * Send call asynchronously + * + * @param to to address + * @param data input data + * @param callback callback function + */ + @Override + protected void asyncSendCall(String to, String data, RespCallback callback) {} + + /** + * Send call asynchronously with signature of call data + * + * @param to to address + * @param data input data + * @param signature signature of call data + * @param callback callback function + */ + @Override + protected void asyncSendCall( + String to, String data, String signature, RespCallback callback) {} +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/ProxySignTransactionManager.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/ProxySignTransactionManager.java new file mode 100644 index 000000000..bc1cc22cd --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/ProxySignTransactionManager.java @@ -0,0 +1,567 @@ +package org.fisco.bcos.sdk.v3.transaction.manager.v2; + +import java.math.BigInteger; +import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.client.protocol.response.Call; +import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; +import org.fisco.bcos.sdk.v3.model.TransactionReceipt; +import org.fisco.bcos.sdk.v3.model.callback.InjectFetcher; +import org.fisco.bcos.sdk.v3.model.callback.RespCallback; +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; +import org.fisco.bcos.sdk.v3.transaction.signer.TransactionJniSignerService; +import org.fisco.bcos.sdk.v3.transaction.signer.TransactionSignerInterface; + +/** + * ProxySignTransactionManager: customizable signer method, default use JNI signer. customizable key + * pair to sign, default use client key pair. + */ +public class ProxySignTransactionManager extends TransactionManager { + + private TransactionSignerInterface txSigner; + private InjectFetcher keyPairFetcher; + + public ProxySignTransactionManager(Client client) { + super(client); + txSigner = new TransactionJniSignerService(); + } + + public ProxySignTransactionManager(Client client, TransactionSignerInterface txSigner) { + super(client); + this.txSigner = txSigner; + } + + public void setTransactionSigner(TransactionSignerInterface signerInterface) { + this.txSigner = signerInterface; + } + + /** + * Send tx with abi field + * + * @param to to address + * @param data input data + * @param value transfer value + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransaction( + String to, String data, BigInteger value, String abi, boolean constructor) { + return sendTransaction(this.client.getCryptoSuite(), to, data, value, abi, constructor); + } + + public TransactionReceipt sendTransaction( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + String abi, + boolean constructor) { + return sendTransaction( + cryptoSuite, + to, + data, + value, + BigInteger.ONE, + TransferTransactionService.GAS_LIMIT, + abi, + constructor); + } + + /** + * Send tx with gasPrice and gasLimit fields + * + * @param to to address + * @param data input data + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + String abi, + boolean constructor) { + return sendTransaction( + this.client.getCryptoSuite(), + to, + data, + value, + gasPrice, + gasLimit, + abi, + constructor); + } + + public TransactionReceipt sendTransaction( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + String abi, + boolean constructor) { + return null; + } + + /** + * Send tx with gasPrice and gasLimit fields + * + * @param to to address + * @param data input data + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor) { + return sendTransaction( + this.client.getCryptoSuite(), + to, + data, + value, + gasPrice, + gasLimit, + blockLimit, + nonce, + abi, + constructor); + } + + protected TransactionReceipt sendTransaction( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor) { + return null; + } + + /** + * Send tx with abi field asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransaction( + String to, + String data, + BigInteger value, + String abi, + boolean constructor, + TransactionCallback callback) { + return asyncSendTransaction( + this.client.getCryptoSuite(), to, data, value, abi, constructor, callback); + } + + public String asyncSendTransaction( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with gasPrice and gasLimit fields asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + String abi, + boolean constructor, + TransactionCallback callback) { + return asyncSendTransaction( + this.client.getCryptoSuite(), + to, + data, + value, + gasPrice, + gasLimit, + abi, + constructor, + callback); + } + + public String asyncSendTransaction( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with blockLimit and nonce fields asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger blockLimit, + BigInteger nonce, + TransactionCallback callback) { + return null; + } + + public String asyncSendTransaction( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + BigInteger blockLimit, + BigInteger nonce, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with gasPrice and gasLimit fields asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + public String asyncSendTransaction( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with EIP1559 + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + String abi, + boolean constructor) { + return null; + } + + public TransactionReceipt sendTransactionEIP1559( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + String abi, + boolean constructor) { + return null; + } + + /** + * Send tx with EIP1559 + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + @Override + protected TransactionReceipt sendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor) { + return null; + } + + public TransactionReceipt sendTransactionEIP1559( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor) { + return null; + } + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + public String asyncSendTransactionEIP1559( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + TransactionCallback callback) { + return null; + } + + public String asyncSendTransactionEIP1559( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + TransactionCallback callback) { + return null; + } + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + @Override + protected String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + public String asyncSendTransactionEIP1559( + CryptoSuite cryptoSuite, + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor, + TransactionCallback callback) { + return null; + } + + /** + * Send call + * + * @param to to address + * @param data input data + * @return call result + */ + @Override + protected Call sendCall(String to, String data) { + return null; + } + + /** + * Send call with signature of call data + * + * @param to to address + * @param data input data + * @param signature signature of call data + */ + @Override + protected Call sendCall(String to, String data, String signature) { + return null; + } + + /** + * Send call asynchronously + * + * @param to to address + * @param data input data + * @param callback callback function + */ + @Override + protected void asyncSendCall(String to, String data, RespCallback callback) {} + + /** + * Send call asynchronously with signature of call data + * + * @param to to address + * @param data input data + * @param signature signature of call data + * @param callback callback function + */ + @Override + protected void asyncSendCall( + String to, String data, String signature, RespCallback callback) {} +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/TransactionManager.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/TransactionManager.java new file mode 100644 index 000000000..7cf6c5dc8 --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/TransactionManager.java @@ -0,0 +1,428 @@ +package org.fisco.bcos.sdk.v3.transaction.manager.v2; + +import java.math.BigInteger; +import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.client.protocol.response.Call; +import org.fisco.bcos.sdk.v3.model.TransactionReceipt; +import org.fisco.bcos.sdk.v3.model.callback.RespCallback; +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; + +public abstract class TransactionManager { + + public class EIP1559Struct { + BigInteger maxFeePerGas; + BigInteger maxPriorityFeePerGas; + BigInteger gasLimit; + + public EIP1559Struct( + BigInteger maxFeePerGas, BigInteger maxPriorityFeePerGas, BigInteger gasLimit) { + this.maxFeePerGas = maxFeePerGas; + this.maxPriorityFeePerGas = maxPriorityFeePerGas; + this.gasLimit = gasLimit; + } + } + + protected Client client; + + public Client getClient() { + return client; + } + + protected TransactionManager(Client client) { + this.client = client; + } + + /** + * Simple send tx + * + * @param to to address + * @param data input data + * @param value transfer value + * @return receipt + */ + protected TransactionReceipt sendTransaction(String to, String data, BigInteger value) { + return sendTransaction(to, data, value, "", false); + } + + /** + * Send tx with abi field + * + * @param to to address + * @param data input data + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param value transfer value + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + protected abstract TransactionReceipt sendTransaction( + String to, String data, BigInteger value, String abi, boolean constructor); + + /** + * Send tx with gasPrice and gasLimit fields + * + * @param to to address + * @param data input data + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + protected abstract TransactionReceipt sendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + String abi, + boolean constructor); + + /** + * Send tx with blockLimit and nonce fields + * + * @param to to address + * @param data input data + * @param value transfer value + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @return receipt + */ + protected TransactionReceipt sendTransaction( + String to, String data, BigInteger value, BigInteger blockLimit, BigInteger nonce) { + return sendTransaction(to, data, value, blockLimit, nonce, "", false); + } + + /** + * Send tx with gasPrice and gasLimit fields + * + * @param to to address + * @param data input data + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + protected abstract TransactionReceipt sendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor); + + /** + * Simple send tx asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @return receipt + */ + protected String asyncSendTransaction( + String to, String data, BigInteger value, TransactionCallback callback) { + return asyncSendTransaction(to, data, value, "", false, callback); + } + + /** + * Send tx with abi field asynchronously + * + * @param to to address + * @param data input data + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param value transfer value + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + protected abstract String asyncSendTransaction( + String to, + String data, + BigInteger value, + String abi, + boolean constructor, + TransactionCallback callback); + + /** + * Send tx with gasPrice and gasLimit fields asynchronously + * + * @param to to address + * @param data input data + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + protected abstract String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + String abi, + boolean constructor, + TransactionCallback callback); + + /** + * Send tx with blockLimit and nonce fields asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param callback callback function + * @return receipt + */ + protected abstract String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger blockLimit, + BigInteger nonce, + TransactionCallback callback); + + /** + * Send tx with gasPrice and gasLimit fields asynchronously + * + * @param to to address + * @param data input data + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param value transfer value + * @param gasPrice price of gas + * @param gasLimit use limit of gas + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + protected abstract String asyncSendTransaction( + String to, + String data, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor, + TransactionCallback callback); + + /** + * Send tx with EIP1559 + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @return receipt + */ + protected TransactionReceipt sendTransactionEIP1559( + String to, String data, BigInteger value, EIP1559Struct eip1559Struct) { + return sendTransactionEIP1559(to, data, value, eip1559Struct, "", false); + } + + /** + * Send tx with EIP1559 + * + * @param to to address + * @param data input data + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + protected abstract TransactionReceipt sendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + String abi, + boolean constructor); + + /** + * Send tx with EIP1559 + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @return receipt + */ + protected TransactionReceipt sendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce) { + return sendTransactionEIP1559(to, data, value, eip1559Struct, blockLimit, nonce, "", false); + } + + /** + * Send tx with EIP1559 + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * @param constructor if you deploy contract, should set to be true + * @return receipt + */ + protected abstract TransactionReceipt sendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor); + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param callback callback function + * @return receipt + */ + protected String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + TransactionCallback callback) { + return asyncSendTransactionEIP1559(to, data, value, eip1559Struct, "", false, callback); + } + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * contract + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + protected abstract String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + String abi, + boolean constructor, + TransactionCallback callback); + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param callback callback function + * @return receipt + */ + protected abstract String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + TransactionCallback callback); + + /** + * Send tx with EIP1559 asynchronously + * + * @param to to address + * @param data input data + * @param value transfer value + * @param eip1559Struct EIP1559 transaction payload + * @param blockLimit block limit + * @param nonce tx nonce, for avoiding tx replay attack + * @param abi ABI JSON string, generated by compile contract, should fill in when you deploy + * @param constructor if you deploy contract, should set to be true + * @param callback callback function + * @return receipt + */ + protected abstract String asyncSendTransactionEIP1559( + String to, + String data, + BigInteger value, + EIP1559Struct eip1559Struct, + BigInteger blockLimit, + BigInteger nonce, + String abi, + boolean constructor, + TransactionCallback callback); + + /** + * Send call + * + * @param to to address + * @param data input data + * @return call result + */ + protected abstract Call sendCall(String to, String data); + + /** + * Send call with signature of call data + * + * @param to to address + * @param data input data + * @param signature signature of call data + */ + protected abstract Call sendCall(String to, String data, String signature); + + /** + * Send call asynchronously + * + * @param to to address + * @param data input data + * @param callback callback function + */ + protected abstract void asyncSendCall(String to, String data, RespCallback callback); + + /** + * Send call asynchronously with signature of call data + * + * @param to to address + * @param data input data + * @param signature signature of call data + * @param callback callback function + */ + protected abstract void asyncSendCall( + String to, String data, String signature, RespCallback callback); +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/TransferTransactionService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/TransferTransactionService.java new file mode 100644 index 000000000..4095473f2 --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/v2/TransferTransactionService.java @@ -0,0 +1,33 @@ +package org.fisco.bcos.sdk.v3.transaction.manager.v2; + +import java.math.BigDecimal; +import java.math.BigInteger; +import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.model.TransactionReceipt; +import org.fisco.bcos.sdk.v3.transaction.tools.Convert; + +public class TransferTransactionService { + // This is the cost to send Ether between parties + public static final BigInteger GAS_LIMIT = BigInteger.valueOf(21000); + + private ProxySignTransactionManager transactionManager; + + public TransferTransactionService(ProxySignTransactionManager transactionManager) { + this.transactionManager = transactionManager; + } + + TransferTransactionService(Client client) { + this.transactionManager = new ProxySignTransactionManager(client); + } + + public TransactionReceipt sendFunds(String to, BigDecimal value, Convert.Unit unit) { + BigDecimal weiValue = Convert.toWei(value, unit); + return transactionManager.sendTransaction(to, "", weiValue.toBigIntegerExact()); + } + + public TransactionReceipt sendFunds( + CryptoSuite cryptoSuite, String to, BigDecimal value, Convert.Unit unit) { + return sendFunds(to, value, unit); + } +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionJniSignerService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionJniSignerService.java new file mode 100644 index 000000000..a86c4d888 --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionJniSignerService.java @@ -0,0 +1,79 @@ +package org.fisco.bcos.sdk.v3.transaction.signer; + +import org.fisco.bcos.sdk.jni.common.JniException; +import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderJniObj; +import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; +import org.fisco.bcos.sdk.v3.crypto.signature.ECDSASignatureResult; +import org.fisco.bcos.sdk.v3.crypto.signature.SM2SignatureResult; +import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult; +import org.fisco.bcos.sdk.v3.model.CryptoType; +import org.fisco.bcos.sdk.v3.utils.Hex; + +public class TransactionJniSignerService implements TransactionSignerInterface { + + /** + * sign raw transaction hash string and get raw signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result, hex string + */ + @Override + public String signWithRawResult(String hash, CryptoKeyPair cryptoKeyPair) { + try { + return TransactionBuilderJniObj.signTransactionDataHash( + cryptoKeyPair.getJniKeyPair(), hash); + } catch (JniException e) { + throw new RuntimeException(e); + } + } + + /** + * sign raw transaction hash byte array and get raw signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result, hex string + */ + @Override + public String signWithRawResult(byte[] hash, CryptoKeyPair cryptoKeyPair) { + return signWithRawResult(Hex.toHexString(hash), cryptoKeyPair); + } + + /** + * sign raw transaction hash string and get signature result + * + * @param hash raw transaction hash string to be signed + * @param cryptoKeyPair keypair + * @return signature result + */ + @Override + public SignatureResult sign(String hash, CryptoKeyPair cryptoKeyPair) { + try { + String signTransaction = + TransactionBuilderJniObj.signTransactionDataHash( + cryptoKeyPair.getJniKeyPair(), hash); + if (cryptoKeyPair.getKeyType() == CryptoType.ECDSA_TYPE) { + return new ECDSASignatureResult(signTransaction); + } + if (cryptoKeyPair.getKeyType() == CryptoType.SM_TYPE) { + return new SM2SignatureResult(cryptoKeyPair.getHexPublicKey(), signTransaction); + } + } catch (JniException e) { + throw new RuntimeException(e); + } + return null; + } + + /** + * sign raw transaction hash byte array and get signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result + */ + @Override + public SignatureResult sign(byte[] hash, CryptoKeyPair cryptoKeyPair) { + return sign(Hex.toHexString(hash), cryptoKeyPair); + } +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerInterface.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerInterface.java index 8fc6121be..eaba7af97 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerInterface.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerInterface.java @@ -24,7 +24,7 @@ */ public interface TransactionSignerInterface { /** - * sign raw transaction hash string and get signatrue result + * sign raw transaction hash string and get signature result * * @param hash raw transaction hash string to be signed * @param cryptoKeyPair keypair @@ -33,11 +33,29 @@ public interface TransactionSignerInterface { SignatureResult sign(String hash, CryptoKeyPair cryptoKeyPair); /** - * sign raw transaction hash byte array and get signatrue result + * sign raw transaction hash byte array and get signature result * * @param hash raw transaction hash byte array to be signed * @param cryptoKeyPair keypair * @return signature result */ SignatureResult sign(byte[] hash, CryptoKeyPair cryptoKeyPair); + + /** + * sign raw transaction hash string and get raw signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result, hex string + */ + String signWithRawResult(String hash, CryptoKeyPair cryptoKeyPair); + + /** + * sign raw transaction hash byte array and get raw signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result, hex string + */ + String signWithRawResult(byte[] hash, CryptoKeyPair cryptoKeyPair); } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerService.java index 7f55291d1..bad82cf8b 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerService.java @@ -44,6 +44,30 @@ public SignatureResult sign(byte[] hash, CryptoKeyPair cryptoKeyPair) { return signature.sign(hash, cryptoKeyPair); } + /** + * sign raw transaction hash string and get raw signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result, hex string + */ + @Override + public String signWithRawResult(String hash, CryptoKeyPair cryptoKeyPair) { + return signature.sign(hash, cryptoKeyPair).convertToString(); + } + + /** + * sign raw transaction hash byte array and get raw signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result, hex string + */ + @Override + public String signWithRawResult(byte[] hash, CryptoKeyPair cryptoKeyPair) { + return sign(hash, cryptoKeyPair).convertToString(); + } + /** @return the signature */ public Signature getSignature() { return signature; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerWithRemoteSignService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerWithRemoteSignService.java index 598142cc6..3ab21b0a4 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerWithRemoteSignService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/signer/TransactionSignerWithRemoteSignService.java @@ -16,6 +16,7 @@ import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult; +import org.fisco.bcos.sdk.v3.utils.Hex; public class TransactionSignerWithRemoteSignService implements TransactionSignerInterface, AsyncTransactionSignercInterface { @@ -30,7 +31,7 @@ public TransactionSignerWithRemoteSignService( @Override public SignatureResult sign(String rawTxHash, CryptoKeyPair cryptoKeyPair) { - return sign(rawTxHash.getBytes(), cryptoKeyPair); + return sign(Hex.decode(rawTxHash), cryptoKeyPair); } @Override @@ -38,6 +39,30 @@ public SignatureResult sign(byte[] rawTxHash, CryptoKeyPair cryptoKeyPair) { return transactionSignProvider.requestForSign(rawTxHash, encryptType); } + /** + * sign raw transaction hash string and get raw signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result, hex string + */ + @Override + public String signWithRawResult(String hash, CryptoKeyPair cryptoKeyPair) { + return sign(Hex.decode(hash), cryptoKeyPair).convertToString(); + } + + /** + * sign raw transaction hash byte array and get raw signature result + * + * @param hash raw transaction hash byte array to be signed + * @param cryptoKeyPair keypair + * @return signature result, hex string + */ + @Override + public String signWithRawResult(byte[] hash, CryptoKeyPair cryptoKeyPair) { + return sign(hash, cryptoKeyPair).convertToString(); + } + @Override public void signAsync(byte[] dataToSign, RemoteSignCallbackInterface transactionSignCallback) { transactionSignProvider.requestForSignAsync( diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/tools/Convert.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/tools/Convert.java new file mode 100644 index 000000000..5e8b9ed3e --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/tools/Convert.java @@ -0,0 +1,64 @@ +package org.fisco.bcos.sdk.v3.transaction.tools; + +import java.math.BigDecimal; + +public final class Convert { + private Convert() {} + + public static BigDecimal fromWei(String number, Unit unit) { + return fromWei(new BigDecimal(number), unit); + } + + public static BigDecimal fromWei(BigDecimal number, Unit unit) { + return number.divide(unit.getWeiFactor()); + } + + public static BigDecimal toWei(String number, Unit unit) { + return toWei(new BigDecimal(number), unit); + } + + public static BigDecimal toWei(BigDecimal number, Unit unit) { + return number.multiply(unit.getWeiFactor()); + } + + public enum Unit { + WEI("wei", 0), + KWEI("kwei", 3), + MWEI("mwei", 6), + GWEI("gwei", 9), + SZABO("szabo", 12), + FINNEY("finney", 15), + ETHER("ether", 18), + KETHER("kether", 21), + METHER("mether", 24), + GETHER("gether", 27); + + private String name; + private BigDecimal weiFactor; + + Unit(String name, int factor) { + this.name = name; + this.weiFactor = BigDecimal.TEN.pow(factor); + } + + public BigDecimal getWeiFactor() { + return weiFactor; + } + + @Override + public String toString() { + return name; + } + + public static Unit fromString(String name) { + if (name != null) { + for (Unit unit : Unit.values()) { + if (name.equalsIgnoreCase(unit.name)) { + return unit; + } + } + } + return Unit.valueOf(name); + } + } +}