walletSendRequestFromParsedLegs function

WalletSendRequest walletSendRequestFromParsedLegs(
  1. List<ParsedPayment> legs
)

Map the audited parser's per-leg ParsedPayment result to a prefill request, enforcing the seam's single-leg + text-memo policy. PURE — no bridge, no network — so it is unit-tested at its boundary against fixture legs. The adapter (WalletSession.parseSendRequest) calls this after the (audited, network-checked) parsePaymentUri bridge crossing; malformed/wrong-network are already typed by then and mapped from the bridge's WalletApiError.

Throws WalletSendRequestException for a shape the editable single-recipient form cannot hold (empty ⇒ WalletSendRequestFault.malformed; multi-leg; non-text memo).

Implementation

WalletSendRequest walletSendRequestFromParsedLegs(List<ParsedPayment> legs) {
  // The core parser rejects an empty request before returning, but defend the
  // boundary: zero legs is nothing to prefill — treat as malformed, never an
  // empty form.
  if (legs.isEmpty) {
    throw const WalletSendRequestException(WalletSendRequestFault.malformed);
  }
  if (legs.length > 1) {
    throw const WalletSendRequestException(
      WalletSendRequestFault.multiplePayments,
    );
  }
  final leg = legs.first;
  final memo = switch (leg.memo) {
    ParsedMemo_Empty() => null,
    ParsedMemo_Text(:final text) => text,
    // Arbitrary (machine) / Reserved / an unknown future arm: not representable
    // in the editable text field — reject rather than silently drop.
    _ => throw const WalletSendRequestException(
      WalletSendRequestFault.unsupportedMemo,
    ),
  };
  return WalletSendRequest(
    address: leg.recipientAddress,
    amountZat: leg.amountZat,
    memo: memo,
    label: leg.label,
  );
}