The created recoverable ECDSA signature differs from the results of other libraries. #4993
Replies: 1 comment
-
|
It looks like the signature you are getting from Python is using a non-standard signature format. The final byte (last 2 nibbles) should be either As a quick background, the recovery parameter is an artifact that a signature can be in either the "top" or "bottom" half of the elliptic curve, so it is used to indicate which to choose. The standard uses So, I think to convert the Python to be standards compliant, you can use the following Python code to convert your python signature: Or to convert the output of Ethers to match the output of Python: Let me know if that helps. (Moving to discussions) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Ethers Version
6.12.0
Search Terms
No response
Describe the Problem
The result generated when creating the recoverable ECDSA signature is different from the result generated using Python. Is there a way to generate the same result?
Code Snippet
ethers.js const rawhex1 = "0xf8ad82e69c85012a05f200830329189455d398326f99059ff775485246999027b319795580b844a9059cbb0000000000000000000000006df68f71f19081751850160118fc755bfeb036120000000000000000000000000000000000000000000000056bc75e2d631000008193a0ade014e655a2d1645efb5f37a142c2369ab96d2d962861dd353fa33fe15b76cda041b291fb742d07c0d475cb2611e9dd6010bbd052c79f99e73c7f9e3653afb6f7"; const rawhex2 = "0xf8ec8307f58584b8c6fe72830927c094bddbcbaa9cf9603b7055aad963506ede71692f1280b8830000000300000000000000000000000000000000000000000000000087250559b9145f9000000000000000000000000000000000000000000000011f35cc4934f0b52a80bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c55d398326f99059ff775485246999027b319795500006400000000000000000af935e000000000000000008193a0f63bed83d054bdecfb6852f980ed62f35922fe5b68296b603b290a3eff41fabea04b73872c7e68f541bc97eba52abf956eac4a0e198b762e1f9a872336f6748eed"; const hash1 = ethers.keccak256(rawhex1); const hash2 = ethers.keccak256(rawhex2); console.log("hash1: ", hash1); console.log("hash2: ", hash2); const signer = new ethers.SigningKey("0x48acf19375e8a27309fe5394728abc2eb6d5a0a4feb6b6c53207ca1c256a6739"); const result = ethers.concat([hash1, hash2]); const signature = signer.sign(ethers.keccak256(result)); console.log("sign: ", signature.serialized); out: sign: 0x83e4b3a6af20e58315554b5bc38a8398cfca44a75d42973a4454378b0dc9cae63c229b52341d1ddfc4e3ad4360e518c1f1363e2d21fcba507e8e2e10e266edd21c python: from web3 import Web3 from coincurve import PrivateKey hex_raw_transaction1 = '0xf8ad82e69c85012a05f200830329189455d398326f99059ff775485246999027b319795580b844a9059cbb0000000000000000000000006df68f71f19081751850160118fc755bfeb036120000000000000000000000000000000000000000000000056bc75e2d631000008193a0ade014e655a2d1645efb5f37a142c2369ab96d2d962861dd353fa33fe15b76cda041b291fb742d07c0d475cb2611e9dd6010bbd052c79f99e73c7f9e3653afb6f7' hex_raw_transaction2 = '0xf8ec8307f58584b8c6fe72830927c094bddbcbaa9cf9603b7055aad963506ede71692f1280b8830000000300000000000000000000000000000000000000000000000087250559b9145f9000000000000000000000000000000000000000000000011f35cc4934f0b52a80bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c55d398326f99059ff775485246999027b319795500006400000000000000000af935e000000000000000008193a0f63bed83d054bdecfb6852f980ed62f35922fe5b68296b603b290a3eff41fabea04b73872c7e68f541bc97eba52abf956eac4a0e198b762e1f9a872336f6748eed' private_key_hex = '48acf19375e8a27309fe5394728abc2eb6d5a0a4feb6b6c53207ca1c256a6739' tx1_hash = Web3.keccak(Web3.to_bytes(hexstr=hex_raw_transaction1)) tx2_hash = Web3.keccak(Web3.to_bytes(hexstr=hex_raw_transaction2)) print("tx1_hash:", tx1_hash.hex()) print("tx2_hash:", tx2_hash.hex()) print("tx1_hash + tx2_hash:", Web3.keccak(tx1_hash + tx2_hash).hex()) private_key = PrivateKey(bytes.fromhex(private_key_hex)) signature = private_key.sign_recoverable(Web3.keccak(tx1_hash + tx2_hash), hasher=None) print("signature:", signature.hex()) out: signature: 83e4b3a6af20e58315554b5bc38a8398cfca44a75d42973a4454378b0dc9cae63c229b52341d1ddfc4e3ad4360e518c1f1363e2d21fcba507e8e2e10e266edd201Contract ABI
Errors
Environment
No response
Environment (Other)
No response
Beta Was this translation helpful? Give feedback.
All reactions