Coverage for tests/utils/wallet_calls.py: 100%

35 statements  

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

1from __future__ import annotations 

2 

3from dataclasses import dataclass 

4from typing import Callable 

5from typing_extensions import override 

6 

7from monero import ( 

8 MoneroWallet, MoneroRpcConnection, MoneroWalletListener, MoneroTransferQuery, MoneroOutputQuery, MoneroTxQuery, 

9 MoneroTxConfig, MoneroTxSet, MoneroTxWallet, MoneroMessageSignatureType 

10) 

11 

12from .string_utils import StringUtils 

13 

14# what the error of a method that a wallet does not implement says 

15NOT_SUPPORTED_HINTS: tuple[str, ...] = ("not supported", "does not support", "doesn't support") 

16 

17 

18@dataclass(frozen=True) 

19class WalletCall: 

20 """A call of a method of the MoneroWallet interface, with arguments of the right types and no meaning: what it returns or raises 

21 says whether the wallet implements the method, not whether the method works.""" 

22 

23 method: str 

24 """The method of MoneroWallet that it calls.""" 

25 args: str 

26 """The parameters of the overload that it calls, as the stubs name them.""" 

27 call: Callable[[MoneroWallet], object] 

28 """Calls the method on a wallet.""" 

29 in_base: bool = False 

30 """MoneroWallet implements it on top of its other methods, so it does not raise "not supported" there.""" 

31 waits: bool = False 

32 """A wallet that implements it waits on the network, so it can only be made on the wallets that do not.""" 

33 

34 def __call__(self, wallet: MoneroWallet) -> object: 

35 return self.call(wallet) 

36 

37 def is_implemented_by(self, wallet: MoneroWallet) -> bool: 

38 """Whether the wallet implements the method: the call returns, or raises an error that is not a "not supported" one.""" 

39 try: 

40 self.call(wallet) 

41 except Exception as error: 

42 return not any(hint in str(error).lower() for hint in NOT_SUPPORTED_HINTS) 

43 return True 

44 

45 @override 

46 def __str__(self) -> str: 

47 return f"{self.method}({self.args})" 

48 

49 

50def _tx_with_metadata() -> MoneroTxWallet: 

51 tx: MoneroTxWallet = MoneroTxWallet() 

52 tx.metadata = StringUtils.get_random_string() 

53 return tx 

54 

55 

56# A call of every overload of every method of MoneroWallet, in the order of the sections of the docs. tests/wallet/test_interface.py makes 

57# them on MoneroWallet, which implements almost none, and tests/wallet/test_support.py on each wallet, to check the support matrix of the 

58# docs. close() is last: the calls after it would be on a closed wallet. 

