fix(payments): never reuse a subaddress or count older payments toward an invoice

Subaddress indices wrapped after 300 invoices per merchant, and the
reconciler counted every transfer ever received on the subaddress. A later
invoice could therefore show as paid by an earlier invoice's payment.

Indices now only increase, and the reconciler counts only transfers whose
timestamp is no more than two hours before the invoice was created. The slack
covers Monero's block timestamp rules, so a genuine payment mined after
creation always counts.
This commit is contained in:
2026-11-18 15:37:34 -05:00
parent b44181d8d9
commit bdacfd278f
4 changed files with 149 additions and 12 deletions
+8 -10
View File
@@ -7,6 +7,8 @@ from .security import decrypt_secret
from .subaddress_derivation import derive_subaddress
# Caps how many donation invoices may be active at once. It no longer bounds
# subaddress indices, which only ever increase.
MAX_SUBADDRESS_INDEX = 300
@@ -26,17 +28,13 @@ def create_subaddress_for_user(
if locked is None:
raise ValueError("User not found")
start_index = int(locked.subaddress_start_index or 0)
if start_index < 0 or start_index >= MAX_SUBADDRESS_INDEX:
if start_index < 0:
start_index = 0
range_start = start_index + 1
range_end = MAX_SUBADDRESS_INDEX
next_index = int(locked.next_subaddress_index or range_start)
if next_index < range_start or next_index > range_end:
next_index = range_start
next_next_index = next_index + 1
if next_next_index > range_end:
next_next_index = range_start
locked.next_subaddress_index = next_next_index
# Indices only ever increase. Wrapping back to the start of a range made a
# later invoice share a subaddress with an earlier one, so the earlier
# invoice's payment counted toward the later invoice.
next_index = max(int(locked.next_subaddress_index or 0), start_index + 1)
locked.next_subaddress_index = next_index + 1
db.flush()
view_key = decrypt_secret(locked.view_key_encrypted)
address = derive_subaddress(