Ejemplo n.º 1
0
def recover_eip712_signer(channel_adr, channel_seq, balance, signature):
    """
    Recover the signer address the given EIP712 signature was signed with.

    :param channel_adr: Channel contract address.
    :type channel_adr: bytes

    :param channel_seq: Payment channel off-chain transaction sequence number.
    :type channel_seq: int

    :param balance: Balance remaining in the payment/paying channel after buying/selling the key.
    :type balance: int

    :param signature: The EIP712 (32+32+1 raw bytes) signature to verify.
    :type signature: bytes

    :return: The (computed) signer address the signature was signed with.
    :rtype: bytes
    """
    assert type(channel_adr) == bytes and len(channel_adr) == 20
    assert type(channel_seq) == int
    assert type(balance) == int
    assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN

    # recreate EIP712 typed data object
    data = _create_eip712_data(channel_adr, channel_seq, balance)

    # this returns the signer (checksummed) address as a string, eg "0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d"
    signer_address = signing.recover_typed_data(
        data, *signing.signature_to_v_r_s(signature))

    return a2b_hex(signer_address[2:])
Ejemplo n.º 2
0
def recover_eip712_member_login(chainId: int, verifyingContract: bytes,
                                member: bytes, loggedIn: int, timestamp: int,
                                member_email: str, client_pubkey: bytes,
                                signature: bytes) -> bytes:
    """
    Recover the signer address the given EIP712 signature was signed with.

    :return: The (computed) signer address the signature was signed with.
    :rtype: bytes
    """
    assert type(chainId) == int
    assert type(verifyingContract) == bytes and len(verifyingContract) == 20
    assert type(member) == bytes and len(member) == 20
    assert type(loggedIn) == int
    assert type(timestamp) == int
    assert type(member_email) == str
    assert type(client_pubkey) == bytes and len(client_pubkey) == 32
    assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN

    # recreate EIP712 typed data object
    data = _create_eip712_member_login(chainId, verifyingContract, member,
                                       loggedIn, timestamp, member_email,
                                       client_pubkey)

    # this returns the signer (checksummed) address as a string, eg "0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d"
    signer_address = signing.recover_typed_data(
        data, *signing.signature_to_v_r_s(signature))

    return a2b_hex(signer_address[2:])
Ejemplo n.º 3
0
def main(accounts):
    from py_eth_sig_utils import signing

    data['message'] = {
        'channel_adr': '0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B',
        'channel_seq': 39,
        'balance': 2700,
    }
    # signature: 0xe32976b152f5d3107a789bee8512741493c262984145415c1ffb3a42c1a80e7224dd52cc552bf86665dd185d9e04004eb8d783f624eeb6aab0011c21757e6bb21b

    # generate a new raw random private key
    if True:
        # maker_key
        # pkey_raw = a2b_hex('6370fd033278c143179d81c5526140625662b8daa446c22ee2d73db3707e620c')

        # consumer_delegate_key
        #pkey_raw = a2b_hex('e485d098507f54e7733a205420dfddbe58db035fa577fc294ebd14db90767a52')
        pkey_raw = a2b_hex(
            'a4985a2ed93107886e9a1f12c7b8e2e351cc1d26c42f3aab7f220f3a7d08fda6')
    else:
        pkey_raw = os.urandom(32)
    print('Using private key: {}'.format(b2a_hex(pkey_raw).decode()))

    # make a private key object from the raw private key bytes
    pkey = eth_keys.keys.PrivateKey(pkey_raw)

    # make a private account from the private key
    acct = Account.privateKeyToAccount(pkey)

    # get the public key of the account
    addr = pkey.public_key.to_canonical_address()
    print('Account address: {}'.format(b2a_hex(addr).decode()))

    # get the canonical address of the account
    caddr = web3.Web3.toChecksumAddress(addr)
    print('Account canonical address: {}'.format(caddr))

    # step-wise computation of signature
    msg_hash = signing.encode_typed_data(data)
    print('Ok, MSG_HASH = 0x{}'.format(b2a_hex(msg_hash).decode()))
    sig_vrs = utils.ecsign(msg_hash, pkey_raw)
    sig = signing.v_r_s_to_signature(*sig_vrs)

    signature = signing.v_r_s_to_signature(
        *signing.sign_typed_data(data, pkey_raw))
    assert len(signature) == 32 + 32 + 1
    #assert signature == sig
    print('Ok, signed typed data (using key {}):\nSIGNATURE = 0x{}'.format(
        caddr,
        b2a_hex(signature).decode()))

    signer_address = signing.recover_typed_data(
        data, *signing.signature_to_v_r_s(signature))
    assert signer_address == caddr
    print('Ok, verified signature was signed by {}'.format(signer_address))
