def test_ecrecover_output( web3: Web3, token_network: Contract, signature_test_contract: Contract, get_accounts: Callable, create_channel: Callable, create_balance_proof: Callable, ) -> None: """ ecrecover returns the address that was used to sign a balance proof """ (A, B) = get_accounts(2) channel_identifier = create_channel(A, B)[0] balance_proof_A = create_balance_proof(channel_identifier, A, 2, 0, 3) signature = balance_proof_A.original_signature r = signature[:32] s = signature[32:64] v = signature[64:] balance_proof_hash = eth_sign_hash_message( pack_balance_proof( token_network_address=token_network.address, chain_identifier=web3.eth.chainId, channel_identifier=channel_identifier, balance_hash=balance_proof_A.balance_hash, nonce=balance_proof_A.nonce, additional_hash=balance_proof_A.additional_hash, msg_type=MessageTypeId.BALANCE_PROOF, )) address = signature_test_contract.functions.verifyEcrecoverOutput( balance_proof_hash, r, s, int.from_bytes(v, byteorder="big")).call() assert address == A
def test_verify( token_network: Contract, signature_test_contract: Contract, get_accounts: Callable, create_channel: Callable, create_balance_proof: Callable, ) -> None: """ECVerify.ecverify returns the correct address This test checks if the signature test contract returns the correct addresses on the balance hash signed by both ends of a channel""" (A, B) = get_accounts(2) channel_identifier = create_channel(A, B)[0] balance_proof_A = create_balance_proof(channel_identifier, A, 2, 0, 3) signature = balance_proof_A.original_signature balance_proof_hash = eth_sign_hash_message( pack_balance_proof( token_network_address=token_network.address, chain_identifier=ChainID( token_network.functions.chain_id().call()), channel_identifier=channel_identifier, balance_hash=balance_proof_A.balance_hash, nonce=balance_proof_A.nonce, additional_hash=balance_proof_A.additional_hash, msg_type=MessageTypeId.BALANCE_PROOF, )) address = signature_test_contract.functions.verify(balance_proof_hash, signature).call() assert address == A balance_proof_B = create_balance_proof(channel_identifier, B, 0, 0, 0) signature = balance_proof_B.original_signature balance_proof_hash = eth_sign_hash_message( pack_balance_proof( token_network_address=token_network.address, chain_identifier=ChainID( token_network.functions.chain_id().call()), channel_identifier=channel_identifier, balance_hash=balance_proof_B.balance_hash, nonce=balance_proof_B.nonce, additional_hash=balance_proof_B.additional_hash, msg_type=MessageTypeId.BALANCE_PROOF, )) address = signature_test_contract.functions.verify(balance_proof_hash, signature).call() assert address == B
def serialize_bin(self, msg_type: MessageTypeId = MessageTypeId.BALANCE_PROOF) -> bytes: return pack_balance_proof( to_checksum_address(self.token_network_address), self.chain_id, self.channel_identifier, BalanceHash(decode_hex(self.balance_hash)), self.nonce, AdditionalHash(decode_hex(self.additional_hash)), msg_type, )
def pack_balance_proof( nonce: Nonce, balance_hash: BalanceHash, additional_hash: AdditionalHash, canonical_identifier: CanonicalIdentifier, ) -> bytes: """Packs balance proof data to be signed Packs the given arguments in a byte array in the same configuration the contracts expect the signed data to have. """ return proofs.pack_balance_proof( token_network_address=to_hex_address( canonical_identifier.token_network_address), chain_identifier=canonical_identifier.chain_identifier, channel_identifier=canonical_identifier.channel_identifier, msg_type=MessageTypeId.BALANCE_PROOF, nonce=nonce, balance_hash=balance_hash, additional_hash=additional_hash, )