Coverage for tests/wallet/test_proofs.py: 97%

312 statements  

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

1from __future__ import annotations 

2 

3import logging 

4from typing import Optional 

5 

6import pytest 

7from monero import ( 

8 MoneroAccount, 

9 MoneroCheckReserve, 

10 MoneroCheckTx, 

11 MoneroDaemonRpc, 

12 MoneroDestination, 

13 MoneroSubmitTxResult, 

14 MoneroTransferQuery, 

15 MoneroTx, 

16 MoneroTxConfig, 

17 MoneroTxQuery, 

18 MoneroTxWallet, 

19 MoneroWallet, 

20 MoneroWalletConfig, 

21) 

22from typing_extensions import override 

23 

24from utils import TestUtils, TxWalletUtils, WalletErrorUtils, WalletTestUtils, WalletTxsUtils, WalletUtils 

25from .base_test_monero_wallet import BaseTestMoneroWallet 

26from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport 

27 

28logger: logging.Logger = logging.getLogger("TestMoneroWalletProofs") 

29 

30 

31class BaseTestMoneroWalletProofs(BaseTestMoneroWallet): 

32 """Common wallet proofs tests.""" 

33 

34 # Can check a transfer using the transaction's secret key and the destination 

35 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

36 def test_check_tx_key(self, wallet: MoneroWallet) -> None: 

37 # get random txs that are confirmed and have outgoing destinations 

38 txs: list[MoneroTxWallet] = [] 

39 try: 

40 query: MoneroTxQuery = MoneroTxQuery() 

41 query.is_confirmed = True 

42 query.transfer_query = MoneroTransferQuery() 

43 query.transfer_query.has_destinations = True 

44 txs = WalletTxsUtils.get_random_transactions(wallet, query, 1, WalletUtils.MAX_TX_PROOFS) 

45 except AssertionError as e: 

46 if "found with" in str(e): 

47 raise Exception("No txs with outgoing destinations found; run send tests") 

48 raise 

49 

50 # test good checks 

51 assert len(txs) > 0, "No transactions found with outgoing destinations" 

52 for tx in txs: 

53 assert tx.hash is not None 

54 key: str = wallet.get_tx_key(tx.hash) 

55 assert tx.outgoing_transfer is not None 

56 assert len(tx.outgoing_transfer.destinations) > 0 

57 for destination in tx.outgoing_transfer.destinations: 

58 assert destination.address is not None 

59 check: MoneroCheckTx = wallet.check_tx_key(tx.hash, key, destination.address) 

60 assert destination.amount is not None 

61 if destination.amount > 0: 

62 # TODO monero-wallet-rpc: indicates amount received amount is 0 despite transaction with transfer to this address 

63 # TODO monero-wallet-rpc: returns 0-4 errors, not consistent 

64 assert check.received_amount is not None 

65 if check.received_amount == 0: 

66 msg: str = f"WARNING: key proof indicates no funds received despite transfer (txid={tx.hash}, key={key}, address={destination.address} amount={destination.amount})" 

67 logger.warning(msg) 

68 else: 

69 assert check.received_amount == 0 

70 TxWalletUtils.test_check_tx(tx, check) 

71 

72 # test get tx key with invalid hash 

73 with pytest.raises(Exception) as exc_info: 

74 wallet.get_tx_key("invalid_tx_id") 

75 WalletErrorUtils.test_invalid_tx_hash_error(exc_info.value) 

76 # test check with invalid tx hash 

77 tx: MoneroTxWallet = txs[0] 

78 assert tx.hash is not None 

79 key: str = wallet.get_tx_key(tx.hash) 

80 assert tx.outgoing_transfer is not None 

81 destination: MoneroDestination = tx.outgoing_transfer.destinations[0] 

82 assert destination.address is not None 

83 with pytest.raises(Exception) as exc_info: 

84 wallet.check_tx_key("invalid_tx_id", key, destination.address) 

85 WalletErrorUtils.test_invalid_tx_hash_error(exc_info.value) 

86 # test check with invalid key 

87 with pytest.raises(Exception) as exc_info: 

88 wallet.check_tx_key(tx.hash, "invalid_tx_key", destination.address) 

89 WalletErrorUtils.test_invalid_tx_key_error(exc_info.value) 

90 # test check with invalid address 

91 with pytest.raises(Exception) as exc_info: 

92 wallet.check_tx_key(tx.hash, key, "invalid_tx_address") 

