def test_issue_4_bug(): unsigned_message = '6a74f15f29c3227c5d1d2e27894da58d417a484ef53bc7aa57ee323b42ded656' v = 28 r = int("5897c2c7c7412b0a555fb6f053ddb6047c59666bbebc6f5573134e074992d841", 16) s = int("1c71d1c62b74caff8695a186e2a24dd701070ba9946748318135e3ac0950b1d4", 16) ecdsa_raw_recover(unsigned_message, (v, r, s))
def ecdsa_verify( v: int, r: int, s: int, msghash: bytes, pubkey: Union[bytes, Tuple[int, int]] ) -> bool: """ Takes a v, r, s, a pubkey, and a hash of a message to verify via ECDSA. :param v: V of sig :param r: R of sig :param s: S of sig :param bytes msghash: The hashed message to verify :param bytes pubkey: Pubkey to validate signature for :rtype: Boolean :return: Is the signature valid or not? """ if bytes == type(pubkey): pubkey = ecdsa_bytes2pub(pubkey) verify_sig = ecdsa_raw_recover(msghash, (v, r, s)) # TODO: Should this equality test be done better? return verify_sig == pubkey
def recover(self, rawhash): assert isinstance(self, EcdsaSignature) v, r, s = self if coincurve and hasattr(coincurve, "PublicKey"): try: pk = coincurve.PublicKey.from_signature_and_message( ''.join([r, s, ascii_chr(v - 27)]), rawhash, hasher=None, ) pub = pk.format(compressed=False)[1:] except BaseException: pub = b"\x00" * 64 else: r = big_endian_to_int(r) s = big_endian_to_int(s) result = ecdsa_raw_recover(rawhash, (v, r, s)) if result: x, y = result pub = encode_int32(x) + encode_int32(y) else: raise ValueError('Invalid VRS') assert len(pub) == 64 # Convert to Ethereum address return keccak_256(pub).digest()[12:]
def ecrecover_to_pub(rawhash, v, r, s): result = ecdsa_raw_recover(rawhash, (v, r, s)) if result: x, y = result pub = encode_int32(x) + encode_int32(y) else: raise ValueError('Invalid VRS') assert len(pub) == 64 return pub
def test_gpsi_bitcoin(): """Tests an API call to the General Purpose Signing Interface using a Bitcoin wallet (secp256k1)""" user, pw = create_user() clientele = create_oauth_client(user.username, pw) wallet = clientele.wallets.create(BITCOIN_TESTNET_ASSET_ID, pw) message = fresh.bs( 1024) # e.g. imagine this is a PDF or a authentication challenge signature = wallet.sign(pw, message) assert signature.curve == "secp256k1" assert hashlib.sha256(message).digest() == signature.signed_hash x, y = ecdsa_raw_recover( signature.signed_hash, (27 + signature.recover, signature.r, signature.s)) assert x == signature.x assert y == signature.y assert pubkey_to_bitcoin_address( signature, prefix=0x6f) == clientele.wallets.get(wallet.id).address
def ecrecover_to_pub(rawhash, v, r, s): if coincurve and hasattr(coincurve, "PublicKey"): try: pk = coincurve.PublicKey.from_signature_and_message( zpad(bytearray_to_bytestr(int_to_32bytearray(r)), 32) + zpad(bytearray_to_bytestr(int_to_32bytearray(s)), 32) + ascii_chr(v - 27), rawhash, hasher=None, ) pub = pk.format(compressed=False)[1:] except BaseException: pub = b"\x00" * 64 else: result = ecdsa_raw_recover(rawhash, (v, r, s)) if result: x, y = result pub = encode_int32(x) + encode_int32(y) else: raise ValueError('Invalid VRS') assert len(pub) == 64 return pub
def test_ecdsa_raw_sign(): v, r, s = ecdsa_raw_sign(b'\x35' * 32, priv) assert ecdsa_raw_recover(b'\x35' * 32, (v, r, s)) == pub
from py_ecc.secp256k1 import privtopub, ecdsa_raw_sign, ecdsa_raw_recover import binascii priv = binascii.unhexlify( '792eca682b890b31356247f2b04662bff448b6bb19ea1c8ab48da222c894ef9b') pub = ( 20033694065814990006010338153307081985267967222430278129327181081381512401190L, 72089573118161052907088366229362685603474623289048716349537937839432544970413L ) assert privtopub(priv) == pub v, r, s = ecdsa_raw_sign(b'\x35' * 32, priv) assert ecdsa_raw_recover(b'\x35' * 32, (v, r, s)) == pub print('secp256k1 tests passed')