def is_valid_signature( balance_proof: BalanceProofSignedState, sender_address: typing.Address, ) -> typing.SuccessOrError: data_that_was_signed = signing_data( balance_proof.nonce, balance_proof.transferred_amount, balance_proof.locked_amount, balance_proof.channel_address, balance_proof.locksroot, balance_proof.message_hash, ) try: # ValueError is raised if the PublicKey instantiation failed, let it # propagate because it's a memory pressure problem publickey = recover_publickey( data_that_was_signed, balance_proof.signature, ) except Exception: # pylint: disable=broad-except # secp256k1 is using bare Exception classes # raised if the recovery failed msg = 'Signature invalid, could not be recovered.' return (False, msg) is_correct_sender = sender_address == publickey_to_address(publickey) if is_correct_sender: return (True, None) msg = 'Signature was valid but the expected address does not match.' return (False, msg)
def wrap_and_validate(data): ''' Try to decode data into a message and validate the signature, might return None if the data is invalid. ''' try: first_byte = data[0] except KeyError: log.warn('data is empty') return try: message_type = CMDID_MESSAGE[first_byte] except KeyError: log.error('unknown cmdid {}'.format(first_byte)) try: message = message_type(data) except ValueError: log.error('trying to decode invalid message') assert message_type.fields_spec[ -1].name == 'signature', 'signature is not the last field' message_data = message.data[:-signature.size_bytes] try: publickey = recover_publickey(message_data, message.signature) except ValueError: log.error('invalid signature') return message, publickey
def wrap_and_validate(data): ''' Try to decode data into a message and validate the signature, might return None if the data is invalid. ''' try: first_byte = data[0] except KeyError: log.warn('data is empty') return try: message_type = CMDID_MESSAGE[first_byte] except KeyError: log.error('unknown cmdid %s', first_byte) return try: message = message_type(data) except ValueError: log.error('trying to decode invalid message') return assert message_type.fields_spec[ -1].name == 'signature', 'signature is not the last field' message_data = message.data[:-signature. size_bytes] # XXX: this slice must be from the end of the buffer message_signature = message.data[-signature.size_bytes:] try: publickey = recover_publickey(message_data, message_signature) except (ValueError, Exception): log.error('invalid signature') return return message, publickey
def decode(cls, data): packed = messages.wrap(data) if packed is None: return # signature must be at the end message_type = type(packed) signature = message_type.fields_spec[-1] assert signature.name == 'signature', 'signature is not the last field' data_that_was_signed = data[:-signature.size_bytes] message_signature = data[-signature.size_bytes:] try: publickey = recover_publickey(data_that_was_signed, message_signature) except ValueError: # raised if the signature has the wrong length log.error('invalid signature') return except TypeError as e: # raised if the PublicKey instantiation failed log.error('invalid key data: {}'.format(e.message)) return except Exception as e: # pylint: disable=broad-except # secp256k1 is using bare Exception classes: raised if the recovery failed log.error('error while recovering pubkey: {}'.format(e.message)) return message = cls.unpack(packed) # pylint: disable=no-member message.sender = publickey_to_address(publickey) return message
def wrap_and_validate(data): ''' Try to decode data into a message and validate the signature, might return None if the data is invalid. ''' try: first_byte = data[0] except KeyError: log.warn('data is empty') return try: message_type = CMDID_MESSAGE[first_byte] except KeyError: log.error('unknown cmdid %s', first_byte) return try: message = message_type(data) except ValueError: log.error('trying to decode invalid message') return assert message_type.fields_spec[-1].name == 'signature', 'signature is not the last field' # this slice must be from the end of the buffer message_data = message.data[:-signature.size_bytes] message_signature = message.data[-signature.size_bytes:] try: publickey = recover_publickey(message_data, message_signature) except (ValueError, Exception): log.error('invalid signature') return return message, publickey
def wrap_and_validate(data): ''' Try to decode data into a message and validate the signature, might return None if the data is invalid. ''' try: first_byte = data[0] except KeyError: log.warn('data is empty') return try: message_type = CMDID_MESSAGE[first_byte] except KeyError: log.error('unknown cmdid {}'.format(first_byte)) try: message = message_type(data) except ValueError: log.error('trying to decode invalid message') assert message_type.fields_spec[-1].name == 'signature', 'signature is not the last field' message_data = message.data[:-signature.size_bytes] try: publickey = recover_publickey(message_data, message.signature) except ValueError: log.error('invalid signature') return message, publickey
def wrap_and_validate(data): ''' Try to decode data into a message and validate the signature, might return None if the data is invalid. ''' try: first_byte = data[0] except KeyError: log.warn('data is empty') return try: message_type = CMDID_MESSAGE[first_byte] except KeyError: log.error('unknown cmdid %s', first_byte) return try: message = message_type(data) except ValueError: log.error('trying to decode invalid message') return assert message_type.fields_spec[ -1].name == 'signature', 'signature is not the last field' # this slice must be from the end of the buffer message_data = message.data[:-signature.size_bytes] message_signature = message.data[-signature.size_bytes:] try: publickey = recover_publickey(message_data, message_signature) except ValueError: # raised if the signature has the wrong length log.error('invalid signature') return except TypeError as e: # raised if the PublicKey instantiation failed log.error('invalid key data: {}'.format(e.message)) return except Exception as e: # secp256k1 is using bare Exception classes: raised if the recovery failed log.error('error while recovering pubkey: {}'.format(e.message)) return return message, publickey
def test_ncc(): token_library_path = get_contract_path('StandardToken.sol') token_path = get_contract_path('HumanStandardToken.sol') s = tester.state() assert s.block.number < 1150000 s.block.number = 1158001 assert s.block.number > 1150000 # Token creation lib_token = s.abi_contract(None, path=token_library_path, language="solidity") token = s.abi_contract(None, path=token_path, language="solidity", libraries={'StandardToken': lib_token.address.encode('hex')}, constructor_parameters=[10000, "raiden", 0, "rd"]) # Getter creation lib_getter = s.abi_contract(None, path=decode_lib, language="solidity") getter = s.abi_contract(None, path=getter_path, language="solidity", libraries={'Decoder': lib_getter.address.encode('hex')}) INITIATOR_PRIVKEY = tester.k0 INITIATOR_ADDRESS = privtoaddr(INITIATOR_PRIVKEY) RECIPIENT_PRIVKEY = tester.k1 RECIPIENT_ADDRESS = privtoaddr(RECIPIENT_PRIVKEY) ASSET_ADDRESS = token.address HASHLOCK = sha3(INITIATOR_PRIVKEY) LOCK_AMOUNT = 29 LOCK_EXPIRATION = 31 LOCK = Lock(LOCK_AMOUNT, LOCK_EXPIRATION, HASHLOCK) LOCKSROOT = merkleroot([ sha3(LOCK.as_bytes), ]) # print direct_transfer.encode('hex') nonce = 1 asset = ASSET_ADDRESS balance = 1 recipient = RECIPIENT_ADDRESS locksroot = LOCKSROOT msg = DirectTransfer( nonce, asset, balance, recipient, locksroot, ).sign(INITIATOR_PRIVKEY) packed = msg.packed() direct_transfer = str(packed.data) # pure python recover sen = recover_publickey(direct_transfer[:148], str(packed.signature)) assert address_from_key(sen) == tester.a0 # addr = getter.ecTest(direct_transfer[:148], sig) # assert addr == INITIATOR_ADDRESS.encode('hex') sender = getter.getSender(direct_transfer) assert sender == tester.a0.encode('hex') # with sigSplit directly in Getters.sol r, s, v = getter.sigSplit(str(packed.signature)) assert r == str(packed.signature[:32]) assert s == str(packed.signature[32:64]) assert v == packed.signature[64] + 27 sender = getter.getSender(direct_transfer) assert sender == tester.a0.encode('hex')