diff --git a/lib/src/core/transaction.dart b/lib/src/core/transaction.dart index 20d589ab..de7a59d7 100644 --- a/lib/src/core/transaction.dart +++ b/lib/src/core/transaction.dart @@ -95,4 +95,30 @@ class Transaction { } bool get isEIP1559 => maxFeePerGas != null || maxPriorityFeePerGas != null; + + /// The transaction pre-image. + /// + /// The hash of this is the digest which needs to be signed to + /// authorize this transaction. + Uint8List getUnsignedSerialized({ + int? chainId = 1, + }) { + if (isEIP1559 && chainId != null) { + final encodedTx = LengthTrackingByteSink(); + encodedTx.addByte(0x02); + encodedTx.add( + rlp.encode(_encodeEIP1559ToRlp(this, null, BigInt.from(chainId))), + ); + + encodedTx.close(); + + return encodedTx.asBytes(); + } + + final innerSignature = chainId == null + ? null + : MsgSignature(BigInt.zero, BigInt.zero, chainId); + + return uint8ListFromList(rlp.encode(_encodeToRlp(this, innerSignature))); + } } diff --git a/lib/src/core/transaction_signer.dart b/lib/src/core/transaction_signer.dart index acd16b58..c681dda6 100644 --- a/lib/src/core/transaction_signer.dart +++ b/lib/src/core/transaction_signer.dart @@ -105,33 +105,16 @@ Uint8List signTransactionRaw( Credentials c, { int? chainId = 1, }) { - if (transaction.isEIP1559 && chainId != null) { - final encodedTx = LengthTrackingByteSink(); - encodedTx.addByte(0x02); - encodedTx.add( - rlp.encode(_encodeEIP1559ToRlp(transaction, null, BigInt.from(chainId))), - ); - - encodedTx.close(); - final signature = c.signToEcSignature( - encodedTx.asBytes(), - chainId: chainId, - isEIP1559: transaction.isEIP1559, - ); + final encoded = transaction.getUnsignedSerialized(chainId: chainId); + final signature = c.signToEcSignature(encoded, chainId: chainId, isEIP1559: transaction.isEIP1559); + if (transaction.isEIP1559 && chainId != null) { return uint8ListFromList( rlp.encode( _encodeEIP1559ToRlp(transaction, signature, BigInt.from(chainId)), ), ); } - final innerSignature = - chainId == null ? null : MsgSignature(BigInt.zero, BigInt.zero, chainId); - - final encoded = - uint8ListFromList(rlp.encode(_encodeToRlp(transaction, innerSignature))); - final signature = c.signToEcSignature(encoded, chainId: chainId); - return uint8ListFromList(rlp.encode(_encodeToRlp(transaction, signature))); }