Skip to content

Commit 59944dc

Browse files
committed
fix: clean-up transaction encoding and 155 logic, bugfix for wrong use
1 parent b512649 commit 59944dc

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

ape_aws/accounts.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def sign_raw_msghash(self, msghash: HexBytes | Hash32) -> Optional[MessageSignat
5151
return None
5252

5353
msg_sig = MessageSignature(**_convert_der_to_rsv(signature, 27))
54-
# TODO: Figure out how to properly compute v in `_convert_der_to_rsv`
5554
if not self.check_signature(msghash, msg_sig, recover_using_eip191=False):
55+
# NOTE: Best way to determine parity is just to try recovery
5656
msg_sig = MessageSignature(v=msg_sig.v + 1, r=msg_sig.r, s=msg_sig.s)
5757

5858
return msg_sig
@@ -86,19 +86,16 @@ def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[Tr
8686
txn_dict = txn.model_dump()
8787
# NOTE: remove this so doesn't raise below
8888
txn_dict.pop("from", None)
89-
unsigned_txn = serializable_unsigned_transaction_from_dict(txn_dict).hash()
89+
unsigned_txn_hash = serializable_unsigned_transaction_from_dict(txn_dict).hash()
9090

91-
if not (msg_sig := self.sign_raw_msghash(HexBytes(unsigned_txn))):
91+
if not (signature := self.sign_raw_msghash(HexBytes(unsigned_txn_hash))):
9292
return None
9393

94-
txn.signature = TransactionSignature(
95-
# Include 155 chain ID protection
96-
# NOTE: We already added 27 above
97-
# v=((msg_sig.v - 27) + (2 * txn.chain_id) + 35) if txn.chain_id else msg_sig.v,
98-
v=msg_sig.v - 27,
99-
# TODO: Figure out why EIP 155 protection isn't working right...
100-
r=msg_sig.r,
101-
s=msg_sig.s,
102-
)
94+
# NOTE: We already added 27 to v above, so substract it from v to normalize to {0,1}
95+
v = signature.v - 27
96+
if txn.chain_id and txn.type == 0:
97+
# Include 155 chain ID protection offset
98+
v += (2 * txn.chain_id) + 35
10399

100+
txn.signature = TransactionSignature(v=v, r=signature.r, s=signature.s)
104101
return txn

0 commit comments

Comments
 (0)