From 469dd830dbcdbabe88a393bf1819550be536688b Mon Sep 17 00:00:00 2001 From: Dzmitry Aliaksandrau Date: Tue, 12 Mar 2024 15:39:06 +0300 Subject: [PATCH] Add chain_id for recovery_id calculation --- .../_lambda/functions/eth_client/lambda_helper.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/aws_kms_lambda_ethereum/_lambda/functions/eth_client/lambda_helper.py b/aws_kms_lambda_ethereum/_lambda/functions/eth_client/lambda_helper.py index 5aa5b91..0e4e034 100644 --- a/aws_kms_lambda_ethereum/_lambda/functions/eth_client/lambda_helper.py +++ b/aws_kms_lambda_ethereum/_lambda/functions/eth_client/lambda_helper.py @@ -129,13 +129,15 @@ def find_eth_signature(params: EthKmsParams, plaintext: bytes) -> dict: return {'r': r, 's': s} -def get_recovery_id(msg_hash: bytes, r: int, s: int, eth_checksum_addr: str) -> dict: - for idx, v in enumerate([27, 28]): +def get_recovery_id(msg_hash: bytes, r: int, s: int, eth_checksum_addr: str, chain_id: int) -> dict: + v_lower = chain_id * 2 + 35 + v_range = [v_lower, v_lower + 1] + + for v in v_range: recovered_addr = Account.recoverHash(message_hash=msg_hash, vrs=(v, r, s)) - if recovered_addr == eth_checksum_addr: - return {'recovered_addr': recovered_addr, 'v': idx} + return {'recovered_addr': recovered_addr, "y_parity": v - v_lower} return {} @@ -150,9 +152,10 @@ def assemble_tx(tx_params: dict, params: EthKmsParams, eth_checksum_addr: str) - tx_eth_recovered_pub_addr = get_recovery_id(msg_hash=tx_hash, r=tx_sig['r'], s=tx_sig['s'], - eth_checksum_addr=eth_checksum_addr) + eth_checksum_addr=eth_checksum_addr, + chain_id=tx_params["chainId"]) tx_encoded = encode_transaction(unsigned_transaction=tx_unsigned, - vrs=(tx_eth_recovered_pub_addr['v'], tx_sig['r'], tx_sig['s'])) + vrs=(tx_eth_recovered_pub_addr['y_parity'], tx_sig['r'], tx_sig['s'])) return w3.toHex(tx_encoded)