Skip to content

Commit

Permalink
Add Transaction.getUnsignedSerialized (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinullrich authored Dec 23, 2023
1 parent d9fe445 commit 3e25bcb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
26 changes: 26 additions & 0 deletions lib/src/core/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
23 changes: 3 additions & 20 deletions lib/src/core/transaction_signer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down

0 comments on commit 3e25bcb

Please sign in to comment.