From 7f95c1e9f7daaa7439c21baee82cd8770bf46335 Mon Sep 17 00:00:00 2001 From: Tim Molter Date: Wed, 2 Aug 2023 22:59:12 +0200 Subject: [PATCH 1/4] allow access to underlying raw transaction signing method --- lib/src/core/client.dart | 2 +- lib/src/core/transaction_signer.dart | 2 +- test/core/sign_transaction_test.dart | 39 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/src/core/client.dart b/lib/src/core/client.dart index 79195648..e2a940db 100644 --- a/lib/src/core/client.dart +++ b/lib/src/core/client.dart @@ -380,7 +380,7 @@ class Web3Client { client: this, ); - return _signTransaction( + return signTransactionRaw( signingInput.transaction, signingInput.credentials, signingInput.chainId, diff --git a/lib/src/core/transaction_signer.dart b/lib/src/core/transaction_signer.dart index 6c9cae4f..df573338 100644 --- a/lib/src/core/transaction_signer.dart +++ b/lib/src/core/transaction_signer.dart @@ -100,7 +100,7 @@ Uint8List prependTransactionType(int type, Uint8List transaction) { ..setAll(1, transaction); } -Uint8List _signTransaction( +Uint8List signTransactionRaw( Transaction transaction, Credentials c, int? chainId, diff --git a/test/core/sign_transaction_test.dart b/test/core/sign_transaction_test.dart index 626a268b..4452f649 100644 --- a/test/core/sign_transaction_test.dart +++ b/test/core/sign_transaction_test.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:typed_data'; import 'package:http/http.dart'; import 'package:test/test.dart'; @@ -149,6 +150,44 @@ void main() { }); }); + test('sign eip 1559 transaction without client', () { + final data = jsonDecode(rawJson) as List; + + Future.forEach(data, (element) { + final tx = element as Map; + final credentials = + EthPrivateKey.fromHex(strip0x(tx['privateKey'] as String)); + final transaction = Transaction( + from: credentials.address, + to: EthereumAddress.fromHex(tx['to'] as String), + nonce: tx['nonce'] as int, + maxGas: tx['gasLimit'] as int, + value: EtherAmount.inWei(BigInt.from(tx['value'] as int)), + maxFeePerGas: EtherAmount.fromBigInt( + EtherUnit.wei, + BigInt.from(tx['maxFeePerGas'] as int), + ), + maxPriorityFeePerGas: EtherAmount.fromBigInt( + EtherUnit.wei, + BigInt.from(tx['maxPriorityFeePerGas'] as int), + ), + data: tx['data'] ?? Uint8List(0), + ); + + final signature = signTransactionRaw(transaction, credentials, 4); + + expect( + bytesToHex( + uint8ListFromList( + rlp.encode(prependTransactionType(0x02, signature)), + ), + ), + strip0x(tx['signedTransactionRLP'] as String), + ); + }); + + }); + test('signs transactions', () async { final credentials = EthPrivateKey.fromHex( 'a2fd51b96dc55aeb14b30d55a6b3121c7b9c599500c1beb92a389c3377adc86e', From d1c7599043de0a20928aa90ad36088f85a238f6d Mon Sep 17 00:00:00 2001 From: Tim Molter Date: Wed, 2 Aug 2023 23:17:11 +0200 Subject: [PATCH 2/4] change method signature to default chainId to 1 --- lib/src/core/client.dart | 2 +- lib/src/core/transaction_signer.dart | 6 +++--- test/core/sign_transaction_test.dart | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/src/core/client.dart b/lib/src/core/client.dart index e2a940db..8705c16b 100644 --- a/lib/src/core/client.dart +++ b/lib/src/core/client.dart @@ -383,7 +383,7 @@ class Web3Client { return signTransactionRaw( signingInput.transaction, signingInput.credentials, - signingInput.chainId, + chainId: signingInput.chainId, ); } diff --git a/lib/src/core/transaction_signer.dart b/lib/src/core/transaction_signer.dart index df573338..9468666b 100644 --- a/lib/src/core/transaction_signer.dart +++ b/lib/src/core/transaction_signer.dart @@ -102,9 +102,9 @@ Uint8List prependTransactionType(int type, Uint8List transaction) { Uint8List signTransactionRaw( Transaction transaction, - Credentials c, - int? chainId, -) { + Credentials c, { + int? chainId = 1, + }) { if (transaction.isEIP1559 && chainId != null) { final encodedTx = LengthTrackingByteSink(); encodedTx.addByte(0x02); diff --git a/test/core/sign_transaction_test.dart b/test/core/sign_transaction_test.dart index 4452f649..52d6dd8b 100644 --- a/test/core/sign_transaction_test.dart +++ b/test/core/sign_transaction_test.dart @@ -174,7 +174,7 @@ void main() { data: tx['data'] ?? Uint8List(0), ); - final signature = signTransactionRaw(transaction, credentials, 4); + final signature = signTransactionRaw(transaction, credentials, chainId: 4); expect( bytesToHex( From 1a3cc00679208f1a6f87e58fcb520c3e19c47a98 Mon Sep 17 00:00:00 2001 From: Tim Molter Date: Tue, 19 Sep 2023 13:07:36 +0200 Subject: [PATCH 3/4] remove firefox override as it fails the tests --- dart_test.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dart_test.yaml b/dart_test.yaml index 13793ef4..454b150d 100644 --- a/dart_test.yaml +++ b/dart_test.yaml @@ -2,7 +2,7 @@ tags: expensive: timeout: 2x -override_platforms: - firefox: - settings: - arguments: -headless \ No newline at end of file +#override_platforms: +# firefox: +# settings: +# arguments: -headless \ No newline at end of file From 8f0d5a1f219c2aa753dd934ddbad038a0463fb72 Mon Sep 17 00:00:00 2001 From: Tim Molter Date: Tue, 19 Sep 2023 14:28:05 +0200 Subject: [PATCH 4/4] update dependencies --- README.md | 6 ++++++ pubspec.yaml | 16 ++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2530c25a..be29e2a7 100644 --- a/README.md +++ b/README.md @@ -186,3 +186,9 @@ Please file feature requests and bugs at the [issue tracker][tracker]. If you want to contribute to this library, please submit a Pull Request. [tracker]: https://github.com/xclud/web3dart/issues/new + +## Testing + +```dart +dart test test +``` \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 08e542b0..eb3d00d4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,19 +9,19 @@ environment: dependencies: pointycastle: ^3.7.3 - sec: ^1.0.1 + sec: ^1.1.0 http: ">=0.13.1 <2.0.0" - uuid: ^3.0.7 + uuid: ^4.0.0 json_rpc_2: ^3.0.2 stream_transform: ^2.1.0 - stream_channel: ^2.1.1 + stream_channel: ^2.1.2 eip55: ^1.0.2 eip1559: ^0.6.2 - typed_data: ^1.3.1 + typed_data: ^1.3.2 convert: ^3.1.1 - async: ^2.10.0 - wallet: ^0.0.10 + async: ^2.11.0 + wallet: ^0.0.12+1 dev_dependencies: - test: ^1.24.0 - lints: ^2.0.0 + test: ^1.24.6 + lints: ^2.1.1