constnumber=(value:unknown)=>{constparsed=Number(value);returnNumber.isFinite(parsed)?parsed:0;};exportconstdebitTotal=({amount,tip=0,fee=0,ourfee=0,}:{amount:unknown;tip?:unknown;fee?:unknown;ourfee?:unknown;})=>number(amount)+number(tip)+number(fee)+number(ourfee);// Enforce a caller-specific spending allowance at debit's final choke point,// after mutable invoice metadata (notably an internal invoice tip) and fees have// been captured. Checking earlier lets a recipient mutate the tip between the// NWC budget check and the ledger debit.exportconstassertWithinSpendLimit=({amount,tip=0,fee=0,ourfee=0,maxTotal,}:{amount:unknown;tip?:unknown;fee?:unknown;ourfee?:unknown;maxTotal?:number;})=>{consttotal=debitTotal({amount,tip,fee,ourfee});if(maxTotal!==undefined&&total>maxTotal)thrownewError(`Budget exceeded: ${total} of ${maxTotal} remaining`);returntotal;};