예제 #1
0
def test_extract_closing_signature(get_accounts, token_instance,
                                   uraiden_instance):
    (A, B) = get_accounts(2)
    token = token_instance
    uraiden = uraiden_instance

    sender = '0x5601ea8445a5d96eeebf89a67c4199fbb7a43fbb'
    block = 4804175
    balance = 22000000000000000000

    message_hash = closing_message_hash(sender, block, balance,
                                        uraiden.address)
    balance_msg_sig, signer = sign.check(message_hash, tester.k2)
    assert signer == A

    signature_address = uraiden.call().extractClosingSignature(
        sender, block, balance, balance_msg_sig)
    assert signature_address == signer

    # Wrong sender
    signature_address = uraiden.call().extractClosingSignature(
        B, block, balance, balance_msg_sig)
    assert signature_address != signer

    # Wrong block
    signature_address = uraiden.call().extractClosingSignature(
        sender, 10, balance, balance_msg_sig)
    assert signature_address != signer

    # Wrong balance
    signature_address = uraiden.call().extractClosingSignature(
        sender, block, 20, balance_msg_sig)
    assert signature_address != signer
예제 #2
0
    def get(uraiden=None, token=None, deposit=None, sender=None, receiver=None, contract_type=None):
        deposit = deposit or channel_params['deposit']
        contract_type = contract_type or channel_params['type']
        balance = channel_params['balance']
        uraiden = uraiden or uraiden_instance
        token = token or token_instance

        if not sender:
            (sender, receiver) = get_accounts(2)

        # Supply accounts with tokens
        token.transact({"from": owner}).transfer(sender, deposit + 500)
        token.transact({"from": owner}).transfer(receiver, 100)

        # Create channel (ERC20 or ERC223 logic)
        if contract_type == '20':
            token.transact({"from": sender}).approve(
                uraiden.address,
                deposit
            )
            txn_hash = uraiden.transact({"from": sender}).createChannelERC20(
                receiver,
                deposit
            )
        else:
            txdata = bytes.fromhex(receiver[2:].zfill(40))
            txn_hash = token.transact({"from": sender}).transfer(
                uraiden.address,
                deposit,
                txdata
            )

        open_block_number = get_block(txn_hash)

        balance_message_hash = balance_proof_hash(
            receiver,
            open_block_number,
            balance,
            uraiden_instance.address
        )
        balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)

        closing_msg_hash = closing_message_hash(
            sender,
            open_block_number,
            balance,
            uraiden_instance.address
        )
        closing_sig, addr = sign.check(closing_msg_hash, tester.k3)

        return (sender, receiver, open_block_number, balance_msg_sig, closing_sig)
예제 #3
0
    def get(uraiden=None, token=None, deposit=None, sender=None, receiver=None, contract_type=None):
        deposit = deposit or channel_params['deposit']
        contract_type = contract_type or channel_params['type']
        balance = channel_params['balance']
        uraiden = uraiden or uraiden_instance
        token = token or token_instance

        if not sender:
            (sender, receiver) = get_accounts(2)

        # Supply accounts with tokens
        token.transact({"from": owner}).transfer(sender, deposit + 500)
        token.transact({"from": owner}).transfer(receiver, 100)

        # Create channel (ERC20 or ERC223 logic)
        if contract_type == '20':
            token.transact({"from": sender}).approve(
                uraiden.address,
                deposit
            )
            txn_hash = uraiden.transact({"from": sender}).createChannel(
                receiver,
                deposit
            )
        else:
            txdata = bytes.fromhex(sender[2:] + receiver[2:])
            txn_hash = token.transact({"from": sender}).transfer(
                uraiden.address,
                deposit,
                txdata
            )

        open_block_number = get_block(txn_hash)

        balance_message_hash = balance_proof_hash(
            receiver,
            open_block_number,
            balance,
            uraiden_instance.address
        )
        balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)

        closing_msg_hash = closing_message_hash(
            sender,
            open_block_number,
            balance,
            uraiden_instance.address
        )
        closing_sig, addr = sign.check(closing_msg_hash, tester.k3)

        return (sender, receiver, open_block_number, balance_msg_sig, closing_sig)