93 WalletErrorUtils.test_invalid_address_error(exc_info.value) 

94 # test check with different address 

95 different_address: Optional[str] = None 

96 for a_tx in wallet.get_txs(): 

97 if a_tx.outgoing_transfer is None or len(a_tx.outgoing_transfer.destinations) == 0: 

98 continue 

99 for a_destination in a_tx.outgoing_transfer.destinations: 

100 assert a_destination.address is not None 

101 if a_destination.address != destination.address: 

102 different_address = a_destination.address 

103 break 

104 

105 assert different_address is not None, "Could not get a different outgoing address to test; run send tests" 

106 check: MoneroCheckTx = wallet.check_tx_key(tx.hash, key, different_address) 

107 assert check.is_good is True 

108 assert check.received_amount is not None 

109 assert check.received_amount >= 0 

110 TxWalletUtils.test_check_tx(tx, check) 

111 

112 # Can prove a transaction by getting its signature 

113 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

114 def test_check_tx_proof(self, wallet: MoneroWallet) -> None: 

115 # get random txs with outgoing destinations 

116 txs: list[MoneroTxWallet] = [] 

117 try: 

118 query: MoneroTxQuery = MoneroTxQuery() 

119 query.transfer_query = MoneroTransferQuery() 

120 query.transfer_query.has_destinations = True 

121 txs = WalletTxsUtils.get_random_transactions(wallet, query, 2, WalletUtils.MAX_TX_PROOFS) 

122 except Exception as e: 

123 if "found with" in str(e): 

124 raise Exception("No txs with outgoing destinations found; run send tests") 

125 raise 

126 

127 # test good checks with messages 

128 for tx in txs: 

129 assert tx.hash is not None 

130 assert tx.outgoing_transfer is not None 

131 for destination in tx.outgoing_transfer.destinations: 

132 assert destination.address is not None 

133 signature: str = wallet.get_tx_proof(tx.hash, destination.address, "This transaction definitely happened.") 

134 check: MoneroCheckTx = wallet.check_tx_proof(tx.hash, destination.address, "This transaction definitely happened.", signature) 

135 TxWalletUtils.test_check_tx(tx, check) 

136 

137 # test good check without message 

138 tx: MoneroTx = txs[0] 

139 assert tx.hash is not None 

140 assert tx.outgoing_transfer is not None 

141 assert len(tx.outgoing_transfer.destinations) > 0 

142 destination: MoneroDestination = tx.outgoing_transfer.destinations[0] 

143 assert destination.address is not None 

144 signature: str = wallet.get_tx_proof(tx.hash, destination.address) 

145 check: MoneroCheckTx = wallet.check_tx_proof(tx.hash, destination.address, '', signature) 

146 TxWalletUtils.test_check_tx(tx, check) 

147 

148 # test get proof with invalid hash 

149 with pytest.raises(Exception) as exc_info: 

150 wallet.get_tx_proof("invalid_tx_id", destination.address) 

151 WalletErrorUtils.test_invalid_tx_hash_error(exc_info.value) 

152 # test check tx proof with invalid tx hash 

153 with pytest.raises(Exception) as exc_info: 

154 wallet.check_tx_proof("invalid_tx_id", destination.address, '', signature) 

155 WalletErrorUtils.test_invalid_tx_hash_error(exc_info.value) 

156 # test check with invalid address 

157 with pytest.raises(Exception) as exc_info: 

158 wallet.check_tx_proof(tx.hash, "invalid_tx_address", '', signature) 

159 WalletErrorUtils.test_invalid_address_error(exc_info.value) 

160 # test check with wrong message 

161 signature = wallet.get_tx_proof(tx.hash, destination.address, "This is the right message") 

162 check = wallet.check_tx_proof(tx.hash, destination.address, "This is the wrong message", signature) 

163 assert check.is_good is False 

164 TxWalletUtils.test_check_tx(tx, check) 

165 

166 # test check with wrong signature 

167 other_tx: MoneroTxWallet = txs[1] 

168 assert other_tx.hash is not None 

169 assert other_tx.outgoing_transfer is not None 

170 assert len(other_tx.outgoing_transfer.destinations) > 0 

171 address: Optional[str] = other_tx.outgoing_transfer.destinations[0].address 

172 assert address is not None 

173 wrong_signature: str = wallet.get_tx_proof(other_tx.hash, address, "This is the right message") 

