Debian Codegraph @

tests/functional_tests/wallet.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 #encoding=utf-8
3
4 # Copyright (c) 2019-2022, The Monero Project
5 #
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without modification, are
9 # permitted provided that the following conditions are met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice, this list of
12 # conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
15 # of conditions and the following disclaimer in the documentation and/or other
16 # materials provided with the distribution.
17 #
18 # 3. Neither the name of the copyright holder nor the names of its contributors may be
19 # used to endorse or promote products derived from this software without specific
20 # prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 """Test basic wallet functionality
33 """
34
35 from __future__ import print_function
36 import sys
37 import util_resources
38
39 from framework.wallet import Wallet
40 from framework.daemon import Daemon
41
42 class WalletTest():
43 def run_test(self):
44 self.reset()
45 self.create()
46 self.check_main_address()
47 self.check_keys()
48 self.create_subaddresses()
49 self.tags()
50 self.update_lookahead()
51 self.attributes()
52 self.open_close()
53 self.languages()
54 self.change_password()
55 self.store()
56
57 def reset(self):
58 print('Resetting blockchain')
59 daemon = Daemon()
60 res = daemon.get_height()
61 daemon.pop_blocks(res.height - 1)
62 daemon.flush_txpool()
63
64 def create(self):
65 print('Creating wallet')
66 wallet = Wallet()
67 # close the wallet if any, will throw if none is loaded
68 try: wallet.close_wallet()
69 except: pass
70 seed = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted'
71 res = wallet.restore_deterministic_wallet(seed = seed)
72 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
73 assert res.seed == seed
74
75 def check_main_address(self):
76 print('Getting address')
77 wallet = Wallet()
78 res = wallet.get_address()
79 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', res
80 assert len(res.addresses) == 1
81 assert res.addresses[0].address == res.address
82 assert res.addresses[0].address_index == 0
83 assert res.addresses[0].used == False
84
85 def check_keys(self):
86 print('Checking keys')
87 wallet = Wallet()
88 res = wallet.query_key('view_key')
89 assert res.key == '49774391fa5e8d249fc2c5b45dadef13534bf2483dede880dac88f061e809100'
90 res = wallet.query_key('spend_key')
91 assert res.key == '148d78d2aba7dbca5cd8f6abcfb0b3c009ffbdbea1ff373d50ed94d78286640e'
92 res = wallet.query_key('mnemonic')
93 assert res.key == 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted'
94
95 def create_subaddresses(self):
96 print('Creating subaddresses')
97 wallet = Wallet()
98 res = wallet.create_account("idx1")
99 assert res.account_index == 1, res
100 assert res.address == '82pP87g1Vkd3LUMssBCumk3MfyEsFqLAaGDf6oxddu61EgSFzt8gCwUD4tr3kp9TUfdPs2CnpD7xLZzyC1Ei9UsW3oyCWDf', res
101 res = wallet.create_account("idx2")
102 assert res.account_index == 2, res
103 assert res.address == '8Bdb75y2MhvbkvaBnG7vYP6DCNneLWcXqNmfPmyyDkavAUUgrHQEAhTNK3jEq69kGPDrd3i5inPivCwTvvA12eQ4SJk9iyy', res
104
105 res = wallet.get_address(0, 0)
106 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', res
107 assert len(res.addresses) == 1
108 assert res.addresses[0].address_index == 0, res
109 res = wallet.get_address(1, 0)
110 assert res.address == '82pP87g1Vkd3LUMssBCumk3MfyEsFqLAaGDf6oxddu61EgSFzt8gCwUD4tr3kp9TUfdPs2CnpD7xLZzyC1Ei9UsW3oyCWDf', res
111 assert len(res.addresses) == 1
112 assert res.addresses[0].label == 'idx1', res
113 assert res.addresses[0].address_index == 0, res
114 res = wallet.get_address(2, 0)
115 assert res.address == '8Bdb75y2MhvbkvaBnG7vYP6DCNneLWcXqNmfPmyyDkavAUUgrHQEAhTNK3jEq69kGPDrd3i5inPivCwTvvA12eQ4SJk9iyy', res
116 assert len(res.addresses) == 1
117 assert res.addresses[0].label == 'idx2', res
118 assert res.addresses[0].address_index == 0, res
119
120 res = wallet.create_address(0, "sub_0_1")
121 res = wallet.create_address(1, "sub_1_1")
122 res = wallet.create_address(1, "sub_1_2")
123
124 res = wallet.get_address(0, [1])
125 assert len(res.addresses) == 1
126 assert res.addresses[0].address == '84QRUYawRNrU3NN1VpFRndSukeyEb3Xpv8qZjjsoJZnTYpDYceuUTpog13D7qPxpviS7J29bSgSkR11hFFoXWk2yNdsR9WF'
127 assert res.addresses[0].label == 'sub_0_1'
128 res = wallet.get_address(1, [1])
129 assert len(res.addresses) == 1
130 assert res.addresses[0].address == '87qyoPVaEcWikVBmG1TaP1KumZ3hB3Q5f4wZRjuppNdwYjWzs2RgbLYQgtpdu2YdoTT3EZhiUGaPJQt2FsykeFZbCtaGXU4'
131 assert res.addresses[0].label == 'sub_1_1'
132 res = wallet.get_address(1, [2])
133 assert len(res.addresses) == 1
134 assert res.addresses[0].address == '87KfgTZ8ER5D3Frefqnrqif11TjVsTPaTcp37kqqKMrdDRUhpJRczeR7KiBmSHF32UJLP3HHhKUDmEQyJrv2mV8yFDCq8eB'
135 assert res.addresses[0].label == 'sub_1_2'
136 res = wallet.get_address(1, [0, 1, 2])
137 assert len(res.addresses) == 3
138 assert res.addresses[0].address == '82pP87g1Vkd3LUMssBCumk3MfyEsFqLAaGDf6oxddu61EgSFzt8gCwUD4tr3kp9TUfdPs2CnpD7xLZzyC1Ei9UsW3oyCWDf'
139 assert res.addresses[0].label == 'idx1'
140 assert res.addresses[1].address == '87qyoPVaEcWikVBmG1TaP1KumZ3hB3Q5f4wZRjuppNdwYjWzs2RgbLYQgtpdu2YdoTT3EZhiUGaPJQt2FsykeFZbCtaGXU4'
141 assert res.addresses[1].label == 'sub_1_1'
142 assert res.addresses[2].address == '87KfgTZ8ER5D3Frefqnrqif11TjVsTPaTcp37kqqKMrdDRUhpJRczeR7KiBmSHF32UJLP3HHhKUDmEQyJrv2mV8yFDCq8eB'
143 assert res.addresses[2].label == 'sub_1_2'
144
145 res = wallet.label_address((1, 2), "sub_1_2_new")
146 res = wallet.get_address(1, [2])
147 assert len(res.addresses) == 1
148 assert res.addresses[0].address == '87KfgTZ8ER5D3Frefqnrqif11TjVsTPaTcp37kqqKMrdDRUhpJRczeR7KiBmSHF32UJLP3HHhKUDmEQyJrv2mV8yFDCq8eB'
149 assert res.addresses[0].label == 'sub_1_2_new'
150
151 res = wallet.label_account(1, "idx1_new")
152 res = wallet.get_address(1, [0])
153 assert len(res.addresses) == 1
154 assert res.addresses[0].address == '82pP87g1Vkd3LUMssBCumk3MfyEsFqLAaGDf6oxddu61EgSFzt8gCwUD4tr3kp9TUfdPs2CnpD7xLZzyC1Ei9UsW3oyCWDf'
155 assert res.addresses[0].label == 'idx1_new'
156
157 res = wallet.get_address_index('87KfgTZ8ER5D3Frefqnrqif11TjVsTPaTcp37kqqKMrdDRUhpJRczeR7KiBmSHF32UJLP3HHhKUDmEQyJrv2mV8yFDCq8eB')
158 assert res.index == {'major': 1, 'minor': 2}
159 res = wallet.get_address_index('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm')
160 assert res.index == {'major': 0, 'minor': 0}
161 res = wallet.get_address_index('84QRUYawRNrU3NN1VpFRndSukeyEb3Xpv8qZjjsoJZnTYpDYceuUTpog13D7qPxpviS7J29bSgSkR11hFFoXWk2yNdsR9WF')
162 assert res.index == {'major': 0, 'minor': 1}
163 res = wallet.get_address_index('82pP87g1Vkd3LUMssBCumk3MfyEsFqLAaGDf6oxddu61EgSFzt8gCwUD4tr3kp9TUfdPs2CnpD7xLZzyC1Ei9UsW3oyCWDf')
164 assert res.index == {'major': 1, 'minor': 0}
165
166 res = wallet.label_account(0, "main")
167
168 def update_lookahead(self):
169 print('Updating subaddress lookahead')
170 wallet = Wallet()
171 address_0_999 = '8BQKgTSSqJjP14AKnZUBwnXWj46MuNmLvHfPTpmry52DbfNjjHVvHUk4mczU8nj8yZ57zBhksTJ8kM5xKeJXw55kCMVqyG7' # this is the address for address 999 of the main account in the test wallet
172 try: # assert address_1_999 is not in the current pubkey table
173 wallet.get_address_index(address_0_999)
174 except Exception as e:
175 assert str(e) == "{'error': {'code': -2, 'message': \"Address doesn't belong to the wallet\"}, 'id': '0', 'jsonrpc': '2.0'}"
176 # update the lookahead and assert the high index address is now in the table
177 wallet.set_subaddress_lookahead(50, 1000)
178 r = wallet.get_address_index(address_0_999)
179 assert r['index']['major'] == 0
180 assert r['index']['minor'] == 999
181
182 def tags(self):
183 print('Testing tags')
184 wallet = Wallet()
185 res = wallet.get_account_tags()
186 assert not 'account_tags' in res or len(res.account_tags) == 0
187 ok = False
188 try: res = wallet.get_accounts('tag')
189 except: ok = True
190 assert ok or not 'subaddress_accounts' in res or res.subaddress_accounts == 0
191 wallet.tag_accounts('tag0', [1])
192 res = wallet.get_account_tags()
193 assert len(res.account_tags) == 1
194 assert res.account_tags[0].tag == 'tag0'
195 assert res.account_tags[0].label == ''
196 assert res.account_tags[0].accounts == [1]
197 res = wallet.get_accounts('tag0')
198 assert len(res.subaddress_accounts) == 1
199 assert res.subaddress_accounts[0].account_index == 1
200 assert res.subaddress_accounts[0].base_address == '82pP87g1Vkd3LUMssBCumk3MfyEsFqLAaGDf6oxddu61EgSFzt8gCwUD4tr3kp9TUfdPs2CnpD7xLZzyC1Ei9UsW3oyCWDf'
201 assert res.subaddress_accounts[0].balance == 0
202 assert res.subaddress_accounts[0].unlocked_balance == 0
203 assert res.subaddress_accounts[0].label == 'idx1_new'
204 assert res.subaddress_accounts[0].tag == 'tag0'
205 wallet.untag_accounts([0])
206 res = wallet.get_account_tags()
207 assert len(res.account_tags) == 1
208 assert res.account_tags[0].tag == 'tag0'
209 assert res.account_tags[0].label == ''
210 assert res.account_tags[0].accounts == [1]
211 wallet.untag_accounts([1])
212 res = wallet.get_account_tags()
213 assert not 'account_tags' in res or len(res.account_tags) == 0
214 wallet.tag_accounts('tag0', [0])
215 wallet.tag_accounts('tag1', [1])
216 res = wallet.get_account_tags()
217 assert len(res.account_tags) == 2
218 x = [x for x in res.account_tags if x.tag == 'tag0']
219 assert len(x) == 1
220 assert x[0].tag == 'tag0'
221 assert x[0].label == ''
222 assert x[0].accounts == [0]
223 x = [x for x in res.account_tags if x.tag == 'tag1']
224 assert len(x) == 1
225 assert x[0].tag == 'tag1'
226 assert x[0].label == ''
227 assert x[0].accounts == [1]
228 wallet.tag_accounts('tagA', [0, 1])
229 res = wallet.get_account_tags()
230 assert len(res.account_tags) == 1
231 assert res.account_tags[0].tag == 'tagA'
232 assert res.account_tags[0].label == ''
233 assert res.account_tags[0].accounts == [0, 1]
234 wallet.tag_accounts('tagB', [1, 0])
235 res = wallet.get_account_tags()
236 assert len(res.account_tags) == 1
237 assert res.account_tags[0].tag == 'tagB'
238 assert res.account_tags[0].label == ''
239 assert res.account_tags[0].accounts == [0, 1]
240 wallet.set_account_tag_description('tagB', 'tag B')
241 res = wallet.get_account_tags()
242 assert len(res.account_tags) == 1
243 assert res.account_tags[0].tag == 'tagB'
244 assert res.account_tags[0].label == 'tag B'
245 assert res.account_tags[0].accounts == [0, 1]
246 res = wallet.get_accounts('tagB')
247 assert len(res.subaddress_accounts) == 2
248 subaddress_accounts = []
249 for x in res.subaddress_accounts:
250 assert x.balance == 0
251 assert x.unlocked_balance == 0
252 subaddress_accounts.append((x.account_index, x.base_address, x.label))
253 assert sorted(subaddress_accounts) == [(0, '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 'main'), (1, '82pP87g1Vkd3LUMssBCumk3MfyEsFqLAaGDf6oxddu61EgSFzt8gCwUD4tr3kp9TUfdPs2CnpD7xLZzyC1Ei9UsW3oyCWDf', 'idx1_new')]
254
255 def attributes(self):
256 print('Testing attributes')
257 wallet = Wallet()
258
259 ok = False
260 try: res = wallet.get_attribute('foo')
261 except: ok = True
262 assert ok
263 res = wallet.set_attribute('foo', 'bar')
264 res = wallet.get_attribute('foo')
265 assert res.value == 'bar'
266 res = wallet.set_attribute('foo', 'いっしゅん')
267 res = wallet.get_attribute('foo')
268 assert res.value == u'いっしゅん'
269 ok = False
270 try: res = wallet.get_attribute('いちりゅう')
271 except: ok = True
272 assert ok
273 res = wallet.set_attribute('いちりゅう', 'いっぽう')
274 res = wallet.get_attribute('いちりゅう')
275 assert res.value == u'いっぽう'
276
277 def open_close(self):
278 print('Testing open/close')
279 wallet = Wallet()
280
281 res = wallet.get_address()
282 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
283
284 wallet.close_wallet()
285 ok = False
286 try: res = wallet.get_address()
287 except: ok = True
288 assert ok
289
290 wallet.restore_deterministic_wallet(seed = 'peeled mixture ionic radar utopia puddle buying illness nuns gadget river spout cavernous bounced paradise drunk looking cottage jump tequila melting went winter adjust spout')
291 res = wallet.get_address()
292 assert res.address == '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW'
293
294 wallet.close_wallet()
295 ok = False
296 try: wallet.get_address()
297 except: ok = True
298 assert ok
299
300 wallet.restore_deterministic_wallet(seed = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted')
301 res = wallet.get_address()
302 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
303
304 def languages(self):
305 print('Testing languages')
306 wallet = Wallet()
307 res = wallet.get_languages()
308 assert 'English' in res.languages
309 assert 'English' in res.languages_local
310 assert 'Dutch' in res.languages
311 assert 'Nederlands' in res.languages_local
312 assert 'Japanese' in res.languages
313 assert u'日本語' in res.languages_local
314 try: wallet.close_wallet()
315 except: pass
316 languages = res.languages
317 languages_local = res.languages_local
318 for language in languages + languages_local:
319 sys.stdout.write('Creating ' + language + ' wallet\n')
320 wallet.create_wallet(filename = '', language = language)
321 res = wallet.query_key('mnemonic')
322 wallet.close_wallet()
323
324 def change_password(self):
325 print('Testing password change')
326 wallet = Wallet()
327
328 # close the wallet if any, will throw if none is loaded
329 try: wallet.close_wallet()
330 except: pass
331
332 util_resources.remove_wallet_files('test1')
333
334 seed = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted'
335 res = wallet.restore_deterministic_wallet(seed = seed, filename = 'test1')
336 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
337 assert res.seed == seed
338
339 wallet.close_wallet()
340 res = wallet.open_wallet('test1', password = '')
341 res = wallet.get_address()
342 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
343
344 res = wallet.change_wallet_password(old_password = '', new_password = 'foo')
345 wallet.close_wallet()
346
347 ok = False
348 try: res = wallet.open_wallet('test1', password = '')
349 except: ok = True
350 assert ok
351
352 res = wallet.open_wallet('test1', password = 'foo')
353 res = wallet.get_address()
354 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
355
356 wallet.close_wallet()
357
358 util_resources.remove_wallet_files('test1')
359
360 def store(self):
361 print('Testing store')
362 wallet = Wallet()
363
364 # close the wallet if any, will throw if none is loaded
365 try: wallet.close_wallet()
366 except: pass
367
368 util_resources.remove_wallet_files('test1')
369
370 seed = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted'
371 res = wallet.restore_deterministic_wallet(seed = seed, filename = 'test1')
372 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
373 assert res.seed == seed
374
375 util_resources.remove_file('test1')
376 assert util_resources.file_exists('test1.keys')
377 assert not util_resources.file_exists('test1')
378 wallet.store()
379 assert util_resources.file_exists('test1.keys')
380 assert util_resources.file_exists('test1')
381
382 wallet.close_wallet()
383
384 wallet.open_wallet(filename = 'test1', password = '')
385 wallet.close_wallet()
386
387 util_resources.remove_wallet_files('test1')
388
389
390 if __name__ == '__main__':
391 WalletTest().run_test()