@lunora/x402 1.0.0-alpha.79 → 1.0.0-alpha.80
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/dist/charge/index.d.mts +12 -12
- package/dist/charge/index.d.ts +12 -12
- package/dist/charge/index.mjs +1 -1
- package/dist/packem_shared/DEFAULT_ALLOWED_ASSETS-CYVf8dQC.mjs +1 -0
- package/dist/packem_shared/createChargeMiddleware-CCcn8Fg_.mjs +1 -0
- package/dist/packem_shared/{createPayFetch-yWtKD0NN.mjs → createPayFetch-A6ED62ml.mjs} +1 -1
- package/dist/packem_shared/{createProcedureChargeGate-Ki_K8dQE.mjs → createProcedureChargeGate-g4DR6RkQ.mjs} +1 -1
- package/dist/packem_shared/{withX402-CXFSHtTo.mjs → withX402-BN0YZQRe.mjs} +1 -1
- package/dist/pay/index.mjs +1 -1
- package/package.json +2 -2
- package/dist/packem_shared/DEFAULT_ALLOWED_ASSETS-Bda-nP2p.mjs +0 -1
- package/dist/packem_shared/createChargeMiddleware-CURPSr23.mjs +0 -1
package/dist/charge/index.d.mts
CHANGED
|
@@ -5,8 +5,8 @@ export { type b as Caip2, c as DEFAULT_FACILITATOR_URL,
|
|
|
5
5
|
*
|
|
6
6
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
7
7
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
8
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
9
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
8
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
9
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
10
10
|
*
|
|
11
11
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
12
12
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
|
@@ -17,8 +17,8 @@ type E as EvmAddress, type P as PaymentEventRow, type g as X402Network, type s a
|
|
|
17
17
|
*
|
|
18
18
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
19
19
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
20
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
21
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
20
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
21
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
22
22
|
*
|
|
23
23
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
24
24
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
|
@@ -139,8 +139,8 @@ interface X402ProcedureSpec {
|
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
141
141
|
* Gate one paid RPC. Returns a real `402` + `PAYMENT-REQUIRED` challenge when the
|
|
142
|
-
* request is unpaid, or the dispatched response (with `
|
|
143
|
-
* attached) once the client's `
|
|
142
|
+
* request is unpaid, or the dispatched response (with `PAYMENT-RESPONSE`
|
|
143
|
+
* attached) once the client's `PAYMENT-SIGNATURE` is verified and settled. `dispatch`
|
|
144
144
|
* runs the actual shard forward — settlement happens **before** `dispatch` is
|
|
145
145
|
* invoked (settle-first), so a settlement failure means the shard forward
|
|
146
146
|
* (the mutation's commit) never runs at all — no committed-but-unpaid write is
|
|
@@ -166,8 +166,8 @@ export { type ChargeHandler, type ChargeHandlerDeps, type ChargeMiddleware, type
|
|
|
166
166
|
*
|
|
167
167
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
168
168
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
169
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
170
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
169
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
170
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
171
171
|
*
|
|
172
172
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
173
173
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
|
@@ -178,8 +178,8 @@ type FacilitatorConfig, type HttpActionHandler,
|
|
|
178
178
|
*
|
|
179
179
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
180
180
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
181
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
182
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
181
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
182
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
183
183
|
*
|
|
184
184
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
185
185
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
|
@@ -190,8 +190,8 @@ type X402ChargeConfig,
|
|
|
190
190
|
*
|
|
191
191
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
192
192
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
193
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
194
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
193
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
194
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
195
195
|
*
|
|
196
196
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
197
197
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
package/dist/charge/index.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ export { type b as Caip2, c as DEFAULT_FACILITATOR_URL,
|
|
|
5
5
|
*
|
|
6
6
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
7
7
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
8
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
9
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
8
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
9
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
10
10
|
*
|
|
11
11
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
12
12
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
|
@@ -17,8 +17,8 @@ type E as EvmAddress, type P as PaymentEventRow, type g as X402Network, type s a
|
|
|
17
17
|
*
|
|
18
18
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
19
19
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
20
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
21
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
20
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
21
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
22
22
|
*
|
|
23
23
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
24
24
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
|
@@ -139,8 +139,8 @@ interface X402ProcedureSpec {
|
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
141
141
|
* Gate one paid RPC. Returns a real `402` + `PAYMENT-REQUIRED` challenge when the
|
|
142
|
-
* request is unpaid, or the dispatched response (with `
|
|
143
|
-
* attached) once the client's `
|
|
142
|
+
* request is unpaid, or the dispatched response (with `PAYMENT-RESPONSE`
|
|
143
|
+
* attached) once the client's `PAYMENT-SIGNATURE` is verified and settled. `dispatch`
|
|
144
144
|
* runs the actual shard forward — settlement happens **before** `dispatch` is
|
|
145
145
|
* invoked (settle-first), so a settlement failure means the shard forward
|
|
146
146
|
* (the mutation's commit) never runs at all — no committed-but-unpaid write is
|
|
@@ -166,8 +166,8 @@ export { type ChargeHandler, type ChargeHandlerDeps, type ChargeMiddleware, type
|
|
|
166
166
|
*
|
|
167
167
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
168
168
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
169
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
170
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
169
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
170
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
171
171
|
*
|
|
172
172
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
173
173
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
|
@@ -178,8 +178,8 @@ type FacilitatorConfig, type HttpActionHandler,
|
|
|
178
178
|
*
|
|
179
179
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
180
180
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
181
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
182
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
181
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
182
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
183
183
|
*
|
|
184
184
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
185
185
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
|
@@ -190,8 +190,8 @@ type X402ChargeConfig,
|
|
|
190
190
|
*
|
|
191
191
|
* A Lunora deployment gates a resource (an HTTP-action route, a procedure, or
|
|
192
192
|
* an MCP tool) behind a USDC price: it returns `402 Payment Required` with a
|
|
193
|
-
* `PAYMENT-REQUIRED` header, verifies the client's `
|
|
194
|
-
* facilitator), runs the handler, settles, and attaches `
|
|
193
|
+
* `PAYMENT-REQUIRED` header, verifies the client's `PAYMENT-SIGNATURE` payload (via a
|
|
194
|
+
* facilitator), runs the handler, settles, and attaches `PAYMENT-RESPONSE`.
|
|
195
195
|
*
|
|
196
196
|
* `withX402` gates a Lunora HTTP action; `createChargeMiddleware` is the
|
|
197
197
|
* framework-agnostic core for other surfaces (procedures, MCP tools).
|
package/dist/charge/index.mjs
CHANGED
|
@@ -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-
|
|
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-BN0YZQRe.mjs";import{createChargeMiddleware as C}from"../packem_shared/createChargeMiddleware-CCcn8Fg_.mjs";import{createProcedureChargeGate as F}from"../packem_shared/createProcedureChargeGate-g4DR6RkQ.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};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as l}from"@lunora/errors";import{toCaip2 as f}from"./EVM_NETWORKS-C5yyzK27.mjs";const h=/^\d+(?:\.\d+)?$/,u=(e,t)=>t.startsWith("eip155:")?e.toLowerCase():e,p=(e,t)=>`${t}|${u(e,t)}`,v=/^\d+$/,w=e=>v.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(!h.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[i="0",r=""]=s.split("."),a=r.slice(0,t).padEnd(t,"0");return BigInt(i)*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 i=f(s.network),r=p(s.asset,i),a=n.get(r);if(a!==void 0&&a.decimals!==s.decimals)throw new l("BAD_REQUEST",`x402 policy: allowedAssets lists ${s.asset} on ${i} twice with different decimals (${String(a.decimals)} and ${String(s.decimals)}). One asset cannot have two precisions.`);n.set(r,{...s,network:i})}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:i}=e;return(r,a)=>a.filter(o=>{const c=t.get(p(o.asset,o.network));if(c===void 0)return!1;const d=w(o.amount);if(d===void 0)return!1;const m=n.get(c.decimals);if(m!==void 0&&d>m||s!==void 0&&!s.includes(o.network))return!1;if(i!==void 0){const A=u(o.payTo,o.network);return i.some(y=>u(y,o.network)===A)}return!0})},P=(e,t)=>{const n=b(e),s=g(e.maxPerRun,n);let i;return async r=>{const a=r.selectedRequirements,o=n.get(p(a.asset,a.network));if(o===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(t.spentAtomic>0n&&i!==o.decimals)return{abort:!0,reason:`x402 policy: this payment is in a ${String(o.decimals)}-decimal asset but the run's spend is tracked in ${String(i)}-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(o.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),i=o.decimals;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};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{x402HTTPResourceServer as E}from"@x402/core/http";import{toCaip2 as k,isEvmNetwork as x}from"./EVM_NETWORKS-C5yyzK27.mjs";import{toReceipt as P}from"./toPaymentEventRow-DTXuTDh0.mjs";import{createFacilitatorClient as S}from"./createFacilitatorClient-piLSnKs5.mjs";import{i as g}from"./optional-peer-BBhVxozi.mjs";const H=async e=>{const{x402ResourceServer:r}=await import("@x402/core/server"),t=new r(S(e.facilitator)),s=k(e.network);if(x(e.network)){const{registerExactEvmScheme:a}=await g(()=>import("@x402/evm/exact/server"),"x402 charge: EVM networks need the optional @x402/evm + viem peers — install them, or configure an SVM network.");a(t,{networks:[s]})}else{const{registerExactSvmScheme:a}=await g(()=>import("@x402/svm/exact/server"),"x402 charge: SVM networks need the optional @x402/svm + @solana/kit peers — install them, or configure an EVM network.");a(t,{networks:[s]})}return t},T="PAYMENT-SIGNATURE",A=e=>{const r=e.toLowerCase();return r==="cdn-cache-control"||r.endsWith("-cdn-cache-control")||r==="surrogate-control"},C=e=>["private",...(e??"").split(",").map(t=>t.trim()).filter(t=>{const s=t.split("=")[0]?.trim().toLowerCase();return s!==""&&s!=="public"&&s!=="private"&&s!=="s-maxage"})].join(", "),b=e=>{const r=Object.create(null);for(const[t,s]of e)r[t]=s;return r},R=(e,r,t,s)=>{if(e!==void 0)try{const a=Promise.resolve(e(P(r,{resource:t,ts:Date.now()}))).catch(()=>{});s?.(a)}catch{}},M=e=>{const r=x(e.network),t=r?e.recipient.evm:e.recipient.svm;if(t===void 0||t.length===0)throw new Error(`x402 charge on "${e.network}" needs recipient.${r?"evm":"svm"} set.`);return t},U=e=>({accepts:{network:k(e.network),payTo:M(e),price:e.price,scheme:"exact"}}),j=(e,r)=>({getAcceptHeader:()=>e.headers.get("accept")??"",getHeader:t=>e.headers.get(t)??void 0,getMethod:()=>e.method,getPath:()=>r.pathname,getQueryParam:t=>{const s=r.searchParams.getAll(t);if(s.length!==0)return s.length===1?s[0]:s},getQueryParams:()=>{const t=Object.create(null);for(const s of new Set(r.searchParams.keys())){const a=r.searchParams.getAll(s),[h,...d]=a;h!==void 0&&(t[s]=d.length>0?a:h)}return t},getUrl:()=>e.url,getUserAgent:()=>e.headers.get("user-agent")??""}),y=e=>{const r=new Headers(e.headers),{body:t}=e;return t==null?new Response(void 0,{headers:r,status:e.status}):typeof t=="string"?(e.isHtml&&!r.has("content-type")&&r.set("content-type","text/html; charset=utf-8"),new Response(t,{headers:r,status:e.status})):Response.json(t,{headers:r,status:e.status})},N=(e,r)=>{const t=new Headers(e.headers);for(const[s,a]of Object.entries(r))t.set(s,a);return new Response(e.body,{headers:t,status:e.status,statusText:e.statusText})},Y=async(e,r,t)=>{const s=await H(e),a=new E(s,{...U(e),...r});await a.initialize();const h=t?.settleBeforeHandler??!0,d=(o,l)=>{const i=N(o,{...l,"cache-control":C(o.headers.get("cache-control"))});for(const m of[...i.headers.keys()].filter(p=>A(p)))i.headers.delete(m);return i};return{handle:async(o,l,i)=>{const m=new URL(o.url),p={adapter:j(o,m),method:o.method,path:m.pathname,paymentHeader:o.headers.get(T)??void 0},n=await a.processHTTPRequest(p);if(n.type==="no-payment-required")return l();if(n.type==="payment-error")return y(n.response);const f=r?.resource??o.url;if(h){const c=await a.processSettlement(n.paymentPayload,n.paymentRequirements,n.declaredExtensions,{request:p});if(!c.success)return y(c.response);R(e.onReceipt,c,f,i?.waitUntil);const v=await l();return d(v,c.headers)}let w;try{w=await l()}catch(c){try{await n.cancellationDispatcher.cancel({error:c,reason:"handler_threw"})}catch(v){console.error("x402 charge: failed to cancel payment after the handler threw",v)}throw c}const u=await a.processSettlement(n.paymentPayload,n.paymentRequirements,n.declaredExtensions,{request:p,responseHeaders:b(w.headers)});return u.success?(R(e.onReceipt,u,f,i?.waitUntil),d(w,u.headers)):y(u.response)}}};export{U as buildRoute,Y as createChargeMiddleware,j as createRequestAdapter,R as reportReceipt,M as resolvePayTo,y as toResponse,N as withHeaders};
|
|
@@ -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-
|
|
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-CYVf8dQC.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 +1 @@
|
|
|
1
|
-
import{createChargeMiddleware as a}from"./createChargeMiddleware-
|
|
1
|
+
import{createChargeMiddleware as a}from"./createChargeMiddleware-CCcn8Fg_.mjs";const d=(e,r,i,n)=>{const c=e.get(r);if(c!==void 0)return c;const t=i().catch(o=>{throw e.get(r)===t&&e.delete(r),o});return e.set(r,t),t},u=e=>{const r=new Map;return async(i,n,c,t)=>(await d(r,n.functionPath,async()=>a({...e,price:n.price},{resource:n.functionPath}))).handle(i,c,t)};export{u as createProcedureChargeGate};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createChargeMiddleware as w}from"./createChargeMiddleware-
|
|
1
|
+
import{createChargeMiddleware as w}from"./createChargeMiddleware-CCcn8Fg_.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};
|
package/dist/pay/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createPayFetch as c}from"../packem_shared/createPayFetch-
|
|
1
|
+
import{createPayFetch as c}from"../packem_shared/createPayFetch-A6ED62ml.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-CYVf8dQC.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.
|
|
3
|
+
"version": "1.0.0-alpha.80",
|
|
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.
|
|
55
|
+
"@lunora/errors": "1.0.0-alpha.48",
|
|
56
56
|
"@x402/core": "2.25.0",
|
|
57
57
|
"@x402/fetch": "2.25.0"
|
|
58
58
|
},
|
|
@@ -1 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
import{x402HTTPResourceServer as E}from"@x402/core/http";import{toCaip2 as x,isEvmNetwork as P}from"./EVM_NETWORKS-C5yyzK27.mjs";import{toReceipt as S}from"./toPaymentEventRow-DTXuTDh0.mjs";import{createFacilitatorClient as H}from"./createFacilitatorClient-piLSnKs5.mjs";import{i as R}from"./optional-peer-BBhVxozi.mjs";const T=async e=>{const{x402ResourceServer:r}=await import("@x402/core/server"),t=new r(H(e.facilitator)),s=x(e.network);if(P(e.network)){const{registerExactEvmScheme:a}=await R(()=>import("@x402/evm/exact/server"),"x402 charge: EVM networks need the optional @x402/evm + viem peers — install them, or configure an SVM network.");a(t,{networks:[s]})}else{const{registerExactSvmScheme:a}=await R(()=>import("@x402/svm/exact/server"),"x402 charge: SVM networks need the optional @x402/svm + @solana/kit peers — install them, or configure an EVM network.");a(t,{networks:[s]})}return t},A="X-PAYMENT",M=e=>{const r=Object.create(null);for(const[t,s]of e)r[t]=s;return r},g=(e,r,t,s)=>{if(e!==void 0)try{const a=Promise.resolve(e(S(r,{resource:t,ts:Date.now()}))).catch(()=>{});s?.(a)}catch{}},b=e=>{const r=P(e.network),t=r?e.recipient.evm:e.recipient.svm;if(t===void 0||t.length===0)throw new Error(`x402 charge on "${e.network}" needs recipient.${r?"evm":"svm"} set.`);return t},U=e=>({accepts:{network:x(e.network),payTo:b(e),price:e.price,scheme:"exact"}}),j=(e,r)=>({getAcceptHeader:()=>e.headers.get("accept")??"",getHeader:t=>e.headers.get(t)??void 0,getMethod:()=>e.method,getPath:()=>r.pathname,getQueryParam:t=>{const s=r.searchParams.getAll(t);if(s.length!==0)return s.length===1?s[0]:s},getQueryParams:()=>{const t=Object.create(null);for(const s of new Set(r.searchParams.keys())){const a=r.searchParams.getAll(s),[i,...w]=a;i!==void 0&&(t[s]=w.length>0?a:i)}return t},getUrl:()=>e.url,getUserAgent:()=>e.headers.get("user-agent")??""}),u=e=>{const r=new Headers(e.headers),{body:t}=e;return t==null?new Response(void 0,{headers:r,status:e.status}):typeof t=="string"?(e.isHtml&&!r.has("content-type")&&r.set("content-type","text/html; charset=utf-8"),new Response(t,{headers:r,status:e.status})):Response.json(t,{headers:r,status:e.status})},k=(e,r)=>{const t=new Headers(e.headers);for(const[s,a]of Object.entries(r))t.set(s,a);return new Response(e.body,{headers:t,status:e.status,statusText:e.statusText})},B=async(e,r,t)=>{const s=await T(e),a=new E(s,{...U(e),...r});await a.initialize();const i=t?.settleBeforeHandler??!0;return{handle:async(c,p,y)=>{const v=new URL(c.url),h={adapter:j(c,v),method:c.method,path:v.pathname,paymentHeader:c.headers.get(A)??void 0},n=await a.processHTTPRequest(h);if(n.type==="no-payment-required")return p();if(n.type==="payment-error")return u(n.response);const f=r?.resource??c.url;if(i){const o=await a.processSettlement(n.paymentPayload,n.paymentRequirements,n.declaredExtensions,{request:h});if(!o.success)return u(o.response);g(e.onReceipt,o,f,y?.waitUntil);const d=await p();return k(d,o.headers)}let m;try{m=await p()}catch(o){try{await n.cancellationDispatcher.cancel({error:o,reason:"handler_threw"})}catch(d){console.error("x402 charge: failed to cancel payment after the handler threw",d)}throw o}const l=await a.processSettlement(n.paymentPayload,n.paymentRequirements,n.declaredExtensions,{request:h,responseHeaders:M(m.headers)});return l.success?(g(e.onReceipt,l,f,y?.waitUntil),k(m,l.headers)):u(l.response)}}};export{U as buildRoute,B as createChargeMiddleware,j as createRequestAdapter,g as reportReceipt,b as resolvePayTo,u as toResponse,k as withHeaders};
|