示例#1
0
def test_transacting_power_sign_transaction(testerchain):

    eth_address = testerchain.unassigned_accounts[2]
    power = TransactingPower(password=INSECURE_DEVELOPMENT_PASSWORD,
                             signer=Web3Signer(testerchain.client),
                             account=eth_address)

    assert power.is_active is False
    assert power.is_unlocked is False

    transaction_dict = {
        'nonce': testerchain.client.w3.eth.getTransactionCount(eth_address),
        'gasPrice': testerchain.client.w3.eth.gasPrice,
        'gas': 100000,
        'from': eth_address,
        'to': testerchain.unassigned_accounts[1],
        'value': 1,
        'data': b''
    }

    # The default state of the account is locked.
    assert not power.is_unlocked

    # Test a signature without unlocking the account
    with pytest.raises(power.AccountLocked):
        power.sign_transaction(transaction_dict=transaction_dict)

    # Sign
    power.activate()
    assert power.is_unlocked is True
    signed_transaction = power.sign_transaction(
        transaction_dict=transaction_dict)

    # Demonstrate that the transaction is valid RLP encoded.
    from eth_account._utils.transactions import Transaction
    restored_transaction = Transaction.from_bytes(
        serialized_bytes=signed_transaction)
    restored_dict = restored_transaction.as_dict()
    assert to_checksum_address(restored_dict['to']) == transaction_dict['to']

    # Try signing with missing transaction fields
    del transaction_dict['gas']
    del transaction_dict['nonce']
    with pytest.raises(TypeError):
        power.sign_transaction(transaction_dict=transaction_dict)

    # Try signing with a re-locked account.
    power.lock_account()
    with pytest.raises(power.AccountLocked):
        power.sign_transaction(transaction_dict=transaction_dict)

    power.unlock_account(password=INSECURE_DEVELOPMENT_PASSWORD)
    assert power.is_unlocked is True

    # Tear-Down Test
    power = TransactingPower(password=INSECURE_DEVELOPMENT_PASSWORD,
                             signer=Web3Signer(testerchain.client),
                             account=testerchain.etherbase_account)
    power.activate(password=INSECURE_DEVELOPMENT_PASSWORD)
示例#2
0
def test_transacting_power_sign_message(testerchain):

    # Manually create a TransactingPower
    testerchain.connect()
    eth_address = testerchain.etherbase_account
    power = TransactingPower(blockchain=testerchain,
                             password=INSECURE_DEVELOPMENT_PASSWORD,
                             account=eth_address)

    # The default state of the account is locked.
    # Test a signature without unlocking the account
    with pytest.raises(PowerUpError):
        power.sign_message(message=b'test')

    # Manually unlock
    power.unlock_account(password=INSECURE_DEVELOPMENT_PASSWORD)

    # Sign
    data_to_sign = b'Premium Select Luxury Pencil Holder'
    signature = power.sign_message(message=data_to_sign)

    # Verify
    is_verified = verify_eip_191(address=eth_address,
                                 message=data_to_sign,
                                 signature=signature)
    assert is_verified is True

    # Test invalid address/pubkey pair
    is_verified = verify_eip_191(address=testerchain.client.accounts[1],
                                 message=data_to_sign,
                                 signature=signature)
    assert is_verified is False

    # Test lockAccount call
    power.lock_account()

    # Test a signature without unlocking the account
    with pytest.raises(PowerUpError):
        power.sign_message(message=b'test')

    del power  # Locks account