174 try: 

175 check = wallet.check_tx_proof(tx.hash, destination.address, "This is the right message", wrong_signature) 

176 assert check.is_good is False 

177 except Exception as e: 

178 WalletErrorUtils.test_invalid_signature_error(e) 

179 

180 # test check with empty signature 

181 try: 

182 check = wallet.check_tx_proof(tx.hash, destination.address, "This is the right message", "") 

183 assert check.is_good is False 

184 except Exception as e: 

185 assert str(e) == "Must provide signature to check tx proof" 

186 

187 # Can prove a spend using a generated signature and no destination public address 

188 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disablde") 

189 def test_check_spend_proof(self, wallet: MoneroWallet) -> None: 

190 # get random confirmed outgoing txs 

191 query: MoneroTxQuery = MoneroTxQuery() 

192 query.is_incoming = False 

193 query.in_tx_pool = False 

194 query.is_failed = False 

195 txs: list[MoneroTxWallet] = WalletTxsUtils.get_random_transactions(wallet, query, 2, WalletUtils.MAX_TX_PROOFS) 

196 for tx in txs: 

197 assert tx.is_confirmed is True 

198 assert len(tx.incoming_transfers) == 0 

199 assert tx.outgoing_transfer is not None 

200 

201 # test good checks with messages 

202 for tx in txs: 

203 assert tx.hash is not None 

204 logger.debug(f"Getting tx hash {tx.hash} proof") 

205 signature: str = wallet.get_spend_proof(tx.hash, "I am a message.") 

206 logger.debug(f"Checking tx hash {tx.hash} proof") 

207 result: bool = wallet.check_spend_proof(tx.hash, "I am a message.", signature) 

208 assert result is True 

209 

210 # test good check without message 

211 tx: MoneroTxWallet = txs[0] 

212 assert tx.hash is not None 

213 signature: str = wallet.get_spend_proof(tx.hash) 

214 result: bool = wallet.check_spend_proof(tx.hash, '', signature) 

215 assert result is True 

216 

217 # test get proof with invalid hash 

218 with pytest.raises(Exception) as exc_info: 

219 wallet.get_spend_proof("invalid_tx_id") 

220 WalletErrorUtils.test_invalid_tx_hash_error(exc_info.value) 

221 # test check with invalid tx hash 

222 with pytest.raises(Exception) as exc_info: 

223 wallet.check_spend_proof("invalid_tx_id", '', signature) 

224 WalletErrorUtils.test_invalid_tx_hash_error(exc_info.value) 

225 # test check with invalid message 

226 signature = wallet.get_spend_proof(tx.hash, "This is the right message") 

227 result = wallet.check_spend_proof(tx.hash, "This is the wrong message", signature) 

228 assert result is False 

229 

230 # test check with wrong signature 

231 other_tx: MoneroTxWallet = txs[1] 

232 assert other_tx.hash is not None 

233 signature = wallet.get_spend_proof(other_tx.hash, "This is the right message") 

234 result = wallet.check_spend_proof(tx.hash, "This is the right message", signature) 

235 assert result is False 

236 

237 # Can prove reserves in the wallet 

238 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disablde") 

239 def test_get_reserve_proof_wallet(self, wallet: MoneroWallet) -> None: 

240 # get proof of entire wallet 

241 signature: str = wallet.get_reserve_proof_wallet("Test message") 

242 

243 # check proof of entire wallet 

244 check: MoneroCheckReserve = wallet.check_reserve_proof(wallet.get_primary_address(), "Test message", signature) 

245 assert check.is_good is True 

246 TxWalletUtils.test_check_reserve(check) 

247 balance: int = wallet.get_balance() 

248 if balance != check.total_amount: 

249 # TODO monero-wallet-rpc: this check fails with unconfirmed txs 

250 query: MoneroTxQuery = MoneroTxQuery() 

251 query.in_tx_pool = True 

252 unconfirmed_txs: list[MoneroTxWallet] = wallet.get_txs(query) 

253 assert len(unconfirmed_txs) > 0, "Reserve amount must equal balance unless wallet has unconfirmed txs" 

254 

255 # test different wallet address 

256 different_address: str = WalletTestUtils.get_external_wallet_address() 

257 with pytest.raises(Exception) as exc_info: 

258 wallet.check_reserve_proof(different_address, "Test message", signature) 

