|
7 | 7 | import org.fisco.bcos.sdk.jni.common.JniException; |
8 | 8 | import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV1JniObj; |
9 | 9 | import org.fisco.bcos.sdk.jni.utilities.tx.TransactionVersion; |
| 10 | +import org.fisco.bcos.sdk.jni.utilities.tx.TxPair; |
10 | 11 | import org.fisco.bcos.sdk.v3.client.Client; |
11 | 12 | import org.fisco.bcos.sdk.v3.client.protocol.model.TransactionAttribute; |
12 | 13 | import org.fisco.bcos.sdk.v3.client.protocol.request.Transaction; |
|
23 | 24 | import org.fisco.bcos.sdk.v3.transaction.gasProvider.ContractGasProvider; |
24 | 25 | import org.fisco.bcos.sdk.v3.transaction.gasProvider.DefaultGasProvider; |
25 | 26 | import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; |
| 27 | +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.AbiEncodedRequest; |
26 | 28 | import org.fisco.bcos.sdk.v3.transaction.nonce.DefaultNonceAndBlockLimitProvider; |
27 | 29 | import org.fisco.bcos.sdk.v3.transaction.nonce.NonceAndBlockLimitProvider; |
28 | 30 | import org.fisco.bcos.sdk.v3.transaction.signer.AsyncTransactionSignercInterface; |
@@ -173,6 +175,12 @@ public TransactionReceipt sendTransaction( |
173 | 175 | return client.sendTransaction(signedTransaction, false).getTransactionReceipt(); |
174 | 176 | } |
175 | 177 |
|
| 178 | + @Override |
| 179 | + public TransactionReceipt sendTransaction(AbiEncodedRequest request) throws JniException { |
| 180 | + TxPair txPair = createSignedTransaction(request); |
| 181 | + return client.sendTransaction(txPair.getSignedTx(), false).getTransactionReceipt(); |
| 182 | + } |
| 183 | + |
176 | 184 | /** |
177 | 185 | * This method is used to create a signed transaction. |
178 | 186 | * |
@@ -265,6 +273,94 @@ public String createSignedTransaction( |
265 | 273 | client.getExtraData()); |
266 | 274 | } |
267 | 275 |
|
| 276 | + @Override |
| 277 | + public TxPair createSignedTransaction(AbiEncodedRequest request) throws JniException { |
| 278 | + if (!request.isTransactionEssentialSatisfy()) { |
| 279 | + throw new JniException("Transaction essential fields are not satisfied"); |
| 280 | + } |
| 281 | + int transactionAttribute; |
| 282 | + if (client.isWASM()) { |
| 283 | + transactionAttribute = TransactionAttribute.LIQUID_SCALE_CODEC; |
| 284 | + if (request.isCreate()) { |
| 285 | + transactionAttribute |= TransactionAttribute.LIQUID_CREATE; |
| 286 | + } |
| 287 | + } else { |
| 288 | + transactionAttribute = TransactionAttribute.EVM_ABI_CODEC; |
| 289 | + } |
| 290 | + byte[] methodId = new byte[4]; |
| 291 | + if (!request.isCreate() && (request.getEncodedData().length >= 4)) { |
| 292 | + System.arraycopy(request.getEncodedData(), 0, methodId, 0, 4); |
| 293 | + } |
| 294 | + String nonce = |
| 295 | + request.getNonce() == null ? getNonceProvider().getNonce() : request.getNonce(); |
| 296 | + BigInteger blockLimit = |
| 297 | + request.getBlockLimit() == null |
| 298 | + ? getNonceProvider().getBlockLimit(client) |
| 299 | + : request.getBlockLimit(); |
| 300 | + BigInteger gasPrice = |
| 301 | + request.getGasPrice() == null |
| 302 | + ? getGasProvider().getGasPrice(methodId) |
| 303 | + : request.getGasPrice(); |
| 304 | + BigInteger gasLimit = |
| 305 | + request.getGasLimit() == null |
| 306 | + ? getGasProvider().getGasLimit(methodId) |
| 307 | + : request.getGasLimit(); |
| 308 | + |
| 309 | + String dataHash = |
| 310 | + TransactionBuilderV1JniObj.calcTransactionDataHashWithFullFields( |
| 311 | + cryptoType, |
| 312 | + TransactionVersion.V1, |
| 313 | + client.getGroup(), |
| 314 | + client.getChainId(), |
| 315 | + request.getTo(), |
| 316 | + nonce, |
| 317 | + request.getEncodedData(), |
| 318 | + request.isCreate() ? request.getAbi() : "", |
| 319 | + blockLimit.longValue(), |
| 320 | + Numeric.toHexString(request.getValue()), |
| 321 | + Numeric.toHexString(gasPrice), |
| 322 | + gasLimit.longValue(), |
| 323 | + "", |
| 324 | + ""); |
| 325 | + CompletableFuture<SignatureResult> signFuture = new CompletableFuture<>(); |
| 326 | + SignatureResult signatureResult; |
| 327 | + try { |
| 328 | + asyncTxSigner.signAsync( |
| 329 | + Hex.decode(dataHash), |
| 330 | + signature -> { |
| 331 | + signFuture.complete(signature); |
| 332 | + return 0; |
| 333 | + }); |
| 334 | + signatureResult = |
| 335 | + signFuture.get( |
| 336 | + client.getConfigOption().getNetworkConfig().getTimeout(), |
| 337 | + TimeUnit.MILLISECONDS); |
| 338 | + } catch (Exception e) { |
| 339 | + logger.error("Sign transaction failed, error message: {}", e.getMessage(), e); |
| 340 | + throw new JniException("Sign transaction failed, error message: " + e.getMessage()); |
| 341 | + } |
| 342 | + String signedTransactionWithSignature = |
| 343 | + TransactionBuilderV1JniObj.createSignedTransactionWithSignature( |
| 344 | + signatureResult.encode(), |
| 345 | + dataHash, |
| 346 | + TransactionVersion.V1, |
| 347 | + client.getGroup(), |
| 348 | + client.getChainId(), |
| 349 | + request.getTo(), |
| 350 | + nonce, |
| 351 | + request.getEncodedData(), |
| 352 | + request.isCreate() ? request.getAbi() : "", |
| 353 | + blockLimit.longValue(), |
| 354 | + Numeric.toHexString(request.getValue()), |
| 355 | + Numeric.toHexString(gasPrice), |
| 356 | + gasLimit.longValue(), |
| 357 | + "", |
| 358 | + "", |
| 359 | + transactionAttribute, |
| 360 | + client.getExtraData()); |
| 361 | + return new TxPair(dataHash, signedTransactionWithSignature); |
| 362 | + } |
| 363 | + |
268 | 364 | /** |
269 | 365 | * Send tx with abi field asynchronously |
270 | 366 | * |
@@ -474,6 +570,14 @@ public void onResponse(TransactionReceipt receipt) { |
474 | 570 | return dataHash; |
475 | 571 | } |
476 | 572 |
|
| 573 | + @Override |
| 574 | + public String asyncSendTransaction(AbiEncodedRequest request, TransactionCallback callback) |
| 575 | + throws JniException { |
| 576 | + TxPair txPair = createSignedTransaction(request); |
| 577 | + client.sendTransactionAsync(txPair.getSignedTx(), false, callback); |
| 578 | + return txPair.getTxHash(); |
| 579 | + } |
| 580 | + |
477 | 581 | /** |
478 | 582 | * Send tx with EIP1559 |
479 | 583 | * |
|
0 commit comments