File tree Expand file tree Collapse file tree 3 files changed +25
-0
lines changed
Expand file tree Collapse file tree 3 files changed +25
-0
lines changed Original file line number Diff line number Diff line change 1515 to_hex
1616)
1717
18+ from mnemonic import Mnemonic
19+
1820from convex_api .utils import to_address_checksum
1921
2022
@@ -61,6 +63,15 @@ def export_to_text(self, password):
6163 )
6264 return private_data .decode ()
6365
66+ @property
67+ def export_to_mnemonic (self ):
68+ mnemonic = Mnemonic ('english' )
69+ return mnemonic .to_mnemonic (self ._private_key .private_bytes (
70+ encoding = serialization .Encoding .Raw ,
71+ format = serialization .PrivateFormat .Raw ,
72+ encryption_algorithm = serialization .NoEncryption ()
73+ ))
74+
6475 def export_to_file (self , filename , password ):
6576 """
6677 Export the private key to a file. This uses `export_to_text` to export as a string.
@@ -161,6 +172,12 @@ def import_from_text(text, password):
161172 if private_key :
162173 return Account (private_key )
163174
175+ @staticmethod
176+ def import_from_mnemonic (words ):
177+ mnemonic = Mnemonic ('english' )
178+ value = mnemonic .to_entropy (words )
179+ return Account (Ed25519PrivateKey .from_private_bytes (value ))
180+
164181 @staticmethod
165182 def import_from_file (filename , password ):
166183 """
Original file line number Diff line number Diff line change 2121 'cryptography' ,
2222 'eth-utils' ,
2323 'requests' ,
24+ 'mnemonic'
2425]
2526
2627setup_requirements = ['pytest-runner' , ]
Original file line number Diff line number Diff line change @@ -72,3 +72,10 @@ def test_account_import_export_to_file(test_account):
7272 assert (import_account .address == test_account .address )
7373 os .remove (filename )
7474
75+ def test_account_export_to_mnemonic (test_account ):
76+ words = test_account .export_to_mnemonic
77+ assert (words )
78+ new_account = Account .import_from_mnemonic (words )
79+ assert (new_account )
80+ assert (test_account .address == new_account .address )
81+ assert (test_account .export_to_mnemonic == new_account .export_to_mnemonic )
You can’t perform that action at this time.
0 commit comments