Ejemplo n.º 4
0
def recover(data, signature):
    """
    Recover the Ethereum address of the signer, given the data and signature.

    :param data: Signed data.
    :param signature: Signature.
    :return: Signing address.
    """
    assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN
    signer_address = signing.recover_typed_data(data, *signing.signature_to_v_r_s(signature))

    return a2b_hex(signer_address[2:])
Ejemplo n.º 5
0
def main(accounts):
    from py_eth_sig_utils import signing, utils
    from autobahn.xbr import _util

    verifying_adr = a2b_hex('0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B'[2:])
    channel_adr = a2b_hex('0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B'[2:])

    data = _util._create_eip712_data(verifying_adr, channel_adr, 39, 2700,
                                     False)

    # use fixed or generate a new raw random private key
    if True:
        # maker_key
        pkey_raw = a2b_hex(
            'a4985a2ed93107886e9a1f12c7b8e2e351cc1d26c42f3aab7f220f3a7d08fda6')
    else:
        pkey_raw = os.urandom(32)
    print('Using private key: {}'.format(b2a_hex(pkey_raw).decode()))

    # make a private key object from the raw private key bytes
    pkey = eth_keys.keys.PrivateKey(pkey_raw)

    # make a private account from the private key
    acct = Account.privateKeyToAccount(pkey)

    # get the public key of the account
    addr = pkey.public_key.to_canonical_address()
    print('Account address: {}'.format(b2a_hex(addr).decode()))

    # get the canonical address of the account
    caddr = web3.Web3.toChecksumAddress(addr)
    print('Account canonical address: {}'.format(caddr))

    # step-wise computation of signature
    msg_hash = signing.encode_typed_data(data)
    print('Ok, MSG_HASH = 0x{}'.format(b2a_hex(msg_hash).decode()))
    sig_vrs = utils.ecsign(msg_hash, pkey_raw)
    sig = signing.v_r_s_to_signature(*sig_vrs)

    signature = signing.v_r_s_to_signature(
        *signing.sign_typed_data(data, pkey_raw))
    assert len(signature) == 32 + 32 + 1
    #assert signature == sig
    print('Ok, signed typed data (using key {}):\nSIGNATURE = 0x{}'.format(
        caddr,
        b2a_hex(signature).decode()))

    signer_address = signing.recover_typed_data(
        data, *signing.signature_to_v_r_s(signature))
    assert signer_address == caddr
    print('Ok, verified signature was signed by {}'.format(signer_address))
Ejemplo n.º 6
0
def recover_eip712_signer(eth_adr, ed25519_pubkey, key_id, channel_seq, amount,
                          balance, signature):
    """
    Recover the signer address the given EIP712 signature was signed with.

    :param eth_adr: Input typed data for signature.
    :type eth_adr: bytes

    :param ed25519_pubkey: Input typed data for signature.
    :type ed25519_pubkey: bytes

    :param key_id: Input typed data for signature.
    :type key_id: bytes

    :param channel_seq: Input typed data for signature.
    :type channel_seq: int

    :param amount: Input typed data for signature.
    :type amount: int

    :param balance: Input typed data for signature.
    :type balance: int

    :param signature: The EIP712 signature to verify.
    :type signature: bytes

    :return: The (computed) signer address the signature was signed with.
    :rtype: bytes
    """
    assert type(eth_adr) == bytes and len(eth_adr) == 20
    assert type(ed25519_pubkey) == bytes and len(ed25519_pubkey) == 32
    assert type(key_id) == bytes and len(key_id) == 16
    assert type(channel_seq) == int
    assert type(amount) == int
    assert type(balance) == int
    assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN

    # recreate EIP712 typed data object
    data = _create_eip712_data(eth_adr, ed25519_pubkey, key_id, channel_seq,
                               amount, balance)

    # this returns the signer (checksummed) address as a string, eg "0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d"
    signer_address = signing.recover_typed_data(
        data, *signing.signature_to_v_r_s(signature))

    return a2b_hex(signer_address[2:])
Ejemplo n.º 7
0
def recover_eip712_channel_close(chainId: int, verifyingContract: bytes,
                                 closeAt: int, marketId: bytes,
                                 channelId: bytes, channelSeq: int,
                                 balance: int, isFinal: bool,
                                 signature: bytes) -> bytes:
    """
    Recover the signer address the given EIP712 signature was signed with.

    :return: The (computed) signer address the signature was signed with.
    :rtype: bytes
    """
    # create EIP712 typed data object
    data = _create_eip712_channel_close(chainId, verifyingContract, closeAt,
                                        marketId, channelId, channelSeq,
                                        balance, isFinal)

    assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN
    signer_address = signing.recover_typed_data(
        data, *signing.signature_to_v_r_s(signature))

    return a2b_hex(signer_address[2:])
