|
| 1 | +"""Test signing for multiple chains""" |
| 2 | +import pytest |
| 3 | +from eth_account import Account |
| 4 | + |
| 5 | +from ledgereth.accounts import get_accounts |
| 6 | +from ledgereth.objects import MAX_CHAIN_ID, MAX_LEGACY_CHAIN_ID |
| 7 | +from ledgereth.transactions import create_transaction |
| 8 | + |
| 9 | + |
| 10 | +def test_max_legacy_chain_ids(yield_dongle): |
| 11 | + """Test that the max legacy chain ID works""" |
| 12 | + destination = "0xf0155486a14539f784739be1c02e93f28eb8e900" |
| 13 | + |
| 14 | + with yield_dongle() as dongle: |
| 15 | + sender = get_accounts(dongle=dongle, count=1)[0].address |
| 16 | + |
| 17 | + signed = create_transaction( |
| 18 | + destination=destination, |
| 19 | + amount=int(10e17), |
| 20 | + gas=int(1e6), |
| 21 | + gas_price=int(1e9), |
| 22 | + data="", |
| 23 | + nonce=2023, |
| 24 | + chain_id=MAX_LEGACY_CHAIN_ID, |
| 25 | + dongle=dongle, |
| 26 | + ) |
| 27 | + |
| 28 | + assert sender == Account.recover_transaction(signed.rawTransaction) |
| 29 | + |
| 30 | + |
| 31 | +def test_invalid_legacy_chain_ids(yield_dongle): |
| 32 | + """Test that chain IDs above max legacy chain ID fail""" |
| 33 | + destination = "0xf0155486a14539f784739be1c02e93f28eb8e901" |
| 34 | + |
| 35 | + with yield_dongle() as dongle: |
| 36 | + sender = get_accounts(dongle=dongle, count=1)[0].address |
| 37 | + |
| 38 | + with pytest.raises( |
| 39 | + ValueError, |
| 40 | + match="chain_id must be a 32-bit integer for type 0 transactions", |
| 41 | + ): |
| 42 | + create_transaction( |
| 43 | + destination=destination, |
| 44 | + amount=int(10e17), |
| 45 | + gas=int(1e6), |
| 46 | + gas_price=int(1e9), |
| 47 | + data="", |
| 48 | + nonce=2023, |
| 49 | + chain_id=MAX_LEGACY_CHAIN_ID + 1, |
| 50 | + dongle=dongle, |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def test_max_type1_chain_ids(yield_dongle): |
| 55 | + """Test that the max type-1 chain ID works""" |
| 56 | + destination = "0xf0155486a14539f784739be1c02e93f28eb8e902" |
| 57 | + |
| 58 | + with yield_dongle() as dongle: |
| 59 | + sender = get_accounts(dongle=dongle, count=1)[0].address |
| 60 | + |
| 61 | + signed = create_transaction( |
| 62 | + destination=destination, |
| 63 | + amount=int(10e17), |
| 64 | + gas=int(1e6), |
| 65 | + access_list=[], |
| 66 | + gas_price=int(1e9), |
| 67 | + data="", |
| 68 | + nonce=2023, |
| 69 | + chain_id=MAX_CHAIN_ID, |
| 70 | + dongle=dongle, |
| 71 | + ) |
| 72 | + |
| 73 | + assert sender == Account.recover_transaction(signed.rawTransaction) |
| 74 | + |
| 75 | + |
| 76 | +def test_invalid_type1_chain_ids(yield_dongle): |
| 77 | + """Test that IDs above the max chain ID fail""" |
| 78 | + destination = "0xf0155486a14539f784739be1c02e93f28eb8e903" |
| 79 | + |
| 80 | + with yield_dongle() as dongle: |
| 81 | + sender = get_accounts(dongle=dongle, count=1)[0].address |
| 82 | + |
| 83 | + with pytest.raises( |
| 84 | + ValueError, |
| 85 | + match="chain_id must not be above 999999999999999", |
| 86 | + ): |
| 87 | + create_transaction( |
| 88 | + destination=destination, |
| 89 | + amount=int(10e17), |
| 90 | + gas=int(1e6), |
| 91 | + access_list=[], |
| 92 | + gas_price=int(1e9), |
| 93 | + data="", |
| 94 | + nonce=2023, |
| 95 | + chain_id=MAX_CHAIN_ID + 1, |
| 96 | + dongle=dongle, |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def test_max_type2_chain_ids(yield_dongle): |
| 101 | + """Test that the max chain ID works for type-2 transactions""" |
| 102 | + destination = "0xf0155486a14539f784739be1c02e93f28eb8e904" |
| 103 | + |
| 104 | + with yield_dongle() as dongle: |
| 105 | + sender = get_accounts(dongle=dongle, count=1)[0].address |
| 106 | + |
| 107 | + signed = create_transaction( |
| 108 | + destination=destination, |
| 109 | + amount=int(10e17), |
| 110 | + gas=int(1e6), |
| 111 | + max_fee_per_gas=int(1e9), |
| 112 | + max_priority_fee_per_gas=int(1e8), |
| 113 | + data="", |
| 114 | + nonce=2023, |
| 115 | + chain_id=MAX_CHAIN_ID, |
| 116 | + dongle=dongle, |
| 117 | + ) |
| 118 | + |
| 119 | + assert sender == Account.recover_transaction(signed.rawTransaction) |
| 120 | + |
| 121 | + |
| 122 | +def test_invalid_type2_chain_ids(yield_dongle): |
| 123 | + """Test that IDs above the max chain ID fail for type-2 transactions""" |
| 124 | + destination = "0xf0155486a14539f784739be1c02e93f28eb8e905" |
| 125 | + |
| 126 | + with yield_dongle() as dongle: |
| 127 | + sender = get_accounts(dongle=dongle, count=1)[0].address |
| 128 | + |
| 129 | + with pytest.raises( |
| 130 | + ValueError, |
| 131 | + match="chain_id must not be above 999999999999999", |
| 132 | + ): |
| 133 | + create_transaction( |
| 134 | + destination=destination, |
| 135 | + amount=int(10e17), |
| 136 | + gas=int(1e6), |
| 137 | + max_fee_per_gas=int(1e9), |
| 138 | + max_priority_fee_per_gas=int(1e8), |
| 139 | + data="", |
| 140 | + nonce=2023, |
| 141 | + chain_id=MAX_CHAIN_ID + 1, |
| 142 | + dongle=dongle, |
| 143 | + ) |
0 commit comments