Coverage for tests/wallet/test_full_offline.py: 100%

90 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1from __future__ import annotations 

2 

3import logging 

4import os 

5 

6import pytest 

7from monero import MoneroKeyImage, MoneroNetworkType, MoneroWalletConfig, MoneroWalletFull 

8 

9from utils import BaseTestClass, TestUtils as Utils 

10 

11logger: logging.Logger = logging.getLogger("TestMoneroWalletFullOffline") 

12 

13 

14@pytest.mark.unit 

15class TestMoneroWalletFullOffline(BaseTestClass): 

16 """Full wallet unit tests that run against a disconnected wallet, no daemon needed.""" 

17 

18 @pytest.fixture(scope="class") 

19 @classmethod 

20 def wallet(cls) -> MoneroWalletFull: 

21 """Shared disconnected full wallet.""" 

22 return Utils.get_wallet_full_offline() 

23 

24 def test_get_default_ringdb_path_mainnet(self) -> None: 

25 path: str = MoneroWalletFull.get_default_ringdb_path(MoneroNetworkType.MAINNET) 

26 assert path.endswith(".shared-ringdb") 

27 assert not path.endswith("testnet") 

28 assert not path.endswith("stagenet") 

29 

30 def test_get_default_ringdb_path_testnet(self) -> None: 

31 path: str = MoneroWalletFull.get_default_ringdb_path(MoneroNetworkType.TESTNET) 

32 assert path.endswith(os.path.join(".shared-ringdb", "testnet")) 

33 

34 def test_get_default_ringdb_path_stagenet(self) -> None: 

35 path: str = MoneroWalletFull.get_default_ringdb_path(MoneroNetworkType.STAGENET) 

36 assert path.endswith(os.path.join(".shared-ringdb", "stagenet")) 

37 

38 # import_key_images guards unset key image fields 

39 def test_import_key_images_hex_not_defined(self, wallet: MoneroWalletFull) -> None: 

40 with pytest.raises(RuntimeError) as exc_info: 

41 wallet.import_key_images([MoneroKeyImage()]) 

42 assert str(exc_info.value) == "key image hex is not defined" 

43 

44 def test_import_key_images_signature_not_defined(self, wallet: MoneroWalletFull) -> None: 

45 key_image: MoneroKeyImage = MoneroKeyImage() 

46 key_image.hex = "a" * 64 

47 with pytest.raises(RuntimeError) as exc_info: 

48 wallet.import_key_images([key_image]) 

49 assert str(exc_info.value) == "key image signature is not defined" 

50 

51 # Can be moved 

52 def test_move_to(self) -> None: 

53 config: MoneroWalletConfig = MoneroWalletConfig() 

54 config.path = "" 

55 config.password = Utils.WALLET_PASSWORD 

56 config.network_type = MoneroNetworkType.MAINNET 

57 wallet: MoneroWalletFull = MoneroWalletFull.create_wallet(config) 

58 try: 

59 seed: str = wallet.get_seed() 

60 wallet.set_attribute("mykey", "myval1") 

61 

62 # move the in-memory wallet to disk 

63 path1: str = Utils.get_random_wallet_path() 

64 assert MoneroWalletFull.wallet_exists(path1) is False 

65 wallet.move_to(path1, Utils.WALLET_PASSWORD) 

66 assert MoneroWalletFull.wallet_exists(path1) is True 

67 assert wallet.get_seed() == seed 

68 assert wallet.get_attribute("mykey") == "myval1" 

69 

70 # moving to the same path saves in place 

71 wallet.set_attribute("mykey", "myval2") 

72 wallet.move_to(path1, Utils.WALLET_PASSWORD) 

73 wallet.close() 

74 wallet = MoneroWalletFull.open_wallet(path1, Utils.WALLET_PASSWORD, MoneroNetworkType.MAINNET) 

75 assert wallet.get_seed() == seed 

76 assert wallet.get_attribute("mykey") == "myval2" 

77 

78 # move to a new path 

79 path2: str = Utils.get_random_wallet_path() 

80 wallet.set_attribute("mykey", "myval3") 

81 wallet.move_to(path2, Utils.WALLET_PASSWORD) 

82 assert MoneroWalletFull.wallet_exists(path1) is False 

83 assert MoneroWalletFull.wallet_exists(path2) is True 

84 wallet.close() 

85 wallet = MoneroWalletFull.open_wallet(path2, Utils.WALLET_PASSWORD, MoneroNetworkType.MAINNET) 

86 assert wallet.get_seed() == seed 

87 assert wallet.get_attribute("mykey") == "myval3" 

88 finally: 

89 wallet.close() 

90 

91 # Can export and import wallet files 

92 def test_export_and_import_wallet_files(self) -> None: 

93 config: MoneroWalletConfig = MoneroWalletConfig() 

94 config.path = "" 

95 config.password = Utils.WALLET_PASSWORD 

96 config.network_type = MoneroNetworkType.MAINNET 

97 wallet: MoneroWalletFull = MoneroWalletFull.create_wallet(config) 

98 from_keys: MoneroWalletFull | None = None 

99 from_both: MoneroWalletFull | None = None 

100 try: 

101 keys_data: bytes = wallet.get_keys_file_buffer(Utils.WALLET_PASSWORD, False) 

102 cache_data: bytes = wallet.get_cache_file_buffer() 

103 assert len(keys_data) > 0 

104 assert len(cache_data) > 0 

105 

106 # open from the keys buffer alone, then from keys + cache 

107 from_keys = MoneroWalletFull.open_wallet_data(Utils.WALLET_PASSWORD, MoneroNetworkType.MAINNET, keys_data, b"", None, Utils.REGTEST) 

108 from_both = MoneroWalletFull.open_wallet_data(Utils.WALLET_PASSWORD, MoneroNetworkType.MAINNET, keys_data, cache_data, None, Utils.REGTEST) 

109 

110 for restored in (from_keys, from_both): 

111 assert restored.get_seed() == wallet.get_seed() 

112 assert restored.get_primary_address() == wallet.get_primary_address() 

113 assert restored.get_private_view_key() == wallet.get_private_view_key() 

114 assert restored.get_private_spend_key() == wallet.get_private_spend_key() 

115 finally: 

116 wallet.close() 

117 if from_keys is not None: 

118 from_keys.close() 

119 if from_both is not None: 

120 from_both.close()