44
55from eth_account .messages import (
66 defunct_hash_message ,
7+ encode_defunct ,
78)
89from eth_utils import (
910 is_checksum_address ,
@@ -96,56 +97,56 @@ def test_eth_account_create_variation(acct):
9697 assert account1 != account2
9798
9899
99- def test_eth_account_privateKeyToAccount_reproducible (acct , PRIVATE_BYTES ):
100- account1 = acct .privateKeyToAccount (PRIVATE_BYTES )
101- account2 = acct .privateKeyToAccount (PRIVATE_BYTES )
100+ def test_eth_account_from_key_reproducible (acct , PRIVATE_BYTES ):
101+ account1 = acct .from_key (PRIVATE_BYTES )
102+ account2 = acct .from_key (PRIVATE_BYTES )
102103 assert bytes (account1 ) == PRIVATE_BYTES
103104 assert bytes (account1 ) == bytes (account2 )
104105 assert isinstance (str (account1 ), str )
105106
106107
107- def test_eth_account_privateKeyToAccount_diverge (acct , PRIVATE_BYTES , PRIVATE_BYTES_ALT ):
108- account1 = acct .privateKeyToAccount (PRIVATE_BYTES )
109- account2 = acct .privateKeyToAccount (PRIVATE_BYTES_ALT )
108+ def test_eth_account_from_key_diverge (acct , PRIVATE_BYTES , PRIVATE_BYTES_ALT ):
109+ account1 = acct .from_key (PRIVATE_BYTES )
110+ account2 = acct .from_key (PRIVATE_BYTES_ALT )
110111 assert bytes (account2 ) == PRIVATE_BYTES_ALT
111112 assert bytes (account1 ) != bytes (account2 )
112113
113114
114- def test_eth_account_privateKeyToAccount_seed_restrictions (acct ):
115+ def test_eth_account_from_key_seed_restrictions (acct ):
115116 with pytest .raises (ValueError ):
116- acct .privateKeyToAccount (b'' )
117+ acct .from_key (b'' )
117118 with pytest .raises (ValueError ):
118- acct .privateKeyToAccount (b'\xff ' * 31 )
119+ acct .from_key (b'\xff ' * 31 )
119120 with pytest .raises (ValueError ):
120- acct .privateKeyToAccount (b'\xff ' * 33 )
121+ acct .from_key (b'\xff ' * 33 )
121122
122123
123- def test_eth_account_privateKeyToAccount_properties (acct , PRIVATE_BYTES ):
124- account = acct .privateKeyToAccount (PRIVATE_BYTES )
124+ def test_eth_account_from_key_properties (acct , PRIVATE_BYTES ):
125+ account = acct .from_key (PRIVATE_BYTES )
125126 assert callable (account .signHash )
126127 assert callable (account .sign_transaction )
127128 assert is_checksum_address (account .address )
128129 assert account .address == '0xa79F6f349C853F9Ea0B29636779ae3Cb4E3BA729'
129- assert account .privateKey == PRIVATE_BYTES
130+ assert account .key == PRIVATE_BYTES
130131
131132
132133def test_eth_account_create_properties (acct ):
133134 account = acct .create ()
134135 assert callable (account .signHash )
135136 assert callable (account .sign_transaction )
136137 assert is_checksum_address (account .address )
137- assert isinstance (account .privateKey , bytes ) and len (account .privateKey ) == 32
138+ assert isinstance (account .key , bytes ) and len (account .key ) == 32
138139
139140
140141def test_eth_account_recover_transaction_example (acct ):
141142 raw_tx_hex = '0xf8640d843b9aca00830e57e0945b2063246f2191f18f2675cedb8b28102e957458018025a00c753084e5a8290219324c1a3a86d4064ded2d15979b1ea790734aaa2ceaafc1a0229ca4538106819fd3a5509dd383e8fe4b731c6870339556a5c06feb9cf330bb' # noqa: E501
142- from_account = acct .recoverTransaction (raw_tx_hex )
143+ from_account = acct .recover_transaction (raw_tx_hex )
143144 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
144145
145146
146147def test_eth_account_recover_transaction_with_literal (acct ):
147148 raw_tx = 0xf8640d843b9aca00830e57e0945b2063246f2191f18f2675cedb8b28102e957458018025a00c753084e5a8290219324c1a3a86d4064ded2d15979b1ea790734aaa2ceaafc1a0229ca4538106819fd3a5509dd383e8fe4b731c6870339556a5c06feb9cf330bb # noqa: E501
148- from_account = acct .recoverTransaction (raw_tx )
149+ from_account = acct .recover_transaction (raw_tx )
149150 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
150151
151152
@@ -156,8 +157,8 @@ def test_eth_account_recover_message(acct):
156157 '0x3e5bfbbf4d3e39b1a2fd816a7680c19ebebaf3a141b239934ad43cb33fcec8ce' ,
157158 )
158159 message = "I♥SF"
159- message_hash = defunct_hash_message (text = message )
160- from_account = acct .recoverHash (message_hash , vrs = (v , r , s ))
160+ message_hash = encode_defunct (text = message )
161+ from_account = acct .recover_message (message_hash , vrs = (v , r , s ))
161162 assert from_account == '0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E'
162163
163164
@@ -183,11 +184,11 @@ def test_eth_account_recover_vrs(acct):
183184 5634810156301565519126305729385531885322755941350706789683031279718535704513 ,
184185 15655399131600894366408541311673616702363115109327707006109616887384920764603 ,
185186 )
186- msg_hash = b'\xbb \r \x8a \xba \x9f \xf7 \xa1 <N,s{i\x81 \x86 r\x83 {\xba \x9f \xe2 \x1d \xaa \xdd \xb3 \xd6 \x01 \xda \x00 \xb7 )\xa1 ' # noqa: E501
187- from_account = acct .recoverHash (msg_hash , vrs = (v , r , s ))
187+ msg_hash = encode_defunct ( b'\xbb \r \x8a \xba \x9f \xf7 \xa1 <N,s{i\x81 \x86 r\x83 {\xba \x9f \xe2 \x1d \xaa \xdd \xb3 \xd6 \x01 \xda \x00 \xb7 )\xa1 ' ) # noqa: E501
188+ from_account = acct .recover_message (msg_hash , vrs = (v , r , s ))
188189 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
189190
190- from_account = acct .recoverHash (msg_hash , vrs = map (to_hex , (v , r , s )))
191+ from_account = acct .recover_message (msg_hash , vrs = map (to_hex , (v , r , s )))
191192 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
192193
193194
@@ -197,8 +198,8 @@ def test_eth_account_recover_vrs_standard_v(acct):
197198 5634810156301565519126305729385531885322755941350706789683031279718535704513 ,
198199 15655399131600894366408541311673616702363115109327707006109616887384920764603 ,
199200 )
200- msg_hash = b'\xbb \r \x8a \xba \x9f \xf7 \xa1 <N,s{i\x81 \x86 r\x83 {\xba \x9f \xe2 \x1d \xaa \xdd \xb3 \xd6 \x01 \xda \x00 \xb7 )\xa1 ' # noqa: E501
201- from_account = acct .recoverHash (msg_hash , vrs = (v , r , s ))
201+ msg_hash = encode_defunct ( b'\xbb \r \x8a \xba \x9f \xf7 \xa1 <N,s{i\x81 \x86 r\x83 {\xba \x9f \xe2 \x1d \xaa \xdd \xb3 \xd6 \x01 \xda \x00 \xb7 )\xa1 ' ) # noqa: E501
202+ from_account = acct .recover_message (msg_hash , vrs = (v , r , s ))
202203 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
203204
204205
@@ -230,18 +231,22 @@ def test_eth_account_recover_vrs_standard_v(acct):
230231 ids = ['web3js_example' , '31byte_r_and_s' ],
231232)
232233def test_eth_account_sign (acct , message , key , expected_bytes , expected_hash , v , r , s , signature ):
233- message_hash = defunct_hash_message (text = message )
234- assert message_hash == expected_hash
234+ message_hash = encode_defunct (text = message )
235+ signed_message = Web3 .keccak (
236+ b"\x19 Ethereum Signed Message:\n " +
237+ bytes (f"{ len (message_hash .body )} " , encoding = 'utf-8' ) + message_hash .body
238+ )
239+ assert signed_message == expected_hash
235240
236- signed = acct .signHash (message_hash , private_key = key )
241+ signed = acct .sign_message (message_hash , private_key = key )
237242 assert signed .messageHash == expected_hash
238243 assert signed .v == v
239244 assert signed .r == r
240245 assert signed .s == s
241246 assert signed .signature == signature
242247
243- account = acct .privateKeyToAccount (key )
244- assert account .signHash (message_hash ) == signed
248+ account = acct .from_key (key )
249+ assert account .sign_message (message_hash ) == signed
245250
246251
247252@pytest .mark .parametrize (
@@ -290,7 +295,7 @@ def test_eth_account_sign_transaction(acct, txn, private_key, expected_raw_tx, t
290295 assert signed .rawTransaction == expected_raw_tx
291296 assert signed .hash == tx_hash
292297
293- account = acct .privateKeyToAccount (private_key )
298+ account = acct .from_key (private_key )
294299 assert account .sign_transaction (txn ) == signed
295300
296301
@@ -313,8 +318,8 @@ def test_eth_account_sign_transaction_from_eth_test(acct, transaction_info):
313318 assert signed .r == Web3 .toInt (hexstr = expected_raw_txn [- 130 :- 66 ])
314319
315320 # confirm that signed transaction can be recovered to the sender
316- expected_sender = acct .privateKeyToAccount (key ).address
317- assert acct .recoverTransaction (signed .rawTransaction ) == expected_sender
321+ expected_sender = acct .from_key (key ).address
322+ assert acct .recover_transaction (signed .rawTransaction ) == expected_sender
318323
319324
320325@pytest .mark .parametrize (
@@ -324,8 +329,8 @@ def test_eth_account_sign_transaction_from_eth_test(acct, transaction_info):
324329def test_eth_account_recover_transaction_from_eth_test (acct , transaction ):
325330 raw_txn = transaction ['signed' ]
326331 key = transaction ['key' ]
327- expected_sender = acct .privateKeyToAccount (key ).address
328- assert acct .recoverTransaction (raw_txn ) == expected_sender
332+ expected_sender = acct .from_key (key ).address
333+ assert acct .recover_transaction (raw_txn ) == expected_sender
329334
330335
331336def test_eth_account_encrypt (acct , web3js_key , web3js_password ):
@@ -340,7 +345,7 @@ def test_eth_account_encrypt(acct, web3js_key, web3js_password):
340345
341346
342347def test_eth_account_prepared_encrypt (acct , web3js_key , web3js_password ):
343- account = acct .privateKeyToAccount (web3js_key )
348+ account = acct .from_key (web3js_key )
344349 encrypted = account .encrypt (web3js_password )
345350
346351 assert encrypted ['address' ] == '2c7536e3605d9c16a7a3d7b1898e529396a65c23'
0 commit comments