Пример #1
0
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
Пример #2
0
def get_last_iou(
    url: str,
    token_network_address: typing.Union[typing.TokenNetworkAddress,
                                        typing.TokenNetworkID],
    sender: typing.Address,
    receiver: typing.Address,
) -> typing.Optional[typing.Dict]:

    timestamp = datetime.utcnow().isoformat(timespec='seconds')
    signature = to_hex(
        eth_sign_hash_message(
            Web3.toBytes(hexstr=sender) + Web3.toBytes(hexstr=receiver) +
            bytes(timestamp, 'utf-8'), ))

    try:
        return requests.get(
            f'{url}/api/v1/{to_checksum_address(token_network_address)}/payment/iou',
            data=dict(sender=sender,
                      receiver=receiver,
                      timestamp=timestamp,
                      signature=signature),
            timeout=DEFAULT_HTTP_REQUEST_TIMEOUT,
        ).json().get('last_iou')
    except (requests.exceptions.RequestException, ValueError) as e:
        raise ServiceRequestFailed(str(e))
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