Select and validate payment metadata rows
When receiving metadata from the extension, select the correct payment row by matching expected values (amount, currency, recipient, or paymentId). Use
row.originalIndex for the provider index rather than the array index of a filtered UI list.function selectPaymentRow(
rows: PeerMetadataRow[],
expected: { amount?: string; currency?: string; paymentId?: string; recipient?: string; },
): PeerMetadataRow | null {
const visibleRows = rows.filter((row) => !row.hidden && isBuyerTeeParams(row.params));
if (expected.paymentId) {
const byPaymentId = visibleRows.find((row) => row.paymentId === expected.paymentId);
if (byPaymentId) return byPaymentId;
}
return visibleRows.find((row) =>
(!expected.amount || row.amount === expected.amount) &&
(!expected.currency || row.currency === expected.currency) &&
(!expected.recipient || row.recipient === expected.recipient)
) ?? null;
}