59WALLET_CALLS: list[WalletCall] = [ 

60 # wallet 

61 WalletCall("get_version", "", lambda w: w.get_version()), 

62 WalletCall("get_path", "", lambda w: w.get_path()), 

63 WalletCall("get_network_type", "", lambda w: w.get_network_type()), 

64 WalletCall("is_view_only", "", lambda w: w.is_view_only()), 

65 WalletCall("get_attribute", "key", lambda w: w.get_attribute("")), 

66 WalletCall("set_attribute", "key,val", lambda w: w.set_attribute("", "")), 

67 WalletCall("change_password", "old_password,new_password", lambda w: w.change_password("", "")), 

68 WalletCall("move_to", "path,password", lambda w: w.move_to("", "")), 

69 WalletCall("save", "", lambda w: w.save()), 

70 

71 # keys and addresses 

72 WalletCall("get_seed", "", lambda w: w.get_seed()), 

73 WalletCall("get_seed_language", "", lambda w: w.get_seed_language()), 

74 WalletCall("get_public_view_key", "", lambda w: w.get_public_view_key()), 

75 WalletCall("get_private_view_key", "", lambda w: w.get_private_view_key()), 

76 WalletCall("get_public_spend_key", "", lambda w: w.get_public_spend_key()), 

77 WalletCall("get_private_spend_key", "", lambda w: w.get_private_spend_key()), 

78 WalletCall("get_primary_address", "", lambda w: w.get_primary_address()), 

79 WalletCall("get_address", "account_idx,subaddress_idx", lambda w: w.get_address(0, 0)), 

80 WalletCall("get_address_index", "address", lambda w: w.get_address_index("")), 

81 WalletCall("get_integrated_address", "standard_address,payment_id", lambda w: w.get_integrated_address()), 

82 WalletCall("decode_integrated_address", "integrated_address", lambda w: w.decode_integrated_address("")), 

83 

84 # daemon connection 

85 WalletCall("set_daemon_connection", "uri", lambda w: w.set_daemon_connection("")), 

86 WalletCall("set_daemon_connection", "connection", lambda w: w.set_daemon_connection(MoneroRpcConnection())), 

87 WalletCall("get_daemon_connection", "", lambda w: w.get_daemon_connection()), 

88 WalletCall("is_connected_to_daemon", "", lambda w: w.is_connected_to_daemon()), 

89 WalletCall("is_daemon_synced", "", lambda w: w.is_daemon_synced()), 

90 WalletCall("is_daemon_trusted", "", lambda w: w.is_daemon_trusted()), 

91 WalletCall("get_daemon_height", "", lambda w: w.get_daemon_height()), 

92 WalletCall("get_daemon_max_peer_height", "", lambda w: w.get_daemon_max_peer_height()), 

93 WalletCall("get_height_by_date", "year,month,day", lambda w: w.get_height_by_date(0, 0, 0)), 

94 WalletCall("wait_for_next_block", "", lambda w: w.wait_for_next_block(), waits=True), 

95 

96 # sync 

97 WalletCall("is_synced", "", lambda w: w.is_synced()), 

98 WalletCall("get_height", "", lambda w: w.get_height()), 

99 WalletCall("get_restore_height", "", lambda w: w.get_restore_height()), 

100 WalletCall("set_restore_height", "restore_height", lambda w: w.set_restore_height(0)), 

101 WalletCall("sync", "", lambda w: w.sync()), 

102 WalletCall("sync", "listener", lambda w: w.sync(MoneroWalletListener())), 

103 WalletCall("sync", "start_height", lambda w: w.sync(0)), 

104 WalletCall("sync", "start_height,listener", lambda w: w.sync(0, MoneroWalletListener())), 

105 WalletCall("start_syncing", "sync_period_in_ms", lambda w: w.start_syncing()), 

106 WalletCall("stop_syncing", "", lambda w: w.stop_syncing()), 

107 WalletCall("scan_txs", "tx_hashes", lambda w: w.scan_txs(["", "", ""])), 

108 WalletCall("rescan_spent", "", lambda w: w.rescan_spent()), 

109 WalletCall("rescan_blockchain", "", lambda w: w.rescan_blockchain()), 

110 

111 # listeners 

112 WalletCall("add_listener", "listener", lambda w: w.add_listener(MoneroWalletListener())), 

113 WalletCall("remove_listener", "listener", lambda w: w.remove_listener(MoneroWalletListener())), 

114 WalletCall("get_listeners", "", lambda w: w.get_listeners()), 

115 

116 # balances 

117 WalletCall("get_balance", "", lambda w: w.get_balance()), 

118 WalletCall("get_balance", "account_idx", lambda w: w.get_balance(0)), 

119 WalletCall("get_balance", "account_idx,subaddress_idx", lambda w: w.get_balance(0, 0)), 

120 WalletCall("get_unlocked_balance", "", lambda w: w.get_unlocked_balance()), 

121 WalletCall("get_unlocked_balance", "account_idx", lambda w: w.get_unlocked_balance(0)), 

122 WalletCall("get_unlocked_balance", "account_idx,subaddress_idx", lambda w: w.get_unlocked_balance(0, 0)), 

123 

124 # accounts and subaddresses 

125 WalletCall("get_accounts", "", lambda w: w.get_accounts()), 

126 WalletCall("get_accounts", "include_subaddresses", lambda w: w.get_accounts(True)), 

127 WalletCall("get_accounts", "tag", lambda w: w.get_accounts("")), 

128 WalletCall("get_accounts", "include_subaddresses,tag", lambda w: w.get_accounts(True, "")), 

129 WalletCall("get_account", "account_idx", lambda w: w.get_account(0)), 

130 WalletCall("get_account", "account_idx,include_subaddresses", lambda w: w.get_account(0, True)), 

131 WalletCall("create_account", "label", lambda w: w.create_account()), 

132 WalletCall("set_account_label", "account_idx,label", lambda w: w.set_account_label(0, "")), 

133 WalletCall("get_subaddresses", "account_idx", lambda w: w.get_subaddresses(0)), 

134 WalletCall("get_subaddresses", "account_idx,subaddress_indices", lambda w: w.get_subaddresses(0, [0])), 

135 WalletCall("get_subaddress", "account_idx,subaddress_idx", lambda w: w.get_subaddress(0, 0)), 

136 WalletCall("create_subaddress", "account_idx,label", lambda w: w.create_subaddress(0, "")), 

137 WalletCall("set_subaddress_label", "account_idx,subaddress_idx,label", lambda w: w.set_subaddress_label(1, 1, "")), 

138 

139 # account tags 

140 WalletCall("tag_accounts", "tag,account_indices", lambda w: w.tag_accounts("", [])), 

141 WalletCall("untag_accounts", "account_indices", lambda w: w.untag_accounts([])), 

142 WalletCall("get_account_tags", "", lambda w: w.get_account_tags()), 

143 WalletCall("set_account_tag_label", "tag,label", lambda w: w.set_account_tag_label("", "")), 

144 

145 # txs, transfers and outputs 

146 WalletCall("get_tx", "tx_hash", lambda w: w.get_tx("")), 

147 WalletCall("get_txs", "", lambda w: w.get_txs()), 

148 WalletCall("get_txs", "tx_hashes", lambda w: w.get_txs([""])), 

149 WalletCall("get_txs", "query", lambda w: w.get_txs(MoneroTxQuery())), 

150 WalletCall("get_transfers", "", lambda w: w.get_transfers()), 

151 WalletCall("get_transfers", "query", lambda w: w.get_transfers(MoneroTransferQuery())), 

152 WalletCall("get_transfers", "account_idx", lambda w: w.get_transfers(0)), 

153 WalletCall("get_transfers", "account_idx,subaddress_idx", lambda w: w.get_transfers(0, 0)), 

154 WalletCall("get_incoming_transfers", "", lambda w: w.get_incoming_transfers()), 

155 WalletCall("get_incoming_transfers", "query", lambda w: w.get_incoming_transfers(MoneroTransferQuery())), 

156 WalletCall("get_outgoing_transfers", "", lambda w: w.get_outgoing_transfers()), 

157 WalletCall("get_outgoing_transfers", "query", lambda w: w.get_outgoing_transfers(MoneroTransferQuery())), 

158 WalletCall("get_outputs", "", lambda w: w.get_outputs()), 

159 WalletCall("get_outputs", "query", lambda w: w.get_outputs(MoneroOutputQuery())), 

160 WalletCall("export_outputs", "all", lambda w: w.export_outputs(True)), 

161 WalletCall("import_outputs", "outputs_hex", lambda w: w.import_outputs("")), 

162 WalletCall("export_key_images", "all", lambda w: w.export_key_images(True)), 

163 WalletCall("import_key_images", "key_images,offset", lambda w: w.import_key_images([])), 

164 WalletCall("get_new_key_images_from_last_import", "", lambda w: w.get_new_key_images_from_last_import()), 

165 WalletCall("freeze_output", "key_image", lambda w: w.freeze_output("")), 

166 WalletCall("thaw_output", "key_image", lambda w: w.thaw_output("")), 

167 WalletCall("is_output_frozen", "key_image", lambda w: w.is_output_frozen("")), 

168 

169 # sending 

170 WalletCall("get_default_fee_priority", "", lambda w: w.get_default_fee_priority()), 

171 WalletCall("create_tx", "config", lambda w: w.create_tx(MoneroTxConfig())), 

172 WalletCall("create_txs", "config", lambda w: w.create_txs(MoneroTxConfig())), 

173 WalletCall("sweep_unlocked", "config", lambda w: w.sweep_unlocked(MoneroTxConfig())), 

174 WalletCall("sweep_output", "config", lambda w: w.sweep_output(MoneroTxConfig())), 

175 WalletCall("sweep_dust", "relay", lambda w: w.sweep_dust(True)), 

176 WalletCall("relay_tx", "tx_metadata", lambda w: w.relay_tx(StringUtils.get_random_string())), 

177 WalletCall("relay_tx", "tx", lambda w: w.relay_tx(_tx_with_metadata())), 

178 WalletCall("relay_txs", "txs", lambda w: w.relay_txs([_tx_with_metadata()])), 

179 WalletCall("relay_txs", "tx_metadatas", lambda w: w.relay_txs([StringUtils.get_random_string()])), 

180 WalletCall("describe_tx_set", "tx_set", lambda w: w.describe_tx_set(MoneroTxSet())), 

181 WalletCall("describe_unsigned_tx_set", "unsigned_tx_hex", lambda w: w.describe_unsigned_tx_set("")), 

182 WalletCall("describe_multisig_tx_set", "multisig_tx_hex", lambda w: w.describe_multisig_tx_set("")), 

183 WalletCall("sign_txs", "unsigned_tx_hex", lambda w: w.sign_txs("")), 

184 WalletCall("submit_txs", "signed_tx_hex", lambda w: w.submit_txs("")), 

185 

186 # proofs 

187 WalletCall("get_tx_key", "tx_hash", lambda w: w.get_tx_key("")), 

188 WalletCall("check_tx_key", "tx_hash,tx_key,address", lambda w: w.check_tx_key("", "", "")), 

189 WalletCall("get_tx_proof", "tx_hash,address,message", lambda w: w.get_tx_proof("", "", "")), 

190 WalletCall("check_tx_proof", "tx_hash,address,message,signature", lambda w: w.check_tx_proof("", "", "", "")), 

191 WalletCall("get_spend_proof", "tx_hash,message", lambda w: w.get_spend_proof("", "")), 

192 WalletCall("check_spend_proof", "tx_hash,message,signature", lambda w: w.check_spend_proof("", "", "")), 

193 WalletCall("get_reserve_proof_wallet", "message", lambda w: w.get_reserve_proof_wallet("")), 

194 WalletCall("get_reserve_proof_account", "account_idx,amount,message", lambda w: w.get_reserve_proof_account(0, 0, "")), 

195 WalletCall("check_reserve_proof", "address,message,signature", lambda w: w.check_reserve_proof("", "", "")), 

196 

197 # messages and payment uris 

198 WalletCall("sign_message", "msg,signature_type", lambda w: w.sign_message("", MoneroMessageSignatureType.SIGN_WITH_VIEW_KEY)), 

199 WalletCall("verify_message", "msg,address,signature", lambda w: w.verify_message("", "", ""), in_base=True), 

200 WalletCall("get_payment_uri", "config", lambda w: w.get_payment_uri(MoneroTxConfig())), 

201 WalletCall("parse_payment_uri", "uri", lambda w: w.parse_payment_uri("")), 

202 

203 # tx notes and address book 

204 WalletCall("get_tx_note", "tx_hash", lambda w: w.get_tx_note("")), 

205 WalletCall("get_tx_notes", "tx_hashes", lambda w: w.get_tx_notes(["", ""])), 

206 WalletCall("set_tx_note", "tx_hash,note", lambda w: w.set_tx_note("", "")), 

207 WalletCall("set_tx_notes", "tx_hashes,notes", lambda w: w.set_tx_notes([], [])), 

208 WalletCall("get_address_book_entries", "", lambda w: w.get_address_book_entries()), 

209 WalletCall("get_address_book_entries", "indices", lambda w: w.get_address_book_entries([])), 

210 WalletCall("add_address_book_entry", "address,description", lambda w: w.add_address_book_entry("", "")), 

211 WalletCall("edit_address_book_entry", "index,set_address,address,set_description,description", 

212 lambda w: w.edit_address_book_entry(1, False, "", True, "")), 

213 WalletCall("delete_address_book_entry", "index", lambda w: w.delete_address_book_entry(0)), 

214 

215 # multisig 

216 WalletCall("is_multisig_import_needed", "", lambda w: w.is_multisig_import_needed()), 

217 WalletCall("is_multisig", "", lambda w: w.is_multisig()), 

218 WalletCall("get_multisig_info", "", lambda w: w.get_multisig_info()), 

219 WalletCall("prepare_multisig", "", lambda w: w.prepare_multisig()), 

220 WalletCall("make_multisig", "multisig_hexes,threshold,password", lambda w: w.make_multisig([], 0, "")), 

221 WalletCall("exchange_multisig_keys", "multisig_hexes,password", lambda w: w.exchange_multisig_keys([], "")), 

222 WalletCall("export_multisig_hex", "", lambda w: w.export_multisig_hex()), 

223 WalletCall("import_multisig_hex", "multisig_hexes,refresh_after_import", lambda w: w.import_multisig_hex([])), 

224 WalletCall("sign_multisig_tx_hex", "multisig_tx_hex", lambda w: w.sign_multisig_tx_hex("")), 

225 WalletCall("submit_multisig_tx_hex", "signed_multisig_tx_hex", lambda w: w.submit_multisig_tx_hex("")), 

226 

227 # mining 

228 WalletCall("start_mining", "num_threads,background_mining,ignore_battery", lambda w: w.start_mining()), 

229 WalletCall("stop_mining", "", lambda w: w.stop_mining()), 

230 

231 # closing 

232 WalletCall("is_closed", "", lambda w: w.is_closed()), 

233 WalletCall("close", "save", lambda w: w.close()), 

234]