@lunora/x402 1.0.0-alpha.48 → 1.0.0-alpha.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -70,8 +70,10 @@ The pay rail's `signer` config selects wallet custody:
70
70
  ## Safety
71
71
 
72
72
  The **pay** rail spends real money autonomously. It is `ActionCtx`-only and
73
- fail-closed: every payment is bounded by a spend policy (per-call and per-window
74
- caps) and may require confirmation. The `policy` field is required, so you cannot
73
+ fail-closed: every payment is bounded by a spend policy (`maxPerCall`, a ceiling
74
+ on a single payment, and `maxPerRun`, a ceiling on cumulative spend across the
75
+ wallet's lifetime — the ctx, for `ctx.x402`; there is no windowed cap) and may
76
+ require confirmation. The `policy` field is required, so you cannot
75
77
  wire a signer without one; an unbounded policy is refused at runtime (fail-closed)
76
78
  before any signer is resolved.
77
79
 
@@ -64,7 +64,7 @@ interface ChargeHandlerDeps {
64
64
  /**
65
65
  * Keep background work (the receipt sink) alive past the response — the
66
66
  * request's `ctx.waitUntil`. Absent on paths with no platform execution
67
- * context reaching the middleware (e.g. today's HTTP-action rail).
67
+ * context reaching the middleware (e.g. a non-Workers test).
68
68
  */
69
69
  readonly waitUntil?: (promise: Promise<unknown>) => void;
70
70
  }
@@ -64,7 +64,7 @@ interface ChargeHandlerDeps {
64
64
  /**
65
65
  * Keep background work (the receipt sink) alive past the response — the
66
66
  * request's `ctx.waitUntil`. Absent on paths with no platform execution
67
- * context reaching the middleware (e.g. today's HTTP-action rail).
67
+ * context reaching the middleware (e.g. a non-Workers test).
68
68
  */
69
69
  readonly waitUntil?: (promise: Promise<unknown>) => void;
70
70
  }
@@ -1 +1 @@
1
- import{DEFAULT_FACILITATOR_URL as t,resolveFacilitatorUrl as o}from"../packem_shared/DEFAULT_FACILITATOR_URL-CPJNUMvi.mjs";import{createFacilitatorClient as i}from"../packem_shared/createFacilitatorClient-piLSnKs5.mjs";import{isEvmNetwork as p,isSvmNetwork as c,toCaip2 as f}from"../packem_shared/EVM_NETWORKS-C5yyzK27.mjs";import{withX402 as l}from"../packem_shared/withX402-CJcBVHdI.mjs";import{createChargeMiddleware as C}from"../packem_shared/createChargeMiddleware-CURPSr23.mjs";import{createProcedureChargeGate as F}from"../packem_shared/createProcedureChargeGate-Ki_K8dQE.mjs";import{toPaymentEventRow as d,toReceipt as h}from"../packem_shared/toPaymentEventRow-DTXuTDh0.mjs";export{t as DEFAULT_FACILITATOR_URL,C as createChargeMiddleware,i as createFacilitatorClient,F as createProcedureChargeGate,p as isEvmNetwork,c as isSvmNetwork,o as resolveFacilitatorUrl,f as toCaip2,d as toPaymentEventRow,h as toReceipt,l as withX402};
1
+ import{DEFAULT_FACILITATOR_URL as t,resolveFacilitatorUrl as o}from"../packem_shared/DEFAULT_FACILITATOR_URL-CPJNUMvi.mjs";import{createFacilitatorClient as i}from"../packem_shared/createFacilitatorClient-piLSnKs5.mjs";import{isEvmNetwork as p,isSvmNetwork as c,toCaip2 as f}from"../packem_shared/EVM_NETWORKS-C5yyzK27.mjs";import{withX402 as l}from"../packem_shared/withX402-CXFSHtTo.mjs";import{createChargeMiddleware as C}from"../packem_shared/createChargeMiddleware-CURPSr23.mjs";import{createProcedureChargeGate as F}from"../packem_shared/createProcedureChargeGate-Ki_K8dQE.mjs";import{toPaymentEventRow as d,toReceipt as h}from"../packem_shared/toPaymentEventRow-DTXuTDh0.mjs";export{t as DEFAULT_FACILITATOR_URL,C as createChargeMiddleware,i as createFacilitatorClient,F as createProcedureChargeGate,p as isEvmNetwork,c as isSvmNetwork,o as resolveFacilitatorUrl,f as toCaip2,d as toPaymentEventRow,h as toReceipt,l as withX402};
@@ -1 +1 @@
1
- import{LunoraError as l}from"@lunora/errors";import{toCaip2 as f}from"./EVM_NETWORKS-C5yyzK27.mjs";const v=/^\d+(?:\.\d+)?$/,u=(e,t)=>t.startsWith("eip155:")?e.toLowerCase():e,p=(e,t)=>`${t}|${u(e,t)}`,h=/^\d+$/,w=e=>h.test(e)?BigInt(e):void 0,S=6,x=[{asset:"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",decimals:6,network:"base"},{asset:"0x036CbD53842c5426634e7929541eC2318f3dCF7e",decimals:6,network:"base-sepolia"},{asset:"0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",decimals:6,network:"polygon"},{asset:"0xaf88d065e77c8cC2239327C5EDb3A432268e5831",decimals:6,network:"arbitrum"},{asset:"0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",decimals:6,network:"arbitrum-sepolia"},{asset:"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",decimals:6,network:"solana"},{asset:"4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",decimals:6,network:"solana-devnet"}],C=()=>{let e=0n;return{add:t=>{e+=t},release:t=>{e=e>t?e-t:0n},get spentAtomic(){return e}}},E=(e,t=S)=>{if(!Number.isInteger(t)||t<0)throw new l("BAD_REQUEST",`x402 policy: decimals must be a non-negative integer, got ${String(t)}.`);const n=(typeof e=="number"?e.toString():e).trim(),s=n.startsWith("$")?n.slice(1):n;if(!v.test(s))throw new l("BAD_REQUEST",`x402 policy: "${String(e)}" is not a valid USD amount (use a plain decimal like "0.01").`);const[o="0",r=""]=s.split("."),a=r.slice(0,t).padEnd(t,"0");return BigInt(o)*10n**BigInt(t)+BigInt(a===""?"0":a)},b=e=>{if(e.decimals!==void 0)throw new l("BAD_REQUEST","x402 policy: `decimals` is no longer supported — a single policy-wide decimal count can't describe the assets a server may name. List the asset in `allowedAssets` with its own `decimals` instead.");const t=e.allowedAssets??x;if(t.length===0)throw new l("BAD_REQUEST","x402 policy: `allowedAssets` is empty, so no payment could ever be made. Omit it to accept the default USDC assets, or list at least one asset.");const n=new Map;for(const s of t){if(!Number.isInteger(s.decimals)||s.decimals<0)throw new l("BAD_REQUEST",`x402 policy: allowedAssets[${s.asset}].decimals must be a non-negative integer, got ${String(s.decimals)}.`);const o=f(s.network),r=p(s.asset,o),a=n.get(r);if(a!==void 0&&a.decimals!==s.decimals)throw new l("BAD_REQUEST",`x402 policy: allowedAssets lists ${s.asset} on ${o} twice with different decimals (${String(a.decimals)} and ${String(s.decimals)}). One asset cannot have two precisions.`);n.set(r,{...s,network:o})}return n},g=(e,t)=>{const n=new Map;if(e===void 0)return n;for(const{decimals:s}of t.values())n.has(s)||n.set(s,E(e,s));return n},R=e=>{const t=b(e),n=g(e.maxPerCall,t),s=e.allowedNetworks?.map(r=>f(r)),{allowedRecipients:o}=e;return(r,a)=>a.filter(i=>{const c=t.get(p(i.asset,i.network));if(c===void 0)return!1;const d=w(i.amount);if(d===void 0)return!1;const m=n.get(c.decimals);if(m!==void 0&&d>m||s!==void 0&&!s.includes(i.network))return!1;if(o!==void 0){const A=u(i.payTo,i.network);return o.some(y=>u(y,i.network)===A)}return!0})},P=(e,t)=>{const n=b(e),s=g(e.maxPerRun,n);let o;return async r=>{const a=r.selectedRequirements,i=n.get(p(a.asset,a.network));if(i===void 0)return{abort:!0,reason:`x402 policy: asset ${a.asset} on ${a.network} is not in this wallet's allowed assets, so its amount cannot be priced against the caps.`};if(o===void 0)o=i.decimals;else if(o!==i.decimals)return{abort:!0,reason:`x402 policy: this payment is in a ${String(i.decimals)}-decimal asset but the run's spend is tracked in ${String(o)}-decimal units. One wallet cannot mix asset precisions under a single per-run cap.`};const c=w(a.amount);if(c===void 0)return{abort:!0,reason:`x402 policy: this payment's amount (${a.amount}) is not a canonical atomic quantity, so it cannot be checked against the caps.`};const d=s.get(i.decimals);if(d!==void 0&&t.spentAtomic+c>d)return{abort:!0,reason:`x402 policy: this payment (${a.amount}) would exceed the per-run cap (already spent ${t.spentAtomic.toString()}, cap ${d.toString()}, in atomic base units).`};t.add(c);try{return e.onPaymentRequired!==void 0&&!await e.onPaymentRequired(a)?(t.release(c),{abort:!0,reason:"x402 policy: payment was declined by onPaymentRequired."}):void 0}catch(m){throw t.release(c),m}}},$=e=>t=>{const n=w(t.selectedRequirements.amount);return n!==void 0&&e.release(n),Promise.resolve()},T=e=>{if(!(e.maxPerCall!==void 0||e.maxPerRun!==void 0||e.onPaymentRequired!==void 0))throw new l("FORBIDDEN","x402 pay: refusing to build a wallet with an unbounded spend policy. Set at least one of maxPerCall, maxPerRun, or onPaymentRequired (allowedNetworks/allowedRecipients/allowedAssets narrow but do not bound spend).")};export{x as DEFAULT_ALLOWED_ASSETS,S as DEFAULT_STABLECOIN_DECIMALS,T as assertBoundedPolicy,P as buildPaymentGuard,R as buildSpendPolicy,C as createSpendState,$ as releaseSpendOnFailure,E as usdToAtomic};
1
+ import{LunoraError as l}from"@lunora/errors";import{toCaip2 as f}from"./EVM_NETWORKS-C5yyzK27.mjs";const v=/^\d+(?:\.\d+)?$/,u=(e,t)=>t.startsWith("eip155:")?e.toLowerCase():e,p=(e,t)=>`${t}|${u(e,t)}`,h=/^\d+$/,w=e=>h.test(e)?BigInt(e):void 0,S=6,x=[{asset:"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",decimals:6,network:"base"},{asset:"0x036CbD53842c5426634e7929541eC2318f3dCF7e",decimals:6,network:"base-sepolia"},{asset:"0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",decimals:6,network:"polygon"},{asset:"0xaf88d065e77c8cC2239327C5EDb3A432268e5831",decimals:6,network:"arbitrum"},{asset:"0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",decimals:6,network:"arbitrum-sepolia"},{asset:"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",decimals:6,network:"solana"},{asset:"4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",decimals:6,network:"solana-devnet"}],C=()=>{let e=0n;return{add:t=>{e+=t},release:t=>{e=e>t?e-t:0n},get spentAtomic(){return e}}},E=(e,t=S)=>{if(!Number.isInteger(t)||t<0)throw new l("BAD_REQUEST",`x402 policy: decimals must be a non-negative integer, got ${String(t)}.`);const n=(typeof e=="number"?e.toString():e).trim(),s=n.startsWith("$")?n.slice(1):n;if(!v.test(s))throw new l("BAD_REQUEST",`x402 policy: "${String(e)}" is not a valid USD amount (use a plain decimal like "0.01").`);const[o="0",r=""]=s.split("."),a=r.slice(0,t).padEnd(t,"0");return BigInt(o)*10n**BigInt(t)+BigInt(a===""?"0":a)},b=e=>{if(e.decimals!==void 0)throw new l("BAD_REQUEST","x402 policy: `decimals` is no longer supported — a single policy-wide decimal count can't describe the assets a server may name. List the asset in `allowedAssets` with its own `decimals` instead.");const t=e.allowedAssets??x;if(t.length===0)throw new l("BAD_REQUEST","x402 policy: `allowedAssets` is empty, so no payment could ever be made. Omit it to accept the default USDC assets, or list at least one asset.");const n=new Map;for(const s of t){if(!Number.isInteger(s.decimals)||s.decimals<0)throw new l("BAD_REQUEST",`x402 policy: allowedAssets[${s.asset}].decimals must be a non-negative integer, got ${String(s.decimals)}.`);const o=f(s.network),r=p(s.asset,o),a=n.get(r);if(a!==void 0&&a.decimals!==s.decimals)throw new l("BAD_REQUEST",`x402 policy: allowedAssets lists ${s.asset} on ${o} twice with different decimals (${String(a.decimals)} and ${String(s.decimals)}). One asset cannot have two precisions.`);n.set(r,{...s,network:o})}return n},g=(e,t)=>{const n=new Map;if(e===void 0)return n;for(const{decimals:s}of t.values())n.has(s)||n.set(s,E(e,s));return n},R=e=>{const t=b(e),n=g(e.maxPerCall,t),s=e.allowedNetworks?.map(r=>f(r)),{allowedRecipients:o}=e;return(r,a)=>a.filter(i=>{const c=t.get(p(i.asset,i.network));if(c===void 0)return!1;const d=w(i.amount);if(d===void 0)return!1;const m=n.get(c.decimals);if(m!==void 0&&d>m||s!==void 0&&!s.includes(i.network))return!1;if(o!==void 0){const A=u(i.payTo,i.network);return o.some(y=>u(y,i.network)===A)}return!0})},P=(e,t)=>{const n=b(e),s=g(e.maxPerRun,n);let o;return async r=>{const a=r.selectedRequirements,i=n.get(p(a.asset,a.network));if(i===void 0)return{abort:!0,reason:`x402 policy: asset ${a.asset} on ${a.network} is not in this wallet's allowed assets, so its amount cannot be priced against the caps.`};if(o===void 0)o=i.decimals;else if(o!==i.decimals)return{abort:!0,reason:`x402 policy: this payment is in a ${String(i.decimals)}-decimal asset but the run's spend is tracked in ${String(o)}-decimal units. One wallet cannot mix asset precisions under a single per-run cap.`};const c=w(a.amount);if(c===void 0)return{abort:!0,reason:`x402 policy: this payment's amount (${a.amount}) is not a canonical atomic quantity, so it cannot be checked against the caps.`};const d=s.get(i.decimals);if(d!==void 0&&t.spentAtomic+c>d)return{abort:!0,reason:`x402 policy: this payment (${a.amount}) would exceed the per-run cap (already spent ${t.spentAtomic.toString()}, cap ${d.toString()}, in atomic base units).`};t.add(c);try{return e.onPaymentRequired!==void 0&&await e.onPaymentRequired(a)!==!0?(t.release(c),{abort:!0,reason:"x402 policy: payment was declined by onPaymentRequired."}):void 0}catch(m){throw t.release(c),m}}},$=e=>t=>{const n=w(t.selectedRequirements.amount);return n!==void 0&&e.release(n),Promise.resolve()},T=e=>{if(!(e.maxPerCall!==void 0||e.maxPerRun!==void 0||e.onPaymentRequired!==void 0))throw new l("FORBIDDEN","x402 pay: refusing to build a wallet with an unbounded spend policy. Set at least one of maxPerCall, maxPerRun, or onPaymentRequired (allowedNetworks/allowedRecipients/allowedAssets narrow but do not bound spend).")};export{x as DEFAULT_ALLOWED_ASSETS,S as DEFAULT_STABLECOIN_DECIMALS,T as assertBoundedPolicy,P as buildPaymentGuard,R as buildSpendPolicy,C as createSpendState,$ as releaseSpendOnFailure,E as usdToAtomic};
@@ -1 +1 @@
1
- import{x402Client as l}from"@x402/core/client";import{wrapFetchWithPayment as c}from"@x402/fetch";import{assertBoundedPolicy as s,buildSpendPolicy as p,buildPaymentGuard as y,releaseSpendOnFailure as m,createSpendState as d}from"./DEFAULT_ALLOWED_ASSETS-hq1xeZL6.mjs";import{registerWallet as P}from"./registerWallet-DvCt0Q7M.mjs";const S=async(t,r)=>{s(t.policy);const o=d(),a=p(t.policy),n=y(t.policy,o),i=m(o),e=new l;return await P(e,t,r),e.registerPolicy(a),e.onBeforePaymentCreation(n),e.onPaymentCreationFailure(i),c(r.fetch??globalThis.fetch,e)};export{S as createPayFetch};
1
+ import{x402Client as l}from"@x402/core/client";import{wrapFetchWithPayment as c}from"@x402/fetch";import{assertBoundedPolicy as s,buildSpendPolicy as p,buildPaymentGuard as y,releaseSpendOnFailure as m,createSpendState as d}from"./DEFAULT_ALLOWED_ASSETS-Bda-nP2p.mjs";import{registerWallet as P}from"./registerWallet-DvCt0Q7M.mjs";const S=async(t,r)=>{s(t.policy);const o=d(),a=p(t.policy),n=y(t.policy,o),i=m(o),e=new l;return await P(e,t,r),e.registerPolicy(a),e.onBeforePaymentCreation(n),e.onPaymentCreationFailure(i),c(r.fetch??globalThis.fetch,e)};export{S as createPayFetch};
@@ -0,0 +1 @@
1
+ import{createChargeMiddleware as w}from"./createChargeMiddleware-CURPSr23.mjs";const h=(o,r)=>{let t;return async(n,a)=>{t??=w(o).catch(i=>{throw t=void 0,i});const d=await t,e=n,c=typeof e.waitUntil=="function"?{waitUntil:i=>{e.waitUntil?.(i)}}:void 0;return d.handle(a,()=>r(n,a),c)}};export{h as withX402};
@@ -1 +1 @@
1
- import{createPayFetch as c}from"../packem_shared/createPayFetch-lyg7i3Js.mjs";import{DEFAULT_FACILITATOR_URL as f,resolveFacilitatorUrl as p}from"../packem_shared/DEFAULT_FACILITATOR_URL-CPJNUMvi.mjs";import{isEvmNetwork as d,isSvmNetwork as y,toCaip2 as E}from"../packem_shared/EVM_NETWORKS-C5yyzK27.mjs";import{DEFAULT_ALLOWED_ASSETS as T,DEFAULT_STABLECOIN_DECIMALS as v,assertBoundedPolicy as F,buildPaymentGuard as P,buildSpendPolicy as h,createSpendState as _,releaseSpendOnFailure as x,usdToAtomic as D}from"../packem_shared/DEFAULT_ALLOWED_ASSETS-hq1xeZL6.mjs";import{registerWallet as w,resolveEvmAccount as C,resolveSvmSigner as I}from"../packem_shared/registerWallet-DvCt0Q7M.mjs";const i=async(e,t)=>({fetch:await c(e,t)}),m=(e,t)=>{let r;return{fetch:async(o,a)=>(r??=i(e,t),(await r).fetch(o,a))}};export{T as DEFAULT_ALLOWED_ASSETS,f as DEFAULT_FACILITATOR_URL,v as DEFAULT_STABLECOIN_DECIMALS,F as assertBoundedPolicy,P as buildPaymentGuard,h as buildSpendPolicy,c as createPayFetch,_ as createSpendState,i as createX402Pay,d as isEvmNetwork,y as isSvmNetwork,m as lazyX402Pay,w as registerWallet,x as releaseSpendOnFailure,C as resolveEvmAccount,p as resolveFacilitatorUrl,I as resolveSvmSigner,E as toCaip2,D as usdToAtomic};
1
+ import{createPayFetch as c}from"../packem_shared/createPayFetch-yWtKD0NN.mjs";import{DEFAULT_FACILITATOR_URL as f,resolveFacilitatorUrl as p}from"../packem_shared/DEFAULT_FACILITATOR_URL-CPJNUMvi.mjs";import{isEvmNetwork as d,isSvmNetwork as y,toCaip2 as E}from"../packem_shared/EVM_NETWORKS-C5yyzK27.mjs";import{DEFAULT_ALLOWED_ASSETS as T,DEFAULT_STABLECOIN_DECIMALS as v,assertBoundedPolicy as F,buildPaymentGuard as P,buildSpendPolicy as h,createSpendState as _,releaseSpendOnFailure as x,usdToAtomic as D}from"../packem_shared/DEFAULT_ALLOWED_ASSETS-Bda-nP2p.mjs";import{registerWallet as w,resolveEvmAccount as C,resolveSvmSigner as I}from"../packem_shared/registerWallet-DvCt0Q7M.mjs";const i=async(e,t)=>({fetch:await c(e,t)}),m=(e,t)=>{let r;return{fetch:async(o,a)=>(r??=i(e,t),(await r).fetch(o,a))}};export{T as DEFAULT_ALLOWED_ASSETS,f as DEFAULT_FACILITATOR_URL,v as DEFAULT_STABLECOIN_DECIMALS,F as assertBoundedPolicy,P as buildPaymentGuard,h as buildSpendPolicy,c as createPayFetch,_ as createSpendState,i as createX402Pay,d as isEvmNetwork,y as isSvmNetwork,m as lazyX402Pay,w as registerWallet,x as releaseSpendOnFailure,C as resolveEvmAccount,p as resolveFacilitatorUrl,I as resolveSvmSigner,E as toCaip2,D as usdToAtomic};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/x402",
3
- "version": "1.0.0-alpha.48",
3
+ "version": "1.0.0-alpha.49",
4
4
  "description": "Agentic payments (x402) for Lunora: charge agents per request (charge rail) and let your agents pay x402-gated resources (pay rail)",
5
5
  "keywords": [
6
6
  "agents",
@@ -52,7 +52,7 @@
52
52
  "access": "public"
53
53
  },
54
54
  "dependencies": {
55
- "@lunora/errors": "1.0.0-alpha.29",
55
+ "@lunora/errors": "1.0.0-alpha.30",
56
56
  "@x402/core": "2.21.0",
57
57
  "@x402/fetch": "2.21.0"
58
58
  },
@@ -1 +0,0 @@
1
- import{createChargeMiddleware as n}from"./createChargeMiddleware-CURPSr23.mjs";const w=(t,a)=>{let e;return async(d,r)=>(e??=n(t).catch(i=>{throw e=void 0,i}),(await e).handle(r,()=>a(d,r)))};export{w as withX402};