예제 #4
0
def test_close_after_withdraw(contract_params, channel_params,
                              uraiden_instance, token_instance, get_channel,
                              get_block, print_gas):
    (sender, receiver, open_block_number) = get_channel()[:3]
    deposit = channel_params['deposit']
    balance1 = 20
    balance2 = deposit

    balance_message_hash1 = balance_proof_hash(receiver, open_block_number,
                                               balance1,
                                               uraiden_instance.address)
    balance_msg_sig1, addr = sign.check(balance_message_hash1, tester.k2)
    assert is_same_address(addr, sender)

    # Withdraw some tokens
    txn_hash = uraiden_instance.transact({
        "from": receiver
    }).withdraw(open_block_number, balance1, balance_msg_sig1)

    # Cooperatively close the channel
    balance_message_hash = balance_proof_hash(receiver, open_block_number,
                                              balance2,
                                              uraiden_instance.address)
    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)
    assert is_same_address(addr, sender)

    closing_msg_hash = closing_message_hash(sender, open_block_number,
                                            balance2, uraiden_instance.address)
    closing_sig, addr = sign.check(closing_msg_hash, tester.k3)

    # Memorize balances for tests
    uraiden_pre_balance = token_instance.call().balanceOf(
        uraiden_instance.address)
    sender_pre_balance = token_instance.call().balanceOf(sender)
    receiver_pre_balance = token_instance.call().balanceOf(receiver)

    uraiden_instance.transact({
        "from": receiver
    }).cooperativeClose(receiver, open_block_number, balance2, balance_msg_sig,
                        closing_sig)

    # Check post closing balances
    receiver_post_balance = receiver_pre_balance + (balance2 - balance1)
    sender_post_balance = sender_pre_balance + (deposit - balance2)
    uraiden_post_balance = uraiden_pre_balance - (balance2 - balance1)

    assert token_instance.call().balanceOf(receiver) == receiver_post_balance
    assert token_instance.call().balanceOf(sender) == sender_post_balance
    assert token_instance.call().balanceOf(
        uraiden_instance.address) == uraiden_post_balance
예제 #5
0
def test_extract_closing_signature(get_accounts, token_instance, uraiden_instance):
    (A, B) = get_accounts(2)
    token = token_instance
    uraiden = uraiden_instance

    sender = '0x5601Ea8445A5d96EEeBF89A67C4199FbB7a43Fbb'
    block = 4804175
    balance = 22000000000000000000

    message_hash = closing_message_hash(sender, block, balance, uraiden.address)
    balance_msg_sig, signer = sign.check(message_hash, tester.k2)
    assert is_same_address(signer, A)

    signature_address = uraiden.call().extractClosingSignature(
        sender,
        block,
        balance,
        balance_msg_sig
    )
    assert is_same_address(signature_address, signer)

    # Wrong sender
    signature_address = uraiden.call().extractClosingSignature(
        B,
        block,
        balance,
        balance_msg_sig
    )
    assert not is_same_address(signature_address, signer)

    # Wrong block
    signature_address = uraiden.call().extractClosingSignature(
        sender,
        10,
        balance,
        balance_msg_sig
    )
    assert not is_same_address(signature_address, signer)

    # Wrong balance
    signature_address = uraiden.call().extractClosingSignature(
        sender,
        block,
        20,
        balance_msg_sig
    )
    assert not is_same_address(signature_address, signer)
def test_cooperative_close_fail_wrong_balance(channel_params, get_accounts,
                                              uraiden_instance, token_instance,
                                              get_channel):
    (sender, receiver, open_block_number, balance_msg_sig,
     closing_sig) = get_channel()
    balance = channel_params['balance']
    deposit = channel_params['deposit']

    balance_message_hash_fake = balance_proof_hash(receiver, open_block_number,
                                                   balance - 1,
                                                   uraiden_instance.address)
    balance_msg_sig_fake, addr = sign.check(balance_message_hash_fake,
                                            tester.k2)

    closing_msg_hash_fake = closing_message_hash(sender, open_block_number,
                                                 balance - 1,
                                                 uraiden_instance.address)
    closing_sig_fake, addr = sign.check(closing_msg_hash_fake, tester.k3)

    # Wrong balance as an argument
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).cooperativeClose(receiver, open_block_number, balance + 1,
                            balance_msg_sig, closing_sig)

    # Sender signs wrong balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).cooperativeClose(receiver, open_block_number, balance,
                            balance_msg_sig_fake, closing_sig)

    # Receiver signs wrong balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).cooperativeClose(receiver, open_block_number, balance,
                            balance_msg_sig, closing_sig_fake)

    uraiden_instance.transact({
        "from": sender
    }).cooperativeClose(receiver, open_block_number, balance, balance_msg_sig,
                        closing_sig)