Ejemplo n.º 8
0
def main(accounts):
    from py_eth_sig_utils import signing

    data = data2

    # generate a new raw random private key
    if True:
        pkey_raw = a2b_hex(
            'a4985a2ed93107886e9a1f12c7b8e2e351cc1d26c42f3aab7f220f3a7d08fda6')
    else:
        pkey_raw = os.urandom(32)
    print('Using private key: {}'.format(b2a_hex(pkey_raw).decode()))

    # make a private key object from the raw private key bytes
    pkey = eth_keys.keys.PrivateKey(pkey_raw)

    # make a private account from the private key
    acct = Account.privateKeyToAccount(pkey)

    # get the public key of the account
    addr = pkey.public_key.to_canonical_address()
    print('Account address: {}'.format(b2a_hex(addr).decode()))

    # get the canonical address of the account
    caddr = web3.Web3.toChecksumAddress(addr)
    print('Account canonical address: {}'.format(caddr))

    signature = signing.v_r_s_to_signature(
        *signing.sign_typed_data(data, pkey_raw))
    assert len(signature) == 32 + 32 + 1
    print('Ok, signed typed data using {}:\nSIGNATURE = 0x{}'.format(
        caddr,
        b2a_hex(signature).decode()))

    signer_address = signing.recover_typed_data(
        data, *signing.signature_to_v_r_s(signature))
    assert signer_address == caddr
    print('Ok, verified signature was signed by {}'.format(signer_address))
Ejemplo n.º 9
0
def recover_eip712_channel_open(chainId: int, verifyingContract: bytes,
                                ctype: int, openedAt: int, marketId: bytes,
                                channelId: bytes, actor: bytes,
                                delegate: bytes, marketmaker: bytes,
                                recipient: bytes, amount: int,
                                signature: bytes) -> bytes:
    """
    Recover the signer address the given EIP712 signature was signed with.

    :return: The (computed) signer address the signature was signed with.
    :rtype: bytes
    """
    # create EIP712 typed data object
    data = _create_eip712_channel_open(chainId, verifyingContract, ctype,
                                       openedAt, marketId, channelId, actor,
                                       delegate, marketmaker, recipient,
                                       amount)

    assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN
    signer_address = signing.recover_typed_data(
        data, *signing.signature_to_v_r_s(signature))

    return a2b_hex(signer_address[2:])
Ejemplo n.º 10
0
def recover_eip712_member_register(chainId: int, verifyingContract: bytes, member: bytes, registered: int,
                                   eula: str, profile: str, signature: bytes) -> bytes:
    """
    Recover the signer address the given EIP712 signature was signed with.

    :return: The (computed) signer address the signature was signed with.
    :rtype: bytes
    """
    assert type(chainId) == int
    assert type(verifyingContract) == bytes and len(verifyingContract) == 20
    assert type(member) == bytes and len(member) == 20
    assert type(registered) == int
    assert type(eula) == str
    assert profile is None or type(profile) == str
    assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN

    # recreate EIP712 typed data object
    data = _create_eip712_member_register(chainId, verifyingContract, member, registered, eula, profile)

    # this returns the signer (checksummed) address as a string, eg "0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d"
    signer_address = signing.recover_typed_data(data, *signing.signature_to_v_r_s(signature))

    return a2b_hex(signer_address[2:])
Ejemplo n.º 11
0
def main(accounts):
    from py_eth_sig_utils import signing

    data = data2

    # generate a new raw random private key
    if True:
        # maker_key
        # pkey_raw = a2b_hex('6370fd033278c143179d81c5526140625662b8daa446c22ee2d73db3707e620c')

        # consumer_delegate_key
        pkey_raw = a2b_hex('e485d098507f54e7733a205420dfddbe58db035fa577fc294ebd14db90767a52')
    else:
        pkey_raw = os.urandom(32)
    print('Using private key: {}'.format(b2a_hex(pkey_raw).decode()))

    # make a private key object from the raw private key bytes
    pkey = eth_keys.keys.PrivateKey(pkey_raw)

    # make a private account from the private key
    acct = Account.privateKeyToAccount(pkey)

    # get the public key of the account
    addr = pkey.public_key.to_canonical_address()
    print('Account address: {}'.format(b2a_hex(addr).decode()))

    # get the canonical address of the account
    caddr = web3.Web3.toChecksumAddress(addr)
    print('Account canonical address: {}'.format(caddr))

    signature = signing.v_r_s_to_signature(*signing.sign_typed_data(data, pkey_raw))
    assert len(signature) == 32 + 32 + 1
    print('Ok, signed typed data using {}:\nSIGNATURE = 0x{}'.format(caddr, b2a_hex(signature).decode()))

    signer_address = signing.recover_typed_data(data, *signing.signature_to_v_r_s(signature))
    assert signer_address == caddr
    print('Ok, verified signature was signed by {}'.format(signer_address))
Ejemplo n.º 12
0
def recover(data, signature):
    assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN
    signer_address = signing.recover_typed_data(data, *signing.signature_to_v_r_s(signature))

    return a2b_hex(signer_address[2:])