Coverage for tests/wallet/test_creation.py: 99%
748 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1from __future__ import annotations
3import logging
5import pytest
6from monero import (
7 MoneroDaemonRpc,
8 MoneroDestination,
9 MoneroError,
10 MoneroNetworkType,
11 MoneroRpcConnection,
12 MoneroTxConfig,
13 MoneroTxWallet,
14 MoneroUtils,
15 MoneroVersion,
16 MoneroWallet,
17 MoneroWalletConfig,
18 MoneroWalletFull,
19 MoneroWalletKeys,
20 MoneroWalletRpc,
21)
22from typing_extensions import override
24from utils import (
25 AssertUtils,
26 GenUtils,
27 StringUtils,
28 SyncProgressTester,
29 TestUtils,
30 TestUtils as Utils,
31 TxWalletUtils,
32 ViewOnlyAndOfflineWalletTester,
33 WalletErrorUtils,
34 WalletUtils,
35)
36from .base_test_monero_wallet import BaseTestMoneroWallet
37from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport
39logger: logging.Logger = logging.getLogger("TestMoneroWalletCreation")
42class BaseTestMoneroWalletCreation(BaseTestMoneroWallet):
43 """Common wallet creation tests."""
45 # Can create a random wallet
46 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
47 def test_create_wallet_random(self) -> None:
48 """
49 Can create a random wallet.
50 """
51 config: MoneroWalletConfig = MoneroWalletConfig()
52 wallet: MoneroWallet = self._create_wallet(config)
53 path: str = wallet.get_path()
55 try:
56 MoneroUtils.validate_address(wallet.get_primary_address(), TestUtils.NETWORK_TYPE)
57 MoneroUtils.validate_private_view_key(wallet.get_private_view_key())
58 MoneroUtils.validate_private_spend_key(wallet.get_private_spend_key())
59 MoneroUtils.validate_mnemonic(wallet.get_seed())
60 if not isinstance(wallet, MoneroWalletRpc):
61 # TODO monero-wallet-rpc: get seed language
62 assert MoneroWallet.DEFAULT_LANGUAGE == wallet.get_seed_language()
63 finally:
64 self._close_wallet(wallet)
66 # attempt to create wallet at same path
67 config = MoneroWalletConfig()
68 config.path = path
69 with pytest.raises(Exception) as exc_info:
70 self._create_wallet(config)
71 assert str(exc_info.value) == "Wallet already exists: " + path
73 # attempt to create wallet with unknown language
74 config = MoneroWalletConfig()
75 config.language = "english"
76 with pytest.raises(Exception) as exc_info:
77 self._create_wallet(config)
78 assert str(exc_info.value) == "Unknown language: english"
80 # Can create a wallet from a seed
81 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
82 def test_create_wallet_from_seed(self, wallet: MoneroWallet, test_config: BaseTestMoneroWallet.Config) -> None:
83 # save for comparison
84 primary_address: str = wallet.get_primary_address()
85 private_view_key: str = wallet.get_private_view_key()
86 private_spend_key: str = wallet.get_private_spend_key()
88 # recreate test wallet from seed
89 config: MoneroWalletConfig = MoneroWalletConfig()
90 config.seed = TestUtils.SEED
91 config.restore_height = TestUtils.FIRST_RECEIVE_HEIGHT
93 w: MoneroWallet = self._create_wallet(config)
94 path: str = w.get_path()
95 try:
96 assert primary_address == w.get_primary_address()
97 assert private_view_key == w.get_private_view_key()
98 assert private_spend_key == w.get_private_spend_key()
99 assert TestUtils.SEED, w.get_seed()
100 if not isinstance(w, MoneroWalletRpc):
101 assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language()
102 finally:
103 self._close_wallet(w)
105 # attempt to create wallet with two missing words
106 config = MoneroWalletConfig()
107 config.seed = test_config.seed
108 config.restore_height = TestUtils.FIRST_RECEIVE_HEIGHT
109 with pytest.raises(Exception) as exc_info:
110 self._create_wallet(config)
111 assert str(exc_info.value) == "Invalid mnemonic"
113 # attempt to create wallet at same path
114 config = MoneroWalletConfig()
115 config.path = path
116 with pytest.raises(Exception) as exc_info:
117 self._create_wallet(config)
118 assert str(exc_info.value) == "Wallet already exists: " + path
120 # Can create a wallet from a seed with offset
121 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
122 def test_create_wallet_from_seed_with_offset(self) -> None:
123 # create test wallet with offset
124 config: MoneroWalletConfig = MoneroWalletConfig()
125 config.seed = TestUtils.SEED
126 config.restore_height = TestUtils.FIRST_RECEIVE_HEIGHT
127 config.seed_offset = "my secret offset!"
128 wallet: MoneroWallet = self._create_wallet(config)
129 try:
130 MoneroUtils.validate_mnemonic(wallet.get_seed())
131 assert TestUtils.SEED != wallet.get_seed()
132 MoneroUtils.validate_address(wallet.get_primary_address(), TestUtils.NETWORK_TYPE)
133 assert TestUtils.ADDRESS != wallet.get_primary_address()
134 if not isinstance(wallet, MoneroWalletRpc):
135 # TODO monero-wallet-rpc: support
136 assert MoneroWallet.DEFAULT_LANGUAGE == wallet.get_seed_language()
137 finally:
138 self._close_wallet(wallet)
140 # Can create a wallet from keys
141 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
142 def test_create_wallet_from_keys(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None:
143 # save for comparison
144 primary_address: str = wallet.get_primary_address()
145 private_view_key: str = wallet.get_private_view_key()
146 private_spend_key: str = wallet.get_private_spend_key()
148 # recreate test wallet from keys
149 config: MoneroWalletConfig = MoneroWalletConfig()
150 config.primary_address = primary_address
151 config.private_view_key = private_view_key
152 config.private_spend_key = private_spend_key
153 config.restore_height = daemon.get_height()
154 w: MoneroWallet = self._create_wallet(config)
155 path: str = w.get_path()
157 try:
158 assert primary_address == w.get_primary_address()
159 assert private_view_key == w.get_private_view_key()
160 assert private_spend_key == w.get_private_spend_key()
161 if not w.is_connected_to_daemon():
162 # TODO monero-project: keys wallets not connected
163 logger.warning(f"WARNING: {WalletErrorUtils.CREATED_WALLET_KEYS_ERROR}")
164 assert w.is_connected_to_daemon(), WalletErrorUtils.CREATED_WALLET_KEYS_ERROR
165 if not isinstance(w, MoneroWalletRpc):
166 # TODO monero-wallet-rpc: cannot get seed from wallet created from keys?
167 MoneroUtils.validate_mnemonic(w.get_seed())
168 assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language()
169 finally:
170 self._close_wallet(w)
172 # recreate test wallet from spend key
173 if not isinstance(w, MoneroWalletRpc): # TODO monero-wallet-rpc: cannot create wallet from spend key?
174 config = MoneroWalletConfig()
175 config.private_spend_key = private_spend_key
176 config.restore_height = daemon.get_height()
177 w = self._create_wallet(config)
179 try:
180 assert primary_address == w.get_primary_address()
181 assert private_view_key == w.get_private_view_key()
182 assert private_spend_key == w.get_private_spend_key()
183 if not w.is_connected_to_daemon():
184 # TODO monero-project: keys wallets not connected
185 logger.warning(f"{WalletErrorUtils.CREATED_WALLET_KEYS_ERROR}")
186 assert w.is_connected_to_daemon(), WalletErrorUtils.CREATED_WALLET_KEYS_ERROR
187 if not isinstance(w, MoneroWalletRpc):
188 # TODO monero-wallet-rpc: cannot get seed from wallet created from keys?
189 MoneroUtils.validate_mnemonic(w.get_seed())
190 assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language()
191 finally:
192 self._close_wallet(w)
194 # attempt to create wallet at same path
195 config = MoneroWalletConfig()
196 config.path = path
197 with pytest.raises(Exception) as exc_info:
198 self._create_wallet(config)
199 assert str(exc_info.value) == "Wallet already exists: " + path
201 # Can create wallets with subaddress lookahead
202 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
203 def test_subaddress_lookahead(self, wallet: MoneroWallet) -> None:
204 receiver: MoneroWallet | None = None
205 try:
206 # create wallet with high subaddress lookahead
207 config: MoneroWalletConfig = MoneroWalletConfig()
208 config.account_lookahead = 1
209 config.subaddress_lookahead = 100000
210 receiver = self._create_wallet(config)
212 # transfer funds to subaddress with high index
213 tx_config: MoneroTxConfig = MoneroTxConfig()
214 tx_config.account_index = 0
215 dest: MoneroDestination = MoneroDestination()
216 dest.address = receiver.get_subaddress(0, 85000).address
217 dest.amount = TxWalletUtils.MAX_FEE
218 tx_config.destinations.append(dest)
219 tx_config.relay = True
221 wallet.create_tx(tx_config)
223 # observe unconfirmed funds
224 GenUtils.wait_for(1000)
225 receiver.sync()
226 assert receiver.get_balance() > 0
227 finally:
228 if receiver is not None:
229 self._close_wallet(receiver)
231 # Can get the wallet's version
232 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
233 def test_get_version(self, wallet: MoneroWallet) -> None:
234 version: MoneroVersion = wallet.get_version()
235 assert version.number is not None
236 assert version.number > 0
237 assert version.is_release is not None
239 # Can get the wallet's path
240 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
241 def test_get_path(self) -> None:
242 # create random wallet
243 config: MoneroWalletConfig = MoneroWalletConfig()
244 wallet: MoneroWallet = self._create_wallet(config)
246 # set a random attribute
247 #String uuid = UUID.randomUUID().toString()
248 uuid: str = StringUtils.get_random_string()
249 wallet.set_attribute("uuid", uuid)
251 # record the wallet's path then save and close
252 path: str = wallet.get_path()
253 self._close_wallet(wallet, True)
255 # re-open the wallet using its path
256 wallet = self._open_wallet_from_path(path, None)
258 # test the attribute
259 assert uuid == wallet.get_attribute("uuid")
260 self._close_wallet(wallet)
262 # Can change the wallet password
263 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
264 def test_change_password(self) -> None:
265 # create random wallet
266 config: MoneroWalletConfig = MoneroWalletConfig()
267 config.password = TestUtils.WALLET_PASSWORD
268 wallet: MoneroWallet = self._create_wallet(config)
269 path: str = wallet.get_path()
271 # change password
272 new_password: str = ""
273 wallet.change_password(TestUtils.WALLET_PASSWORD, new_password)
275 # close wallet without saving
276 self._close_wallet(wallet)
278 # old password does not work (password change is auto saved)
279 config = MoneroWalletConfig()
280 config.path = path
281 config.password = TestUtils.WALLET_PASSWORD
282 with pytest.raises(Exception) as exc_info:
283 self._open_wallet(config)
284 # TODO: different errors from rpc and wallet2
285 e_str = str(exc_info.value).lower()
286 assert "failed to open wallet" in e_str or "invalid password" in e_str
288 # open wallet with new password
289 config = MoneroWalletConfig()
290 config.path = path
291 config.password = new_password
292 wallet = self._open_wallet(config)
294 # change password with incorrect password
295 with pytest.raises(Exception) as exc_info:
296 wallet.change_password("badpassword", new_password)
297 assert "Invalid original password." == str(exc_info.value)
298 # save and close
299 self._close_wallet(wallet, True)
301 # open wallet
302 config = MoneroWalletConfig()
303 config.path = path
304 config.password = new_password
305 wallet = self._open_wallet(config)
307 # close wallet
308 self._close_wallet(wallet)
310 # Can save and close the wallet in a single call
311 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
312 def test_save_and_close(self) -> None:
313 # create random wallet
314 password: str = ""
315 config: MoneroWalletConfig = MoneroWalletConfig()
316 config.password = password
317 wallet: MoneroWallet = self._create_wallet(config)
318 path: str = wallet.get_path()
320 # set an attribute
321 uuid: str = StringUtils.get_random_string()
322 wallet.set_attribute("id", uuid)
324 # close the wallet without saving
325 self._close_wallet(wallet)
327 # re-open the wallet and ensure attribute was not saved
328 config = MoneroWalletConfig()
329 config.path = path
330 config.password = password
331 wallet = self._open_wallet(config)
332 assert wallet.get_attribute("id") == ""
334 # set the attribute and close with saving
335 wallet.set_attribute("id", uuid)
336 self._close_wallet(wallet, True)
338 # re-open the wallet and ensure attribute was saved
339 wallet = self._open_wallet(config)
340 assert uuid == wallet.get_attribute("id")
341 self._close_wallet(wallet)
343 # Supports view-only and offline wallets to create, sign and submit transactions
344 @pytest.mark.skipif(TestUtils.LITE_MODE, reason="LITE_MODE enabled")
345 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False and TestUtils.TEST_RELAYS is False, reason="TEST_NON_RELAYS and TEST_RELAYS disabled")
346 def test_view_only_and_offline_wallets(self, wallet: MoneroWallet) -> None:
347 # create view-only and offline wallets
348 config: MoneroWalletConfig = MoneroWalletConfig()
349 config.primary_address = wallet.get_primary_address()
350 config.private_view_key = wallet.get_private_view_key()
351 config.restore_height = TestUtils.FIRST_RECEIVE_HEIGHT
352 view_only_wallet: MoneroWallet = self._create_wallet(config)
354 config = MoneroWalletConfig()
355 config.primary_address = wallet.get_primary_address()
356 config.private_view_key = wallet.get_private_view_key()
357 config.private_spend_key = wallet.get_private_spend_key()
358 config.server = MoneroRpcConnection(TestUtils.OFFLINE_SERVER_URI)
359 config.restore_height = 0
360 offline_wallet: MoneroWallet = self._create_wallet(config)
361 assert offline_wallet.is_connected_to_daemon() is False
362 view_only_wallet.sync()
364 # test tx signing with wallets
365 try:
366 tester: ViewOnlyAndOfflineWalletTester = ViewOnlyAndOfflineWalletTester(wallet, view_only_wallet, offline_wallet)
367 tester.test()
368 finally:
369 self._close_wallet(view_only_wallet)
370 self._close_wallet(offline_wallet)
372 # Can get and set arbitrary key/value attributes
373 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
374 def test_set_attributes(self, wallet: MoneroWallet) -> None:
375 # set attributes
376 attrs: dict[str, str] = {}
377 for i in range(5):
378 key: str = f"attr{i}"
379 val: str = StringUtils.get_random_string()
380 attrs[key] = val
381 wallet.set_attribute(key, val)
383 # test attributes
384 for key in attrs:
385 val = attrs[key]
386 assert val == wallet.get_attribute(key)
388 # get an undefined attribute
389 assert wallet.get_attribute("unset_key") == ""
392@pytest.mark.integration
393class TestMoneroWalletFullCreation(FullWalletSupport, BaseTestMoneroWalletCreation):
394 """Full wallet creation integration tests."""
396 # Can create a random full wallet
397 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
398 def test_create_wallet_random_full(self, daemon: MoneroDaemonRpc) -> None:
399 # create random wallet with defaults
400 path: str = Utils.get_random_wallet_path()
401 config: MoneroWalletConfig = MoneroWalletConfig()
402 config.path = path
403 config.network_type = MoneroNetworkType.MAINNET
404 config.server = MoneroRpcConnection(Utils.OFFLINE_SERVER_URI)
405 wallet: MoneroWalletFull = self._create_wallet(config)
406 MoneroUtils.validate_mnemonic(wallet.get_seed())
407 MoneroUtils.validate_address(wallet.get_primary_address(), MoneroNetworkType.MAINNET)
408 assert wallet.get_network_type() == MoneroNetworkType.MAINNET
409 AssertUtils.assert_equals(wallet.get_daemon_connection(), MoneroRpcConnection(Utils.OFFLINE_SERVER_URI))
410 assert wallet.is_connected_to_daemon() is False
411 assert wallet.get_seed_language() == "English"
412 assert wallet.get_path() == path
413 assert wallet.is_synced() is False
414 # TODO monero-project: why does height of new unsynced wallet start at 1?
415 assert wallet.get_height() == 1
416 assert wallet.get_restore_height() >= 0
418 # cannot get daemon chain height: wallet2 masks errors from untrusted daemons
419 with pytest.raises(Exception) as exc_info:
420 wallet.get_daemon_height()
421 assert str(exc_info.value) == "daemon error"
423 # set daemon and check chain height
424 wallet.set_daemon_connection(daemon.get_rpc_connection())
425 assert daemon.get_height() == wallet.get_daemon_height()
427 # close wallet which releases resources
428 wallet.close()
430 # create random wallet with non defaults
431 path = Utils.get_random_wallet_path()
432 config = MoneroWalletConfig()
433 config.path = path
434 config.network_type = MoneroNetworkType.TESTNET
435 config.language = "Spanish"
436 wallet = self._create_wallet(config, False)
437 MoneroUtils.validate_mnemonic(wallet.get_seed())
438 MoneroUtils.validate_address(wallet.get_primary_address(), MoneroNetworkType.TESTNET)
439 assert wallet.get_network_type() == MoneroNetworkType.TESTNET
440 assert wallet.get_daemon_connection() is not None
441 assert wallet.get_daemon_connection() != daemon.get_rpc_connection()
442 AssertUtils.assert_equals(wallet.get_daemon_connection(), daemon.get_rpc_connection())
443 assert wallet.is_connected_to_daemon()
444 assert wallet.get_seed_language() == "Spanish"
445 assert path == wallet.get_path()
446 assert wallet.is_synced() is False
447 # TODO monero-project: why is height of unsynced wallet 1?
448 assert wallet.get_height() == 1
449 if daemon.is_connected():
450 assert daemon.get_height() == wallet.get_restore_height()
451 else:
452 assert wallet.get_restore_height() >= 0
454 wallet.close()
456 # Can create a full wallet from seed
457 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
458 def test_create_wallet_from_seed_full(self, daemon: MoneroDaemonRpc) -> None:
459 # create unconnected wallet with seed
460 path: str = Utils.get_random_wallet_path()
461 config: MoneroWalletConfig = MoneroWalletConfig()
462 config.path = path
463 config.seed = Utils.SEED
464 config.server = MoneroRpcConnection(Utils.OFFLINE_SERVER_URI)
465 wallet: MoneroWalletFull = self._create_wallet(config)
466 assert wallet.get_seed() == Utils.SEED
467 assert wallet.get_primary_address() == Utils.ADDRESS
468 assert wallet.get_network_type() == Utils.NETWORK_TYPE
469 AssertUtils.assert_equals(MoneroRpcConnection(Utils.OFFLINE_SERVER_URI), wallet.get_daemon_connection())
470 assert wallet.is_connected_to_daemon() is False
471 assert wallet.get_seed_language() == "English"
472 assert wallet.get_path() == path
473 assert wallet.is_synced() is False
474 assert wallet.get_height() == 1
475 assert wallet.get_restore_height() == 0
476 wallet.start_syncing() # succeeds while offline, syncing when a daemon becomes reachable
478 wallet.close()
480 # create wallet without restore height
481 path = Utils.get_random_wallet_path()
482 config = MoneroWalletConfig()
483 config.path = path
484 config.seed = Utils.SEED
485 wallet = self._create_wallet(config, False)
486 assert wallet.get_seed() == Utils.SEED
487 assert wallet.get_primary_address() == Utils.ADDRESS
488 assert wallet.get_network_type() == Utils.NETWORK_TYPE
489 assert wallet.get_daemon_connection() is not None
490 assert wallet.get_daemon_connection() != daemon.get_rpc_connection()
491 AssertUtils.assert_equals(wallet.get_daemon_connection(), daemon.get_rpc_connection())
492 assert wallet.is_connected_to_daemon()
493 assert wallet.get_seed_language() == "English"
494 assert wallet.get_path() == path
495 assert wallet.is_synced() is False
496 assert wallet.get_height() == 1
497 # TODO (java annotation) restore height is lost after closing only in JNI
498 assert wallet.get_restore_height() == 0
499 wallet.close()
501 # create wallet with seed, no connection, and restore height
502 restore_height: int = 10000
503 path = Utils.get_random_wallet_path()
504 config = MoneroWalletConfig()
505 config.path = path
506 config.seed = Utils.SEED
507 config.restore_height = restore_height
508 config.server = MoneroRpcConnection(Utils.OFFLINE_SERVER_URI)
509 wallet = self._create_wallet(config)
510 assert wallet.get_seed() == Utils.SEED
511 assert wallet.get_primary_address() == Utils.ADDRESS
512 assert wallet.get_network_type() == Utils.NETWORK_TYPE
513 AssertUtils.assert_equals(MoneroRpcConnection(Utils.OFFLINE_SERVER_URI), wallet.get_daemon_connection())
514 assert wallet.is_connected_to_daemon() is False
515 assert wallet.get_seed_language() == "English"
516 # TODO monero-project: why does height of new unsynced wallet start at 1?
517 assert wallet.get_height() == 1
518 assert wallet.get_restore_height() == restore_height
519 assert wallet.get_path() == path
520 wallet.close(True)
521 config = MoneroWalletConfig()
522 config.path = path
523 config.server = MoneroRpcConnection(Utils.OFFLINE_SERVER_URI)
524 wallet = self._open_wallet(config)
525 assert wallet.is_connected_to_daemon() is False
526 assert wallet.is_synced() is False
527 assert wallet.get_height() == 1
528 assert wallet.get_restore_height() == restore_height
529 wallet.close()
531 # create wallet with seed, connection, and restore height
532 path = Utils.get_random_wallet_path()
533 config = MoneroWalletConfig()
534 config.path = path
535 config.seed = Utils.SEED
536 config.restore_height = restore_height
537 wallet = self._create_wallet(config, False)
538 assert wallet.get_seed() == Utils.SEED
539 assert wallet.get_primary_address() == Utils.ADDRESS
540 assert wallet.get_network_type() == Utils.NETWORK_TYPE
541 assert wallet.get_daemon_connection() is not None
542 assert wallet.get_daemon_connection() != daemon.get_rpc_connection()
543 AssertUtils.assert_equals(wallet.get_daemon_connection(), daemon.get_rpc_connection())
544 assert wallet.is_connected_to_daemon()
545 assert wallet.get_seed_language() == "English"
546 assert wallet.get_path() == path
547 assert wallet.is_synced() is False
548 # TODO monero-project: why does height of new unsynced wallet start at 1?
549 assert wallet.get_height() == 1
550 assert wallet.get_restore_height() == restore_height
551 wallet.close()
553 # Can create a full wallet from keys
554 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
555 def test_create_wallet_from_keys_pybind(self, wallet: MoneroWalletFull) -> None:
556 # recreate test wallet from keys
557 path: str = Utils.get_random_wallet_path()
558 config: MoneroWalletConfig = MoneroWalletConfig()
559 config.path = path
560 config.primary_address = wallet.get_primary_address()
561 config.private_view_key = wallet.get_private_view_key()
562 config.private_spend_key = wallet.get_private_spend_key()
563 config.restore_height = Utils.FIRST_RECEIVE_HEIGHT
564 wallet_keys: MoneroWalletFull = self._create_wallet(config)
566 try:
567 # test wallet keys equality
568 assert wallet.get_seed() == wallet_keys.get_seed()
569 assert wallet.get_primary_address() == wallet_keys.get_primary_address()
570 assert wallet.get_private_view_key() == wallet_keys.get_private_view_key()
571 assert wallet.get_public_view_key() == wallet_keys.get_public_view_key()
572 assert wallet.get_private_spend_key() == wallet_keys.get_private_spend_key()
573 assert wallet.get_public_spend_key() == wallet_keys.get_public_spend_key()
574 assert Utils.FIRST_RECEIVE_HEIGHT == wallet_keys.get_restore_height()
575 assert wallet_keys.is_connected_to_daemon()
576 assert wallet_keys.is_synced() is False
577 finally:
578 wallet_keys.close()
580 # Does not interfere with other wallet notifications
581 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
582 @pytest.mark.skipif(Utils.LITE_MODE, reason="LITE_MODE enabled")
583 def test_wallets_do_not_interfere(self, daemon: MoneroDaemonRpc) -> None:
584 # create 2 wallets with a recent restore height
585 height: int = daemon.get_height()
586 restore_height: int = height - 5
587 config: MoneroWalletConfig = MoneroWalletConfig()
588 config.seed = Utils.SEED
589 config.restore_height = restore_height
590 wallet1: MoneroWalletFull = self._create_wallet(config, False)
592 config = MoneroWalletConfig()
593 config.seed = Utils.SEED
594 config.restore_height = restore_height
595 wallet2: MoneroWalletFull = self._create_wallet(config, False)
597 # track notifications of each wallet
598 tester1: SyncProgressTester = SyncProgressTester(wallet1, restore_height, height)
599 tester2: SyncProgressTester = SyncProgressTester(wallet2, restore_height, height)
600 wallet1.add_listener(tester1)
601 wallet2.add_listener(tester2)
603 # sync first wallet and test that 2nd is not notified
604 wallet1.sync()
605 assert tester1.is_notified
606 assert not tester2.is_notified
608 # sync 2nd wallet and test that 1st is not notified
609 tester3: SyncProgressTester = SyncProgressTester(wallet1, restore_height, height)
610 wallet1.add_listener(tester3)
611 wallet2.sync()
612 assert tester2.is_notified
613 assert not tester3.is_notified
615 # Can be closed
616 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
617 def test_close(self) -> None:
618 # create test wallet
619 path: str = Utils.get_random_wallet_path()
620 config: MoneroWalletConfig = MoneroWalletConfig()
621 config.path = path
622 wallet: MoneroWalletFull = self._create_wallet(config)
623 wallet.sync()
624 assert wallet.get_height() > 1, "Wallet height is still 1"
625 assert wallet.is_synced(), "Wallet is not synced"
626 assert wallet.is_closed() is False
628 # close wallet
629 wallet.close()
631 assert wallet.is_closed()
633 # attempt to interact with the wallet
634 with pytest.raises(Exception) as exc_info:
635 wallet.get_height()
636 WalletErrorUtils.test_wallet_is_closed_error(exc_info.value)
638 with pytest.raises(Exception) as exc_info:
639 wallet.get_seed()
640 WalletErrorUtils.test_wallet_is_closed_error(exc_info.value)
642 with pytest.raises(Exception) as exc_info:
643 wallet.sync()
644 WalletErrorUtils.test_wallet_is_closed_error(exc_info.value)
646 with pytest.raises(Exception) as exc_info:
647 wallet.start_syncing()
648 WalletErrorUtils.test_wallet_is_closed_error(exc_info.value)
650 with pytest.raises(Exception) as exc_info:
651 wallet.stop_syncing()
652 WalletErrorUtils.test_wallet_is_closed_error(exc_info.value)
654 # re-open the wallet
655 config = MoneroWalletConfig()
656 config.path = path
658 wallet = self._open_wallet(config)
659 assert wallet.is_closed() is False
660 wallet.sync()
661 assert wallet.get_daemon_height() == wallet.get_height()
662 assert wallet.is_closed() is False
664 # close the wallet
665 wallet.close()
666 assert wallet.is_closed()
669@pytest.mark.integration
670class TestMoneroWalletRpcCreation(RpcWalletSupport, BaseTestMoneroWalletCreation):
671 """Rpc wallet creation integration tests."""
673 # Can create a wallet with a randomly generated seed
674 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
675 def test_create_wallet_random_rpc(self) -> None:
676 # create random wallet with defaults
677 path: str = StringUtils.get_random_string()
678 config: MoneroWalletConfig = MoneroWalletConfig()
679 config.path = path
680 wallet: MoneroWalletRpc = self._create_wallet(config)
681 seed: str = wallet.get_seed()
682 MoneroUtils.validate_mnemonic(seed)
683 assert Utils.SEED != seed
684 MoneroUtils.validate_address(wallet.get_primary_address(), Utils.NETWORK_TYPE)
685 # very quick because restore height is chain height
686 wallet.sync()
687 self._close_wallet(wallet)
689 # create random wallet non defaults
690 path = StringUtils.get_random_string()
691 config = MoneroWalletConfig()
692 config.path = path
693 config.language = "Spanish"
694 wallet = self._create_wallet(config)
695 MoneroUtils.validate_mnemonic(wallet.get_seed())
696 assert seed != wallet.get_seed()
697 seed = wallet.get_seed()
698 MoneroUtils.validate_address(wallet.get_primary_address(), Utils.NETWORK_TYPE)
700 # attempt to create wallet which already exists
701 with pytest.raises(MoneroError) as exc_info:
702 wallet.create_wallet(config)
703 assert str(exc_info.value) == f"Wallet already exists: {path}"
704 assert seed == wallet.get_seed()
706 self._close_wallet(wallet)
708 # Can create a RPC wallet from a seed
709 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
710 def test_create_wallet_from_seed_rpc(self, daemon: MoneroDaemonRpc) -> None:
711 # create wallet with seed and defaults
712 path: str = StringUtils.get_random_string()
713 config: MoneroWalletConfig = MoneroWalletConfig()
714 config.path = path
715 config.seed = Utils.SEED
716 config.restore_height = Utils.FIRST_RECEIVE_HEIGHT
717 wallet: MoneroWalletRpc = self._create_wallet(config)
719 # validate wallet
720 assert Utils.SEED == wallet.get_seed()
721 assert Utils.ADDRESS == wallet.get_primary_address()
722 wallet.sync()
723 assert daemon.get_height() == wallet.get_height()
724 txs: list[MoneroTxWallet] = wallet.get_txs()
725 # expect used wallet
726 assert len(txs) > 0, "Wallet is not used"
727 assert Utils.FIRST_RECEIVE_HEIGHT == txs[0].get_height()
728 # TODO: monero-wallet-rpc: if wallet is not closed, primary address will not change
729 self._close_wallet(wallet)
731 # create wallet with non-defaults
732 path = StringUtils.get_random_string()
733 config = MoneroWalletConfig()
734 config.path = path
735 config.seed = Utils.SEED
736 config.restore_height = Utils.FIRST_RECEIVE_HEIGHT
737 config.language = "German"
738 config.seed_offset = "my offset!"
739 config.save_current = False
740 wallet = self._create_wallet(config)
742 # validate wallet
743 MoneroUtils.validate_mnemonic(wallet.get_seed())
744 assert wallet.get_seed() != Utils.SEED
745 assert wallet.get_primary_address() != Utils.ADDRESS
746 assert not wallet.is_view_only()
747 wallet.sync()
748 assert daemon.get_height() == wallet.get_height()
749 txs = wallet.get_txs()
750 # expect non used wallet
751 assert len(txs) == 0, "Wallet is used"
752 self._close_wallet(wallet)
754 # Can open wallets
755 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
756 def test_open_wallet(self)-> None:
757 # create names of tests wallets
758 num_test_wallets: int = 1
759 names: list[str] = [ ]
760 for i in range(num_test_wallets):
761 logger.debug(f"Creating test wallet {i + 1}")
762 names.append(StringUtils.get_random_string())
764 # create test wallets
765 seeds: list[str] = []
766 for name in names:
767 config: MoneroWalletConfig = MoneroWalletConfig()
768 config.path = name
769 wallet: MoneroWalletRpc = self._create_wallet(config)
770 seeds.append(wallet.get_seed())
771 self._close_wallet(wallet, True)
773 # open test wallets
774 wallets: list[MoneroWalletRpc] = []
775 for i in range(num_test_wallets):
776 config: MoneroWalletConfig = MoneroWalletConfig()
777 config.path = names[i]
778 config.password = Utils.WALLET_PASSWORD
779 wallet: MoneroWalletRpc = self._open_wallet(config)
780 assert seeds[i] == wallet.get_seed()
781 wallets.append(wallet)
783 # attempt to open non-existent
784 config: MoneroWalletConfig = MoneroWalletConfig()
785 config.path = "btc_integrity"
786 config.password = Utils.WALLET_PASSWORD
787 with pytest.raises(Exception):
788 self._open_wallet(config)
790 # close wallets:
791 for wallet in wallets:
792 self._close_wallet(wallet)
794 # Can save the wallet
795 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
796 def test_save(self, wallet: MoneroWallet) -> None:
797 wallet.save()
799 # Can close a wallet
800 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
801 def test_close(self, daemon: MoneroDaemonRpc) -> None:
802 # create a test wallet
803 path: str = StringUtils.get_random_string()
804 config: MoneroWalletConfig = MoneroWalletConfig()
805 config.path = path
806 wallet: MoneroWalletRpc = self._create_wallet(config)
807 wallet.sync()
809 # close the wallet
810 wallet.close()
811 Utils.free_wallet_rpc_resource(wallet)
813 # attempt to interact with the wallet
814 with pytest.raises(Exception) as exc_info:
815 wallet.get_height()
816 WalletErrorUtils.test_no_wallet_file_error(exc_info.value)
818 with pytest.raises(Exception) as exc_info:
819 wallet.get_seed()
820 WalletErrorUtils.test_no_wallet_file_error(exc_info.value)
822 with pytest.raises(Exception) as exc_info:
823 wallet.sync()
824 WalletErrorUtils.test_no_wallet_file_error(exc_info.value)
826 # re-open the wallet
827 wallet.open_wallet(path, Utils.WALLET_PASSWORD)
828 wallet.sync()
829 assert daemon.get_height() == wallet.get_height()
831 # close the wallet
832 self._close_wallet(wallet, True)
834 # Can tell that the server has no wallet open
835 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
836 @pytest.mark.xfail(
837 reason="TODO monero-cpp: is_closed() looks for the code -8 and the server answers -13 when no wallet is open; fixed in its branch "
838 "fix/wallet-rpc-is-closed, waiting for the pull request to be merged and the submodule to be updated. Then remove this mark "
839 "and the workaround Utils.is_wallet_rpc_open()",
840 raises=AssertionError, strict=True)
841 def test_is_closed(self) -> None:
842 # create a test wallet
843 config: MoneroWalletConfig = MoneroWalletConfig()
844 config.path = StringUtils.get_random_string()
845 wallet: MoneroWalletRpc = self._create_wallet(config)
846 assert wallet.is_closed() is False
848 # close the wallet
849 wallet.close()
850 Utils.free_wallet_rpc_resource(wallet)
852 # the server has no wallet open
853 assert wallet.is_closed() is True
855 # Can stop the RPC server
856 @pytest.mark.skip(reason="Disabled so server not actually stopped")
857 def test_stop(self, wallet: MoneroWalletRpc) -> None:
858 wallet.stop()
860 @pytest.mark.not_supported
861 @override
862 def test_subaddress_lookahead(self, wallet: MoneroWallet) -> None:
863 return super().test_subaddress_lookahead(wallet)
866@pytest.mark.unit
867class TestMoneroWalletKeysCreation(KeysWalletSupport, BaseTestMoneroWalletCreation):
868 """Keys-only wallet creation unit tests."""
870 @pytest.mark.not_supported
871 @override
872 def test_get_path(self) -> None:
873 return super().test_get_path()
875 @pytest.mark.not_supported
876 @override
877 def test_subaddress_lookahead(self, wallet: MoneroWallet) -> None:
878 return super().test_subaddress_lookahead(wallet)
880 @pytest.mark.not_supported
881 @override
882 def test_set_attributes(self, wallet: MoneroWallet) -> None:
883 return super().test_set_attributes(wallet)
885 @pytest.mark.not_supported
886 @override
887 def test_change_password(self) -> None:
888 return super().test_change_password()
890 @pytest.mark.not_supported
891 @override
892 def test_save_and_close(self) -> None:
893 return super().test_save_and_close()
895 @pytest.mark.not_supported
896 @override
897 def test_view_only_and_offline_wallets(self, wallet: MoneroWallet) -> None:
898 return super().test_view_only_and_offline_wallets(wallet)
900 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
901 @override
902 def test_create_wallet_random(self) -> None:
903 """
904 Can create a random wallet.
905 """
906 config: MoneroWalletConfig = MoneroWalletConfig()
907 wallet: MoneroWalletKeys = self._create_wallet(config)
909 # validate wallet
910 MoneroUtils.validate_address(wallet.get_primary_address(), Utils.NETWORK_TYPE)
911 MoneroUtils.validate_private_view_key(wallet.get_private_view_key())
912 MoneroUtils.validate_private_spend_key(wallet.get_private_spend_key())
913 MoneroUtils.validate_mnemonic(wallet.get_seed())
914 # TODO monero-wallet-rpc: get seed language
915 assert MoneroWallet.DEFAULT_LANGUAGE == wallet.get_seed_language()
917 # attempt to create wallet with unknown language
918 config = MoneroWalletConfig()
919 config.language = "english"
920 with pytest.raises(Exception, match="Unknown language: english"):
921 self._create_wallet(config)
923 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
924 @override
925 def test_create_wallet_from_seed(self, wallet: MoneroWallet, test_config: BaseTestMoneroWallet.Config) -> None:
926 # save for comparison
927 primary_address: str = wallet.get_primary_address()
928 private_view_key: str = wallet.get_private_view_key()
929 private_spend_key: str = wallet.get_private_spend_key()
931 # recreate test wallet from seed
932 config: MoneroWalletConfig = MoneroWalletConfig()
933 config.seed = Utils.SEED
934 w: MoneroWallet = self._create_wallet(config)
936 assert primary_address == w.get_primary_address()
937 assert private_view_key == w.get_private_view_key()
938 assert private_spend_key == w.get_private_spend_key()
939 assert Utils.SEED == w.get_seed()
940 assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language()
942 # attempt to create wallet with two missing words
943 config = MoneroWalletConfig()
944 config.seed = test_config.seed
945 with pytest.raises(Exception) as exc_info:
946 self._create_wallet(config)
947 assert str(exc_info.value) == "Invalid mnemonic"
949 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
950 @override
951 def test_create_wallet_from_seed_with_offset(self) -> None:
952 # create test wallet with offset
953 config: MoneroWalletConfig = MoneroWalletConfig()
954 config.seed = Utils.SEED
955 config.seed_offset = "my secret offset!"
956 wallet: MoneroWallet = self._create_wallet(config)
958 MoneroUtils.validate_mnemonic(wallet.get_seed())
959 assert Utils.SEED != wallet.get_seed()
960 MoneroUtils.validate_address(wallet.get_primary_address(), Utils.NETWORK_TYPE)
961 assert Utils.ADDRESS != wallet.get_primary_address()
962 assert MoneroWallet.DEFAULT_LANGUAGE == wallet.get_seed_language()
964 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
965 @override
966 def test_create_wallet_from_keys(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None:
967 # save for comparison
968 primary_address: str = wallet.get_primary_address()
969 private_view_key: str = wallet.get_private_view_key()
970 private_spend_key: str = wallet.get_private_spend_key()
972 # recreate test wallet from keys
973 config: MoneroWalletConfig = MoneroWalletConfig()
974 config.primary_address = primary_address
975 config.private_view_key = private_view_key
976 config.private_spend_key = private_spend_key
977 w: MoneroWallet = self._create_wallet(config)
978 WalletUtils.test_wallet_keys(primary_address, private_view_key, private_spend_key, w)
979 assert w.get_network_type() == Utils.NETWORK_TYPE
980 assert not w.is_closed()
981 w.close()
983 # recreate test wallet from spend key
984 config = MoneroWalletConfig()
985 config.primary_address = primary_address
986 config.private_spend_key = private_spend_key
987 w = self._create_wallet(config)
988 WalletUtils.test_wallet_keys(primary_address, private_view_key, private_spend_key, w)
989 assert w.get_network_type() == Utils.NETWORK_TYPE
990 assert not w.is_closed()
991 w.close()
993 # recreate test wallet from view keys
994 config = MoneroWalletConfig()
995 config.primary_address = primary_address
996 config.private_view_key = private_view_key
997 w = self._create_wallet(config)
998 logger.info(f"Created wallet with config: {config.serialize()}")
999 assert primary_address == w.get_primary_address()
1000 assert private_view_key == w.get_private_view_key()
1001 assert w.get_network_type() == Utils.NETWORK_TYPE
1002 assert w.is_view_only()
1003 # a view-only keys wallet has no seed
1004 with pytest.raises(RuntimeError, match="watch-only"):
1005 w.get_seed()
1006 assert not w.is_closed()
1007 w.close()
1009 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
1010 def test_create_wallet_from_keys_no_keys(self) -> None:
1011 """
1012 create_wallet_from_keys() must require at least one of the private spend/view keys.
1013 """
1014 config: MoneroWalletConfig = MoneroWalletConfig()
1015 config.network_type = Utils.NETWORK_TYPE
1016 with pytest.raises(RuntimeError, match="Neither spend key nor view key supplied"):
1017 MoneroWalletKeys.create_wallet_from_keys(config)
1019 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
1020 def test_create_wallet_from_keys_invalid_spend_key(self) -> None:
1021 """
1022 create_wallet_from_keys() must fail to parse a malformed private spend key.
1023 """
1024 config: MoneroWalletConfig = MoneroWalletConfig()
1025 config.network_type = Utils.NETWORK_TYPE
1026 config.private_spend_key = "not-a-valid-hex-secret-key"
1027 with pytest.raises(RuntimeError, match="failed to parse secret spend key"):
1028 MoneroWalletKeys.create_wallet_from_keys(config)
1030 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
1031 def test_create_wallet_from_keys_invalid_view_key(self) -> None:
1032 """
1033 create_wallet_from_keys() must fail to parse a malformed private view key.
1034 """
1035 config: MoneroWalletConfig = MoneroWalletConfig()
1036 config.network_type = Utils.NETWORK_TYPE
1037 config.primary_address = Utils.ADDRESS
1038 config.private_view_key = "not-a-valid-hex-secret-key"
1039 with pytest.raises(RuntimeError, match="failed to parse secret view key"):
1040 MoneroWalletKeys.create_wallet_from_keys(config)
1042 # Test invalid wallet configuration
1043 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
1044 def test_create_wallet_from_keys_view_key_without_address(self) -> None:
1045 """
1046 create_wallet_from_keys() must require a primary address when a private view key is given.
1047 """
1048 config: MoneroWalletConfig = MoneroWalletConfig()
1049 config.network_type = Utils.NETWORK_TYPE
1050 config.private_view_key = "a" * 64
1052 with pytest.raises(RuntimeError, match="must provide address if providing private view key"):
1053 MoneroWalletKeys.create_wallet_from_keys(config)
1055 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
1056 def test_is_closed(self) -> None:
1057 """A keys-only wallet reports its open/closed state and rejects use once closed."""
1058 config: MoneroWalletConfig = MoneroWalletConfig()
1059 config.network_type = Utils.NETWORK_TYPE
1060 w: MoneroWalletKeys = MoneroWalletKeys.create_wallet_random(config)
1062 # a freshly created wallet is open
1063 assert w.is_closed() is False
1064 primary_address: str = w.get_primary_address()
1066 # closing it (without saving) marks it closed
1067 w.close()
1068 assert w.is_closed() is True
1070 # any accessor now raises "Wallet is closed"
1071 with pytest.raises(RuntimeError, match="Wallet is closed"):
1072 w.get_primary_address()
1073 with pytest.raises(RuntimeError, match="Wallet is closed"):
1074 w.get_private_view_key()
1076 # closing an already-closed wallet is a no-op (and does not re-raise)
1077 w.close()
1078 assert w.is_closed() is True
1080 # sanity: the address read before closing is unaffected
1081 MoneroUtils.validate_address(primary_address, Utils.NETWORK_TYPE)
1083 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
1084 def test_close_with_save_not_supported(self) -> None:
1085 """`close(save=True)` is rejected because a keys-only wallet has nothing to persist."""
1086 config: MoneroWalletConfig = MoneroWalletConfig()
1087 config.network_type = Utils.NETWORK_TYPE
1088 w: MoneroWalletKeys = MoneroWalletKeys.create_wallet_random(config)
1090 with pytest.raises(RuntimeError, match="does not support saving"):
1091 w.close(True)
1093 # the failed save-and-close left the wallet open
1094 assert w.is_closed() is False
1095 w.close()