Created
February 17, 2024 18:58
-
-
Save mattlockyer/431426c48972e1deebdcb4c801759a9d to your computer and use it in GitHub Desktop.
Produce the same Ethereum address using non-deterministic ECDSA signatures (r,s)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# references: | |
# https://ethereum.stackexchange.com/questions/114680/is-it-possible-to-use-non-deterministic-ecdsa-signatures-schemes-for-signing-eth | |
# https://eth-account.readthedocs.io/en/stable/eth_account.html#eth_account.account.Account.recover_message | |
from ecdsa import SigningKey, SECP256k1 | |
from eth_account import Account | |
from eth_account.messages import encode_defunct, defunct_hash_message | |
message = encode_defunct(text="I♥SF") | |
mh = defunct_hash_message(text="I♥SF") | |
def encode_func(r, s, _): | |
return f"0x{r:032x}", f"0x{s:032x}" | |
# address = 0x71556C38F44e17EC21F355Bd18416155000BF5a6 | |
sk = SigningKey.from_string(bytes.fromhex(f"{0x1337:064x}"), curve=SECP256k1) | |
out = sk.sign_digest(mh, sigencode=encode_func) | |
print("r, s", out) | |
vrs = ( | |
27, | |
out[0], | |
out[1], | |
) | |
print("v 0", Account.recover_message(message, vrs=vrs)) | |
vrs = ( | |
28, | |
out[0], | |
out[1], | |
) | |
print("v 1", Account.recover_message(message, vrs=vrs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment