Skip to content

Commit

Permalink
allow access to underlying raw transaction signing method (#116)
Browse files Browse the repository at this point in the history
* allow access to underlying raw transaction signing method

* change method signature to default chainId to 1
  • Loading branch information
tim-eucalyptus authored Aug 3, 2023
1 parent baac0ea commit 1f8fc65
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/src/core/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ class Web3Client {
client: this,
);

return _signTransaction(
return signTransactionRaw(
signingInput.transaction,
signingInput.credentials,
signingInput.chainId,
chainId: signingInput.chainId,
);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/core/transaction_signer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ Uint8List prependTransactionType(int type, Uint8List transaction) {
..setAll(1, transaction);
}

Uint8List _signTransaction(
Uint8List signTransactionRaw(
Transaction transaction,
Credentials c,
int? chainId,
) {
Credentials c, {
int? chainId = 1,
}) {
if (transaction.isEIP1559 && chainId != null) {
final encodedTx = LengthTrackingByteSink();
encodedTx.addByte(0x02);
Expand Down
39 changes: 39 additions & 0 deletions test/core/sign_transaction_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:http/http.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -149,6 +150,44 @@ void main() {
});
});

test('sign eip 1559 transaction without client', () {
final data = jsonDecode(rawJson) as List<dynamic>;

Future.forEach(data, (element) {
final tx = element as Map<String, dynamic>;
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, chainId: 4);

expect(
bytesToHex(
uint8ListFromList(
rlp.encode(prependTransactionType(0x02, signature)),
),
),
strip0x(tx['signedTransactionRLP'] as String),
);
});

});

test('signs transactions', () async {
final credentials = EthPrivateKey.fromHex(
'a2fd51b96dc55aeb14b30d55a6b3121c7b9c599500c1beb92a389c3377adc86e',
Expand Down

0 comments on commit 1f8fc65

Please sign in to comment.