-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for LedgerAccount ; require connected device
- Loading branch information
Showing
1 changed file
with
83 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,86 @@ | ||
#TODO | ||
# Requirements: pip install rlp eth-account | ||
from ledger import LedgerAccount | ||
import rlp | ||
from eth_account.internal.transactions import serializable_unsigned_transaction_from_dict, encode_transaction | ||
# Require a connected Ledger ... | ||
|
||
import pytest | ||
|
||
from eth_account import ( | ||
Account, | ||
) | ||
|
||
from eth_account.signers.ledger import ( | ||
LedgerAccount, | ||
) | ||
|
||
from hexbytes import ( | ||
HexBytes, | ||
) | ||
|
||
|
||
# Not a fixture because it does not support multiple LedgerAccount | ||
ledger = LedgerAccount() | ||
|
||
print(ledger.list()) | ||
|
||
transaction = { | ||
# Note that the address must be in checksum format: | ||
'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', | ||
'value': 0, | ||
'gas': 2000000, | ||
'gasPrice': 65536, | ||
'nonce': 0, | ||
'chainId': 1 | ||
} | ||
unsigned_transaction = serializable_unsigned_transaction_from_dict(transaction) | ||
(v, r, s) = ledger.sign(rlp.encode(unsigned_transaction), offset=0) | ||
encoded_transaction = encode_transaction(unsigned_transaction, vrs=(v, r, s)) | ||
print('0x' + encoded_transaction.hex()) | ||
|
||
@pytest.fixture | ||
def acct(request): | ||
return Account | ||
|
||
|
||
@pytest.fixture | ||
def ledger_invalid(): | ||
return LedgerAccount(address='0x0000000000000000000000000000000000000000') | ||
|
||
|
||
@pytest.fixture | ||
def transaction(): | ||
return { | ||
'to': '0x0000000000000000000000000000000000000000', | ||
'value': 0, | ||
'gas': 2000000, | ||
'gasPrice': 65536, | ||
'nonce': 0, | ||
'chainId': 1 | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def tx_hash(): | ||
return HexBytes('0xdd4998746632108893ed905116ec4c1839833f6f3c9ae276b6550e15bad308c8') | ||
|
||
|
||
def test_address(): | ||
address = ledger.address | ||
account_id = ledger.get_account_id(address) | ||
|
||
assert address == ledger.get_address(account_id) | ||
|
||
|
||
def test_list(): | ||
accounts_1 = ledger.list(1) | ||
assert len(accounts_1) == 1 | ||
|
||
accounts_5 = ledger.list(5) | ||
assert len(accounts_5) == 5 | ||
assert accounts_1[0] == accounts_5[0] | ||
|
||
accounts_10 = ledger.list(10) | ||
assert len(accounts_10) == 10 | ||
assert accounts_5 == accounts_10[:5] | ||
|
||
accounts_4p0 = ledger.list(limit=4, page=0) | ||
accounts_4p1 = ledger.list(limit=4, page=1) | ||
accounts_4p2 = ledger.list(limit=4, page=2) | ||
assert len(accounts_4p1) == 4 | ||
assert accounts_4p0 == accounts_10[:4] | ||
assert accounts_4p1 == accounts_10[4:8] | ||
assert accounts_10 == (accounts_4p0 + accounts_4p1 + accounts_4p2[:2]) | ||
|
||
def test_sign_transaction(transaction, tx_hash, acct): | ||
expected_sender = ledger.address | ||
signed = ledger.signTransaction(transaction) | ||
|
||
assert type(signed.v) is int | ||
assert type(signed.r) is str | ||
assert type(signed.s) is str | ||
|
||
assert signed.hash == tx_hash | ||
|
||
assert acct.recoverTransaction(signed.rawTransaction).lower() == expected_sender |