259 WalletErrorUtils.test_no_subaddress_error(exc_info.value) 

260 # test subaddress 

261 with pytest.raises(Exception) as exc_info: 

262 address: Optional[str] = wallet.get_subaddress(0, 1).address 

263 assert address is not None 

264 wallet.check_reserve_proof(address, "Test message", signature) 

265 WalletErrorUtils.test_no_subaddress_error(exc_info.value) 

266 # test wrong message 

267 check = wallet.check_reserve_proof(wallet.get_primary_address(), "Wrong message", signature) 

268 # TODO: specifically test reserve checks, probably separate objects 

269 assert check.is_good is False 

270 TxWalletUtils.test_check_reserve(check) 

271 

272 # test wrong signature 

273 with pytest.raises(Exception) as exc_info: 

274 wallet.check_reserve_proof(wallet.get_primary_address(), "Test message", "wrong signature") 

275 WalletErrorUtils.test_signature_header_error(exc_info.value) 

276 

277 # Can prove reserves in an account 

278 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disablde") 

279 def test_get_reserve_proof_account(self, wallet: MoneroWallet) -> None: 

280 # test proofs of accounts 

281 num_non_zero_tests: int = 0 

282 msg: str = "Test message" 

283 accounts: list[MoneroAccount] = wallet.get_accounts() 

284 signature: str = '' 

285 for account in accounts: 

286 assert account.balance is not None 

287 assert account.index is not None 

288 if account.balance > 0: 

289 check_amount: int = int(account.balance/2) 

290 signature = wallet.get_reserve_proof_account(account.index, check_amount, msg) 

291 check: MoneroCheckReserve = wallet.check_reserve_proof(wallet.get_primary_address(), msg, signature) 

292 assert check.is_good is True 

293 TxWalletUtils.test_check_reserve(check) 

294 assert check.total_amount is not None 

295 assert check.total_amount >= 0 

296 num_non_zero_tests += 1 

297 else: 

298 with pytest.raises(Exception) as exc_info: 

299 wallet.get_reserve_proof_account(account.index, account.balance, msg) 

300 logger.debug(str(exc_info.value)) 

301 

302 with pytest.raises(Exception) as exc_info: 

303 wallet.get_reserve_proof_account(account.index, TxWalletUtils.MAX_FEE, msg) 

304 logger.debug(str(exc_info.value)) 

305 

306 assert num_non_zero_tests > 1, "Must have more than one account with non-zero balance; run send-to-multiple tests" 

307 

308 # test error when not enough balance for requested minimum reserve amount 

309 # TODO monero-project#6595: an over-balance reserve proof should be rejected but isn't 

310 account: MoneroAccount = accounts[0] 

311 assert account.balance is not None 

312 amount: int = account.balance + TxWalletUtils.MAX_FEE 

313 try: 

314 proof: str = wallet.get_reserve_proof_account(0, amount, "Test message") 

315 reserve: MoneroCheckReserve = wallet.check_reserve_proof(wallet.get_primary_address(), "Test message", proof) 

316 logger.warning(f"Got reserve proof despite insufficient balance: {reserve.serialize()}") 

317 except Exception as e: 

318 logger.debug(str(e)) 

319 

320 # test different wallet address 

321 different_address: str = WalletTestUtils.get_external_wallet_address() 

322 with pytest.raises(Exception) as exc_info: 

323 wallet.check_reserve_proof(different_address, "Test message", signature) 

324 logger.debug(str(exc_info.value)) 

325 

326 # test subaddress 

327 address: Optional[str] = wallet.get_subaddress(0, 1).address 

328 assert address is not None 

329 with pytest.raises(Exception) as exc_info: 

330 wallet.check_reserve_proof(address, "Test message", signature) 

331 logger.debug(str(exc_info.value)) 

332 

333 # test wrong message 

334 check: MoneroCheckReserve = wallet.check_reserve_proof(wallet.get_primary_address(), "Wrong message", signature) 

335 # TODO: specifically test reserve checks, probably separate objects 

336 assert check.is_good is False 

337 TxWalletUtils.test_check_reserve(check) 

338 

339 # test wrong signature 

340 with pytest.raises(Exception) as exc_info: 

341 wallet.check_reserve_proof(wallet.get_primary_address(), "Test message", "wrong signature") 

342 logger.debug(str(exc_info.value)) 

343 

344 # Can prove unrelayed txs 

345 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

