def sign_eip712_channel_open(eth_privkey: bytes, chainId: int, verifyingContract: bytes, ctype: int, openedAt: int, marketId: bytes, channelId: bytes, actor: bytes, delegate: bytes, marketmaker: bytes, recipient: bytes, amount: int) -> bytes: """ :param eth_privkey: Ethereum address of buyer (a raw 20 bytes Ethereum address). :type eth_privkey: bytes :return: The signature according to EIP712 (32+32+1 raw bytes). :rtype: bytes """ # create EIP712 typed data object data = _create_eip712_channel_open(chainId, verifyingContract, ctype, openedAt, marketId, channelId, actor, delegate, marketmaker, recipient, amount) # FIXME: this fails on PyPy (but ot on CPy!) with # Unknown format b'%M\xff\xcd2w\xc0\xb1f\x0fmB\xef\xbbuN\xda\xba\xbc+', attempted to normalize to 0x254dffcd3277c0b1660f6d42efbb754edababc2b _args = signing.sign_typed_data(data, eth_privkey) signature = signing.v_r_s_to_signature(*_args) assert len(signature) == _EIP712_SIG_LEN return signature
def sign(eth_privkey, data): # FIXME: this fails on PyPy (but ot on CPy!) with # Unknown format b'%M\xff\xcd2w\xc0\xb1f\x0fmB\xef\xbbuN\xda\xba\xbc+', attempted to normalize to 0x254dffcd3277c0b1660f6d42efbb754edababc2b _args = signing.sign_typed_data(data, eth_privkey) signature = signing.v_r_s_to_signature(*_args) assert len(signature) == _EIP712_SIG_LEN return signature
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))
def sign_eip712_data(eth_privkey, channel_adr, channel_seq, balance, is_final=False): """ :param eth_privkey: Ethereum address of buyer (a raw 20 bytes Ethereum address). :type eth_privkey: bytes :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 is_final: Flag to indicate the transaction is considered final. :type is_final: bool :return: The signature according to EIP712 (32+32+1 raw bytes). :rtype: bytes """ assert type(eth_privkey) == bytes and len(eth_privkey) == 32 assert type(channel_adr) == bytes and len(channel_adr) == 20 assert type(channel_seq) == int and channel_seq > 0 assert type(balance) == int and balance >= 0 assert type(is_final) == bool verifying_adr = a2b_hex('0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B'[2:]) # make a private key object from the raw private key bytes # pkey = eth_keys.keys.PrivateKey(eth_privkey) # get the canonical address of the account # eth_adr = web3.Web3.toChecksumAddress(pkey.public_key.to_canonical_address()) # eth_adr = pkey.public_key.to_canonical_address() # create EIP712 typed data object data = _create_eip712_data(verifying_adr, channel_adr, channel_seq, balance, is_final) # FIXME: this fails on PyPy (but ot on CPy!) with # Unknown format b'%M\xff\xcd2w\xc0\xb1f\x0fmB\xef\xbbuN\xda\xba\xbc+', attempted to normalize to 0x254dffcd3277c0b1660f6d42efbb754edababc2b _args = signing.sign_typed_data(data, eth_privkey) signature = signing.v_r_s_to_signature(*_args) assert len(signature) == _EIP712_SIG_LEN return signature
def sign(eth_privkey, data): """ Sign the given data using the given Ethereum private key. :param eth_privkey: Signing key. :param data: Data to sign. :return: Signature. """ _args = signing.sign_typed_data(data, eth_privkey) signature = signing.v_r_s_to_signature(*_args) assert len(signature) == _EIP712_SIG_LEN return signature
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))
def sign_eip712_data(eth_privkey, channel_adr, channel_seq, balance, is_final=False): """ :param eth_privkey: Ethereum address of buyer (a raw 20 bytes Ethereum address). :type eth_privkey: bytes :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 is_final: Flag to indicate the transaction is considered final. :type is_final: bool :return: The signature according to EIP712 (32+32+1 raw bytes). :rtype: bytes """ assert type(eth_privkey) == bytes and len(eth_privkey) == 32 assert type(channel_adr) == bytes and len(channel_adr) == 20 assert type(channel_seq) == int and channel_seq > 0 assert type(balance) == int and balance >= 0 assert type(is_final) == bool verifying_adr = '0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B' # make a private key object from the raw private key bytes # pkey = eth_keys.keys.PrivateKey(eth_privkey) # get the canonical address of the account # eth_adr = web3.Web3.toChecksumAddress(pkey.public_key.to_canonical_address()) # eth_adr = pkey.public_key.to_canonical_address() # create EIP712 typed data object data = _create_eip712_data(verifying_adr, channel_adr, channel_seq, balance, is_final) signature = signing.v_r_s_to_signature( *signing.sign_typed_data(data, eth_privkey)) assert len(signature) == _EIP712_SIG_LEN return signature
def sign_eip712_data(eth_privkey, ed25519_pubkey, key_id, channel_seq, amount, balance): """ :param buyer_adr: Ethereum address of buyer (a raw 20 bytes Ethereum address). :type buyer_adr: bytes :param buyer_pubkey: Public key of buyer (a raw 32 bytes Ed25519 public key). :type buyer_pubkey: bytes :param key_id: Unique ID of the key bought/sold (a UUID in raw 16 bytes) :type key_id: bytes :param channel_seq: Payment channel off-chain transaction sequence number. :type channel_seq: int :param amount: Amount paid/earned for the key. :type amount: int :param balance: Balance remaining in the payment/paying channel after buying/selling the key. :type balance: int :return: The signature according to EIP712 (32+32+1 raw bytes). :rtype: bytes """ assert type(eth_privkey) == bytes and len(eth_privkey) == 32 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 # make a private key object from the raw private key bytes pkey = eth_keys.keys.PrivateKey(eth_privkey) # get the canonical address of the account # eth_adr = web3.Web3.toChecksumAddress(pkey.public_key.to_canonical_address()) eth_adr = pkey.public_key.to_canonical_address() # create EIP712 typed data object data = _create_eip712_data(eth_adr, ed25519_pubkey, key_id, channel_seq, amount, balance) signature = signing.v_r_s_to_signature( *signing.sign_typed_data(data, eth_privkey)) assert len(signature) == _EIP712_SIG_LEN return signature
def sign(eth_privkey: bytes, data: Dict[str, Any]) -> bytes: """ Sign the given data using the given Ethereum private key. :param eth_privkey: Signing key. :param data: Data to sign. :return: Signature. """ # internally, this is using py_eth_sig_utils.eip712.encode_typed_data _args = signing.sign_typed_data(data, eth_privkey) # serialize structured signature (v, r, s) into bytes signature = signing.v_r_s_to_signature(*_args) # be paranoid about what to expect assert type(signature) == bytes and len(signature) == _EIP712_SIG_LEN return signature
def sign_eip712_member_login(eth_privkey: bytes, chainId: int, verifyingContract: bytes, member: bytes, loggedIn: int, timestamp: int, member_email: str, client_pubkey: bytes) -> bytes: """ :param eth_privkey: Ethereum address of buyer (a raw 20 bytes Ethereum address). :type eth_privkey: bytes :return: The signature according to EIP712 (32+32+1 raw bytes). :rtype: bytes """ assert type(eth_privkey) == bytes and len(eth_privkey) == 32 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 # make a private key object from the raw private key bytes # pkey = eth_keys.keys.PrivateKey(eth_privkey) # get the canonical address of the account # eth_adr = web3.Web3.toChecksumAddress(pkey.public_key.to_canonical_address()) # eth_adr = pkey.public_key.to_canonical_address() # create EIP712 typed data object data = _create_eip712_member_login(chainId, verifyingContract, member, loggedIn, timestamp, member_email, client_pubkey) # FIXME: this fails on PyPy (but ot on CPy!) with # Unknown format b'%M\xff\xcd2w\xc0\xb1f\x0fmB\xef\xbbuN\xda\xba\xbc+', attempted to normalize to 0x254dffcd3277c0b1660f6d42efbb754edababc2b _args = signing.sign_typed_data(data, eth_privkey) signature = signing.v_r_s_to_signature(*_args) assert len(signature) == _EIP712_SIG_LEN return signature
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))
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))