33import pytest
44
55from eth_account .messages import (
6- defunct_hash_message ,
6+ encode_defunct ,
77)
88from eth_utils import (
99 is_checksum_address ,
@@ -96,56 +96,56 @@ def test_eth_account_create_variation(acct):
9696 assert account1 != account2
9797
9898
99- def test_eth_account_privateKeyToAccount_reproducible (acct , PRIVATE_BYTES ):
100- account1 = acct .privateKeyToAccount (PRIVATE_BYTES )
101- account2 = acct .privateKeyToAccount (PRIVATE_BYTES )
99+ def test_eth_account_from_key_reproducible (acct , PRIVATE_BYTES ):
100+ account1 = acct .from_key (PRIVATE_BYTES )
101+ account2 = acct .from_key (PRIVATE_BYTES )
102102 assert bytes (account1 ) == PRIVATE_BYTES
103103 assert bytes (account1 ) == bytes (account2 )
104104 assert isinstance (str (account1 ), str )
105105
106106
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 )
107+ def test_eth_account_from_key_diverge (acct , PRIVATE_BYTES , PRIVATE_BYTES_ALT ):
108+ account1 = acct .from_key (PRIVATE_BYTES )
109+ account2 = acct .from_key (PRIVATE_BYTES_ALT )
110110 assert bytes (account2 ) == PRIVATE_BYTES_ALT
111111 assert bytes (account1 ) != bytes (account2 )
112112
113113
114- def test_eth_account_privateKeyToAccount_seed_restrictions (acct ):
114+ def test_eth_account_from_key_seed_restrictions (acct ):
115115 with pytest .raises (ValueError ):
116- acct .privateKeyToAccount (b'' )
116+ acct .from_key (b'' )
117117 with pytest .raises (ValueError ):
118- acct .privateKeyToAccount (b'\xff ' * 31 )
118+ acct .from_key (b'\xff ' * 31 )
119119 with pytest .raises (ValueError ):
120- acct .privateKeyToAccount (b'\xff ' * 33 )
120+ acct .from_key (b'\xff ' * 33 )
121121
122122
123- def test_eth_account_privateKeyToAccount_properties (acct , PRIVATE_BYTES ):
124- account = acct .privateKeyToAccount (PRIVATE_BYTES )
123+ def test_eth_account_from_key_properties (acct , PRIVATE_BYTES ):
124+ account = acct .from_key (PRIVATE_BYTES )
125125 assert callable (account .signHash )
126126 assert callable (account .sign_transaction )
127127 assert is_checksum_address (account .address )
128128 assert account .address == '0xa79F6f349C853F9Ea0B29636779ae3Cb4E3BA729'
129- assert account .privateKey == PRIVATE_BYTES
129+ assert account .key == PRIVATE_BYTES
130130
131131
132132def test_eth_account_create_properties (acct ):
133133 account = acct .create ()
134134 assert callable (account .signHash )
135135 assert callable (account .sign_transaction )
136136 assert is_checksum_address (account .address )
137- assert isinstance (account .privateKey , bytes ) and len (account .privateKey ) == 32
137+ assert isinstance (account .key , bytes ) and len (account .key ) == 32
138138
139139
140140def test_eth_account_recover_transaction_example (acct ):
141141 raw_tx_hex = '0xf8640d843b9aca00830e57e0945b2063246f2191f18f2675cedb8b28102e957458018025a00c753084e5a8290219324c1a3a86d4064ded2d15979b1ea790734aaa2ceaafc1a0229ca4538106819fd3a5509dd383e8fe4b731c6870339556a5c06feb9cf330bb' # noqa: E501
142- from_account = acct .recoverTransaction (raw_tx_hex )
142+ from_account = acct .recover_transaction (raw_tx_hex )
143143 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
144144
145145
146146def test_eth_account_recover_transaction_with_literal (acct ):
147147 raw_tx = 0xf8640d843b9aca00830e57e0945b2063246f2191f18f2675cedb8b28102e957458018025a00c753084e5a8290219324c1a3a86d4064ded2d15979b1ea790734aaa2ceaafc1a0229ca4538106819fd3a5509dd383e8fe4b731c6870339556a5c06feb9cf330bb # noqa: E501
148- from_account = acct .recoverTransaction (raw_tx )
148+ from_account = acct .recover_transaction (raw_tx )
149149 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
150150
151151
@@ -156,8 +156,8 @@ def test_eth_account_recover_message(acct):
156156 '0x3e5bfbbf4d3e39b1a2fd816a7680c19ebebaf3a141b239934ad43cb33fcec8ce' ,
157157 )
158158 message = "I♥SF"
159- message_hash = defunct_hash_message (text = message )
160- from_account = acct .recoverHash (message_hash , vrs = (v , r , s ))
159+ message_hash = encode_defunct (text = message )
160+ from_account = acct .recover_message (message_hash , vrs = (v , r , s ))
161161 assert from_account == '0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E'
162162
163163
@@ -183,11 +183,11 @@ def test_eth_account_recover_vrs(acct):
183183 5634810156301565519126305729385531885322755941350706789683031279718535704513 ,
184184 15655399131600894366408541311673616702363115109327707006109616887384920764603 ,
185185 )
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 ))
186+ 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
187+ from_account = acct .recover_message (msg_hash , vrs = (v , r , s ))
188188 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
189189
190- from_account = acct .recoverHash (msg_hash , vrs = map (to_hex , (v , r , s )))
190+ from_account = acct .recover_message (msg_hash , vrs = map (to_hex , (v , r , s )))
191191 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
192192
193193
@@ -197,8 +197,8 @@ def test_eth_account_recover_vrs_standard_v(acct):
197197 5634810156301565519126305729385531885322755941350706789683031279718535704513 ,
198198 15655399131600894366408541311673616702363115109327707006109616887384920764603 ,
199199 )
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 ))
200+ 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
201+ from_account = acct .recover_message (msg_hash , vrs = (v , r , s ))
202202 assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
203203
204204
@@ -230,18 +230,22 @@ def test_eth_account_recover_vrs_standard_v(acct):
230230 ids = ['web3js_example' , '31byte_r_and_s' ],
231231)
232232def 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
233+ message_hash = encode_defunct (text = message )
234+ signed_message = Web3 .keccak (
235+ b"\x19 Ethereum Signed Message:\n " +
236+ bytes (f"{ len (message_hash .body )} " , encoding = 'utf-8' ) + message_hash .body
237+ )
238+ assert signed_message == expected_hash
235239
236- signed = acct .signHash (message_hash , private_key = key )
240+ signed = acct .sign_message (message_hash , private_key = key )
237241 assert signed .messageHash == expected_hash
238242 assert signed .v == v
239243 assert signed .r == r
240244 assert signed .s == s
241245 assert signed .signature == signature
242246
243- account = acct .privateKeyToAccount (key )
244- assert account .signHash (message_hash ) == signed
247+ account = acct .from_key (key )
248+ assert account .sign_message (message_hash ) == signed
245249
246250
247251@pytest .mark .parametrize (
@@ -290,7 +294,7 @@ def test_eth_account_sign_transaction(acct, txn, private_key, expected_raw_tx, t
290294 assert signed .rawTransaction == expected_raw_tx
291295 assert signed .hash == tx_hash
292296
293- account = acct .privateKeyToAccount (private_key )
297+ account = acct .from_key (private_key )
294298 assert account .sign_transaction (txn ) == signed
295299
296300
@@ -313,8 +317,8 @@ def test_eth_account_sign_transaction_from_eth_test(acct, transaction_info):
313317 assert signed .r == Web3 .toInt (hexstr = expected_raw_txn [- 130 :- 66 ])
314318
315319 # 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
320+ expected_sender = acct .from_key (key ).address
321+ assert acct .recover_transaction (signed .rawTransaction ) == expected_sender
318322
319323
320324@pytest .mark .parametrize (
@@ -324,8 +328,8 @@ def test_eth_account_sign_transaction_from_eth_test(acct, transaction_info):
324328def test_eth_account_recover_transaction_from_eth_test (acct , transaction ):
325329 raw_txn = transaction ['signed' ]
326330 key = transaction ['key' ]
327- expected_sender = acct .privateKeyToAccount (key ).address
328- assert acct .recoverTransaction (raw_txn ) == expected_sender
331+ expected_sender = acct .from_key (key ).address
332+ assert acct .recover_transaction (raw_txn ) == expected_sender
329333
330334
331335def test_eth_account_encrypt (acct , web3js_key , web3js_password ):
@@ -340,7 +344,7 @@ def test_eth_account_encrypt(acct, web3js_key, web3js_password):
340344
341345
342346def test_eth_account_prepared_encrypt (acct , web3js_key , web3js_password ):
343- account = acct .privateKeyToAccount (web3js_key )
347+ account = acct .from_key (web3js_key )
344348 encrypted = account .encrypt (web3js_password )
345349
346350 assert encrypted ['address' ] == '2c7536e3605d9c16a7a3d7b1898e529396a65c23'
0 commit comments