Example #1
0
def get_proposer(canonicalized_block):
    """Extract the signer from a block as retrieved from Parity via its JSON RPC interface."""
    if canonicalized_block.signature == EMPTY_SIGNATURE:
        return EMPTY_ADDRESS

    message = bare_hash(canonicalized_block)
    signature = keys.Signature(canonicalized_block.signature)
    recovered_public_key = keys.ecdsa_recover(message, signature)
    return recovered_public_key.to_canonical_address()
Example #2
0
def get_signing_address(signed_hash: Union[bytes, str], v: int, r: int, s: int) -> str:
    """
    :return: checksummed ethereum address, for example `0x568c93675A8dEb121700A6FAdDdfE7DFAb66Ae4A`
    :rtype: str or `NULL_ADDRESS` if signature is not valid
    """
    try:
        public_key = keys.ecdsa_recover(signed_hash, keys.Signature(vrs=(v - 27, r, s)))
        return public_key.to_checksum_address()
    except BadSignature:
        return NULL_ADDRESS
Example #3
0
def recover(data: bytes,
            signature: Signature,
            hasher: Callable[[bytes], bytes] = eth_sign_sha3) -> Address:
    """ eth_recover address from data hash and signature """
    _hash = hasher(data)

    # ecdsa_recover accepts only standard [0,1] v's so we add support also for [27,28] here
    # anything else will raise BadSignature
    if signature[-1] >= 27:  # support (0,1,27,28) v values
        signature = Signature(signature[:-1] + bytes([signature[-1] - 27]))

    try:
        sig = keys.Signature(signature_bytes=signature)
        public_key = keys.ecdsa_recover(message_hash=_hash, signature=sig)
    except BadSignature as e:
        raise InvalidSignature from e
    return public_key.to_canonical_address()