346 def test_prove_unrelayed_txs(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

347 # create unrelayed tx to verify 

348 address1: str = WalletTestUtils.get_external_wallet_address() 

349 address2: str = wallet.get_address(0, 0) 

350 address3: str = wallet.get_address(1, 0) 

351 tx_config: MoneroTxConfig = MoneroTxConfig() 

352 tx_config.account_index = 0 

353 tx_config.destinations.append(MoneroDestination(address1, TxWalletUtils.MAX_FEE)) 

354 tx_config.destinations.append(MoneroDestination(address2, TxWalletUtils.MAX_FEE * 2)) 

355 tx_config.destinations.append(MoneroDestination(address3, TxWalletUtils.MAX_FEE * 3)) 

356 tx: MoneroTxWallet = wallet.create_tx(tx_config) 

357 assert tx.full_hex is not None 

358 assert tx.hash is not None 

359 assert tx.key is not None 

360 

361 # submit tx to daemon but do not relay 

362 result: MoneroSubmitTxResult = daemon.submit_tx_hex(tx.full_hex, True) 

363 assert result.is_good 

364 

365 # create random wallet to verify transfers 

366 verifying_wallet: MoneroWallet = self._create_wallet(MoneroWalletConfig()) 

367 

368 try: 

369 # verify transfer 1 

370 check: MoneroCheckTx = verifying_wallet.check_tx_key(tx.hash, tx.key, address1) 

371 assert check.is_good 

372 assert check.in_tx_pool is True 

373 assert check.num_confirmations == 0 

374 assert check.received_amount == TxWalletUtils.MAX_FEE 

375 

376 # verify transfer 2 

377 check = verifying_wallet.check_tx_key(tx.hash, tx.key, address2) 

378 assert check.is_good 

379 assert check.in_tx_pool is True 

380 assert check.num_confirmations == 0 

381 # + change amount 

382 assert check.received_amount is not None 

383 assert check.received_amount >= TxWalletUtils.MAX_FEE * 2 

384 

385 # verify transfer 3 

386 check = verifying_wallet.check_tx_key(tx.hash, tx.key, address3) 

387 assert check.is_good 

388 assert check.in_tx_pool is True 

389 assert check.num_confirmations == 0 

390 assert TxWalletUtils.MAX_FEE * 3 == check.received_amount 

391 finally: 

392 # cleanup, otherwise it permanently ties up the dest outputs for entire session 

393 daemon.flush_tx_pool(tx.hash) 

394 self._close_wallet(verifying_wallet) 

395 

396 

397@pytest.mark.integration 

398class TestMoneroWalletFullProofs(FullWalletSupport, BaseTestMoneroWalletProofs): 

399 """Full wallet proofs integration tests.""" 

400 

401 

402@pytest.mark.integration 

403class TestMoneroWalletRpcProofs(RpcWalletSupport, BaseTestMoneroWalletProofs): 

404 """Rpc wallet proofs integration tests.""" 

405 

406 

407@pytest.mark.unit 

408class TestMoneroWalletKeysProofs(KeysWalletSupport, BaseTestMoneroWalletProofs): 

409 """Keys-only wallet proofs unit tests.""" 

410 

411 @pytest.mark.not_supported 

412 @override 

413 def test_check_tx_key(self, wallet: MoneroWallet) -> None: 

414 return super().test_check_tx_key(wallet) 

415 

416 @pytest.mark.not_supported 

417 @override 

418 def test_check_tx_proof(self, wallet: MoneroWallet) -> None: 

419 return super().test_check_tx_proof(wallet) 

420 

421 @pytest.mark.not_supported 

422 @override 

423 def test_check_spend_proof(self, wallet: MoneroWallet) -> None: 

424 return super().test_check_spend_proof(wallet) 

425 

426 @pytest.mark.not_supported 

427 @override 

428 def test_get_reserve_proof_wallet(self, wallet: MoneroWallet) -> None: 

429 return super().test_get_reserve_proof_wallet(wallet) 

430 

431 @pytest.mark.not_supported 

432 @override 

433 def test_get_reserve_proof_account(self, wallet: MoneroWallet) -> None: 

434 return super().test_get_reserve_proof_account(wallet) 

435 

436 @pytest.mark.not_supported 

437 @override 

438 def test_prove_unrelayed_txs(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) -> None: 

439 return super().test_prove_unrelayed_txs(daemon, wallet)