28
28
from bittensor_wallet .wallet_impl import Wallet
29
29
from bittensor_wallet ._keyfile import Keyfile , keyfile
30
30
from bittensor_wallet ._keyfile .keyfile_impl import validate_password , ask_password_to_encrypt , decrypt_keyfile_data , KeyFileError
31
+ from bittensor_wallet .mock import MockKeyfile , MockWallet
31
32
32
33
class TestWallet (unittest .TestCase ):
33
34
34
35
def test_regen_coldkeypub_from_ss58_addr (self ):
35
36
ss58_address = "5DD26kC2kxajmwfbbZmVmxhrY9VeeyR1Gpzy9i8wxLUg6zxm"
36
- mock_wallet = Wallet ( )
37
+ mock_wallet = MockWallet ( name = f"mock" , hotkey = "mock_hk" , path = f"/tmp/mock-wallet- { self . id () } " )
37
38
with patch .object (mock_wallet , 'set_coldkeypub' ) as mock_set_coldkeypub :
38
39
mock_wallet .regenerate_coldkeypub ( ss58_address = ss58_address )
39
40
@@ -43,12 +44,12 @@ def test_regen_coldkeypub_from_ss58_addr(self):
43
44
44
45
ss58_address_bad = "5DD26kC2kxajmwfbbZmVmxhrY9VeeyR1Gpzy9i8wxLUg6zx" # 1 character short
45
46
with pytest .raises (ValueError ):
46
- self . mock_wallet .regenerate_coldkeypub (ss58_address = ss58_address_bad )
47
+ mock_wallet .regenerate_coldkeypub (ss58_address = ss58_address_bad )
47
48
48
49
def test_regen_coldkeypub_from_hex_pubkey_str (self ):
49
50
pubkey_str = "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f"
50
51
51
- mock_wallet = Wallet ( )
52
+ mock_wallet = MockWallet ( name = f"mock" , hotkey = "mock_hk" , path = f"/tmp/mock-wallet- { self . id () } " )
52
53
with patch .object (mock_wallet , 'set_coldkeypub' ) as mock_set_coldkeypub :
53
54
mock_wallet .regenerate_coldkeypub (public_key = pubkey_str )
54
55
@@ -58,13 +59,13 @@ def test_regen_coldkeypub_from_hex_pubkey_str(self):
58
59
59
60
pubkey_str_bad = "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512" # 1 character short
60
61
with pytest .raises (ValueError ):
61
- self . mock_wallet .regenerate_coldkeypub (ss58_address = pubkey_str_bad )
62
+ mock_wallet .regenerate_coldkeypub (ss58_address = pubkey_str_bad )
62
63
63
64
def test_regen_coldkeypub_from_hex_pubkey_bytes (self ):
64
65
pubkey_str = "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f"
65
66
pubkey_bytes = bytes .fromhex (pubkey_str [2 :]) # Remove 0x from beginning
66
67
67
- mock_wallet = Wallet ( )
68
+ mock_wallet = MockWallet ( name = f"mock" , hotkey = "mock_hk" , path = f"/tmp/mock-wallet- { self . id () } " )
68
69
with patch .object (mock_wallet , 'set_coldkeypub' ) as mock_set_coldkeypub :
69
70
mock_wallet .regenerate_coldkeypub (public_key = pubkey_bytes )
70
71
@@ -73,7 +74,7 @@ def test_regen_coldkeypub_from_hex_pubkey_bytes(self):
73
74
self .assertEqual (keypair .public_key , pubkey_bytes )
74
75
75
76
def test_regen_coldkeypub_no_pubkey (self ):
76
- mock_wallet = Wallet ( )
77
+ mock_wallet = MockWallet ( name = f"mock" , hotkey = "mock_hk" , path = f"/tmp/mock-wallet- { self . id () } " )
77
78
78
79
with pytest .raises (ValueError ):
79
80
# Must provide either public_key or ss58_address
@@ -83,7 +84,7 @@ def test_regen_coldkey_from_hex_seed_str(self):
83
84
ss58_addr = "5D5cwd8DX6ij7nouVcoxDuWtJfiR1BnzCkiBVTt7DU8ft5Ta"
84
85
seed_str = "0x659c024d5be809000d0d93fe378cfde020846150b01c49a201fc2a02041f7636"
85
86
86
- mock_wallet = Wallet ( )
87
+ mock_wallet = MockWallet ( name = f"mock" , hotkey = "mock_hk" , path = f"/tmp/mock-wallet- { self . id () } " )
87
88
with patch .object (mock_wallet , 'set_coldkey' ) as mock_set_coldkey :
88
89
mock_wallet .regenerate_coldkey (seed = seed_str )
89
90
@@ -100,8 +101,8 @@ def test_regen_hotkey_from_hex_seed_str(self):
100
101
ss58_addr = "5D5cwd8DX6ij7nouVcoxDuWtJfiR1BnzCkiBVTt7DU8ft5Ta"
101
102
seed_str = "0x659c024d5be809000d0d93fe378cfde020846150b01c49a201fc2a02041f7636"
102
103
103
- mock_wallet = Wallet ( )
104
- with patch .object (self . mock_wallet , 'set_hotkey' ) as mock_set_hotkey :
104
+ mock_wallet = MockWallet ( name = f"mock" , hotkey = "mock_hk" , path = f"/tmp/mock-wallet- { self . id () } " )
105
+ with patch .object (mock_wallet , 'set_hotkey' ) as mock_set_hotkey :
105
106
mock_wallet .regenerate_hotkey (seed = seed_str )
106
107
107
108
mock_set_hotkey .assert_called_once ()
@@ -126,60 +127,60 @@ def tearDown(self) -> None:
126
127
shutil .rmtree (self .root_path )
127
128
128
129
def create_keyfile (self ):
129
- keyfile = keyfile (path = os .path .join (self .root_path , "keyfile" ))
130
+ _keyfile = keyfile (path = os .path .join (self .root_path , "keyfile" ))
130
131
131
132
mnemonic = Keypair .generate_mnemonic (12 )
132
133
alice = Keypair .create_from_mnemonic (mnemonic )
133
- keyfile .set_keypair (alice , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
134
+ _keyfile .set_keypair (alice , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
134
135
135
136
bob = Keypair .create_from_uri ('/Bob' )
136
- keyfile .set_keypair (bob , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
137
+ _keyfile .set_keypair (bob , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
137
138
138
139
return keyfile
139
140
140
141
def test_create (self ):
141
- keyfile = keyfile (path = os .path .join (self .root_path , "keyfile" ))
142
+ _keyfile = keyfile (path = os .path .join (self .root_path , "keyfile" ))
142
143
143
144
mnemonic = Keypair .generate_mnemonic ( 12 )
144
145
alice = Keypair .create_from_mnemonic (mnemonic )
145
- keyfile .set_keypair (alice , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
146
- assert keyfile .is_readable ()
147
- assert keyfile .is_writable ()
148
- assert keyfile .is_encrypted ()
149
- keyfile .decrypt ( password = 'thisisafakepassword' )
150
- assert not keyfile .is_encrypted ()
151
- keyfile .encrypt ( password = 'thisisafakepassword' )
152
- assert keyfile .is_encrypted ()
146
+ _keyfile .set_keypair (alice , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
147
+ assert _keyfile .is_readable ()
148
+ assert _keyfile .is_writable ()
149
+ assert _keyfile .is_encrypted ()
150
+ _keyfile .decrypt ( password = 'thisisafakepassword' )
151
+ assert not _keyfile .is_encrypted ()
152
+ _keyfile .encrypt ( password = 'thisisafakepassword' )
153
+ assert _keyfile .is_encrypted ()
153
154
str (keyfile )
154
- keyfile .decrypt ( password = 'thisisafakepassword' )
155
- assert not keyfile .is_encrypted ()
155
+ _keyfile .decrypt ( password = 'thisisafakepassword' )
156
+ assert not _keyfile .is_encrypted ()
156
157
str (keyfile )
157
158
158
- assert keyfile .get_keypair ( password = 'thisisafakepassword' ).ss58_address == alice .ss58_address
159
- assert keyfile .get_keypair ( password = 'thisisafakepassword' ).private_key == alice .private_key
160
- assert keyfile .get_keypair ( password = 'thisisafakepassword' ).public_key == alice .public_key
159
+ assert _keyfile .get_keypair ( password = 'thisisafakepassword' ).ss58_address == alice .ss58_address
160
+ assert _keyfile .get_keypair ( password = 'thisisafakepassword' ).private_key == alice .private_key
161
+ assert _keyfile .get_keypair ( password = 'thisisafakepassword' ).public_key == alice .public_key
161
162
162
163
bob = Keypair .create_from_uri ('/Bob' )
163
- keyfile .set_keypair (bob , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
164
- assert keyfile .get_keypair ( password = 'thisisafakepassword' ).ss58_address == bob .ss58_address
165
- assert keyfile .get_keypair ( password = 'thisisafakepassword' ).public_key == bob .public_key
164
+ _keyfile .set_keypair (bob , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
165
+ assert _keyfile .get_keypair ( password = 'thisisafakepassword' ).ss58_address == bob .ss58_address
166
+ assert _keyfile .get_keypair ( password = 'thisisafakepassword' ).public_key == bob .public_key
166
167
167
168
repr (keyfile )
168
169
169
170
def test_legacy_coldkey (self ):
170
171
legacy_filename = os .path .join (self .root_path , "coldlegacy_keyfile" )
171
- keyfile = keyfile (path = legacy_filename )
172
- keyfile .make_dirs ()
172
+ _keyfile = keyfile (path = legacy_filename )
173
+ _keyfile .make_dirs ()
173
174
keyfile_data = b'0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f'
174
175
with open (legacy_filename , "wb" ) as keyfile_obj :
175
176
keyfile_obj .write ( keyfile_data )
176
- assert keyfile .keyfile_data == keyfile_data
177
- keyfile .encrypt ( password = 'this is the fake password' )
178
- keyfile .decrypt ( password = 'this is the fake password' )
177
+ assert _keyfile .keyfile_data == keyfile_data
178
+ _keyfile .encrypt ( password = 'this is the fake password' )
179
+ _keyfile .decrypt ( password = 'this is the fake password' )
179
180
keypair_bytes = b'{"accountId": "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f", "publicKey": "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f", "secretPhrase": null, "secretSeed": null, "ss58Address": "5DD26kC2kxajmwfbbZmVmxhrY9VeeyR1Gpzy9i8wxLUg6zxm"}'
180
- assert keyfile .keyfile_data == keypair_bytes
181
- assert keyfile .get_keypair ().ss58_address == "5DD26kC2kxajmwfbbZmVmxhrY9VeeyR1Gpzy9i8wxLUg6zxm"
182
- assert "0x" + keyfile .get_keypair ().public_key .hex () == "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f"
181
+ assert _keyfile .keyfile_data == keypair_bytes
182
+ assert _keyfile .get_keypair ().ss58_address == "5DD26kC2kxajmwfbbZmVmxhrY9VeeyR1Gpzy9i8wxLUg6zxm"
183
+ assert "0x" + _keyfile .get_keypair ().public_key .hex () == "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f"
183
184
184
185
def test_validate_password (self ):
185
186
assert validate_password (None ) == False
@@ -219,23 +220,21 @@ def test_user_interface(self):
219
220
assert ask_password_to_encrypt () == 'asdury3294y'
220
221
221
222
def test_overwriting (self ):
222
- keyfile = keyfile (path = os .path .join (self .root_path , "keyfile" ))
223
+ _keyfile = keyfile (path = os .path .join (self .root_path , "keyfile" ))
223
224
alice = Keypair .create_from_uri ('/Alice' )
224
- keyfile .set_keypair (alice , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
225
+ _keyfile .set_keypair (alice , encrypt = True , overwrite = True , password = 'thisisafakepassword' )
225
226
bob = Keypair .create_from_uri ('/Bob' )
226
227
227
228
with pytest .raises (KeyFileError ) as pytest_wrapped_e :
228
229
with patch ('builtins.input' , return_value = 'n' ):
229
- keyfile .set_keypair (bob , encrypt = True , overwrite = False , password = 'thisisafakepassword' )
230
+ _keyfile .set_keypair (bob , encrypt = True , overwrite = False , password = 'thisisafakepassword' )
230
231
231
232
def test_keyfile_mock (self ):
232
- file = keyfile ( _mock = True )
233
- assert file .exists_on_device ()
234
- assert not file .is_encrypted ()
235
- assert file .is_readable ()
236
- assert file .data
237
- assert file .keypair
238
- file .set_keypair ( keypair = Keypair .create_from_mnemonic ( mnemonic = Keypair .generate_mnemonic () ))
239
-
240
- def test_keyfile_mock_func (self ):
241
- file = keyfile .mock ()
233
+ with patch ('bittensor_wallet._keyfile.keyfile.__new__' , return_value = MockKeyfile (path = '/tmp/test-wallet/keyfile' )):
234
+ file = keyfile ( )
235
+ assert file .exists_on_device ()
236
+ assert not file .is_encrypted ()
237
+ assert file .is_readable ()
238
+ assert file .data
239
+ assert file .keypair
240
+ file .set_keypair ( keypair = Keypair .create_from_mnemonic ( mnemonic = Keypair .generate_mnemonic () ))
0 commit comments