예제 #7
0
def test_cooperative_close_fail_wrong_balance(
        channel_params,
        get_accounts,
        uraiden_instance,
        token_instance,
        get_channel):
    (sender, receiver, open_block_number, balance_msg_sig, closing_sig) = get_channel()
    balance = channel_params['balance']
    deposit = channel_params['deposit']

    balance_message_hash_fake = balance_proof_hash(
        receiver,
        open_block_number,
        balance - 1,
        uraiden_instance.address
    )
    balance_msg_sig_fake, addr = sign.check(balance_message_hash_fake, tester.k2)

    closing_msg_hash_fake = closing_message_hash(
        sender,
        open_block_number,
        balance - 1,
        uraiden_instance.address
    )
    closing_sig_fake, addr = sign.check(closing_msg_hash_fake, tester.k3)

    # Wrong balance as an argument
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).cooperativeClose(
            receiver,
            open_block_number,
            balance + 1,
            balance_msg_sig,
            closing_sig
        )

    # Sender signs wrong balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).cooperativeClose(
            receiver,
            open_block_number,
            balance,
            balance_msg_sig_fake,
            closing_sig
        )

    # Receiver signs wrong balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).cooperativeClose(
            receiver,
            open_block_number,
            balance,
            balance_msg_sig,
            closing_sig_fake
        )

    uraiden_instance.transact({"from": sender}).cooperativeClose(
        receiver,
        open_block_number,
        balance,
        balance_msg_sig,
        closing_sig
    )
def test_close_after_withdraw(
        contract_params,
        channel_params,
        uraiden_instance,
        token_instance,
        get_channel,
        get_block,
        print_gas):
    (sender, receiver, open_block_number) = get_channel()[:3]
    deposit = channel_params['deposit']
    balance1 = 20
    balance2 = deposit

    balance_message_hash1 = balance_proof_hash(
        receiver,
        open_block_number,
        balance1,
        uraiden_instance.address
    )
    balance_msg_sig1, addr = sign.check(balance_message_hash1, tester.k2)
    assert is_same_address(addr, sender)

    # Withdraw some tokens
    txn_hash = uraiden_instance.transact({"from": receiver}).withdraw(
        open_block_number,
        balance1,
        balance_msg_sig1
    )

    # Cooperatively close the channel
    balance_message_hash = balance_proof_hash(
        receiver,
        open_block_number,
        balance2,
        uraiden_instance.address
    )
    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)
    assert is_same_address(addr, sender)

    closing_msg_hash = closing_message_hash(
        sender,
        open_block_number,
        balance2,
        uraiden_instance.address
    )
    closing_sig, addr = sign.check(closing_msg_hash, tester.k3)

    # Memorize balances for tests
    uraiden_pre_balance = token_instance.call().balanceOf(uraiden_instance.address)
    sender_pre_balance = token_instance.call().balanceOf(sender)
    receiver_pre_balance = token_instance.call().balanceOf(receiver)

    uraiden_instance.transact({"from": receiver}).cooperativeClose(
        receiver,
        open_block_number,
        balance2,
        balance_msg_sig,
        closing_sig
    )

    # Check post closing balances
    receiver_post_balance = receiver_pre_balance + (balance2 - balance1)
    sender_post_balance = sender_pre_balance + (deposit - balance2)
    uraiden_post_balance = uraiden_pre_balance - (balance2 - balance1)

    assert token_instance.call().balanceOf(receiver) == receiver_post_balance
    assert token_instance.call().balanceOf(sender) == sender_post_balance
    assert token_instance.call().balanceOf(uraiden_instance.address) == uraiden_post_balance