Files
wowcheckout/api/app/invoice_transfers.py
T
such-gitea cafa8039de 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 six hours before the invoice was created. The slack
covers Wownero's block timestamp rules, so a genuine payment mined after
creation always counts.
2026-11-18 15:38:01 -05:00

34 lines
1.3 KiB
Python

from __future__ import annotations
from datetime import datetime, timedelta, timezone
from typing import Iterable, TypeVar
# A block's timestamp only has to exceed the median of recent blocks, so with
# Wownero's five-minute blocks a payment mined after an invoice was created can
# carry a timestamp a few hours earlier than the invoice. Six hours keeps every
# genuine payment and still excludes anything clearly older.
BLOCK_TIMESTAMP_SLACK = timedelta(hours=6)
T = TypeVar("T")
def transfers_for_invoice(transfers: Iterable[T], created_at: datetime | None) -> list[T]:
"""Return only the transfers that can belong to an invoice created at created_at.
A subaddress can carry payments that predate its current invoice: older
invoices reused it, or someone paid it before. Those must never count toward
the new invoice. A transfer with no timestamp is excluded once the invoice
has a creation time, because nothing proves it is recent.
"""
items = list(transfers)
if created_at is None:
return items
if created_at.tzinfo is None:
created_at = created_at.replace(tzinfo=timezone.utc)
cutoff = int((created_at - BLOCK_TIMESTAMP_SLACK).timestamp())
return [
item
for item in items
if getattr(item, "timestamp", None) is not None and int(item.timestamp) >= cutoff
]