@dexterai/x402 5.3.0 → 5.3.1
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/client/index.cjs +1 -1
- package/dist/client/index.d.cts +2 -2
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.js +1 -1
- package/dist/tab/adapters/solana/index.cjs +1 -1
- package/dist/tab/adapters/solana/index.d.cts +150 -5
- package/dist/tab/adapters/solana/index.d.ts +150 -5
- package/dist/tab/adapters/solana/index.js +1 -1
- package/dist/tab/index.cjs +4 -4
- package/dist/tab/index.d.cts +11 -3
- package/dist/tab/index.d.ts +11 -3
- package/dist/tab/index.js +4 -4
- package/dist/tab/seller/index.cjs +3 -3
- package/dist/tab/seller/index.d.cts +28 -2
- package/dist/tab/seller/index.d.ts +28 -2
- package/dist/tab/seller/index.js +3 -3
- package/dist/{types-D5eKDMvo.d.cts → types-CSuISjXA.d.cts} +81 -4
- package/dist/{types-D5eKDMvo.d.ts → types-CSuISjXA.d.ts} +81 -4
- package/dist/{types-CWYzWTwF.d.cts → types-CZHPdVkg.d.cts} +1 -1
- package/dist/{types-rSrCtwS2.d.ts → types-Co5YYhVB.d.ts} +1 -1
- package/package.json +4 -4
- package/assets/openai-pricing.md +0 -385
package/dist/tab/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var me=class extends Error{constructor(n){super(`Network ${n} is not yet supported by @dexterai/x402/tab`);this.network=n;this.name="UnsupportedNetworkError"}},pe=class extends Error{constructor(n,r){super(`Session scope exceeded: ${n}${r?` (${r})`:""}`);this.reason=n;this.name="SessionScopeExceededError"}},Q=class extends Error{constructor(n){super(`Tab ${n} is already closed`);this.channelId=n;this.name="TabClosedError"}};import{PublicKey as
|
|
1
|
+
var me=class extends Error{constructor(n){super(`Network ${n} is not yet supported by @dexterai/x402/tab`);this.network=n;this.name="UnsupportedNetworkError"}},pe=class extends Error{constructor(n,r){super(`Session scope exceeded: ${n}${r?` (${r})`:""}`);this.reason=n;this.name="SessionScopeExceededError"}},Q=class extends Error{constructor(n){super(`Tab ${n} is already closed`);this.channelId=n;this.name="TabClosedError"}},mt=class extends Error{constructor(n){super(`a LIVE session already exists for counterparty ${n.allowedCounterparty} (session ${n.sessionPubkeyHex.slice(0,16)}\u2026, expires ${n.expiresAtUnix}; on-chain spent=${n.spentAtomic}, crystallized=${n.crystallizedCumulativeAtomic}, frontier=${n.frontierAtomic}). Replacing it voids any signed-but-unsettled vouchers beyond the frontier. Settle the old tab first (tab.close() or POST its last voucher to /tab/settle), or pass onLiveSession: 'replace' to atomically revoke-then-register over it.`);this.details=n;this.name="LiveSessionExistsError"}};import{PublicKey as _t}from"@solana/web3.js";import{bytesToHex as ee}from"@noble/hashes/utils";import Jt from"tweetnacl";import{sessionRegisterMessage as pt,sessionRevokeMessage as Ft,voucherPayloadMessage as dt,buildVoucherMessage as jt}from"@dexterai/vault/messages";import{sha256 as zt}from"@noble/hashes/sha256";function Pt(t,e,n){return{publicKey:t.publicKey,privateKey:t.privateKey,scope:e,registration:n}}function Rt(t,e,n){if(n.length!==32)throw new Error(`channelIdBytes must be 32 bytes, got ${n.length}`);let r=BigInt(e.cumulativeAmount),s=BigInt(t.scope.maxAmountAtomic);if(r>s)throw new Error(`voucher cumulative ${r} exceeds session cap ${s}`);let o=Math.floor(Date.now()/1e3);if(o>=t.scope.expiresAtUnix)throw new Error(`session expired at ${t.scope.expiresAtUnix}, now ${o}`);let a=dt({channelId:n,cumulativeAmount:r,sequenceNumber:e.sequenceNumber}),i=Jt.sign.detached(a,t.privateKey);return{payload:e,sessionPublicKey:t.publicKey,sessionRegistration:t.registration,sessionSignature:i}}function ze(t){if(!/^\d+$/.test(t))throw new Error(`atomic amount must be a non-negative integer string, got "${t}"`);return BigInt(t)}function Ge(t){let e=new Uint8Array(8);new DataView(e.buffer).setBigUint64(0,t.nonce,!0);let n=new TextEncoder().encode(t.sellerUrl),r=zt.create();return r.update(t.vaultPda.toBytes()),r.update(n),r.update(e),r.digest()}var Gt=3600,Pe="https://x402.dexter.cash",kt=6;function Z(t,e=kt){if(!/^\d+(\.\d+)?$/.test(t))throw new Error(`amount must be a non-negative decimal string, got "${t}"`);let[n,r=""]=t.split(".");if(r.length>e)throw new Error(`amount "${t}" has more than ${e} decimals`);let s=r.padEnd(e,"0"),o=`${n}${s}`.replace(/^0+(?=\d)/,"");return o===""?"0":o}function de(t,e=kt){if(!/^\d+$/.test(t))throw new Error(`atomic must be a non-negative integer string, got "${t}"`);let n=t.padStart(e+1,"0"),r=n.slice(0,-e).replace(/^0+(?=\d)/,"")||"0",s=n.slice(-e).replace(/0+$/,"");return s?`${r}.${s}`:r}var Re=class{channelId;network;counterparty;internals;initialCumulativeAtomic;cumulativeAtomic;sequenceNumber=0;closed=!1;lastSignedVoucher=null;previousSignedVoucher=null;constructor(e){this.internals=e,this.channelId=e.channelIdHex,this.network=e.network,this.counterparty=e.counterparty,this.initialCumulativeAtomic=e.initialCumulativeAtomic??0n,this.cumulativeAtomic=this.initialCumulativeAtomic}get state(){let e=this.internals.totalCapAtomic-this.cumulativeAtomic,n=Math.floor(Date.now()/1e3);return{isOpen:!this.closed,spent:de(this.cumulativeAtomic.toString()),remaining:de(e.toString()),expiresInSec:Math.max(0,this.internals.expiresAtUnix-n)}}async signNextVoucher(e){if(this.closed)throw new Q(this.channelId);let n=BigInt(e);if(n<=0n)throw new Error(`voucher increment must be > 0, got ${e}`);if(n>this.internals.perUnitCapAtomic)throw new pe("cap_exceeded",`single voucher increment ${n} exceeds perUnitCap ${this.internals.perUnitCapAtomic}`);let r=this.cumulativeAtomic+n;if(r>this.internals.totalCapAtomic)throw new pe("cap_exceeded",`cumulative ${r} would exceed totalCap ${this.internals.totalCapAtomic}`);let s=this.sequenceNumber+1,o={channelId:this.channelId,cumulativeAmount:r.toString(),sequenceNumber:s},a=await this.internals.vault.signWithSession(this.internals.session,o);return this.previousSignedVoucher=this.lastSignedVoucher,this.lastSignedVoucher=a,this.sequenceNumber=s,this.cumulativeAtomic=r,a}rollbackVoucher(e){let n=this.lastSignedVoucher;if(!n||n.payload.sequenceNumber!==e.payload.sequenceNumber||n.payload.cumulativeAmount!==e.payload.cumulativeAmount)return!1;let r=this.previousSignedVoucher;if(r)this.sequenceNumber=r.payload.sequenceNumber,this.cumulativeAtomic=BigInt(r.payload.cumulativeAmount),this.lastSignedVoucher=r;else if(n.payload.sequenceNumber===1)this.sequenceNumber=0,this.cumulativeAtomic=this.initialCumulativeAtomic,this.lastSignedVoucher=null;else return!1;return this.previousSignedVoucher=null,!0}async stream(e,n){if(this.closed)throw new Q(this.channelId);let r=await this.signNextVoucher(this.internals.perUnitCapAtomic.toString()),s=new Headers(n?.headers);s.set("X-Tab-Voucher",Xe(r)),s.set("Accept","text/event-stream");let o=await fetch(e,{...n,headers:s});if(!o.ok){let a=await o.text().catch(()=>"");throw new Error(`tab.stream HTTP ${o.status}: ${a.slice(0,500)}`)}if(!o.body)throw new Error("tab.stream response has no body");return en(o.body)}async close(){if(this.closed)throw new Q(this.channelId);let e={settleTx:""};this.lastSignedVoucher&&this.cumulativeAtomic>0n&&(e=await Yt(this.internals.facilitatorUrl,this.lastSignedVoucher,this.internals.network));let n=(this.internals.closeMode??"revoke")==="revoke";return n&&await this.internals.vault.signCloseTab(this.internals.session,this.channelId,this.cumulativeAtomic.toString()),this.closed=!0,this.internals.session.privateKey.fill(0),{settledAmount:de(this.cumulativeAtomic.toString()),sessionRevoked:n,...e}}};function Xe(t){return Buffer.from(JSON.stringify({payload:t.payload,sessionPublicKey:ee(t.sessionPublicKey),sessionRegistration:ee(t.sessionRegistration),sessionSignature:ee(t.sessionSignature)}),"utf8").toString("base64")}function fe(t){return typeof t!="string"||!/^\d+$/.test(t)?null:BigInt(t)}function Xt(t){let e;try{e=JSON.parse(t)}catch{return!1}if(typeof e!="object"||e===null)return!1;let n="non_monotonic_cumulative";if(e.error!==n&&e.reason!==n)return!1;let r=fe(e.attempted_cumulative),s=fe(e.on_chain_spent),o=fe(e.frontier);return r===null||s===null||o===null?!1:r>s&&r<=o}async function Yt(t,e,n){let r=`${t.replace(/\/$/,"")}/tab/settle`,s={channelId:e.payload.channelId,cumulativeAmount:e.payload.cumulativeAmount,sequenceNumber:e.payload.sequenceNumber,sessionPublicKey:ee(e.sessionPublicKey),sessionSignature:ee(e.sessionSignature),sessionRegistration:ee(e.sessionRegistration),network:n},o=await fetch(r,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(s)}),a=await o.text();if(!o.ok){if(o.status===409&&Xt(a))return{settleTx:""};throw new Error(`tab settle ${o.status}: ${a.slice(0,500)}`)}let i;try{i=JSON.parse(a)}catch{throw new Error(`tab settle returned non-JSON: ${a.slice(0,200)}`)}if(i.reason==="covered_by_frontier"){let u=fe(i.attemptedCumulative),p=fe(i.onChainSpent),d=fe(i.frontier);if(u!==null&&p!==null&&d!==null&&u>p&&u<=d)return{settleTx:""}}if(!i.settleTx)throw new Error(`tab settle returned no settleTx: ${a.slice(0,200)}`);let c={settleTx:i.settleTx};return typeof i.grossAmount=="string"&&(c.grossAmount=i.grossAmount),typeof i.feeAmount=="string"&&(c.feeAmount=i.feeAmount),typeof i.netAmount=="string"&&(c.netAmount=i.netAmount),c}async function ft(t,e,n,r,s){let o=`${t.replace(/\/$/,"")}/tab/open`,a=await fetch(o,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({buyer_swig_address:e,seller:s,max_amount_atomic:n.toString(),network:r})}),i=await a.json().catch(()=>({}));if(!a.ok||!i.success||!i.armed)throw new Error(`tab_open_unprotected: ${i.error??`http_${a.status}`}`);return{armed:!0,signature:i.signature??""}}async function gt(t){if(t.network!==t.vault.network)throw new me(`options.network (${t.network}) doesn't match vault.network (${t.vault.network})`);if(t.network!=="solana:mainnet")throw new me(t.network);let e=BigInt(Math.floor(Math.random()*4294967295)),n=new _t(t.vault.vaultPda),r=Ge({vaultPda:n,sellerUrl:t.seller,nonce:BigInt(e)}),s=ee(r),o=BigInt(Z(t.perUnitCap)),a=BigInt(Z(t.totalCap));if(o<=0n)throw new Error("perUnitCap must be > 0");if(a<o)throw new Error("totalCap must be >= perUnitCap");if(t.revolvingCapacity!==void 0&&BigInt(Z(t.revolvingCapacity))<=0n)throw new Error("revolvingCapacity must be > 0");let i=t.sessionDuration??Gt,c=Math.floor(Date.now()/1e3)+i,u=Zt(t.seller),p={channelId:s,maxAmountAtomic:a.toString(),revolvingCapacityAtomic:t.revolvingCapacity?Z(t.revolvingCapacity):a.toString(),expiresAtUnix:c,allowedCounterparty:u},d=await t.vault.authorizeSession(p,{onLiveSession:t.onLiveSession});return await ft(t.facilitatorUrl??Pe,t.vault.swigAddress,a,t.network,u),new Re({vault:t.vault,network:t.network,seller:t.seller,counterparty:u,session:d,channelIdHex:s,channelIdBytes:r,perUnitCapAtomic:o,totalCapAtomic:a,expiresAtUnix:c,facilitatorUrl:t.facilitatorUrl??Pe})}async function Qt(t){throw new Error("resumeTab is Phase 3 work. Session keys are memory-only by design; recovery requires reading active_session on chain and re-authorizing. Tracked in dexter-vault roadmap.")}function Zt(t){if(/^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(t))try{return new _t(t),t}catch{}throw new Error(`seller must be a base58 Solana pubkey for Phase 2 (got "${t}"). URL-based counterparty resolution lands in Phase 3 (seller middleware).`)}async function*en(t){let e=t.getReader(),n=new TextDecoder,r="";try{for(;;){let{done:s,value:o}=await e.read();if(s)break;r+=n.decode(o,{stream:!0});let a;for(;(a=r.indexOf(`
|
|
2
2
|
|
|
3
|
-
`))!==-1;){let i=r.slice(0,
|
|
4
|
-
`);yield new TextEncoder().encode(u)}}}}finally{e.releaseLock()}}function
|
|
3
|
+
`))!==-1;){let i=r.slice(0,a);r=r.slice(a+2);let c=tn(i);if(c.eventName==="end")return;if(c.data!==null){let u=c.data.replace(/\\n/g,`
|
|
4
|
+
`);yield new TextEncoder().encode(u)}}}}finally{e.releaseLock()}}function tn(t){let e=null,n=[];for(let r of t.split(`
|
|
5
5
|
`))r.startsWith("event:")?e=r.slice(6).trim():r.startsWith("data:")&&n.push(r.slice(5).trimStart());return{eventName:e,data:n.length?n.join(`
|
|
6
|
-
`):null}}import ke from"tweetnacl";import{Connection as on,PublicKey as _e}from"@solana/web3.js";import{bytesToHex as cn}from"@noble/hashes/utils";import{fetchSessionAccount as ln,isSessionLive as un}from"@dexterai/vault/session";import{readVaultFull as mn}from"@dexterai/vault/reader";import{buildRegisterSessionKeyInstruction as tn,buildRevokeSessionKeyInstruction as nn,deriveSwigWalletAddress as sr}from"@dexterai/vault/instructions";import{buildSecp256r1VerifyInstruction as rn}from"@dexterai/vault/precompile";import{DEXTER_VAULT_PROGRAM_ID as gt,SECP256R1_PROGRAM_ID as sn,INSTRUCTIONS_SYSVAR_ID as an}from"@dexterai/vault/constants";var yt="solana:mainnet";async function pn(t){let e=t.facilitatorUrl??Pe,n=t.programId??gt,r=new _e(t.vaultPda),s=new _e(t.params.counterparty),a=Ge(t.perUnitCapAtomic);if(a<=0n)throw new Error(`perUnitCapAtomic must be > 0, got ${t.perUnitCapAtomic}`);let o=Ge(t.params.maxAmountAtomic),i=Ge(t.params.maxRevolvingCapacityAtomic),c=dn(t.sessionSecretKey),u=new _e(t.params.sessionPubkey).toBytes();if(!kt(c.publicKey,u))throw new Error(`tab_session_key_mismatch: the supplied secret derives ${new _e(c.publicKey).toBase58()} but params.sessionPubkey is ${t.params.sessionPubkey} \u2014 wrong key handed to this process`);let p=ke.randomBytes(32),d=ke.sign.detached(p,c.secretKey);if(!ke.sign.detached.verify(p,d,u))throw new Error("tab_session_key_mismatch: secret key failed the sign/verify self-check against params.sessionPubkey \u2014 the seed half does not sign for the granted pubkey");let f=mt({programId:n,vaultPda:r,sessionPubkey:c.publicKey,maxAmount:o,expiresAt:BigInt(t.params.expiresAtUnix),allowedCounterparty:s,nonce:t.params.nonce,maxRevolvingCapacity:i}),T=typeof t.connection=="string"?new on(t.connection,"confirmed"):t.connection,l=await ln(T,r,s,n);if(!l||l.version===0)throw new Error("tab_session_not_live: no live SessionAccount PDA for this (vault, counterparty) \u2014 revoked, expiry-swept, or never registered");if(!un(l))throw new Error(l.version!==1?`tab_session_not_live: SessionAccount has unsupported version ${l.version} \u2014 not a live v1 session`:`tab_session_not_live: SessionAccount is present but expired (expiresAt=${l.session.expiresAt})`);if(!kt(l.session.sessionPubkey,c.publicKey))throw new Error(`tab_session_pubkey_mismatch: the on-chain session carries ${new _e(l.session.sessionPubkey).toBase58()} \u2014 the grant this key belongs to was replaced by a newer registration`);if(l.session.maxAmount!==o||l.session.expiresAt!==t.params.expiresAtUnix||l.session.nonce!==t.params.nonce||l.session.maxRevolvingCapacity!==i)throw new Error(`tab_grant_params_stale: params disagree with the on-chain session scope (chain: maxAmount=${l.session.maxAmount} expiresAt=${l.session.expiresAt} nonce=${l.session.nonce} revolving=${l.session.maxRevolvingCapacity}; params: ${t.params.maxAmountAtomic}/${t.params.expiresAtUnix}/${t.params.nonce}/${t.params.maxRevolvingCapacityAtomic}) \u2014 refresh the grant hand-off`);let P=l.session.spent>l.session.crystallizedCumulative?l.session.spent:l.session.crystallizedCumulative;if(P>=o)throw new Error(`tab_exhausted: on-chain frontier ${P} has consumed the session cap ${o} \u2014 no voucher can strictly exceed it; open a new grant`);let x=t.sellerUrl??t.params.counterparty,y=BigInt(Math.floor(Math.random()*4294967295)),g=Xe({vaultPda:r,sellerUrl:x,nonce:y}),S=cn(g),k=t.swigAddress;if(!k){let O=await mn(T,r);if(!O.exists||!O.swigAddress)throw new Error(`tab_vault_unreadable: vault ${r.toBase58()} has no readable swig_address on chain \u2014 pass options.swigAddress explicitly`);k=O.swigAddress}await dt(e,k,o,yt,t.params.counterparty);let U={channelId:S,maxAmountAtomic:t.params.maxAmountAtomic,revolvingCapacityAtomic:t.params.maxRevolvingCapacityAtomic,expiresAtUnix:t.params.expiresAtUnix,allowedCounterparty:t.params.counterparty},K=vt({publicKey:c.publicKey,privateKey:c.secretKey},U,f),$={network:yt,swigAddress:k,vaultPda:r.toBase58(),authorizeSession:async()=>{throw new Error("grant_tab_no_passkey: a grant-held tab cannot authorize sessions \u2014 session birth requires the wallet owner's passkey ceremony")},signWithSession:async(O,D)=>Pt(O,D,g),signOpenTab:async O=>O.registration,signCloseTab:async()=>{throw new Error("grant_tab_cannot_revoke: session revocation is passkey-signed and belongs to the wallet owner's surfaces \u2014 a grant-held tab closes settle-only")}};return new Re({vault:$,network:yt,seller:x,counterparty:t.params.counterparty,session:K,channelIdHex:S,channelIdBytes:g,perUnitCapAtomic:a,totalCapAtomic:o,expiresAtUnix:t.params.expiresAtUnix,facilitatorUrl:e,initialCumulativeAtomic:P,closeMode:"settle-only"})}function dn(t){if(t.length===64)return ke.sign.keyPair.fromSecretKey(t);if(t.length===32)return ke.sign.keyPair.fromSeed(t);throw new Error(`tab_session_key_invalid: expected a 64-byte nacl secretKey or a 32-byte ed25519 seed, got ${t.length} bytes`)}function kt(t,e){if(t.length!==e.length)return!1;let n=0;for(let r=0;r<t.length;r++)n|=t[r]^e[r];return n===0}import{PublicKey as hn}from"@solana/web3.js";var Ct=[{caip2:"eip155:8453",bare:"base",family:"evm"},{caip2:"eip155:1",bare:"ethereum",family:"evm"},{caip2:"eip155:137",bare:"polygon",family:"evm"},{caip2:"eip155:42161",bare:"arbitrum",family:"evm"},{caip2:"eip155:10",bare:"optimism",family:"evm"},{caip2:"eip155:43114",bare:"avalanche",family:"evm"},{caip2:"eip155:56",bare:"bsc",family:"evm"},{caip2:"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",bare:"solana",family:"svm"}],fn=new Map(Ct.map(t=>[t.caip2.toLowerCase(),t])),gn=new Map(Ct.map(t=>[t.bare.toLowerCase(),t]));function Ce(t){if(!t)return null;let e=t.toLowerCase(),n=fn.get(e)||gn.get(e);return n?{caip2:n.caip2,bare:n.bare,family:n.family}:null}function ht(t){let e=t.replace(/-/g,"+").replace(/_/g,"/"),n=e+"=".repeat((4-(e.length%4||4))%4);return JSON.parse(Buffer.from(n,"base64").toString("utf8"))}function yn(t){let e=[];for(let n of t){if(!n||typeof n!="object")continue;let r=n,s=Ce(String(r.network??""));s&&e.push({scheme:String(r.scheme??"exact"),network:s,amount:String(r.amount??r.maxAmountRequired??"0"),asset:String(r.asset??""),payTo:String(r.payTo??""),maxTimeoutSeconds:typeof r.maxTimeoutSeconds=="number"?r.maxTimeoutSeconds:void 0,extra:r.extra&&typeof r.extra=="object"?r.extra:void 0})}return e}async function ze(t){let e=t.headers.get("payment-required");if(!e)return null;let n;try{n=ht(e)}catch{return null}let r=Array.isArray(n.accepts)?n.accepts:[];return r.length===0?null:{x402Version:2,options:yn(r),resourceUrl:n.resource&&typeof n.resource=="object"?String(n.resource.url??""):void 0}}async function Ie(t,e={},n=fetch){let r={method:e.method??"GET",headers:e.headers};typeof e.body=="string"&&(r.body=e.body);let s;try{s=await n(t,r)}catch(i){return{kind:"error",detail:`probe failed: ${i?.message??String(i)}`}}if(s.status!==402)return s.ok?{kind:"free",response:s}:{kind:"error",detail:`probe returned HTTP ${s.status}`};let a=await ze(s);if(!a)return{kind:"error",detail:"the 402 carries no x402 v2 PAYMENT-REQUIRED challenge"};let o=a.options.find(i=>i.scheme==="tab"&&i.network.family==="svm");if(!o)return{kind:"no_tab",schemesOffered:a.options.map(i=>i.scheme)};try{new hn(o.payTo)}catch{return{kind:"error",detail:`tab option payTo is not a valid Solana pubkey: "${o.payTo}"`}}return{kind:"offer",offer:{payTo:o.payTo,amountAtomic:o.amount,asset:o.asset,networkCaip2:o.network.caip2,resourceUrl:a.resourceUrl}}}var Ne="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",Qe="solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",Ze="solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z",te=Ne,Oe=Qe,Ue=Ze,ne="eip155:8453",ge="eip155:84532",ye="eip155:42161",he="eip155:137",Ae="eip155:10",be="eip155:43114",Se="eip155:56",we="eip155:1187947933",Te="eip155:324705682",xe="eip155:1",re=ne,et=ge,Be=ye,De=he,Me=Ae,$e=be,Ke=Se,Le=we,Ve=Te,We=xe,qe="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",It="4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",Nt="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",At="0x55d398326f99059fF775485246999027B3197955",tt="0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",nt={[Se]:tt,[ne]:Nt,[ge]:"0x036CbD53842c5426634e7929541eC2318f3dCF7e",[ye]:"0xaf88d065e77c8cC2239327C5EDb3A432268e5831",[he]:"0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",[Ae]:"0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",[be]:"0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",[we]:"0x85889c8c714505E0c94b30fcfcF64fE3Ac8FCb20",[Te]:"0x2e08028E3C4c2356572E096d8EF835cD5C6030bD",[xe]:"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"},rt={[At]:{symbol:"USDT",decimals:18},[tt]:{symbol:"USDC",decimals:18}};var G="0x000000000022D473030F116dDEE9F6B43aC78BA3",He="0x402085c248EeA27D92E8b30b2C58ed07f9E20001",Fe={[Se]:56,[ne]:8453,[ge]:84532,[ye]:42161,[he]:137,[Ae]:10,[be]:43114,[we]:1187947933,[Te]:324705682,[xe]:1},se={[Ne]:"https://api.dexter.cash/api/solana/rpc",[Qe]:"https://api.devnet.solana.com",[Ze]:"https://api.testnet.solana.com"},B={[Se]:"https://api.dexter.cash/api/evm/bsc/rpc",[ne]:"https://api.dexter.cash/api/base/rpc",[ge]:"https://sepolia.base.org",[ye]:"https://api.dexter.cash/api/evm/arbitrum/rpc",[he]:"https://api.dexter.cash/api/evm/polygon/rpc",[Ae]:"https://api.dexter.cash/api/evm/optimism/rpc",[be]:"https://api.dexter.cash/api/evm/avalanche/rpc",[we]:"https://skale-base.skalenodes.com/v1/base",[Te]:"https://base-sepolia-testnet.skalenodes.com/v1/jubilant-horrible-ancha",[xe]:"https://eth.llamarpc.com"};var I=class t extends Error{code;details;constructor(e,n,r){super(n),this.name="X402Error",this.code=e,this.details=r,Object.setPrototypeOf(this,t.prototype)}};import{PublicKey as ae,Connection as bt,TransactionMessage as An,VersionedTransaction as bn,ComputeBudgetProgram as Ot}from"@solana/web3.js";import{getAssociatedTokenAddress as St,getAccount as Sn,createTransferCheckedInstruction as wn,getMint as Tn,TOKEN_PROGRAM_ID as Ut,TOKEN_2022_PROGRAM_ID as st}from"@solana/spl-token";var xn=12e3,En=1;function oe(t){if(!t||typeof t!="object")return!1;let e=t;return"publicKey"in e&&"signTransaction"in e&&typeof e.signTransaction=="function"}var at=class{name="Solana";networks=[te,Oe,Ue];config;log;constructor(e={}){this.config=e,this.log=e.verbose?console.log.bind(console,"[x402:solana]"):()=>{}}canHandle(e){return!!(this.networks.includes(e)||e==="solana"||e==="solana-devnet"||e==="solana-testnet"||e.startsWith("solana:"))}getDefaultRpcUrl(e){return this.config.rpcUrls?.[e]?this.config.rpcUrls[e]:se[e]?se[e]:e==="solana"?se[te]:e==="solana-devnet"?se[Oe]:e==="solana-testnet"?se[Ue]:se[te]}getAddress(e){return oe(e)?e.publicKey?.toBase58()??null:null}isConnected(e){return oe(e)?e.publicKey!==null:!1}async getBalance(e,n,r){if(!oe(n)||!n.publicKey)return 0;let s=r||this.getDefaultRpcUrl(e.network),a=new bt(s,"confirmed"),o=new ae(n.publicKey.toBase58()),i=new ae(e.asset);try{let u=(await a.getAccountInfo(i,"confirmed"))?.owner.toBase58()===st.toBase58()?st:Ut,p=await St(i,o,!1,u),d=await Sn(a,p,void 0,u),f=e.extra?.decimals??6;return Number(d.amount)/Math.pow(10,f)}catch(c){if(c&&typeof c=="object"&&"name"in c&&(c.name==="TokenAccountNotFoundError"||c.name==="TokenInvalidAccountOwnerError"))return 0;throw c}}async buildTransaction(e,n,r){if(!oe(n))throw new Error("Invalid Solana wallet");if(!n.publicKey)throw new Error("Wallet not connected");let s=r||this.getDefaultRpcUrl(e.network),a=new bt(s,"confirmed"),o=new ae(n.publicKey.toBase58()),{payTo:i,asset:c,extra:u}=e,p=e.amount??e.maxAmountRequired;if(!p)throw new Error("Missing amount in payment requirements");if(!u?.feePayer)throw new Error("Missing feePayer in payment requirements");let d=new ae(u.feePayer),f=new ae(c),T=new ae(i);this.log("Building transaction:",{from:o.toBase58(),to:i,amount:p,asset:c,feePayer:u.feePayer});let l=[];l.push(Ot.setComputeUnitLimit({units:xn})),l.push(Ot.setComputeUnitPrice({microLamports:En}));let P=await a.getAccountInfo(f,"confirmed");if(!P)throw new Error(`Token mint ${c} not found`);let x=P.owner.toBase58()===st.toBase58()?st:Ut,y=await Tn(a,f,void 0,x);typeof u?.decimals=="number"&&y.decimals!==u.decimals&&this.log(`Decimals mismatch: requirements say ${u.decimals}, mint says ${y.decimals}`);let g=await St(f,o,!1,x),S=await St(f,T,!0,x);if(!await a.getAccountInfo(g,"confirmed"))throw new Error(`No token account found for ${c}. Please ensure you have USDC in your wallet.`);if(!await a.getAccountInfo(S,"confirmed"))throw new Error(`Seller token account not found. The seller (${i}) must have a USDC account.`);let K=BigInt(p);l.push(wn(g,f,S,o,K,y.decimals,[],x));let{blockhash:$}=await a.getLatestBlockhash("confirmed"),O=new An({payerKey:d,recentBlockhash:$,instructions:l}).compileToV0Message(),D=new bn(O),h=await n.signTransaction(D);return this.log("Transaction signed successfully"),{serialized:Buffer.from(h.serialize()).toString("base64"),settlementProbe:{kind:"solana",sourceAta:g.toBase58(),destinationAta:S.toBase58(),asset:c,amount:p,blockhash:$}}}async confirmSettlement(e,n){if(e.kind!=="solana")throw new Error(`SolanaAdapter.confirmSettlement cannot handle probe kind "${e.kind}"`);let r=new bt(n,"confirmed"),s=new ae(e.destinationAta),a=await r.getSignaturesForAddress(s,{limit:25});if(a.length===0)return{settled:!1};let o=BigInt(e.amount);for(let i of a){if(i.err)continue;let c=await r.getTransaction(i.signature,{commitment:"confirmed",maxSupportedTransactionVersion:0});if(!c?.meta)continue;let u=c.transaction.message.getAccountKeys().keySegments().flat(),p=c.meta.preTokenBalances??[],d=c.meta.postTokenBalances??[];for(let f of d){if(f.mint!==e.asset)continue;let T=u[f.accountIndex];if(!T||!T.equals(s))continue;let l=p.find(y=>y.accountIndex===f.accountIndex),P=BigInt(l?.uiTokenAmount.amount??"0");if(BigInt(f.uiTokenAmount.amount??"0")-P===o)return{settled:!0,txSignature:i.signature}}}return{settled:!1}}};function X(t){return new at(t)}var vn={PermitWitnessTransferFrom:[{name:"permitted",type:"TokenPermissions"},{name:"spender",type:"address"},{name:"nonce",type:"uint256"},{name:"deadline",type:"uint256"},{name:"witness",type:"Witness"}],TokenPermissions:[{name:"token",type:"address"},{name:"amount",type:"uint256"}],Witness:[{name:"to",type:"address"},{name:"validAfter",type:"uint256"}]},Bt=BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");function ie(t){if(!t||typeof t!="object")return!1;let e=t;return"address"in e&&typeof e.address=="string"&&e.address.startsWith("0x")}var ot=class{name="EVM";networks=[Ke,re,et,We,Be,De,Me,$e,Le,Ve];config;log;constructor(e={}){this.config=e,this.log=e.verbose?console.log.bind(console,"[x402:evm]"):()=>{}}canHandle(e){return!!(this.networks.includes(e)||e==="base"||e==="bsc"||e==="ethereum"||e==="arbitrum"||e==="polygon"||e==="optimism"||e==="avalanche"||e==="skale-base"||e==="skale-base-sepolia"||e.startsWith("eip155:"))}getDefaultRpcUrl(e){return this.config.rpcUrls?.[e]?this.config.rpcUrls[e]:B[e]?B[e]:e==="base"?B[re]:e==="bsc"?B[Ke]:e==="ethereum"?B[We]:e==="arbitrum"?B[Be]:e==="polygon"?B[De]:e==="optimism"?B[Me]:e==="avalanche"?B[$e]:e==="skale-base"?B[Le]:e==="skale-base-sepolia"?B[Ve]:B[re]}getAddress(e){return ie(e)?e.address:null}isConnected(e){return ie(e)?!!e.address:!1}getChainId(e){if(Fe[e])return Fe[e];if(e.startsWith("eip155:")){let n=e.split(":")[1];return parseInt(n,10)}return e==="base"?8453:e==="bsc"?56:e==="ethereum"?1:e==="arbitrum"?42161:8453}async getBalance(e,n,r){if(!ie(n)||!n.address)return 0;let s=r||this.getDefaultRpcUrl(e.network);try{let a=this.encodeBalanceOf(n.address),o=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_call",params:[{to:e.asset,data:a},"latest"]})});if(!o.ok)throw new Error(`RPC request failed: ${o.status}`);let i=await o.json();if(i.error)throw new Error(`RPC error: ${JSON.stringify(i.error)}`);if(!i.result||i.result==="0x")return 0;let c=BigInt(i.result),u=e.extra?.decimals??6;return Number(c)/Math.pow(10,u)}catch(a){throw a}}encodeBalanceOf(e){let n="0x70a08231",r=e.slice(2).toLowerCase().padStart(64,"0");return n+r}async confirmSettlement(e,n){if(e.kind==="eip3009"){let r="0xe94a0102",s=e.from.slice(2).toLowerCase().padStart(64,"0"),a=e.nonce.slice(2).toLowerCase().padStart(64,"0"),o=r+s+a,i=await this.ethCall(n,e.asset,o);return{settled:BigInt(i)!==0n}}if(e.kind==="permit2"){let r="0x4fe02b44",s=BigInt(e.nonce),a=s>>8n,o=s&0xffn,i=e.from.slice(2).toLowerCase().padStart(64,"0"),c=a.toString(16).padStart(64,"0"),u=r+i+c,p=await this.ethCall(n,G,u);return{settled:(BigInt(p)>>o&1n)===1n}}throw new Error(`EvmAdapter.confirmSettlement cannot handle probe kind "${e.kind}"`)}async ethCall(e,n,r){let s=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_call",params:[{to:n,data:r},"latest"]})});if(!s.ok)throw new Error(`RPC request failed: ${s.status}`);let a=await s.json();if(a.error)throw new Error(`RPC error: ${JSON.stringify(a.error)}`);if(!a.result||a.result==="0x")throw new Error("RPC returned an empty result");return a.result}async buildTransaction(e,n,r){if(!ie(n))throw new Error("Invalid EVM wallet");if(!n.address)throw new Error("Wallet not connected");if(e.scheme==="exact-approval")return this.buildApprovalTransaction(e,n,r);if(e.extra?.assetTransferMethod==="permit2")return this.buildPermit2Transaction(e,n,r);let{payTo:s,asset:a,extra:o}=e,i=e.amount??e.maxAmountRequired;if(!i)throw new Error("Missing amount in payment requirements");this.log("Building EVM transaction:",{from:n.address,to:s,amount:i,asset:a,network:e.network});let c=this.getChainId(e.network),u={name:o?.name??"USD Coin",version:o?.version??"2",chainId:BigInt(c),verifyingContract:a},p={TransferWithAuthorization:[{name:"from",type:"address"},{name:"to",type:"address"},{name:"value",type:"uint256"},{name:"validAfter",type:"uint256"},{name:"validBefore",type:"uint256"},{name:"nonce",type:"bytes32"}]},d=new Uint8Array(32);(globalThis.crypto??(await import("crypto")).webcrypto).getRandomValues(d);let f="0x"+[...d].map(g=>g.toString(16).padStart(2,"0")).join(""),T=Math.floor(Date.now()/1e3),l={from:n.address,to:s,value:i,validAfter:String(T-600),validBefore:String(T+(e.maxTimeoutSeconds||60)),nonce:f},P={from:n.address,to:s,value:BigInt(i),validAfter:BigInt(T-600),validBefore:BigInt(T+(e.maxTimeoutSeconds||60)),nonce:f};if(!n.signTypedData)throw new Error("Wallet does not support signTypedData (EIP-712)");let x=await n.signTypedData({domain:u,types:p,primaryType:"TransferWithAuthorization",message:P});return this.log("EIP-712 signature obtained"),{serialized:JSON.stringify({authorization:l,signature:x}),signature:x,settlementProbe:{kind:"eip3009",from:n.address,nonce:f,asset:a,chainId:c}}}async buildApprovalTransaction(e,n,r){let{payTo:s,asset:a,extra:o}=e,i=e.amount??e.maxAmountRequired;if(!i)throw new Error("Missing amount in payment requirements");let c=o?.facilitatorContract;if(!c)throw new Error("exact-approval scheme requires extra.facilitatorContract from the facilitator. The /supported endpoint should provide this.");if(!n.signTypedData)throw new Error("Wallet does not support signTypedData (EIP-712)");this.log("Building approval-based transaction:",{from:n.address,to:s,amount:i,asset:a,network:e.network,facilitatorContract:c});let u=r||this.getDefaultRpcUrl(e.network),p=o?.fee??"0",d=BigInt(i)+BigInt(p),f=await this.readAllowance(u,a,n.address,c);if(f<d){if(!n.sendTransaction)throw new Error("BSC payments require a wallet that supports sendTransaction for the one-time token approval. Use createEvmKeypairWallet() or a browser wallet with transaction support.");let D=this.calculateApprovalAmount(i,p,o?.approvalStrategy);this.log(`Approving ${D} for ${c} (current allowance: ${f})`);let h=await n.sendTransaction({to:a,data:this.encodeApprove(c,D),value:0n});this.log(`Approval tx sent: ${h}`),await this.waitForReceipt(u,h),this.log("Approval confirmed")}else this.log("Sufficient allowance, skipping approval");let T=new Uint8Array(16);(globalThis.crypto??(await import("crypto")).webcrypto).getRandomValues(T);let l=[...T].reduce((D,h)=>D*256n+BigInt(h),0n).toString(),P=new Uint8Array(32);(globalThis.crypto??(await import("crypto")).webcrypto).getRandomValues(P);let x="0x"+[...P].map(D=>D.toString(16).padStart(2,"0")).join(""),g=Math.floor(Date.now()/1e3)+(e.maxTimeoutSeconds||300),S=o?.eip712Domain,k=S?{name:S.name,version:S.version,chainId:BigInt(S.chainId),verifyingContract:S.verifyingContract}:{name:"DexterBSCFacilitator",version:"1",chainId:BigInt(this.getChainId(e.network)),verifyingContract:c},U=o?.eip712Types??{Payment:[{name:"from",type:"address"},{name:"to",type:"address"},{name:"token",type:"address"},{name:"amount",type:"uint256"},{name:"fee",type:"uint256"},{name:"nonce",type:"uint256"},{name:"deadline",type:"uint256"},{name:"paymentId",type:"bytes32"}]},K={from:n.address,to:s,token:a,amount:BigInt(i),fee:BigInt(p),nonce:BigInt(l),deadline:BigInt(g),paymentId:x},$=await n.signTypedData({domain:k,types:U,primaryType:"Payment",message:K});this.log("EIP-712 Payment signature obtained");let O={from:n.address,to:s,token:a,amount:i,fee:p,nonce:l,deadline:g,paymentId:x,signature:$};return{serialized:JSON.stringify(O),signature:$}}async buildPermit2Transaction(e,n,r){let{payTo:s,asset:a}=e,o=e.amount??e.maxAmountRequired;if(!o)throw new Error("Missing amount in payment requirements");if(!n.signTypedData)throw new Error("Wallet does not support signTypedData (EIP-712)");this.log("Building Permit2 transaction:",{from:n.address,to:s,amount:o,asset:a,network:e.network});let i=r||this.getDefaultRpcUrl(e.network),c=await this.readAllowance(i,a,n.address,G),u;if(c<BigInt(o)){let k=this.encodeApprove(G,Bt);if(n.signTransaction){this.log(`Signing Permit2 approval for relay (current allowance: ${c})`);let U=this.getChainId(e.network),K=await this.readGasPrice(i),$=await this.readNonce(i,n.address),O=await n.signTransaction({to:a,data:k,chainId:U,gas:50000n,gasPrice:K,nonce:$});u={erc20ApprovalGasSponsoring:{info:{from:n.address,asset:a,spender:G,amount:Bt.toString(),signedTransaction:O,version:"1"}}},this.log("Permit2 approval signed for facilitator relay")}else if(n.sendTransaction){this.log(`Approving Permit2 directly (current allowance: ${c})`);let U=await n.sendTransaction({to:a,data:k,value:0n});this.log(`Permit2 approval tx sent: ${U}`),await this.waitForReceipt(i,U),this.log("Permit2 approval confirmed")}else throw new Error("Permit2 payments require a wallet that supports signTransaction or sendTransaction for the one-time Permit2 approval. Use createEvmKeypairWallet() or a browser wallet with transaction support.")}else this.log("Sufficient Permit2 allowance, skipping approval");let p=new Uint8Array(32);(globalThis.crypto??(await import("crypto")).webcrypto).getRandomValues(p);let d=[...p].reduce((k,U)=>k*256n+BigInt(U),0n),f=Math.floor(Date.now()/1e3),T=f-600,l=f+(e.maxTimeoutSeconds||300),P=this.getChainId(e.network),x={name:"Permit2",chainId:BigInt(P),verifyingContract:G},y={permitted:{token:a,amount:BigInt(o)},spender:He,nonce:d,deadline:BigInt(l),witness:{to:s,validAfter:BigInt(T)}},g=await n.signTypedData({domain:x,types:vn,primaryType:"PermitWitnessTransferFrom",message:y});this.log("Permit2 PermitWitnessTransferFrom signature obtained");let S={signature:g,permit2Authorization:{from:n.address,permitted:{token:a,amount:o},spender:He,nonce:d.toString(),deadline:String(l),witness:{to:s,validAfter:String(T)}}};return{serialized:JSON.stringify(S),signature:g,extensions:u,settlementProbe:{kind:"permit2",from:n.address,nonce:d.toString(),chainId:P}}}async readAllowance(e,n,r,s){let a="0xdd62ed3e",o=r.slice(2).toLowerCase().padStart(64,"0"),i=s.slice(2).toLowerCase().padStart(64,"0"),c=a+o+i;try{let p=await(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_call",params:[{to:n,data:c},"latest"]})})).json();return p.error||!p.result||p.result==="0x"?0n:BigInt(p.result)}catch{return 0n}}encodeApprove(e,n){let r="0x095ea7b3",s=e.slice(2).toLowerCase().padStart(64,"0"),a=n.toString(16).padStart(64,"0");return r+s+a}async waitForReceipt(e,n,r=3e4){let s=Date.now();for(;Date.now()-s<r;){try{let o=await(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_getTransactionReceipt",params:[n]})})).json();if(o.result){if(o.result.status==="0x0")throw new Error(`Approval transaction reverted: ${n}`);return}}catch(a){if(a instanceof Error&&a.message.includes("reverted"))throw a}await new Promise(a=>setTimeout(a,2e3))}throw new Error(`Approval transaction receipt timeout after ${r}ms: ${n}`)}async readGasPrice(e){try{let r=await(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_gasPrice",params:[]})})).json();return r.result?BigInt(r.result):50000000n}catch{return 50000000n}}async readNonce(e,n){try{let s=await(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_getTransactionCount",params:[n,"latest"]})})).json();return s.result?parseInt(s.result,16):0}catch{return 0}}calculateApprovalAmount(e,n,r){let s=BigInt(e)+BigInt(n);if(!r||r.mode==="exact")return s;let a=BigInt(r.defaultMultiple??10),o=s*a;if(r.maxCapUsd){let i=this.inferDecimals(e),c=BigInt(Math.floor(r.maxCapUsd*Math.pow(10,i)));if(o>c)return c}if(r.exactAboveUsd){let i=this.inferDecimals(e),c=BigInt(Math.floor(r.exactAboveUsd*Math.pow(10,i)));if(BigInt(e)>c)return s}return o}inferDecimals(e){return e.length>12?18:6}};function Ee(t){return new ot(t)}function wt(t){if(t===qe||t===It)return!0;let e=t.toLowerCase();for(let n of Object.values(nt))if(n.toLowerCase()===e)return!0;for(let n of Object.keys(rt))if(n.toLowerCase()===e)return!0;return!1}function Pn(t){return{[Ne]:"Solana",[Qe]:"Solana Devnet",[Ze]:"Solana Testnet",solana:"Solana","solana-devnet":"Solana Devnet","solana-testnet":"Solana Testnet",[ne]:"Base",[ge]:"Base Sepolia",[xe]:"Ethereum",[ye]:"Arbitrum",[he]:"Polygon",[Ae]:"Optimism",[be]:"Avalanche",[Se]:"BSC",[we]:"SKALE Base",[Te]:"SKALE Base Sepolia",base:"Base","base-sepolia":"Base Sepolia",ethereum:"Ethereum",arbitrum:"Arbitrum",polygon:"Polygon",optimism:"Optimism",avalanche:"Avalanche",bsc:"BSC","skale-base":"SKALE Base","skale-base-sepolia":"SKALE Base Sepolia"}[t]||t}function Dt(t,e){let n=Pn(t);return n===t?e:n}async function Rn(t,e){if(typeof t=="string")return[t,e];if(t instanceof URL)return[t.href,e];let n=t,r=new Headers(n.headers);e?.headers&&new Headers(e.headers).forEach((o,i)=>r.set(i,o));let s={method:e?.method??n.method,headers:r,signal:e?.signal??n.signal,redirect:e?.redirect??n.redirect,credentials:e?.credentials??n.credentials},a=(s.method??"GET").toUpperCase();return e&&"body"in e?s.body=e.body:a!=="GET"&&a!=="HEAD"&&n.body&&(s.body=await n.arrayBuffer()),[n.url,s]}var _n=new WeakMap;function Mt(t){let{adapters:e=[X({verbose:t.verbose}),Ee({verbose:t.verbose})],wallets:n,wallet:r,preferredNetwork:s,rpcUrls:a={},maxAmountAtomic:o,fetch:i=globalThis.fetch,verbose:c=!1,accessPass:u,onPaymentRequired:p,onPaymentDispatched:d,maxRetries:f=0,retryDelayMs:T=500}=t,l=c?console.log.bind(console,"[x402]"):()=>{};async function P(h,w){let E;for(let A=0;A<=f;A++)try{let v=await i(h,w);if(v.status>=502&&v.status<=504&&A<f){l(`Retry ${A+1}/${f}: server returned ${v.status}`),await new Promise(R=>setTimeout(R,T*Math.pow(2,A)));continue}return v}catch(v){E=v,A<f&&(l(`Retry ${A+1}/${f}: ${v instanceof Error?v.message:"network error"}`),await new Promise(R=>setTimeout(R,T*Math.pow(2,A))))}throw E}let x=new Map;function y(h){try{let w=new URL(h).host,E=x.get(w);if(E&&E.expiresAt>Date.now()/1e3+10)return E.jwt;E&&x.delete(w)}catch{}return null}function g(h,w){try{let E=new URL(h).host,A=w.split(".");if(A.length===3){let v=JSON.parse(atob(A[1].replace(/-/g,"+").replace(/_/g,"/"))),R=Math.floor(Date.now()/1e3),_=R+86400,L=Math.min(typeof v.exp=="number"?v.exp:R,_);x.set(E,{jwt:w,expiresAt:L}),l("Access pass cached for",E,"| expires:",new Date(L*1e3).toISOString())}}catch{l("Failed to cache access pass")}}let S=n||{};r&&!S.solana&&oe(r)&&(S.solana=r),r&&!S.evm&&ie(r)&&(S.evm=r);function k(h){let w=[];for(let E of h){let A=E.scheme??"exact";if(A!=="exact"&&A!=="exact-approval")continue;let v=e.find(_=>_.canHandle(E.network));if(!v)continue;let R;v.name==="Solana"?R=S.solana:v.name==="EVM"&&(R=S.evm),R&&v.isConnected(R)&&w.push({accept:E,adapter:v,wallet:R})}if(w.length===0)return null;if(s){let E=w.find(A=>A.accept.network===s);if(E)return E}return w[0]}function U(h,w){return Dt(h,w)}function K(h,w){return a[h]||w.getDefaultRpcUrl(h)}async function $(h,w,E,A,v){let R="";if(u?.preferTier&&A.tiers){let m=A.tiers.find(b=>b.id===u.preferTier);if(m){if(u.maxSpend&&parseFloat(m.price)>parseFloat(u.maxSpend))throw new I("access_pass_exceeds_max_spend",`Access pass tier "${m.id}" costs $${m.price}, exceeds max spend $${u.maxSpend}`);R=`tier=${m.id}`}}else if(u?.preferDuration&&A.ratePerHour)R=`duration=${u.preferDuration}`;else if(A.tiers&&A.tiers.length>0){let m=A.tiers[0];if(u?.maxSpend&&parseFloat(m.price)>parseFloat(u.maxSpend))throw new I("access_pass_exceeds_max_spend",`Cheapest access pass costs $${m.price}, exceeds max spend $${u?.maxSpend}`);R=`tier=${m.id}`}let _=R?v.includes("?")?`${v}&${R}`:`${v}?${R}`:v;l("Purchasing access pass:",R||"default tier");let L=E.headers.get("PAYMENT-REQUIRED");if(!L)return null;let M;try{M=JSON.parse(atob(L))}catch{return null}let N=k(M.accepts);if(!N)return null;let{accept:C,adapter:W,wallet:ce}=N;if(W.name==="Solana"&&!C.extra?.feePayer)return null;let H=C.extra?.decimals??(wt(C.asset)?6:void 0);if(typeof H!="number")return null;let ve=C.amount??C.maxAmountRequired;if(!ve)return null;let F=K(C.network,W);try{let m=await W.getBalance(C,ce,F),b=Number(ve)/Math.pow(10,H);if(m<b){let ut=U(C.network,W.name);throw new I("insufficient_balance",`Insufficient balance for access pass on ${ut}. Have $${m.toFixed(4)}, need $${b.toFixed(4)}`)}}catch(m){if(m instanceof I)throw m}let j=await W.buildTransaction(C,ce,F),le;W.name==="EVM"?le=JSON.parse(j.serialized):le={transaction:j.serialized};let Y=typeof h=="string"?h:h instanceof URL?h.href:h.url,ue=M.resource;if(typeof M.resource=="string")try{let m=new URL(M.resource,Y);["http:","https:"].includes(m.protocol)&&(ue=m.toString())}catch{}else if(M.resource&&typeof M.resource=="object"&&"url"in M.resource){let m=M.resource;try{let b=new URL(m.url,Y);["http:","https:"].includes(b.protocol)&&(ue={...m,url:b.toString()})}catch{}}let Je={x402Version:C.x402Version??2,resource:ue,accepted:C,payload:le};j.extensions&&(Je.extensions=j.extensions);let J=btoa(JSON.stringify(Je)),z=await i(_,{...w,method:"POST",headers:{...w?.headers||{},"Content-Type":"application/json","PAYMENT-SIGNATURE":J}});if(!z.ok)return l("Pass purchase failed:",z.status),null;let V=z.headers.get("ACCESS-PASS");return V&&(g(v,V),l("Access pass purchased and cached")),z}async function O(h,w){let E=h;if(l("Making request:",E),u){let m=y(E);if(m){l("Using cached access pass");let b=await i(h,{...w,headers:{...w?.headers||{},Authorization:`Bearer ${m}`}});if(b.status!==401&&b.status!==402)return b;l("Cached pass rejected (status",b.status,"), purchasing new pass");try{x.delete(new URL(E).host)}catch{}}}let A=await P(h,w);if(A.status!==402)return A;l("Received 402 Payment Required");let v=A.headers.get("X-ACCESS-PASS-TIERS");if(u&&v){l("Server offers access passes, purchasing...");try{let m=JSON.parse(atob(v)),b=await $(h,w,A,m,E);if(b)return b}catch(m){l("Access pass purchase failed, falling back to per-request payment:",m)}}let R=A.headers.get("PAYMENT-REQUIRED");if(!R)throw new I("missing_payment_required_header","Server returned 402 but no PAYMENT-REQUIRED header");let _;try{let m=atob(R);_=JSON.parse(m)}catch{throw new I("invalid_payment_required","Failed to decode PAYMENT-REQUIRED header")}l("Payment requirements:",_);let L=A.headers.get("X-Quote-Hash");L&&l("Quote hash received:",L);let M=k(_.accepts);if(!M){let m=_.accepts.map(b=>b.network).join(", ");throw new I("no_matching_payment_option",`No connected wallet for any available network: ${m}`)}let{accept:N,adapter:C,wallet:W}=M;if(l(`Using ${C.name} for ${N.network}`),C.name==="Solana"&&!N.extra?.feePayer)throw new I("missing_fee_payer","Solana payment option missing feePayer in extra");let ce=N.extra?.decimals??(wt(N.asset)?6:void 0);if(typeof ce!="number")throw new I("missing_decimals","Payment option missing decimals - provide in extra or use a known stablecoin");let H=N.amount??N.maxAmountRequired;if(!H)throw new I("missing_amount","Payment option missing amount");if(o&&BigInt(H)>BigInt(o))throw new I("amount_exceeds_max",`Payment amount ${H} exceeds maximum ${o}`);let ve=K(N.network,C);l("Checking balance...");try{let m=await C.getBalance(N,W,ve),b=Number(H)/Math.pow(10,ce);if(m<b){let ut=U(N.network,C.name);throw new I("insufficient_balance",`Insufficient balance on ${ut}. Have $${m.toFixed(4)}, need $${b.toFixed(4)}`)}l(`Balance OK: $${m.toFixed(4)} >= $${b.toFixed(4)}`)}catch(m){if(m instanceof I)throw m;l("Balance check failed (RPC error), proceeding with transaction attempt")}if(p&&!await p(N))throw new I("payment_rejected","Payment rejected by onPaymentRequired callback");l("Building transaction...");let F=await C.buildTransaction(N,W,ve);l("Transaction signed");let j;C.name==="EVM"?j=JSON.parse(F.serialized):j={transaction:F.serialized};let le=h,Y=_.resource;if(typeof _.resource=="string")try{let m=new URL(_.resource,le).toString();m!==_.resource&&l("Resolved relative resource URL:",_.resource,"\u2192",m),Y=m}catch{Y=_.resource}else if(_.resource&&typeof _.resource=="object"&&"url"in _.resource){let m=_.resource;try{let b=new URL(m.url,le).toString();b!==m.url&&(l("Resolved relative resource URL:",m.url,"\u2192",b),Y={...m,url:b})}catch{}}let ue={x402Version:N.x402Version??2,resource:Y,accepted:N,payload:j};F.extensions&&(ue.extensions=F.extensions);let Je=btoa(JSON.stringify(ue));if(d)try{d(N,F.settlementProbe)}catch{}l("Retrying request with payment...");let J=await P(h,{...w,headers:{...w?.headers||{},"PAYMENT-SIGNATURE":Je,...L?{"X-Quote-Hash":L}:{}}});if(l("Retry response status:",J.status),J.status===402){let m="unknown";try{let b=await J.clone().json();m=String(b.error||b.message||JSON.stringify(b)),l("Rejection reason:",m)}catch{}throw new I("payment_rejected",`Payment was rejected by the server: ${m}`)}let z=J.headers.get("PAYMENT-RESPONSE"),V;if(z)try{V=JSON.parse(atob(z))}catch{}return V??={},V.amountAtomic=H,V.assetDecimals=ce,_n.set(J,V),V.extensions&&l("Settlement extensions:",Object.keys(V.extensions).join(", ")),J}async function D(h,w){let[E,A]=await Rn(h,w);return O(E,A)}return{fetch:D}}function q(t){if(t instanceof Error){if(t.message&&t.message.length>0)return t.message;if(t.name&&t.name.length>0)return t.name}let e=String(t);return e.length>0?e:"unknown error"}async function je(t){let e="";try{e=(await t.clone().text()).slice(0,600)}catch{}let n=e.toLowerCase(),r=n.includes("settle")||n.includes("facilitator"),s=e;try{let a=JSON.parse(e),o=[a.error,a.detail,a.message].filter(i=>typeof i=="string"&&i.length>0);o.length>0&&(s=o.join(" \u2014 "))}catch{}return s||(s=`HTTP ${t.status}`),{reason:r?"settlement_failed":"merchant_rejected",detail:`merchant HTTP ${t.status}: ${s}`}}var it="Payment authorization was sent, but the merchant did not respond within the timeout. ",ct=" Do not retry without checking \u2014 inspect the funding wallet for a transfer to the merchant before attempting payment again.";async function lt(t,e,n){if(!t)return{confirmed:!1,detail:it+"This payment scheme has no on-chain confirmation check, so the SDK cannot verify whether it settled."+ct};try{if(t.kind==="solana"){let o=X(),i=n??o.getDefaultRpcUrl(e.caip2);if(!o.confirmSettlement)return Kt();let c=await o.confirmSettlement(t,i);return c.settled?{confirmed:!0,txSignature:c.txSignature}:$t()}let r=Ee(),s=r.getDefaultRpcUrl(e.caip2);if(!r.confirmSettlement)return Kt();let a=await r.confirmSettlement(t,s);return a.settled?{confirmed:!0,txSignature:a.txSignature}:$t()}catch(r){return{confirmed:!1,detail:it+`On-chain confirmation could not be completed (${q(r)}).`+ct}}}function $t(){return{confirmed:!1,detail:it+"On-chain confirmation found no matching settlement yet \u2014 the payment may still be pending, or may not have settled."+ct}}function Kt(){return{confirmed:!1,detail:it+"The chain adapter does not support on-chain confirmation."+ct}}var kn=new Set(["exact","exact-approval"]);async function Cn(t,e,n,r){let s;try{s=await r.signNextVoucher(n.amount)}catch{return null}let a=new Headers(e.headers??void 0);a.set("X-Tab-Voucher",Ye(s));let o={method:e.method??"GET",headers:a};e.signal&&(o.signal=e.signal),typeof e.body=="string"&&(o.body=e.body);let i;try{i=await fetch(t,o)}catch(c){return{ok:!1,reason:"error",detail:c?.message??String(c)}}return i.status===402?(r.rollbackVoucher?.call(r,s),null):i.ok?{ok:!0,paid:!0,response:i,amountPaid:n.amount,network:n.network}:{ok:!1,...await je(i)}}var Lt={version:2,async parseChallenge(t){return ze(t)},async pay(t,e,n,r,s){if(s.tab){let y=n.options.find(g=>g.scheme==="tab"&&g.network.family==="svm"&&g.payTo===s.tab.counterparty);if(y){let g=await Cn(t,e,y,s.tab);if(g)return g}}let a=n.options.filter(y=>kn.has(y.scheme));if(a.length===0)return{ok:!1,reason:"no_payment_options",detail:`no generically payable scheme offered (got: ${n.options.map(y=>y.scheme).join(", ")})`};let o=a.find(y=>y.network.family==="evm"?!!r.evm:y.network.family==="svm"?!!r.solana:!1);if(!o)return{ok:!1,reason:"unsupported_network"};let i=s.timeoutMs??15e3,c=s.responseTimeoutMs??12e4,u=new AbortController,p=setTimeout(()=>u.abort(),i),d=!1,f,T=(y,g)=>{d=!0,f=g,clearTimeout(p),p=setTimeout(()=>u.abort(),c)},l=Mt({wallets:r,preferredNetwork:o.network.caip2,maxAmountAtomic:s.maxAmountAtomic,fetch:globalThis.fetch,onPaymentDispatched:T}),P=e.signal?AbortSignal.any([e.signal,u.signal]):u.signal,x={method:e.method??"GET",headers:e.headers,signal:P};typeof e.body=="string"&&(x.body=e.body);try{let y=await l.fetch(t,x);if(clearTimeout(p),!y.ok)return{ok:!1,...await je(y)};let g,S=y.headers.get("PAYMENT-RESPONSE");if(S)try{let k=ht(S);k&&typeof k.transaction=="string"&&(g=k.transaction)}catch{}return{ok:!0,paid:!0,response:y,amountPaid:o.amount,network:o.network,txSignature:g}}catch(y){clearTimeout(p);let g=y;if(g?.name==="AbortError"){if(!d)return{ok:!1,reason:"timeout"};let S=await lt(f,o.network,s.solanaRpcUrl);return S.confirmed?{ok:!0,paid:!0,response:void 0,amountPaid:o.amount,network:o.network,txSignature:S.txSignature}:{ok:!1,reason:"payment_unconfirmed",detail:S.detail}}return{ok:!1,reason:"error",detail:g?.message??String(y)}}}};import{getAddress as In}from"viem";var Nn={TransferWithAuthorization:[{name:"from",type:"address"},{name:"to",type:"address"},{name:"value",type:"uint256"},{name:"validAfter",type:"uint256"},{name:"validBefore",type:"uint256"},{name:"nonce",type:"bytes32"}]};async function On(){let t=globalThis.crypto??(await import("crypto")).webcrypto,e=new Uint8Array(32);return t.getRandomValues(e),"0x"+Array.from(e,n=>n.toString(16).padStart(2,"0")).join("")}async function Un(t,e,n){if(typeof t.signTypedData!="function")throw new Error("EVM wallet does not support signTypedData");let r=Math.floor(Date.now()/1e3),s=String(r-600),a=String(r+(e.maxTimeoutSeconds??60)),o={from:t.address,to:e.payTo,value:e.amount,validAfter:s,validBefore:a,nonce:await On()},i=Fe[e.network.caip2];if(i===void 0)throw new Error(`unknown chain id for network ${e.network.caip2}`);let c=e.extra,u=await t.signTypedData({domain:{name:c.name,version:c.version,chainId:i,verifyingContract:In(e.asset)},types:Nn,primaryType:"TransferWithAuthorization",message:o});return{payment:{x402Version:1,scheme:e.scheme,network:n,payload:{signature:u,authorization:o}},settlementProbe:{kind:"eip3009",from:o.from,nonce:o.nonce,asset:e.asset,chainId:i}}}async function Tt(t,e,n){try{let r;for(let s of t.options){let a=s.network.family==="evm"&&!!e.evm||s.network.family==="svm"&&!!e.solana;if(s.scheme!=="exact"){a&&(r=s.scheme);continue}if(s.network.family==="evm"&&e.evm){let o=await e.evm;return await Bn(s,o,n)}if(s.network.family==="svm"&&e.solana){let o=await e.solana;return await Dn(s,o,n)}}return r!==void 0?{ok:!1,reason:"merchant_rejected",detail:`v1 supports only the 'exact' scheme, got '${r}'`}:{ok:!1,reason:"unsupported_network"}}catch(r){return{ok:!1,reason:"error",detail:q(r)}}}async function Bn(t,e,n){if(n.maxAmountAtomic!==void 0&&BigInt(t.amount)>BigInt(n.maxAmountAtomic))return{ok:!1,reason:"budget_exceeded"};let r=t.extra??{},s=r.name,a=r.version;if(typeof s!="string"||s.length===0||typeof a!="string"||a.length===0)return{ok:!1,reason:"merchant_rejected",detail:"v1 challenge missing exact-scheme EIP-712 domain (extra.name / extra.version)"};let o=t.network.bare,{payment:i,settlementProbe:c}=await Un(e,t,o);return{ok:!0,headerValue:Buffer.from(JSON.stringify(i),"utf8").toString("base64"),option:t,settlementProbe:c}}async function Dn(t,e,n){if(n.maxAmountAtomic!==void 0&&BigInt(t.amount)>BigInt(n.maxAmountAtomic))return{ok:!1,reason:"budget_exceeded"};if(t.scheme!=="exact")return{ok:!1,reason:"merchant_rejected",detail:`v1 SVM supports only the 'exact' scheme, got '${t.scheme}'`};let r=t.extra??{};if(typeof r.feePayer!="string"||r.feePayer.length===0)return{ok:!1,reason:"merchant_rejected",detail:"v1 SVM challenge missing extra.feePayer (required as the transaction fee payer)"};let s=t.network.bare,a={x402Version:1,scheme:t.scheme,network:s,asset:t.asset,payTo:t.payTo,amount:t.amount,maxAmountRequired:t.amount,maxTimeoutSeconds:t.maxTimeoutSeconds??60,extra:r},i=await X().buildTransaction(a,e,n.solanaRpcUrl),c={x402Version:1,scheme:t.scheme,network:s,payload:{transaction:i.serialized}};return{ok:!0,headerValue:Buffer.from(JSON.stringify(c),"utf8").toString("base64"),option:t,settlementProbe:i.settlementProbe}}function Mn(t){let e=[];for(let n of t){if(!n||typeof n!="object")continue;let r=n,s=Ce(String(r.network??""));s&&e.push({scheme:String(r.scheme??"exact"),network:s,amount:String(r.maxAmountRequired??r.amount??"0"),asset:String(r.asset??""),payTo:String(r.payTo??""),maxTimeoutSeconds:typeof r.maxTimeoutSeconds=="number"?r.maxTimeoutSeconds:void 0,extra:r.extra&&typeof r.extra=="object"?r.extra:void 0})}return e}var Vt={version:1,async parseChallenge(t){if(t.headers.get("payment-required"))return null;let e;try{e=await t.clone().json()}catch{return null}let n=Array.isArray(e.accepts)?e.accepts:[];if(n.length===0)return null;let r=Mn(n);return r.length===0?null:{x402Version:1,options:r}},async pay(t,e,n,r,s){let a,o=new AbortController,i=!1,c,u;try{let p=s.timeoutMs??15e3;a=setTimeout(()=>o.abort(),p);let d=await Tt(n,r,s);if(!d.ok)return{ok:!1,reason:d.reason,detail:d.detail};let f=d.headerValue,T=d.option;u=T,c=d.settlementProbe;let l=new Headers(e.headers??void 0);l.set("X-PAYMENT",f);let P=e.signal!=null?AbortSignal.any([e.signal,o.signal]):o.signal,x={method:e.method,headers:l,signal:P};typeof e.body=="string"&&(x.body=e.body),i=!0,clearTimeout(a);let y=s.responseTimeoutMs??12e4;a=setTimeout(()=>o.abort(),y);let g=await fetch(t,x);return g.ok?{ok:!0,paid:!0,response:g,amountPaid:T.amount,network:T.network,txSignature:$n(g)}:{ok:!1,...await je(g)}}catch(p){if(p instanceof Error&&p.name==="AbortError"){if(!i)return{ok:!1,reason:"timeout"};let d=await lt(c,u.network,s.solanaRpcUrl);return d.confirmed?{ok:!0,paid:!0,response:void 0,amountPaid:u.amount,network:u.network,txSignature:d.txSignature}:{ok:!1,reason:"payment_unconfirmed",detail:d.detail}}return{ok:!1,reason:"error",detail:q(p)}}finally{a!==void 0&&clearTimeout(a)}}};function $n(t){let e=t.headers.get("x-payment-response")??t.headers.get("X-PAYMENT-RESPONSE");if(e)try{let n=JSON.parse(Buffer.from(e,"base64").toString("utf8")),r=n.transaction??n.txHash??n.transactionHash;return typeof r=="string"?r:void 0}catch{return}}import*as qt from"tweetnacl";import{Keypair as gs,VersionedTransaction as ys,Transaction as hs}from"@solana/web3.js";var Wt=Symbol.for("x402:keypair");function xt(t){let e=t.evm;if(e&&typeof e.signMessage=="function"&&typeof e.address=="string")return{address:e.address,signMessage:e.signMessage};let n=t.solana;if(n){let r=n[Wt];if(r&&r.secretKey&&r.publicKey)return{publicKey:r.publicKey,signMessage:async s=>qt.sign.detached(s,r.secretKey)}}return null}var Kn=[Lt,Vt];async function Ln(t){let e=xt(t);if(!e)return fetch;try{return(await import("@x402/extensions/sign-in-with-x")).wrapFetchWithSIWx(fetch,e)}catch(n){return console.warn(`[x402] SIW-X unavailable \u2014 @x402/extensions failed to load; SIW-X merchants will not authenticate. ${q(n)}`),fetch}}async function Et(t,e,n,r){if(e.body!==void 0&&e.body!==null&&typeof e.body!="string")return{ok:!1,reason:"error",detail:"payAndFetch requires a string body; non-string bodies (Buffer, FormData, URLSearchParams, ReadableStream) cannot be safely re-sent on the paid retry"};let s;try{s=await(await Ln(n))(t,{...e})}catch(a){return{ok:!1,reason:"error",detail:q(a)}}if(s.status!==402)return{ok:!0,paid:!1,response:s};for(let a of Kn){let o=await a.parseChallenge(s.clone());if(o)return a.pay(t,e,o,n,r)}return{ok:!1,reason:"no_payment_options"}}async function Vn(t,e={},n){let r=await Ie(t,e);if(r.kind==="free")return{result:{ok:!0,paid:!1,response:r.response},tab:null};if(r.kind==="no_tab")return{result:{ok:!1,reason:"no_payment_options",detail:`no tab option offered (schemes: ${r.schemesOffered.join(", ")})`},tab:null};if(r.kind==="error")return{result:{ok:!1,reason:"error",detail:r.detail},tab:null};let{offer:s}=r,a=BigInt(Z(n.perUnitCap));if(BigInt(s.amountAtomic)>a)return{result:{ok:!1,reason:"budget_exceeded",detail:`seller quotes ${s.amountAtomic} atomic; perUnitCap allows ${a}`},tab:null};let o=n.tabs?.get(s.payTo);return(!o||!o.state.isOpen)&&(o=await ft({vault:n.vault,network:"solana:mainnet",seller:s.payTo,perUnitCap:n.perUnitCap,totalCap:n.totalCap,sessionDuration:n.sessionDuration,facilitatorUrl:n.facilitatorUrl}),n.tabs?.set(s.payTo,o)),{result:await Et(t,e,{},{tab:o}),tab:o}}function Wn(t){return{counterparty:t.payTo,perRequest:{atomic:t.amountAtomic,human:de(t.amountAtomic)},asset:t.asset,network:{caip2:t.networkCaip2},scheme:"tab",settlement:{custody:"non-custodial",protection:"lock",settleOn:"close"},credit:null,...t.resourceUrl!==void 0?{resourceUrl:t.resourceUrl}:{}}}async function qn(t,e={},n={}){let r=n.cache?.get(t);if(r)return{kind:"terms",terms:r};let s=await Ie(t,e,n.fetchImpl??fetch);if(s.kind!=="offer")return s;if(s.offer.asset!==qe)return{kind:"error",detail:`tab offer asset is not USDC ("${s.offer.asset}"); refusing to render a human price`};let a=Wn(s.offer);return n.cache?.set(t,a),{kind:"terms",terms:a}}export{Pe as DEFAULT_FACILITATOR_URL,gt as DEXTER_VAULT_PROGRAM_ID,an as INSTRUCTIONS_SYSVAR_ID,sn as SECP256R1_PROGRAM_ID,pe as SessionScopeExceededError,Q as TabClosedError,me as UnsupportedNetworkError,de as atomicToHuman,tn as buildRegisterSessionKeyInstruction,nn as buildRevokeSessionKeyInstruction,rn as buildSecp256r1VerifyInstruction,Ft as buildVoucherMessage,Z as humanToAtomic,ft as openTab,Vn as payUrlWithTab,Ie as resolveTabOffer,qn as resolveTabTerms,zt as resumeTab,mt as sessionRegisterMessage,Ht as sessionRevokeMessage,pn as tabFromGrant,pt as voucherPayloadMessage,Ye as voucherToHeader};
|
|
6
|
+
`):null}}import ke from"tweetnacl";import{Connection as cn,PublicKey as _e}from"@solana/web3.js";import{bytesToHex as ln}from"@noble/hashes/utils";import{fetchSessionAccount as un,isSessionLive as mn}from"@dexterai/vault/session";import{readVaultFull as pn}from"@dexterai/vault/reader";import{buildRegisterSessionKeyInstruction as nn,buildRevokeSessionKeyInstruction as rn,deriveSwigWalletAddress as or}from"@dexterai/vault/instructions";import{buildSecp256r1VerifyInstruction as sn}from"@dexterai/vault/precompile";import{DEXTER_VAULT_PROGRAM_ID as yt,SECP256R1_PROGRAM_ID as on,INSTRUCTIONS_SYSVAR_ID as an}from"@dexterai/vault/constants";var ht="solana:mainnet";async function dn(t){let e=t.facilitatorUrl??Pe,n=t.programId??yt,r=new _e(t.vaultPda),s=new _e(t.params.counterparty),o=ze(t.perUnitCapAtomic);if(o<=0n)throw new Error(`perUnitCapAtomic must be > 0, got ${t.perUnitCapAtomic}`);let a=ze(t.params.maxAmountAtomic),i=ze(t.params.maxRevolvingCapacityAtomic),c=fn(t.sessionSecretKey),u=new _e(t.params.sessionPubkey).toBytes();if(!Ct(c.publicKey,u))throw new Error(`tab_session_key_mismatch: the supplied secret derives ${new _e(c.publicKey).toBase58()} but params.sessionPubkey is ${t.params.sessionPubkey} \u2014 wrong key handed to this process`);let p=ke.randomBytes(32),d=ke.sign.detached(p,c.secretKey);if(!ke.sign.detached.verify(p,d,u))throw new Error("tab_session_key_mismatch: secret key failed the sign/verify self-check against params.sessionPubkey \u2014 the seed half does not sign for the granted pubkey");let f=pt({programId:n,vaultPda:r,sessionPubkey:c.publicKey,maxAmount:a,expiresAt:BigInt(t.params.expiresAtUnix),allowedCounterparty:s,nonce:t.params.nonce,maxRevolvingCapacity:i}),x=typeof t.connection=="string"?new cn(t.connection,"confirmed"):t.connection,l=await un(x,r,s,n);if(!l||l.version===0)throw new Error("tab_session_not_live: no live SessionAccount PDA for this (vault, counterparty) \u2014 revoked, expiry-swept, or never registered");if(!mn(l))throw new Error(l.version!==1?`tab_session_not_live: SessionAccount has unsupported version ${l.version} \u2014 not a live v1 session`:`tab_session_not_live: SessionAccount is present but expired (expiresAt=${l.session.expiresAt})`);if(!Ct(l.session.sessionPubkey,c.publicKey))throw new Error(`tab_session_pubkey_mismatch: the on-chain session carries ${new _e(l.session.sessionPubkey).toBase58()} \u2014 the grant this key belongs to was replaced by a newer registration`);if(l.session.maxAmount!==a||l.session.expiresAt!==t.params.expiresAtUnix||l.session.nonce!==t.params.nonce||l.session.maxRevolvingCapacity!==i)throw new Error(`tab_grant_params_stale: params disagree with the on-chain session scope (chain: maxAmount=${l.session.maxAmount} expiresAt=${l.session.expiresAt} nonce=${l.session.nonce} revolving=${l.session.maxRevolvingCapacity}; params: ${t.params.maxAmountAtomic}/${t.params.expiresAtUnix}/${t.params.nonce}/${t.params.maxRevolvingCapacityAtomic}) \u2014 refresh the grant hand-off`);let P=l.session.spent>l.session.crystallizedCumulative?l.session.spent:l.session.crystallizedCumulative;if(P>=a)throw new Error(`tab_exhausted: on-chain frontier ${P} has consumed the session cap ${a} \u2014 no voucher can strictly exceed it; open a new grant`);let T=t.sellerUrl??t.params.counterparty,y=BigInt(Math.floor(Math.random()*4294967295)),g=Ge({vaultPda:r,sellerUrl:T,nonce:y}),S=ln(g),k=t.swigAddress;if(!k){let O=await pn(x,r);if(!O.exists||!O.swigAddress)throw new Error(`tab_vault_unreadable: vault ${r.toBase58()} has no readable swig_address on chain \u2014 pass options.swigAddress explicitly`);k=O.swigAddress}await ft(e,k,a,ht,t.params.counterparty);let U={channelId:S,maxAmountAtomic:t.params.maxAmountAtomic,revolvingCapacityAtomic:t.params.maxRevolvingCapacityAtomic,expiresAtUnix:t.params.expiresAtUnix,allowedCounterparty:t.params.counterparty},K=Pt({publicKey:c.publicKey,privateKey:c.secretKey},U,f),$={network:ht,swigAddress:k,vaultPda:r.toBase58(),authorizeSession:async()=>{throw new Error("grant_tab_no_passkey: a grant-held tab cannot authorize sessions \u2014 session birth requires the wallet owner's passkey ceremony")},signWithSession:async(O,D)=>Rt(O,D,g),signOpenTab:async O=>O.registration,signCloseTab:async()=>{throw new Error("grant_tab_cannot_revoke: session revocation is passkey-signed and belongs to the wallet owner's surfaces \u2014 a grant-held tab closes settle-only")}};return new Re({vault:$,network:ht,seller:T,counterparty:t.params.counterparty,session:K,channelIdHex:S,channelIdBytes:g,perUnitCapAtomic:o,totalCapAtomic:a,expiresAtUnix:t.params.expiresAtUnix,facilitatorUrl:e,initialCumulativeAtomic:P,closeMode:"settle-only"})}function fn(t){if(t.length===64)return ke.sign.keyPair.fromSecretKey(t);if(t.length===32)return ke.sign.keyPair.fromSeed(t);throw new Error(`tab_session_key_invalid: expected a 64-byte nacl secretKey or a 32-byte ed25519 seed, got ${t.length} bytes`)}function Ct(t,e){if(t.length!==e.length)return!1;let n=0;for(let r=0;r<t.length;r++)n|=t[r]^e[r];return n===0}import{PublicKey as An}from"@solana/web3.js";var It=[{caip2:"eip155:8453",bare:"base",family:"evm"},{caip2:"eip155:1",bare:"ethereum",family:"evm"},{caip2:"eip155:137",bare:"polygon",family:"evm"},{caip2:"eip155:42161",bare:"arbitrum",family:"evm"},{caip2:"eip155:10",bare:"optimism",family:"evm"},{caip2:"eip155:43114",bare:"avalanche",family:"evm"},{caip2:"eip155:56",bare:"bsc",family:"evm"},{caip2:"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",bare:"solana",family:"svm"}],gn=new Map(It.map(t=>[t.caip2.toLowerCase(),t])),yn=new Map(It.map(t=>[t.bare.toLowerCase(),t]));function Ce(t){if(!t)return null;let e=t.toLowerCase(),n=gn.get(e)||yn.get(e);return n?{caip2:n.caip2,bare:n.bare,family:n.family}:null}function At(t){let e=t.replace(/-/g,"+").replace(/_/g,"/"),n=e+"=".repeat((4-(e.length%4||4))%4);return JSON.parse(Buffer.from(n,"base64").toString("utf8"))}function hn(t){let e=[];for(let n of t){if(!n||typeof n!="object")continue;let r=n,s=Ce(String(r.network??""));s&&e.push({scheme:String(r.scheme??"exact"),network:s,amount:String(r.amount??r.maxAmountRequired??"0"),asset:String(r.asset??""),payTo:String(r.payTo??""),maxTimeoutSeconds:typeof r.maxTimeoutSeconds=="number"?r.maxTimeoutSeconds:void 0,extra:r.extra&&typeof r.extra=="object"?r.extra:void 0})}return e}async function Ye(t){let e=t.headers.get("payment-required");if(!e)return null;let n;try{n=At(e)}catch{return null}let r=Array.isArray(n.accepts)?n.accepts:[];return r.length===0?null:{x402Version:2,options:hn(r),resourceUrl:n.resource&&typeof n.resource=="object"?String(n.resource.url??""):void 0}}async function Ie(t,e={},n=fetch){let r={method:e.method??"GET",headers:e.headers};typeof e.body=="string"&&(r.body=e.body);let s;try{s=await n(t,r)}catch(i){return{kind:"error",detail:`probe failed: ${i?.message??String(i)}`}}if(s.status!==402)return s.ok?{kind:"free",response:s}:{kind:"error",detail:`probe returned HTTP ${s.status}`};let o=await Ye(s);if(!o)return{kind:"error",detail:"the 402 carries no x402 v2 PAYMENT-REQUIRED challenge"};let a=o.options.find(i=>i.scheme==="tab"&&i.network.family==="svm");if(!a)return{kind:"no_tab",schemesOffered:o.options.map(i=>i.scheme)};try{new An(a.payTo)}catch{return{kind:"error",detail:`tab option payTo is not a valid Solana pubkey: "${a.payTo}"`}}return{kind:"offer",offer:{payTo:a.payTo,amountAtomic:a.amount,asset:a.asset,networkCaip2:a.network.caip2,resourceUrl:o.resourceUrl}}}var Ne="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",Qe="solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",Ze="solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z",te=Ne,Oe=Qe,Ue=Ze,ne="eip155:8453",ge="eip155:84532",ye="eip155:42161",he="eip155:137",Ae="eip155:10",be="eip155:43114",Se="eip155:56",we="eip155:1187947933",xe="eip155:324705682",Te="eip155:1",re=ne,et=ge,Be=ye,De=he,Me=Ae,$e=be,Ke=Se,Le=we,Ve=xe,We=Te,qe="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",Nt="4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",Ot="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",bt="0x55d398326f99059fF775485246999027B3197955",tt="0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",nt={[Se]:tt,[ne]:Ot,[ge]:"0x036CbD53842c5426634e7929541eC2318f3dCF7e",[ye]:"0xaf88d065e77c8cC2239327C5EDb3A432268e5831",[he]:"0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",[Ae]:"0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",[be]:"0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",[we]:"0x85889c8c714505E0c94b30fcfcF64fE3Ac8FCb20",[xe]:"0x2e08028E3C4c2356572E096d8EF835cD5C6030bD",[Te]:"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"},rt={[bt]:{symbol:"USDT",decimals:18},[tt]:{symbol:"USDC",decimals:18}};var z="0x000000000022D473030F116dDEE9F6B43aC78BA3",He="0x402085c248EeA27D92E8b30b2C58ed07f9E20001",Fe={[Se]:56,[ne]:8453,[ge]:84532,[ye]:42161,[he]:137,[Ae]:10,[be]:43114,[we]:1187947933,[xe]:324705682,[Te]:1},se={[Ne]:"https://api.dexter.cash/api/solana/rpc",[Qe]:"https://api.devnet.solana.com",[Ze]:"https://api.testnet.solana.com"},B={[Se]:"https://api.dexter.cash/api/evm/bsc/rpc",[ne]:"https://api.dexter.cash/api/base/rpc",[ge]:"https://sepolia.base.org",[ye]:"https://api.dexter.cash/api/evm/arbitrum/rpc",[he]:"https://api.dexter.cash/api/evm/polygon/rpc",[Ae]:"https://api.dexter.cash/api/evm/optimism/rpc",[be]:"https://api.dexter.cash/api/evm/avalanche/rpc",[we]:"https://skale-base.skalenodes.com/v1/base",[xe]:"https://base-sepolia-testnet.skalenodes.com/v1/jubilant-horrible-ancha",[Te]:"https://eth.llamarpc.com"};var I=class t extends Error{code;details;constructor(e,n,r){super(n),this.name="X402Error",this.code=e,this.details=r,Object.setPrototypeOf(this,t.prototype)}};import{PublicKey as oe,Connection as St,TransactionMessage as bn,VersionedTransaction as Sn,ComputeBudgetProgram as Ut}from"@solana/web3.js";import{getAssociatedTokenAddress as wt,getAccount as wn,createTransferCheckedInstruction as xn,getMint as Tn,TOKEN_PROGRAM_ID as Bt,TOKEN_2022_PROGRAM_ID as st}from"@solana/spl-token";var vn=12e3,En=1;function ae(t){if(!t||typeof t!="object")return!1;let e=t;return"publicKey"in e&&"signTransaction"in e&&typeof e.signTransaction=="function"}var ot=class{name="Solana";networks=[te,Oe,Ue];config;log;constructor(e={}){this.config=e,this.log=e.verbose?console.log.bind(console,"[x402:solana]"):()=>{}}canHandle(e){return!!(this.networks.includes(e)||e==="solana"||e==="solana-devnet"||e==="solana-testnet"||e.startsWith("solana:"))}getDefaultRpcUrl(e){return this.config.rpcUrls?.[e]?this.config.rpcUrls[e]:se[e]?se[e]:e==="solana"?se[te]:e==="solana-devnet"?se[Oe]:e==="solana-testnet"?se[Ue]:se[te]}getAddress(e){return ae(e)?e.publicKey?.toBase58()??null:null}isConnected(e){return ae(e)?e.publicKey!==null:!1}async getBalance(e,n,r){if(!ae(n)||!n.publicKey)return 0;let s=r||this.getDefaultRpcUrl(e.network),o=new St(s,"confirmed"),a=new oe(n.publicKey.toBase58()),i=new oe(e.asset);try{let u=(await o.getAccountInfo(i,"confirmed"))?.owner.toBase58()===st.toBase58()?st:Bt,p=await wt(i,a,!1,u),d=await wn(o,p,void 0,u),f=e.extra?.decimals??6;return Number(d.amount)/Math.pow(10,f)}catch(c){if(c&&typeof c=="object"&&"name"in c&&(c.name==="TokenAccountNotFoundError"||c.name==="TokenInvalidAccountOwnerError"))return 0;throw c}}async buildTransaction(e,n,r){if(!ae(n))throw new Error("Invalid Solana wallet");if(!n.publicKey)throw new Error("Wallet not connected");let s=r||this.getDefaultRpcUrl(e.network),o=new St(s,"confirmed"),a=new oe(n.publicKey.toBase58()),{payTo:i,asset:c,extra:u}=e,p=e.amount??e.maxAmountRequired;if(!p)throw new Error("Missing amount in payment requirements");if(!u?.feePayer)throw new Error("Missing feePayer in payment requirements");let d=new oe(u.feePayer),f=new oe(c),x=new oe(i);this.log("Building transaction:",{from:a.toBase58(),to:i,amount:p,asset:c,feePayer:u.feePayer});let l=[];l.push(Ut.setComputeUnitLimit({units:vn})),l.push(Ut.setComputeUnitPrice({microLamports:En}));let P=await o.getAccountInfo(f,"confirmed");if(!P)throw new Error(`Token mint ${c} not found`);let T=P.owner.toBase58()===st.toBase58()?st:Bt,y=await Tn(o,f,void 0,T);typeof u?.decimals=="number"&&y.decimals!==u.decimals&&this.log(`Decimals mismatch: requirements say ${u.decimals}, mint says ${y.decimals}`);let g=await wt(f,a,!1,T),S=await wt(f,x,!0,T);if(!await o.getAccountInfo(g,"confirmed"))throw new Error(`No token account found for ${c}. Please ensure you have USDC in your wallet.`);if(!await o.getAccountInfo(S,"confirmed"))throw new Error(`Seller token account not found. The seller (${i}) must have a USDC account.`);let K=BigInt(p);l.push(xn(g,f,S,a,K,y.decimals,[],T));let{blockhash:$}=await o.getLatestBlockhash("confirmed"),O=new bn({payerKey:d,recentBlockhash:$,instructions:l}).compileToV0Message(),D=new Sn(O),h=await n.signTransaction(D);return this.log("Transaction signed successfully"),{serialized:Buffer.from(h.serialize()).toString("base64"),settlementProbe:{kind:"solana",sourceAta:g.toBase58(),destinationAta:S.toBase58(),asset:c,amount:p,blockhash:$}}}async confirmSettlement(e,n){if(e.kind!=="solana")throw new Error(`SolanaAdapter.confirmSettlement cannot handle probe kind "${e.kind}"`);let r=new St(n,"confirmed"),s=new oe(e.destinationAta),o=await r.getSignaturesForAddress(s,{limit:25});if(o.length===0)return{settled:!1};let a=BigInt(e.amount);for(let i of o){if(i.err)continue;let c=await r.getTransaction(i.signature,{commitment:"confirmed",maxSupportedTransactionVersion:0});if(!c?.meta)continue;let u=c.transaction.message.getAccountKeys().keySegments().flat(),p=c.meta.preTokenBalances??[],d=c.meta.postTokenBalances??[];for(let f of d){if(f.mint!==e.asset)continue;let x=u[f.accountIndex];if(!x||!x.equals(s))continue;let l=p.find(y=>y.accountIndex===f.accountIndex),P=BigInt(l?.uiTokenAmount.amount??"0");if(BigInt(f.uiTokenAmount.amount??"0")-P===a)return{settled:!0,txSignature:i.signature}}}return{settled:!1}}};function G(t){return new ot(t)}var Pn={PermitWitnessTransferFrom:[{name:"permitted",type:"TokenPermissions"},{name:"spender",type:"address"},{name:"nonce",type:"uint256"},{name:"deadline",type:"uint256"},{name:"witness",type:"Witness"}],TokenPermissions:[{name:"token",type:"address"},{name:"amount",type:"uint256"}],Witness:[{name:"to",type:"address"},{name:"validAfter",type:"uint256"}]},Dt=BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");function ie(t){if(!t||typeof t!="object")return!1;let e=t;return"address"in e&&typeof e.address=="string"&&e.address.startsWith("0x")}var at=class{name="EVM";networks=[Ke,re,et,We,Be,De,Me,$e,Le,Ve];config;log;constructor(e={}){this.config=e,this.log=e.verbose?console.log.bind(console,"[x402:evm]"):()=>{}}canHandle(e){return!!(this.networks.includes(e)||e==="base"||e==="bsc"||e==="ethereum"||e==="arbitrum"||e==="polygon"||e==="optimism"||e==="avalanche"||e==="skale-base"||e==="skale-base-sepolia"||e.startsWith("eip155:"))}getDefaultRpcUrl(e){return this.config.rpcUrls?.[e]?this.config.rpcUrls[e]:B[e]?B[e]:e==="base"?B[re]:e==="bsc"?B[Ke]:e==="ethereum"?B[We]:e==="arbitrum"?B[Be]:e==="polygon"?B[De]:e==="optimism"?B[Me]:e==="avalanche"?B[$e]:e==="skale-base"?B[Le]:e==="skale-base-sepolia"?B[Ve]:B[re]}getAddress(e){return ie(e)?e.address:null}isConnected(e){return ie(e)?!!e.address:!1}getChainId(e){if(Fe[e])return Fe[e];if(e.startsWith("eip155:")){let n=e.split(":")[1];return parseInt(n,10)}return e==="base"?8453:e==="bsc"?56:e==="ethereum"?1:e==="arbitrum"?42161:8453}async getBalance(e,n,r){if(!ie(n)||!n.address)return 0;let s=r||this.getDefaultRpcUrl(e.network);try{let o=this.encodeBalanceOf(n.address),a=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_call",params:[{to:e.asset,data:o},"latest"]})});if(!a.ok)throw new Error(`RPC request failed: ${a.status}`);let i=await a.json();if(i.error)throw new Error(`RPC error: ${JSON.stringify(i.error)}`);if(!i.result||i.result==="0x")return 0;let c=BigInt(i.result),u=e.extra?.decimals??6;return Number(c)/Math.pow(10,u)}catch(o){throw o}}encodeBalanceOf(e){let n="0x70a08231",r=e.slice(2).toLowerCase().padStart(64,"0");return n+r}async confirmSettlement(e,n){if(e.kind==="eip3009"){let r="0xe94a0102",s=e.from.slice(2).toLowerCase().padStart(64,"0"),o=e.nonce.slice(2).toLowerCase().padStart(64,"0"),a=r+s+o,i=await this.ethCall(n,e.asset,a);return{settled:BigInt(i)!==0n}}if(e.kind==="permit2"){let r="0x4fe02b44",s=BigInt(e.nonce),o=s>>8n,a=s&0xffn,i=e.from.slice(2).toLowerCase().padStart(64,"0"),c=o.toString(16).padStart(64,"0"),u=r+i+c,p=await this.ethCall(n,z,u);return{settled:(BigInt(p)>>a&1n)===1n}}throw new Error(`EvmAdapter.confirmSettlement cannot handle probe kind "${e.kind}"`)}async ethCall(e,n,r){let s=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_call",params:[{to:n,data:r},"latest"]})});if(!s.ok)throw new Error(`RPC request failed: ${s.status}`);let o=await s.json();if(o.error)throw new Error(`RPC error: ${JSON.stringify(o.error)}`);if(!o.result||o.result==="0x")throw new Error("RPC returned an empty result");return o.result}async buildTransaction(e,n,r){if(!ie(n))throw new Error("Invalid EVM wallet");if(!n.address)throw new Error("Wallet not connected");if(e.scheme==="exact-approval")return this.buildApprovalTransaction(e,n,r);if(e.extra?.assetTransferMethod==="permit2")return this.buildPermit2Transaction(e,n,r);let{payTo:s,asset:o,extra:a}=e,i=e.amount??e.maxAmountRequired;if(!i)throw new Error("Missing amount in payment requirements");this.log("Building EVM transaction:",{from:n.address,to:s,amount:i,asset:o,network:e.network});let c=this.getChainId(e.network),u={name:a?.name??"USD Coin",version:a?.version??"2",chainId:BigInt(c),verifyingContract:o},p={TransferWithAuthorization:[{name:"from",type:"address"},{name:"to",type:"address"},{name:"value",type:"uint256"},{name:"validAfter",type:"uint256"},{name:"validBefore",type:"uint256"},{name:"nonce",type:"bytes32"}]},d=new Uint8Array(32);(globalThis.crypto??(await import("crypto")).webcrypto).getRandomValues(d);let f="0x"+[...d].map(g=>g.toString(16).padStart(2,"0")).join(""),x=Math.floor(Date.now()/1e3),l={from:n.address,to:s,value:i,validAfter:String(x-600),validBefore:String(x+(e.maxTimeoutSeconds||60)),nonce:f},P={from:n.address,to:s,value:BigInt(i),validAfter:BigInt(x-600),validBefore:BigInt(x+(e.maxTimeoutSeconds||60)),nonce:f};if(!n.signTypedData)throw new Error("Wallet does not support signTypedData (EIP-712)");let T=await n.signTypedData({domain:u,types:p,primaryType:"TransferWithAuthorization",message:P});return this.log("EIP-712 signature obtained"),{serialized:JSON.stringify({authorization:l,signature:T}),signature:T,settlementProbe:{kind:"eip3009",from:n.address,nonce:f,asset:o,chainId:c}}}async buildApprovalTransaction(e,n,r){let{payTo:s,asset:o,extra:a}=e,i=e.amount??e.maxAmountRequired;if(!i)throw new Error("Missing amount in payment requirements");let c=a?.facilitatorContract;if(!c)throw new Error("exact-approval scheme requires extra.facilitatorContract from the facilitator. The /supported endpoint should provide this.");if(!n.signTypedData)throw new Error("Wallet does not support signTypedData (EIP-712)");this.log("Building approval-based transaction:",{from:n.address,to:s,amount:i,asset:o,network:e.network,facilitatorContract:c});let u=r||this.getDefaultRpcUrl(e.network),p=a?.fee??"0",d=BigInt(i)+BigInt(p),f=await this.readAllowance(u,o,n.address,c);if(f<d){if(!n.sendTransaction)throw new Error("BSC payments require a wallet that supports sendTransaction for the one-time token approval. Use createEvmKeypairWallet() or a browser wallet with transaction support.");let D=this.calculateApprovalAmount(i,p,a?.approvalStrategy);this.log(`Approving ${D} for ${c} (current allowance: ${f})`);let h=await n.sendTransaction({to:o,data:this.encodeApprove(c,D),value:0n});this.log(`Approval tx sent: ${h}`),await this.waitForReceipt(u,h),this.log("Approval confirmed")}else this.log("Sufficient allowance, skipping approval");let x=new Uint8Array(16);(globalThis.crypto??(await import("crypto")).webcrypto).getRandomValues(x);let l=[...x].reduce((D,h)=>D*256n+BigInt(h),0n).toString(),P=new Uint8Array(32);(globalThis.crypto??(await import("crypto")).webcrypto).getRandomValues(P);let T="0x"+[...P].map(D=>D.toString(16).padStart(2,"0")).join(""),g=Math.floor(Date.now()/1e3)+(e.maxTimeoutSeconds||300),S=a?.eip712Domain,k=S?{name:S.name,version:S.version,chainId:BigInt(S.chainId),verifyingContract:S.verifyingContract}:{name:"DexterBSCFacilitator",version:"1",chainId:BigInt(this.getChainId(e.network)),verifyingContract:c},U=a?.eip712Types??{Payment:[{name:"from",type:"address"},{name:"to",type:"address"},{name:"token",type:"address"},{name:"amount",type:"uint256"},{name:"fee",type:"uint256"},{name:"nonce",type:"uint256"},{name:"deadline",type:"uint256"},{name:"paymentId",type:"bytes32"}]},K={from:n.address,to:s,token:o,amount:BigInt(i),fee:BigInt(p),nonce:BigInt(l),deadline:BigInt(g),paymentId:T},$=await n.signTypedData({domain:k,types:U,primaryType:"Payment",message:K});this.log("EIP-712 Payment signature obtained");let O={from:n.address,to:s,token:o,amount:i,fee:p,nonce:l,deadline:g,paymentId:T,signature:$};return{serialized:JSON.stringify(O),signature:$}}async buildPermit2Transaction(e,n,r){let{payTo:s,asset:o}=e,a=e.amount??e.maxAmountRequired;if(!a)throw new Error("Missing amount in payment requirements");if(!n.signTypedData)throw new Error("Wallet does not support signTypedData (EIP-712)");this.log("Building Permit2 transaction:",{from:n.address,to:s,amount:a,asset:o,network:e.network});let i=r||this.getDefaultRpcUrl(e.network),c=await this.readAllowance(i,o,n.address,z),u;if(c<BigInt(a)){let k=this.encodeApprove(z,Dt);if(n.signTransaction){this.log(`Signing Permit2 approval for relay (current allowance: ${c})`);let U=this.getChainId(e.network),K=await this.readGasPrice(i),$=await this.readNonce(i,n.address),O=await n.signTransaction({to:o,data:k,chainId:U,gas:50000n,gasPrice:K,nonce:$});u={erc20ApprovalGasSponsoring:{info:{from:n.address,asset:o,spender:z,amount:Dt.toString(),signedTransaction:O,version:"1"}}},this.log("Permit2 approval signed for facilitator relay")}else if(n.sendTransaction){this.log(`Approving Permit2 directly (current allowance: ${c})`);let U=await n.sendTransaction({to:o,data:k,value:0n});this.log(`Permit2 approval tx sent: ${U}`),await this.waitForReceipt(i,U),this.log("Permit2 approval confirmed")}else throw new Error("Permit2 payments require a wallet that supports signTransaction or sendTransaction for the one-time Permit2 approval. Use createEvmKeypairWallet() or a browser wallet with transaction support.")}else this.log("Sufficient Permit2 allowance, skipping approval");let p=new Uint8Array(32);(globalThis.crypto??(await import("crypto")).webcrypto).getRandomValues(p);let d=[...p].reduce((k,U)=>k*256n+BigInt(U),0n),f=Math.floor(Date.now()/1e3),x=f-600,l=f+(e.maxTimeoutSeconds||300),P=this.getChainId(e.network),T={name:"Permit2",chainId:BigInt(P),verifyingContract:z},y={permitted:{token:o,amount:BigInt(a)},spender:He,nonce:d,deadline:BigInt(l),witness:{to:s,validAfter:BigInt(x)}},g=await n.signTypedData({domain:T,types:Pn,primaryType:"PermitWitnessTransferFrom",message:y});this.log("Permit2 PermitWitnessTransferFrom signature obtained");let S={signature:g,permit2Authorization:{from:n.address,permitted:{token:o,amount:a},spender:He,nonce:d.toString(),deadline:String(l),witness:{to:s,validAfter:String(x)}}};return{serialized:JSON.stringify(S),signature:g,extensions:u,settlementProbe:{kind:"permit2",from:n.address,nonce:d.toString(),chainId:P}}}async readAllowance(e,n,r,s){let o="0xdd62ed3e",a=r.slice(2).toLowerCase().padStart(64,"0"),i=s.slice(2).toLowerCase().padStart(64,"0"),c=o+a+i;try{let p=await(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_call",params:[{to:n,data:c},"latest"]})})).json();return p.error||!p.result||p.result==="0x"?0n:BigInt(p.result)}catch{return 0n}}encodeApprove(e,n){let r="0x095ea7b3",s=e.slice(2).toLowerCase().padStart(64,"0"),o=n.toString(16).padStart(64,"0");return r+s+o}async waitForReceipt(e,n,r=3e4){let s=Date.now();for(;Date.now()-s<r;){try{let a=await(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_getTransactionReceipt",params:[n]})})).json();if(a.result){if(a.result.status==="0x0")throw new Error(`Approval transaction reverted: ${n}`);return}}catch(o){if(o instanceof Error&&o.message.includes("reverted"))throw o}await new Promise(o=>setTimeout(o,2e3))}throw new Error(`Approval transaction receipt timeout after ${r}ms: ${n}`)}async readGasPrice(e){try{let r=await(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_gasPrice",params:[]})})).json();return r.result?BigInt(r.result):50000000n}catch{return 50000000n}}async readNonce(e,n){try{let s=await(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({jsonrpc:"2.0",id:1,method:"eth_getTransactionCount",params:[n,"latest"]})})).json();return s.result?parseInt(s.result,16):0}catch{return 0}}calculateApprovalAmount(e,n,r){let s=BigInt(e)+BigInt(n);if(!r||r.mode==="exact")return s;let o=BigInt(r.defaultMultiple??10),a=s*o;if(r.maxCapUsd){let i=this.inferDecimals(e),c=BigInt(Math.floor(r.maxCapUsd*Math.pow(10,i)));if(a>c)return c}if(r.exactAboveUsd){let i=this.inferDecimals(e),c=BigInt(Math.floor(r.exactAboveUsd*Math.pow(10,i)));if(BigInt(e)>c)return s}return a}inferDecimals(e){return e.length>12?18:6}};function ve(t){return new at(t)}function xt(t){if(t===qe||t===Nt)return!0;let e=t.toLowerCase();for(let n of Object.values(nt))if(n.toLowerCase()===e)return!0;for(let n of Object.keys(rt))if(n.toLowerCase()===e)return!0;return!1}function Rn(t){return{[Ne]:"Solana",[Qe]:"Solana Devnet",[Ze]:"Solana Testnet",solana:"Solana","solana-devnet":"Solana Devnet","solana-testnet":"Solana Testnet",[ne]:"Base",[ge]:"Base Sepolia",[Te]:"Ethereum",[ye]:"Arbitrum",[he]:"Polygon",[Ae]:"Optimism",[be]:"Avalanche",[Se]:"BSC",[we]:"SKALE Base",[xe]:"SKALE Base Sepolia",base:"Base","base-sepolia":"Base Sepolia",ethereum:"Ethereum",arbitrum:"Arbitrum",polygon:"Polygon",optimism:"Optimism",avalanche:"Avalanche",bsc:"BSC","skale-base":"SKALE Base","skale-base-sepolia":"SKALE Base Sepolia"}[t]||t}function Mt(t,e){let n=Rn(t);return n===t?e:n}async function _n(t,e){if(typeof t=="string")return[t,e];if(t instanceof URL)return[t.href,e];let n=t,r=new Headers(n.headers);e?.headers&&new Headers(e.headers).forEach((a,i)=>r.set(i,a));let s={method:e?.method??n.method,headers:r,signal:e?.signal??n.signal,redirect:e?.redirect??n.redirect,credentials:e?.credentials??n.credentials},o=(s.method??"GET").toUpperCase();return e&&"body"in e?s.body=e.body:o!=="GET"&&o!=="HEAD"&&n.body&&(s.body=await n.arrayBuffer()),[n.url,s]}var kn=new WeakMap;function $t(t){let{adapters:e=[G({verbose:t.verbose}),ve({verbose:t.verbose})],wallets:n,wallet:r,preferredNetwork:s,rpcUrls:o={},maxAmountAtomic:a,fetch:i=globalThis.fetch,verbose:c=!1,accessPass:u,onPaymentRequired:p,onPaymentDispatched:d,maxRetries:f=0,retryDelayMs:x=500}=t,l=c?console.log.bind(console,"[x402]"):()=>{};async function P(h,w){let v;for(let A=0;A<=f;A++)try{let E=await i(h,w);if(E.status>=502&&E.status<=504&&A<f){l(`Retry ${A+1}/${f}: server returned ${E.status}`),await new Promise(R=>setTimeout(R,x*Math.pow(2,A)));continue}return E}catch(E){v=E,A<f&&(l(`Retry ${A+1}/${f}: ${E instanceof Error?E.message:"network error"}`),await new Promise(R=>setTimeout(R,x*Math.pow(2,A))))}throw v}let T=new Map;function y(h){try{let w=new URL(h).host,v=T.get(w);if(v&&v.expiresAt>Date.now()/1e3+10)return v.jwt;v&&T.delete(w)}catch{}return null}function g(h,w){try{let v=new URL(h).host,A=w.split(".");if(A.length===3){let E=JSON.parse(atob(A[1].replace(/-/g,"+").replace(/_/g,"/"))),R=Math.floor(Date.now()/1e3),_=R+86400,L=Math.min(typeof E.exp=="number"?E.exp:R,_);T.set(v,{jwt:w,expiresAt:L}),l("Access pass cached for",v,"| expires:",new Date(L*1e3).toISOString())}}catch{l("Failed to cache access pass")}}let S=n||{};r&&!S.solana&&ae(r)&&(S.solana=r),r&&!S.evm&&ie(r)&&(S.evm=r);function k(h){let w=[];for(let v of h){let A=v.scheme??"exact";if(A!=="exact"&&A!=="exact-approval")continue;let E=e.find(_=>_.canHandle(v.network));if(!E)continue;let R;E.name==="Solana"?R=S.solana:E.name==="EVM"&&(R=S.evm),R&&E.isConnected(R)&&w.push({accept:v,adapter:E,wallet:R})}if(w.length===0)return null;if(s){let v=w.find(A=>A.accept.network===s);if(v)return v}return w[0]}function U(h,w){return Mt(h,w)}function K(h,w){return o[h]||w.getDefaultRpcUrl(h)}async function $(h,w,v,A,E){let R="";if(u?.preferTier&&A.tiers){let m=A.tiers.find(b=>b.id===u.preferTier);if(m){if(u.maxSpend&&parseFloat(m.price)>parseFloat(u.maxSpend))throw new I("access_pass_exceeds_max_spend",`Access pass tier "${m.id}" costs $${m.price}, exceeds max spend $${u.maxSpend}`);R=`tier=${m.id}`}}else if(u?.preferDuration&&A.ratePerHour)R=`duration=${u.preferDuration}`;else if(A.tiers&&A.tiers.length>0){let m=A.tiers[0];if(u?.maxSpend&&parseFloat(m.price)>parseFloat(u.maxSpend))throw new I("access_pass_exceeds_max_spend",`Cheapest access pass costs $${m.price}, exceeds max spend $${u?.maxSpend}`);R=`tier=${m.id}`}let _=R?E.includes("?")?`${E}&${R}`:`${E}?${R}`:E;l("Purchasing access pass:",R||"default tier");let L=v.headers.get("PAYMENT-REQUIRED");if(!L)return null;let M;try{M=JSON.parse(atob(L))}catch{return null}let N=k(M.accepts);if(!N)return null;let{accept:C,adapter:W,wallet:ce}=N;if(W.name==="Solana"&&!C.extra?.feePayer)return null;let H=C.extra?.decimals??(xt(C.asset)?6:void 0);if(typeof H!="number")return null;let Ee=C.amount??C.maxAmountRequired;if(!Ee)return null;let F=K(C.network,W);try{let m=await W.getBalance(C,ce,F),b=Number(Ee)/Math.pow(10,H);if(m<b){let ut=U(C.network,W.name);throw new I("insufficient_balance",`Insufficient balance for access pass on ${ut}. Have $${m.toFixed(4)}, need $${b.toFixed(4)}`)}}catch(m){if(m instanceof I)throw m}let j=await W.buildTransaction(C,ce,F),le;W.name==="EVM"?le=JSON.parse(j.serialized):le={transaction:j.serialized};let X=typeof h=="string"?h:h instanceof URL?h.href:h.url,ue=M.resource;if(typeof M.resource=="string")try{let m=new URL(M.resource,X);["http:","https:"].includes(m.protocol)&&(ue=m.toString())}catch{}else if(M.resource&&typeof M.resource=="object"&&"url"in M.resource){let m=M.resource;try{let b=new URL(m.url,X);["http:","https:"].includes(b.protocol)&&(ue={...m,url:b.toString()})}catch{}}let Je={x402Version:C.x402Version??2,resource:ue,accepted:C,payload:le};j.extensions&&(Je.extensions=j.extensions);let J=btoa(JSON.stringify(Je)),Y=await i(_,{...w,method:"POST",headers:{...w?.headers||{},"Content-Type":"application/json","PAYMENT-SIGNATURE":J}});if(!Y.ok)return l("Pass purchase failed:",Y.status),null;let V=Y.headers.get("ACCESS-PASS");return V&&(g(E,V),l("Access pass purchased and cached")),Y}async function O(h,w){let v=h;if(l("Making request:",v),u){let m=y(v);if(m){l("Using cached access pass");let b=await i(h,{...w,headers:{...w?.headers||{},Authorization:`Bearer ${m}`}});if(b.status!==401&&b.status!==402)return b;l("Cached pass rejected (status",b.status,"), purchasing new pass");try{T.delete(new URL(v).host)}catch{}}}let A=await P(h,w);if(A.status!==402)return A;l("Received 402 Payment Required");let E=A.headers.get("X-ACCESS-PASS-TIERS");if(u&&E){l("Server offers access passes, purchasing...");try{let m=JSON.parse(atob(E)),b=await $(h,w,A,m,v);if(b)return b}catch(m){l("Access pass purchase failed, falling back to per-request payment:",m)}}let R=A.headers.get("PAYMENT-REQUIRED");if(!R)throw new I("missing_payment_required_header","Server returned 402 but no PAYMENT-REQUIRED header");let _;try{let m=atob(R);_=JSON.parse(m)}catch{throw new I("invalid_payment_required","Failed to decode PAYMENT-REQUIRED header")}l("Payment requirements:",_);let L=A.headers.get("X-Quote-Hash");L&&l("Quote hash received:",L);let M=k(_.accepts);if(!M){let m=_.accepts.map(b=>b.network).join(", ");throw new I("no_matching_payment_option",`No connected wallet for any available network: ${m}`)}let{accept:N,adapter:C,wallet:W}=M;if(l(`Using ${C.name} for ${N.network}`),C.name==="Solana"&&!N.extra?.feePayer)throw new I("missing_fee_payer","Solana payment option missing feePayer in extra");let ce=N.extra?.decimals??(xt(N.asset)?6:void 0);if(typeof ce!="number")throw new I("missing_decimals","Payment option missing decimals - provide in extra or use a known stablecoin");let H=N.amount??N.maxAmountRequired;if(!H)throw new I("missing_amount","Payment option missing amount");if(a&&BigInt(H)>BigInt(a))throw new I("amount_exceeds_max",`Payment amount ${H} exceeds maximum ${a}`);let Ee=K(N.network,C);l("Checking balance...");try{let m=await C.getBalance(N,W,Ee),b=Number(H)/Math.pow(10,ce);if(m<b){let ut=U(N.network,C.name);throw new I("insufficient_balance",`Insufficient balance on ${ut}. Have $${m.toFixed(4)}, need $${b.toFixed(4)}`)}l(`Balance OK: $${m.toFixed(4)} >= $${b.toFixed(4)}`)}catch(m){if(m instanceof I)throw m;l("Balance check failed (RPC error), proceeding with transaction attempt")}if(p&&!await p(N))throw new I("payment_rejected","Payment rejected by onPaymentRequired callback");l("Building transaction...");let F=await C.buildTransaction(N,W,Ee);l("Transaction signed");let j;C.name==="EVM"?j=JSON.parse(F.serialized):j={transaction:F.serialized};let le=h,X=_.resource;if(typeof _.resource=="string")try{let m=new URL(_.resource,le).toString();m!==_.resource&&l("Resolved relative resource URL:",_.resource,"\u2192",m),X=m}catch{X=_.resource}else if(_.resource&&typeof _.resource=="object"&&"url"in _.resource){let m=_.resource;try{let b=new URL(m.url,le).toString();b!==m.url&&(l("Resolved relative resource URL:",m.url,"\u2192",b),X={...m,url:b})}catch{}}let ue={x402Version:N.x402Version??2,resource:X,accepted:N,payload:j};F.extensions&&(ue.extensions=F.extensions);let Je=btoa(JSON.stringify(ue));if(d)try{d(N,F.settlementProbe)}catch{}l("Retrying request with payment...");let J=await P(h,{...w,headers:{...w?.headers||{},"PAYMENT-SIGNATURE":Je,...L?{"X-Quote-Hash":L}:{}}});if(l("Retry response status:",J.status),J.status===402){let m="unknown";try{let b=await J.clone().json();m=String(b.error||b.message||JSON.stringify(b)),l("Rejection reason:",m)}catch{}throw new I("payment_rejected",`Payment was rejected by the server: ${m}`)}let Y=J.headers.get("PAYMENT-RESPONSE"),V;if(Y)try{V=JSON.parse(atob(Y))}catch{}return V??={},V.amountAtomic=H,V.assetDecimals=ce,kn.set(J,V),V.extensions&&l("Settlement extensions:",Object.keys(V.extensions).join(", ")),J}async function D(h,w){let[v,A]=await _n(h,w);return O(v,A)}return{fetch:D}}function q(t){if(t instanceof Error){if(t.message&&t.message.length>0)return t.message;if(t.name&&t.name.length>0)return t.name}let e=String(t);return e.length>0?e:"unknown error"}async function je(t){let e="";try{e=(await t.clone().text()).slice(0,600)}catch{}let n=e.toLowerCase(),r=n.includes("settle")||n.includes("facilitator"),s=e;try{let o=JSON.parse(e),a=[o.error,o.detail,o.message].filter(i=>typeof i=="string"&&i.length>0);a.length>0&&(s=a.join(" \u2014 "))}catch{}return s||(s=`HTTP ${t.status}`),{reason:r?"settlement_failed":"merchant_rejected",detail:`merchant HTTP ${t.status}: ${s}`}}var it="Payment authorization was sent, but the merchant did not respond within the timeout. ",ct=" Do not retry without checking \u2014 inspect the funding wallet for a transfer to the merchant before attempting payment again.";async function lt(t,e,n){if(!t)return{confirmed:!1,detail:it+"This payment scheme has no on-chain confirmation check, so the SDK cannot verify whether it settled."+ct};try{if(t.kind==="solana"){let a=G(),i=n??a.getDefaultRpcUrl(e.caip2);if(!a.confirmSettlement)return Lt();let c=await a.confirmSettlement(t,i);return c.settled?{confirmed:!0,txSignature:c.txSignature}:Kt()}let r=ve(),s=r.getDefaultRpcUrl(e.caip2);if(!r.confirmSettlement)return Lt();let o=await r.confirmSettlement(t,s);return o.settled?{confirmed:!0,txSignature:o.txSignature}:Kt()}catch(r){return{confirmed:!1,detail:it+`On-chain confirmation could not be completed (${q(r)}).`+ct}}}function Kt(){return{confirmed:!1,detail:it+"On-chain confirmation found no matching settlement yet \u2014 the payment may still be pending, or may not have settled."+ct}}function Lt(){return{confirmed:!1,detail:it+"The chain adapter does not support on-chain confirmation."+ct}}var Cn=new Set(["exact","exact-approval"]);async function In(t,e,n,r){let s;try{s=await r.signNextVoucher(n.amount)}catch{return null}let o=new Headers(e.headers??void 0);o.set("X-Tab-Voucher",Xe(s));let a={method:e.method??"GET",headers:o};e.signal&&(a.signal=e.signal),typeof e.body=="string"&&(a.body=e.body);let i;try{i=await fetch(t,a)}catch(c){return{ok:!1,reason:"error",detail:c?.message??String(c)}}return i.status===402?(r.rollbackVoucher?.call(r,s),null):i.ok?{ok:!0,paid:!0,response:i,amountPaid:n.amount,network:n.network}:{ok:!1,...await je(i)}}var Vt={version:2,async parseChallenge(t){return Ye(t)},async pay(t,e,n,r,s){if(s.tab){let y=n.options.find(g=>g.scheme==="tab"&&g.network.family==="svm"&&g.payTo===s.tab.counterparty);if(y){let g=await In(t,e,y,s.tab);if(g)return g}}let o=n.options.filter(y=>Cn.has(y.scheme));if(o.length===0)return{ok:!1,reason:"no_payment_options",detail:`no generically payable scheme offered (got: ${n.options.map(y=>y.scheme).join(", ")})`};let a=o.find(y=>y.network.family==="evm"?!!r.evm:y.network.family==="svm"?!!r.solana:!1);if(!a)return{ok:!1,reason:"unsupported_network"};let i=s.timeoutMs??15e3,c=s.responseTimeoutMs??12e4,u=new AbortController,p=setTimeout(()=>u.abort(),i),d=!1,f,x=(y,g)=>{d=!0,f=g,clearTimeout(p),p=setTimeout(()=>u.abort(),c)},l=$t({wallets:r,preferredNetwork:a.network.caip2,maxAmountAtomic:s.maxAmountAtomic,fetch:globalThis.fetch,onPaymentDispatched:x}),P=e.signal?AbortSignal.any([e.signal,u.signal]):u.signal,T={method:e.method??"GET",headers:e.headers,signal:P};typeof e.body=="string"&&(T.body=e.body);try{let y=await l.fetch(t,T);if(clearTimeout(p),!y.ok)return{ok:!1,...await je(y)};let g,S=y.headers.get("PAYMENT-RESPONSE");if(S)try{let k=At(S);k&&typeof k.transaction=="string"&&(g=k.transaction)}catch{}return{ok:!0,paid:!0,response:y,amountPaid:a.amount,network:a.network,txSignature:g}}catch(y){clearTimeout(p);let g=y;if(g?.name==="AbortError"){if(!d)return{ok:!1,reason:"timeout"};let S=await lt(f,a.network,s.solanaRpcUrl);return S.confirmed?{ok:!0,paid:!0,response:void 0,amountPaid:a.amount,network:a.network,txSignature:S.txSignature}:{ok:!1,reason:"payment_unconfirmed",detail:S.detail}}return{ok:!1,reason:"error",detail:g?.message??String(y)}}}};import{getAddress as Nn}from"viem";var On={TransferWithAuthorization:[{name:"from",type:"address"},{name:"to",type:"address"},{name:"value",type:"uint256"},{name:"validAfter",type:"uint256"},{name:"validBefore",type:"uint256"},{name:"nonce",type:"bytes32"}]};async function Un(){let t=globalThis.crypto??(await import("crypto")).webcrypto,e=new Uint8Array(32);return t.getRandomValues(e),"0x"+Array.from(e,n=>n.toString(16).padStart(2,"0")).join("")}async function Bn(t,e,n){if(typeof t.signTypedData!="function")throw new Error("EVM wallet does not support signTypedData");let r=Math.floor(Date.now()/1e3),s=String(r-600),o=String(r+(e.maxTimeoutSeconds??60)),a={from:t.address,to:e.payTo,value:e.amount,validAfter:s,validBefore:o,nonce:await Un()},i=Fe[e.network.caip2];if(i===void 0)throw new Error(`unknown chain id for network ${e.network.caip2}`);let c=e.extra,u=await t.signTypedData({domain:{name:c.name,version:c.version,chainId:i,verifyingContract:Nn(e.asset)},types:On,primaryType:"TransferWithAuthorization",message:a});return{payment:{x402Version:1,scheme:e.scheme,network:n,payload:{signature:u,authorization:a}},settlementProbe:{kind:"eip3009",from:a.from,nonce:a.nonce,asset:e.asset,chainId:i}}}async function Tt(t,e,n){try{let r;for(let s of t.options){let o=s.network.family==="evm"&&!!e.evm||s.network.family==="svm"&&!!e.solana;if(s.scheme!=="exact"){o&&(r=s.scheme);continue}if(s.network.family==="evm"&&e.evm){let a=await e.evm;return await Dn(s,a,n)}if(s.network.family==="svm"&&e.solana){let a=await e.solana;return await Mn(s,a,n)}}return r!==void 0?{ok:!1,reason:"merchant_rejected",detail:`v1 supports only the 'exact' scheme, got '${r}'`}:{ok:!1,reason:"unsupported_network"}}catch(r){return{ok:!1,reason:"error",detail:q(r)}}}async function Dn(t,e,n){if(n.maxAmountAtomic!==void 0&&BigInt(t.amount)>BigInt(n.maxAmountAtomic))return{ok:!1,reason:"budget_exceeded"};let r=t.extra??{},s=r.name,o=r.version;if(typeof s!="string"||s.length===0||typeof o!="string"||o.length===0)return{ok:!1,reason:"merchant_rejected",detail:"v1 challenge missing exact-scheme EIP-712 domain (extra.name / extra.version)"};let a=t.network.bare,{payment:i,settlementProbe:c}=await Bn(e,t,a);return{ok:!0,headerValue:Buffer.from(JSON.stringify(i),"utf8").toString("base64"),option:t,settlementProbe:c}}async function Mn(t,e,n){if(n.maxAmountAtomic!==void 0&&BigInt(t.amount)>BigInt(n.maxAmountAtomic))return{ok:!1,reason:"budget_exceeded"};if(t.scheme!=="exact")return{ok:!1,reason:"merchant_rejected",detail:`v1 SVM supports only the 'exact' scheme, got '${t.scheme}'`};let r=t.extra??{};if(typeof r.feePayer!="string"||r.feePayer.length===0)return{ok:!1,reason:"merchant_rejected",detail:"v1 SVM challenge missing extra.feePayer (required as the transaction fee payer)"};let s=t.network.bare,o={x402Version:1,scheme:t.scheme,network:s,asset:t.asset,payTo:t.payTo,amount:t.amount,maxAmountRequired:t.amount,maxTimeoutSeconds:t.maxTimeoutSeconds??60,extra:r},i=await G().buildTransaction(o,e,n.solanaRpcUrl),c={x402Version:1,scheme:t.scheme,network:s,payload:{transaction:i.serialized}};return{ok:!0,headerValue:Buffer.from(JSON.stringify(c),"utf8").toString("base64"),option:t,settlementProbe:i.settlementProbe}}function $n(t){let e=[];for(let n of t){if(!n||typeof n!="object")continue;let r=n,s=Ce(String(r.network??""));s&&e.push({scheme:String(r.scheme??"exact"),network:s,amount:String(r.maxAmountRequired??r.amount??"0"),asset:String(r.asset??""),payTo:String(r.payTo??""),maxTimeoutSeconds:typeof r.maxTimeoutSeconds=="number"?r.maxTimeoutSeconds:void 0,extra:r.extra&&typeof r.extra=="object"?r.extra:void 0})}return e}var Wt={version:1,async parseChallenge(t){if(t.headers.get("payment-required"))return null;let e;try{e=await t.clone().json()}catch{return null}let n=Array.isArray(e.accepts)?e.accepts:[];if(n.length===0)return null;let r=$n(n);return r.length===0?null:{x402Version:1,options:r}},async pay(t,e,n,r,s){let o,a=new AbortController,i=!1,c,u;try{let p=s.timeoutMs??15e3;o=setTimeout(()=>a.abort(),p);let d=await Tt(n,r,s);if(!d.ok)return{ok:!1,reason:d.reason,detail:d.detail};let f=d.headerValue,x=d.option;u=x,c=d.settlementProbe;let l=new Headers(e.headers??void 0);l.set("X-PAYMENT",f);let P=e.signal!=null?AbortSignal.any([e.signal,a.signal]):a.signal,T={method:e.method,headers:l,signal:P};typeof e.body=="string"&&(T.body=e.body),i=!0,clearTimeout(o);let y=s.responseTimeoutMs??12e4;o=setTimeout(()=>a.abort(),y);let g=await fetch(t,T);return g.ok?{ok:!0,paid:!0,response:g,amountPaid:x.amount,network:x.network,txSignature:Kn(g)}:{ok:!1,...await je(g)}}catch(p){if(p instanceof Error&&p.name==="AbortError"){if(!i)return{ok:!1,reason:"timeout"};let d=await lt(c,u.network,s.solanaRpcUrl);return d.confirmed?{ok:!0,paid:!0,response:void 0,amountPaid:u.amount,network:u.network,txSignature:d.txSignature}:{ok:!1,reason:"payment_unconfirmed",detail:d.detail}}return{ok:!1,reason:"error",detail:q(p)}}finally{o!==void 0&&clearTimeout(o)}}};function Kn(t){let e=t.headers.get("x-payment-response")??t.headers.get("X-PAYMENT-RESPONSE");if(e)try{let n=JSON.parse(Buffer.from(e,"base64").toString("utf8")),r=n.transaction??n.txHash??n.transactionHash;return typeof r=="string"?r:void 0}catch{return}}import*as Ht from"tweetnacl";import{Keypair as ys,VersionedTransaction as hs,Transaction as As}from"@solana/web3.js";var qt=Symbol.for("x402:keypair");function vt(t){let e=t.evm;if(e&&typeof e.signMessage=="function"&&typeof e.address=="string")return{address:e.address,signMessage:e.signMessage};let n=t.solana;if(n){let r=n[qt];if(r&&r.secretKey&&r.publicKey)return{publicKey:r.publicKey,signMessage:async s=>Ht.sign.detached(s,r.secretKey)}}return null}var Ln=[Vt,Wt];async function Vn(t){let e=vt(t);if(!e)return fetch;try{return(await import("@x402/extensions/sign-in-with-x")).wrapFetchWithSIWx(fetch,e)}catch(n){return console.warn(`[x402] SIW-X unavailable \u2014 @x402/extensions failed to load; SIW-X merchants will not authenticate. ${q(n)}`),fetch}}async function Et(t,e,n,r){if(e.body!==void 0&&e.body!==null&&typeof e.body!="string")return{ok:!1,reason:"error",detail:"payAndFetch requires a string body; non-string bodies (Buffer, FormData, URLSearchParams, ReadableStream) cannot be safely re-sent on the paid retry"};let s;try{s=await(await Vn(n))(t,{...e})}catch(o){return{ok:!1,reason:"error",detail:q(o)}}if(s.status!==402)return{ok:!0,paid:!1,response:s};for(let o of Ln){let a=await o.parseChallenge(s.clone());if(a)return o.pay(t,e,a,n,r)}return{ok:!1,reason:"no_payment_options"}}async function Wn(t,e={},n){let r=await Ie(t,e);if(r.kind==="free")return{result:{ok:!0,paid:!1,response:r.response},tab:null};if(r.kind==="no_tab")return{result:{ok:!1,reason:"no_payment_options",detail:`no tab option offered (schemes: ${r.schemesOffered.join(", ")})`},tab:null};if(r.kind==="error")return{result:{ok:!1,reason:"error",detail:r.detail},tab:null};let{offer:s}=r,o=BigInt(Z(n.perUnitCap));if(BigInt(s.amountAtomic)>o)return{result:{ok:!1,reason:"budget_exceeded",detail:`seller quotes ${s.amountAtomic} atomic; perUnitCap allows ${o}`},tab:null};let a=n.tabs?.get(s.payTo);return(!a||!a.state.isOpen)&&(a=await gt({vault:n.vault,network:"solana:mainnet",seller:s.payTo,perUnitCap:n.perUnitCap,totalCap:n.totalCap,sessionDuration:n.sessionDuration,facilitatorUrl:n.facilitatorUrl,onLiveSession:n.onLiveSession}),n.tabs?.set(s.payTo,a)),{result:await Et(t,e,{},{tab:a}),tab:a}}function qn(t){return{counterparty:t.payTo,perRequest:{atomic:t.amountAtomic,human:de(t.amountAtomic)},asset:t.asset,network:{caip2:t.networkCaip2},scheme:"tab",settlement:{custody:"non-custodial",protection:"lock",settleOn:"close"},credit:null,...t.resourceUrl!==void 0?{resourceUrl:t.resourceUrl}:{}}}async function Hn(t,e={},n={}){let r=n.cache?.get(t);if(r)return{kind:"terms",terms:r};let s=await Ie(t,e,n.fetchImpl??fetch);if(s.kind!=="offer")return s;if(s.offer.asset!==qe)return{kind:"error",detail:`tab offer asset is not USDC ("${s.offer.asset}"); refusing to render a human price`};let o=qn(s.offer);return n.cache?.set(t,o),{kind:"terms",terms:o}}export{Pe as DEFAULT_FACILITATOR_URL,yt as DEXTER_VAULT_PROGRAM_ID,an as INSTRUCTIONS_SYSVAR_ID,mt as LiveSessionExistsError,on as SECP256R1_PROGRAM_ID,pe as SessionScopeExceededError,Q as TabClosedError,me as UnsupportedNetworkError,de as atomicToHuman,nn as buildRegisterSessionKeyInstruction,rn as buildRevokeSessionKeyInstruction,sn as buildSecp256r1VerifyInstruction,jt as buildVoucherMessage,Z as humanToAtomic,gt as openTab,Wn as payUrlWithTab,Ie as resolveTabOffer,Hn as resolveTabTerms,Qt as resumeTab,pt as sessionRegisterMessage,Ft as sessionRevokeMessage,dn as tabFromGrant,dt as voucherPayloadMessage,Xe as voucherToHeader};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";var ct=Object.create;var G=Object.defineProperty;var lt=Object.getOwnPropertyDescriptor;var ut=Object.getOwnPropertyNames;var mt=Object.getPrototypeOf,dt=Object.prototype.hasOwnProperty;var pt=(e,t)=>{for(var r in t)G(e,r,{get:t[r],enumerable:!0})},Ke=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of ut(t))!dt.call(e,i)&&i!==r&&G(e,i,{get:()=>t[i],enumerable:!(n=lt(t,i))||n.enumerable});return e};var Me=(e,t,r)=>(r=e!=null?ct(mt(e)):{},Ke(t||!e||!e.__esModule?G(r,"default",{value:e,enumerable:!0}):r,e)),ht=e=>Ke(G({},"__esModule",{value:!0}),e);var Mt={};pt(Mt,{FileChannelLedger:()=>Z,FileVoucherStore:()=>ue,InMemoryChannelLedger:()=>B,InMemoryVoucherStore:()=>le,InvalidRegistrationError:()=>C,InvalidVoucherError:()=>v,InvalidVoucherSignatureError:()=>P,OnChainVerificationError:()=>_,ScopeViolationError:()=>S,TAB_VOUCHER_HEADER:()=>I,enforceScope:()=>se,openSse:()=>Je,parseRegistration:()=>re,requireTab:()=>Xe,tabChallengeMiddleware:()=>it,tabMiddleware:()=>ce,tabOrExactMiddleware:()=>ot,verifyRegistrationOnChain:()=>ne,verifyVoucherSignature:()=>ie});module.exports=ht(Mt);var v=class extends Error{constructor(r,n){super(`Invalid voucher: ${r}${n?` (${n})`:""}`);this.reason=r;this.name="InvalidVoucherError"}};var V=require("fs"),Q=require("path"),Le=new Map;function T(e,t){let n=(Le.get(e)??Promise.resolve()).then(()=>t(),()=>t());return Le.set(e,n.then(()=>{},()=>{})),n}var B=class{map=new Map;async get(t){return this.map.get(t)??null}async set(t,r){this.map.set(t,r)}async delete(t){this.map.delete(t)}async tryAcquireLease(t,r){return T(t,async()=>{let n=this.map.get(t),i=Date.now();if(n?.lease&&n.lease.heldUntilUnixMs>i)return!1;let s=n??{lastVoucher:null,deliveredCumulativeAtomic:"0",lastCrystallizedCumulativeAtomic:"0"};return this.map.set(t,{...s,lease:{heldUntilUnixMs:i+r}}),!0})}async releaseLease(t){await T(t,async()=>{let r=this.map.get(t);r&&this.map.set(t,{...r,lease:void 0})})}};function Te(e){let t="";for(let r of e)t+=r.toString(16).padStart(2,"0");return t}function _e(e){if(e.length%2!==0)throw new Error(`hex length must be even, got ${e.length}`);let t=new Uint8Array(e.length/2);for(let r=0;r<t.length;r++)t[r]=parseInt(e.substr(r*2,2),16);return t}function gt(e){return{lastVoucher:e.lastVoucher?{payload:e.lastVoucher.payload,sessionPublicKey:Te(e.lastVoucher.sessionPublicKey),sessionRegistration:Te(e.lastVoucher.sessionRegistration),sessionSignature:Te(e.lastVoucher.sessionSignature)}:null,deliveredCumulativeAtomic:e.deliveredCumulativeAtomic,lastCrystallizedCumulativeAtomic:e.lastCrystallizedCumulativeAtomic,onChain:e.onChain,lease:e.lease}}function yt(e){return{lastVoucher:e.lastVoucher?{payload:e.lastVoucher.payload,sessionPublicKey:_e(e.lastVoucher.sessionPublicKey),sessionRegistration:_e(e.lastVoucher.sessionRegistration),sessionSignature:_e(e.lastVoucher.sessionSignature)}:null,deliveredCumulativeAtomic:e.deliveredCumulativeAtomic,lastCrystallizedCumulativeAtomic:e.lastCrystallizedCumulativeAtomic??"0",onChain:e.onChain,lease:e.lease}}var Z=class{constructor(t){this.dir=t}pathFor(t){if(!/^[a-z0-9_-]+$/i.test(t))throw new Error(`unsafe channelId for filesystem: ${t}`);return(0,Q.join)(this.dir,`${t}.json`)}async get(t){try{let r=await V.promises.readFile(this.pathFor(t),"utf8");return yt(JSON.parse(r))}catch(r){if(r?.code==="ENOENT")return null;throw r}}async set(t,r){let n=this.pathFor(t);await V.promises.mkdir((0,Q.dirname)(n),{recursive:!0});let i=`${n}.tmp`;await V.promises.writeFile(i,JSON.stringify(gt(r))),await V.promises.rename(i,n)}async delete(t){try{await V.promises.unlink(this.pathFor(t))}catch(r){if(r?.code!=="ENOENT")throw r}}async tryAcquireLease(t,r){return T(t,async()=>{let n=await this.get(t),i=Date.now();if(n?.lease&&n.lease.heldUntilUnixMs>i)return!1;let s=n??{lastVoucher:null,deliveredCumulativeAtomic:"0",lastCrystallizedCumulativeAtomic:"0"};return await this.set(t,{...s,lease:{heldUntilUnixMs:i+r}}),!0})}async releaseLease(t){await T(t,async()=>{let r=await this.get(t);r&&await this.set(t,{...r,lease:void 0})})}};var je=require("@solana/web3.js");var qe=Me(require("tweetnacl"),1),At=require("@noble/hashes/sha256"),bt=require("@noble/curves/p256"),ee=require("@solana/web3.js");var O=require("@dexterai/vault/messages");var Re=require("@dexterai/vault/instructions"),ft=require("@dexterai/vault/precompile"),K=require("@dexterai/vault/constants");var te=require("@dexterai/vault/session"),Ne="OTS_SESSION_REGISTER_V2",C=class extends Error{constructor(r,n){super(`Invalid registration: ${r}${n?` (${n})`:""}`);this.reason=r;this.name="InvalidRegistrationError"}};function re(e){if(e.length!==188)throw new C("wrong_length",`expected 188, got ${e.length}`);let t=new TextDecoder().decode(e.slice(0,Ne.length));if(t!==Ne)throw new C("wrong_domain",`got "${t}"`);for(let g=Ne.length;g<32;g++)if(e[g]!==0)throw new C("wrong_domain",`non-NUL padding at byte ${g}`);let r=new DataView(e.buffer,e.byteOffset,e.byteLength),n=new ee.PublicKey(e.slice(32,64)),i=new ee.PublicKey(e.slice(64,96)),s=e.slice(96,128),h=r.getBigUint64(128,!0),c=r.getBigInt64(136,!0),o=new ee.PublicKey(e.slice(144,176)),d=r.getUint32(176,!0),l=r.getBigUint64(180,!0);if(!n.equals(K.DEXTER_VAULT_PROGRAM_ID))throw new C("wrong_program",`${n.toBase58()} is not ${K.DEXTER_VAULT_PROGRAM_ID.toBase58()}`);if(h===0n)throw new C("cap_zero");let a=BigInt(Math.floor(Date.now()/1e3));if(c<=a)throw new C("expiry_in_past",`expires_at=${c}, now=${a}`);return{programId:n,vaultPda:i,sessionPubkey:new Uint8Array(s),maxAmount:h,expiresAt:c,allowedCounterparty:o,nonce:d,maxRevolvingCapacity:l}}var _=class extends Error{constructor(r,n){super(`On-chain verification failed: ${r}${n?` (${n})`:""}`);this.reason=r;this.name="OnChainVerificationError"}};async function ne(e,t){let r=await(0,te.fetchSessionAccount)(e,t.vaultPda,t.allowedCounterparty);if(!r||r.version===0)throw new _("session_not_active","no live SessionAccount PDA for this (vault, counterparty) \u2014 revoked, expiry-swept, or never registered");if(!(0,te.isSessionLive)(r))throw new _("session_not_active","SessionAccount PDA is present but expired");if(!wt(r.session.sessionPubkey,t.sessionPubkey))throw new _("session_pubkey_mismatch",`on-chain ${De(r.session.sessionPubkey)} != registration ${De(t.sessionPubkey)}`);let n=r.session.spent,i=r.session.crystallizedCumulative;return{frontierAtomic:(n>i?n:i).toString(),spentAtomic:n.toString(),crystallizedCumulativeAtomic:i.toString()}}var P=class extends Error{constructor(t){super(`Invalid voucher signature${t?`: ${t}`:""}`),this.name="InvalidVoucherSignatureError"}};function ie(e,t){if(t.length!==32)throw new P(`channelIdBytes must be 32 bytes, got ${t.length}`);if(e.sessionPublicKey.length!==32)throw new P(`sessionPublicKey must be 32 bytes, got ${e.sessionPublicKey.length}`);if(e.sessionSignature.length!==64)throw new P(`sessionSignature must be 64 bytes, got ${e.sessionSignature.length}`);let r=(0,O.voucherPayloadMessage)({channelId:t,cumulativeAmount:BigInt(e.payload.cumulativeAmount),sequenceNumber:e.payload.sequenceNumber});if(!qe.default.sign.detached.verify(r,e.sessionSignature,e.sessionPublicKey))throw new P("ed25519 verify rejected")}var S=class extends Error{constructor(r,n){super(`Scope violation: ${r}${n?` (${n})`:""}`);this.reason=r;this.name="ScopeViolationError"}};function se(e){let t=BigInt(e.voucher.payload.cumulativeAmount);if(t>e.registration.maxAmount)throw new S("cumulative_exceeds_cap",`${t} > ${e.registration.maxAmount}`);let r=BigInt(Math.floor(Date.now()/1e3));if(r>=e.registration.expiresAt)throw new S("session_expired",`now=${r} >= expiresAt=${e.registration.expiresAt}`);if(!e.registration.allowedCounterparty.equals(e.expectedCounterparty))throw new S("wrong_counterparty",`${e.registration.allowedCounterparty.toBase58()} != ${e.expectedCounterparty.toBase58()}`);if(e.previousCumulativeAtomic!==void 0){let n=BigInt(e.previousCumulativeAtomic);if(t<=n)throw new S("non_monotonic",`cumulative=${t} not > previous=${n}`)}}function wt(e,t){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(e[r]!==t[r])return!1;return!0}function De(e){let t="";for(let r of e)t+=r.toString(16).padStart(2,"0");return t}var xt=require("@solana/web3.js"),Ct=require("@noble/hashes/utils");var vt=Me(require("tweetnacl"),1);var St=require("@noble/hashes/sha256");var ze="https://x402.dexter.cash",He=6;function E(e,t=He){if(!/^\d+(\.\d+)?$/.test(e))throw new Error(`amount must be a non-negative decimal string, got "${e}"`);let[r,n=""]=e.split(".");if(n.length>t)throw new Error(`amount "${e}" has more than ${t} decimals`);let i=n.padEnd(t,"0"),s=`${r}${i}`.replace(/^0+(?=\d)/,"");return s===""?"0":s}function $(e,t=He){if(!/^\d+$/.test(e))throw new Error(`atomic must be a non-negative integer string, got "${e}"`);let r=e.padStart(t+1,"0"),n=r.slice(0,-t).replace(/^0+(?=\d)/,"")||"0",i=r.slice(-t).replace(/0+$/,"");return i?`${n}.${i}`:n}var oe=require("@noble/hashes/utils"),Et=15e3,Pt=["claim_already_exists","non_monotonic_cumulative"];function Tt(e){return`${e.slice(0,16)}\u2026`}function Fe(e,t,r,n){let i=Tt(e);if(n.crystallized){console.info(`[tab/seller] crystallize OK channel=${i} cumulative=${t} seq=${r}${n.claimPda?` claimPda=${n.claimPda}`:""} \u2014 LockVoucher landed`);return}if(n.error){if(Pt.some(s=>n.error.includes(s))){console.warn(`[tab/seller] crystallize duplicate channel=${i} cumulative=${t} seq=${r}: ${n.error} \u2014 voucher already secured on-chain (benign, no action)`);return}console.error(`[tab/seller] crystallize FAILED channel=${i} cumulative=${t} seq=${r}: ${n.error} \u2014 lock NOT secured (degraded: best-effort path, no watermark advance, retries at next threshold/close; seller exposure stays unsecured until it lands)`)}}async function Ie(e,t,r,n,i=fetch){let s=e.lastVoucher;if(!s)return{crystallized:!1};if(s.payload.channelId!==t){let c={crystallized:!1,error:"channel_id_mismatch"};return Fe(t,s.payload.cumulativeAmount,s.payload.sequenceNumber,c),c}let h=c=>(Fe(t,s.payload.cumulativeAmount,s.payload.sequenceNumber,c),c);try{let c=`${r.replace(/\/$/,"")}/tab/lock`,o={channelId:s.payload.channelId,cumulativeAmount:s.payload.cumulativeAmount,sequenceNumber:s.payload.sequenceNumber,sessionPublicKey:(0,oe.bytesToHex)(s.sessionPublicKey),sessionSignature:(0,oe.bytesToHex)(s.sessionSignature),sessionRegistration:(0,oe.bytesToHex)(s.sessionRegistration),network:n},d=await i(c,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(o),signal:AbortSignal.timeout(Et)}),l=await d.text();if(!d.ok)return h({crystallized:!1,error:`tab lock ${d.status}: ${l.slice(0,200)}`});let a;try{let g=JSON.parse(l);g&&typeof g.claimPda=="string"&&(a=g.claimPda)}catch{}return h({crystallized:!0,claimPda:a})}catch(c){return h({crystallized:!1,error:String(c?.message??c)})}}async function We(e,t,r,n,i,s={}){let h=BigInt(e.deliveredCumulativeAtomic),c=BigInt(e.lastCrystallizedCumulativeAtomic??"0"),o=BigInt(i.thresholdAtomic);if(h-c<o)return{crystallized:!1};let d=e.lastVoucher?.payload.cumulativeAmount,l=await Ie(e,t,r,n,s.fetchImpl);return l.crystallized&&d!==void 0&&(e.lastCrystallizedCumulativeAtomic=d),l}var I="x-tab-voucher",ke=class{map=new Map;get(t){return this.map.get(t)}set(t,r){this.map.set(t,r)}update(t,r){let n=this.map.get(t);n&&(n.lastCumulativeAtomic=r)}delete(t){this.map.delete(t)}},Ue=class{constructor(t,r,n,i,s,h){this.recordDeliveredImpl=s;this.chargeImpl=h;this.channelId=t,this.network=r,this.cumulativeAtomic=n,this.deliveredBaselineAtomic=i}channelId;network;sessionPublicKey=null;cumulativeAtomic;deliveredBaselineAtomic;cumulative(){return $(this.cumulativeAtomic.toString())}deliveredCumulative(){return $(this.deliveredBaselineAtomic.toString())}async recordDelivered(t){return this.recordDeliveredImpl(t)}bumpCumulative(t){this.cumulativeAtomic=t}setSessionPublicKey(t){this.sessionPublicKey=t}async charge(t){return this.chargeImpl(t)}};function _t(e){if(typeof e!="string"||e.length===0)throw new v("signature_invalid",`missing ${I} header`);let t;try{t=Buffer.from(e,"base64").toString("utf8")}catch{throw new v("signature_invalid","malformed base64")}let r;try{r=JSON.parse(t)}catch{throw new v("signature_invalid","malformed JSON")}if(!r||typeof r!="object"||!r.payload||!r.sessionPublicKey)throw new v("signature_invalid","missing required fields");return{payload:r.payload,sessionPublicKey:ae(r.sessionPublicKey),sessionRegistration:ae(r.sessionRegistration),sessionSignature:ae(r.sessionSignature)}}function ae(e){if(typeof e!="string"||e.length%2!==0)throw new v("signature_invalid",`bad hex: ${typeof e}`);let t=new Uint8Array(e.length/2);for(let r=0;r<t.length;r++)t[r]=parseInt(e.substr(r*2,2),16);return t}function Rt(e){if(!/^[0-9a-f]{64}$/i.test(e))throw new v("signature_invalid",`channelId must be 64-char hex, got "${e}"`);return ae(e)}function ce(e){let t=e.ledger??new B,r=new ke,n=typeof e.sellerPubkey=="string"?new je.PublicKey(e.sellerPubkey):e.sellerPubkey,i=e.maxPerVoucherAtomic?BigInt(e.maxPerVoucherAtomic):BigInt(E(e.perUnit))*100n,s=e.facilitatorUrl??ze,h={thresholdAtomic:e.lockCadence?.thresholdAtomic??E("0.10"),onClose:e.lockCadence?.onClose??!0};return async(c,o,d)=>{try{let l=_t(c.headers[I]),a=l.payload.channelId,g=Rt(a),m=r.get(a),f=null;if(!m){let y=re(l.sessionRegistration);f=(await ne(e.connection,y))?.frontierAtomic??null,m={registration:y,lastCumulativeAtomic:"0"},r.set(a,m)}ie(l,g),se({registration:m.registration,voucher:l,expectedCounterparty:n,previousCumulativeAtomic:m.lastCumulativeAtomic});let b=BigInt(l.payload.cumulativeAmount),L=BigInt(m.lastCumulativeAtomic),D=b-L;if(D>i)throw new S("cumulative_exceeds_cap",`single voucher increment ${D} exceeds maxPerVoucherAtomic ${i}`);let Ee=e.leaseTtlMs??3e5;if(!await t.tryAcquireLease(a,Ee))throw new v("channel_busy","another stream is live on this channel; tabs serve one stream at a time");let q=!1,J=()=>{q||(q=!0,t.releaseLease(a).catch(y=>{console.error("[tab/seller] failed to release channel lease:",y)}))};o.on("close",J),o.on("finish",J);let Pe=async y=>{let N=y.lastCrystallizedCumulativeAtomic??"0";await We(y,a,s,e.network,h),y.lastCrystallizedCumulativeAtomic!==N&&await T(a,async()=>{let w=await t.get(a);w&&await t.set(a,{...w,lastCrystallizedCumulativeAtomic:y.lastCrystallizedCumulativeAtomic})}).catch(w=>{console.error(`[tab/seller] crystallize watermark persist FAILED channel=${a.slice(0,16)}\u2026 (lock landed; expect a duplicate-lock warn on the next request):`,w)})},Y=!1,u=()=>{!h.onClose||Y||(Y=!0,(async()=>{let y=await t.get(a);if(!y||!y.lastVoucher)return;let N=y.lastVoucher.payload.cumulativeAmount;(await Ie(y,a,s,e.network)).crystallized&&await T(a,async()=>{let z=await t.get(a);z&&await t.set(a,{...z,lastCrystallizedCumulativeAtomic:N})})})().catch(y=>{console.error(`[tab/seller] close-path crystallize CRASHED channel=${a.slice(0,16)}\u2026 (final voucher NOT locked; exposure unsecured until the next request retries):`,y)}))};o.on("close",u),o.on("finish",u);let p=await t.get(a),A=!p?.lastVoucher&&BigInt(p?.deliveredCumulativeAtomic??"0")===0n,x=f!==null&&A&&BigInt(f)>0n?f:null;x!==null&&console.info(`[tab/seller] channel ${a.slice(0,16)}\u2026 resumed: seeding delivered baseline from chain frontier ${x} (session already settled/locked up to it \u2014 that span is not deliverable budget)`);let k=x!==null?BigInt(x):p?BigInt(p.deliveredCumulativeAtomic):0n;await T(a,async()=>{let y=await t.get(a);await t.set(a,{...y,lastVoucher:l,deliveredCumulativeAtomic:x??(y?y.deliveredCumulativeAtomic:"0"),lastCrystallizedCumulativeAtomic:x??y?.lastCrystallizedCumulativeAtomic??"0"})}),r.update(a,l.payload.cumulativeAmount);let U=new Ue(a,e.network,b,k,async y=>{let N=null;await T(a,async()=>{let w=await t.get(a),z=w?BigInt(w.deliveredCumulativeAtomic):0n,Ve=BigInt(y),at=Ve>0n?z+Ve:z,Be={...w,lastVoucher:w?.lastVoucher??l,deliveredCumulativeAtomic:at.toString(),lastCrystallizedCumulativeAtomic:w?.lastCrystallizedCumulativeAtomic??"0"};await t.set(a,Be),N=Be}),N&&Pe(N).catch(w=>{console.error(`[tab/seller] crystallize cadence CRASHED channel=${a.slice(0,16)}\u2026 (threshold lock not attempted; retries at the next delivery):`,w)})},async y=>{throw new Error("SellerTab.charge() is not driven by the route handler; the buyer presents a fresh voucher per chunk. Use openSse(res, tab) for the metered-stream pattern.")});U.setSessionPublicKey(l.sessionPublicKey),c.tab=U,d()}catch(l){if(l instanceof v||l instanceof C||l instanceof _||l instanceof P||l instanceof S){o.status(402).json({error:"invalid_voucher",reason:l.reason??"unknown",detail:l.message});return}d(l)}}}function Xe(e){if(!e.tab)throw new Error("req.tab is missing \u2014 did tabMiddleware run on this route?");return e.tab}function Je(e,t){if(!t.tab)throw new Error("openSse requires options.tab");e.headersSent||(e.setHeader("Content-Type","text/event-stream"),e.setHeader("Cache-Control","no-cache"),e.setHeader("Connection","keep-alive"),typeof e.flushHeaders=="function"&&e.flushHeaders());let r=t.tab,n=BigInt(E(r.cumulative())),i=BigInt(E(r.deliveredCumulative())),s=n-i;s<0n&&(s=0n);let h=t.perUnit?BigInt(E(t.perUnit)):null,c=0n,o=!1;async function d(){await r.recordDelivered(c.toString())}e.on("close",()=>{o||(o=!0,d().catch(m=>{console.error("[tab/seller] terminal persist failed on disconnect:",m)}))});async function l(m=1){if(o)throw new Error("meter ended");if(h===null)throw new Error("charge() needs options.perUnit");let f=h*BigInt(m),b=c+f;if(b>s)throw o=!0,await d(),new S("cumulative_exceeds_cap",`chunk would push delivered to ${$((i+b).toString())} beyond signed cumulative ${$(n.toString())} (per-request budget ${$(s.toString())})`);c=b}function a(m){if(o)throw new Error("meter ended");let b=(typeof m=="string"?m:Buffer.from(m).toString("utf8")).replace(/\n/g,"\\n");e.write(`data: ${b}
|
|
1
|
+
"use strict";var lt=Object.create;var Y=Object.defineProperty;var ut=Object.getOwnPropertyDescriptor;var mt=Object.getOwnPropertyNames;var dt=Object.getPrototypeOf,pt=Object.prototype.hasOwnProperty;var gt=(e,t)=>{for(var r in t)Y(e,r,{get:t[r],enumerable:!0})},Ke=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of mt(t))!pt.call(e,i)&&i!==r&&Y(e,i,{get:()=>t[i],enumerable:!(n=ut(t,i))||n.enumerable});return e};var Me=(e,t,r)=>(r=e!=null?lt(dt(e)):{},Ke(t||!e||!e.__esModule?Y(r,"default",{value:e,enumerable:!0}):r,e)),ht=e=>Ke(Y({},"__esModule",{value:!0}),e);var Dt={};gt(Dt,{FileChannelLedger:()=>Z,FileVoucherStore:()=>me,InMemoryChannelLedger:()=>L,InMemoryVoucherStore:()=>ue,InvalidRegistrationError:()=>E,InvalidVoucherError:()=>w,InvalidVoucherSignatureError:()=>_,OnChainVerificationError:()=>I,ScopeViolationError:()=>S,TAB_VOUCHER_HEADER:()=>k,enforceScope:()=>se,openSse:()=>Ge,parseRegistration:()=>re,requireTab:()=>Je,tabChallengeMiddleware:()=>st,tabMiddleware:()=>le,tabOrExactMiddleware:()=>at,verifyRegistrationOnChain:()=>ne,verifyVoucherSignature:()=>ie});module.exports=ht(Dt);var w=class extends Error{constructor(r,n){super(`Invalid voucher: ${r}${n?` (${n})`:""}`);this.reason=r;this.name="InvalidVoucherError"}};var B=require("fs"),Q=require("path"),De=new Map;function T(e,t){let n=(De.get(e)??Promise.resolve()).then(()=>t(),()=>t());return De.set(e,n.then(()=>{},()=>{})),n}var L=class{map=new Map;async get(t){return this.map.get(t)??null}async set(t,r){this.map.set(t,r)}async delete(t){this.map.delete(t)}async tryAcquireLease(t,r){return T(t,async()=>{let n=this.map.get(t),i=Date.now();if(n?.lease&&n.lease.heldUntilUnixMs>i)return!1;let s=n??{lastVoucher:null,deliveredCumulativeAtomic:"0",lastCrystallizedCumulativeAtomic:"0"};return this.map.set(t,{...s,lease:{heldUntilUnixMs:i+r}}),!0})}async releaseLease(t){await T(t,async()=>{let r=this.map.get(t);r&&this.map.set(t,{...r,lease:void 0})})}};function Te(e){let t="";for(let r of e)t+=r.toString(16).padStart(2,"0");return t}function _e(e){if(e.length%2!==0)throw new Error(`hex length must be even, got ${e.length}`);let t=new Uint8Array(e.length/2);for(let r=0;r<t.length;r++)t[r]=parseInt(e.substr(r*2,2),16);return t}function yt(e){return{lastVoucher:e.lastVoucher?{payload:e.lastVoucher.payload,sessionPublicKey:Te(e.lastVoucher.sessionPublicKey),sessionRegistration:Te(e.lastVoucher.sessionRegistration),sessionSignature:Te(e.lastVoucher.sessionSignature)}:null,deliveredCumulativeAtomic:e.deliveredCumulativeAtomic,lastCrystallizedCumulativeAtomic:e.lastCrystallizedCumulativeAtomic,gateRefusedCumulativeAtomic:e.gateRefusedCumulativeAtomic,onChain:e.onChain,lease:e.lease}}function ft(e){return{lastVoucher:e.lastVoucher?{payload:e.lastVoucher.payload,sessionPublicKey:_e(e.lastVoucher.sessionPublicKey),sessionRegistration:_e(e.lastVoucher.sessionRegistration),sessionSignature:_e(e.lastVoucher.sessionSignature)}:null,deliveredCumulativeAtomic:e.deliveredCumulativeAtomic,lastCrystallizedCumulativeAtomic:e.lastCrystallizedCumulativeAtomic??"0",gateRefusedCumulativeAtomic:e.gateRefusedCumulativeAtomic,onChain:e.onChain,lease:e.lease}}var Z=class{constructor(t){this.dir=t}pathFor(t){if(!/^[a-z0-9_-]+$/i.test(t))throw new Error(`unsafe channelId for filesystem: ${t}`);return(0,Q.join)(this.dir,`${t}.json`)}async get(t){try{let r=await B.promises.readFile(this.pathFor(t),"utf8");return ft(JSON.parse(r))}catch(r){if(r?.code==="ENOENT")return null;throw r}}async set(t,r){let n=this.pathFor(t);await B.promises.mkdir((0,Q.dirname)(n),{recursive:!0});let i=`${n}.tmp`;await B.promises.writeFile(i,JSON.stringify(yt(r))),await B.promises.rename(i,n)}async delete(t){try{await B.promises.unlink(this.pathFor(t))}catch(r){if(r?.code!=="ENOENT")throw r}}async tryAcquireLease(t,r){return T(t,async()=>{let n=await this.get(t),i=Date.now();if(n?.lease&&n.lease.heldUntilUnixMs>i)return!1;let s=n??{lastVoucher:null,deliveredCumulativeAtomic:"0",lastCrystallizedCumulativeAtomic:"0"};return await this.set(t,{...s,lease:{heldUntilUnixMs:i+r}}),!0})}async releaseLease(t){await T(t,async()=>{let r=await this.get(t);r&&await this.set(t,{...r,lease:void 0})})}};var Xe=require("@solana/web3.js");var He=Me(require("tweetnacl"),1),bt=require("@noble/hashes/sha256"),vt=require("@noble/curves/p256"),ee=require("@solana/web3.js");var $=require("@dexterai/vault/messages");var Ie=require("@dexterai/vault/instructions"),At=require("@dexterai/vault/precompile"),K=require("@dexterai/vault/constants");var te=require("@dexterai/vault/session"),Ne="OTS_SESSION_REGISTER_V2",E=class extends Error{constructor(r,n){super(`Invalid registration: ${r}${n?` (${n})`:""}`);this.reason=r;this.name="InvalidRegistrationError"}};function re(e){if(e.length!==188)throw new E("wrong_length",`expected 188, got ${e.length}`);let t=new TextDecoder().decode(e.slice(0,Ne.length));if(t!==Ne)throw new E("wrong_domain",`got "${t}"`);for(let y=Ne.length;y<32;y++)if(e[y]!==0)throw new E("wrong_domain",`non-NUL padding at byte ${y}`);let r=new DataView(e.buffer,e.byteOffset,e.byteLength),n=new ee.PublicKey(e.slice(32,64)),i=new ee.PublicKey(e.slice(64,96)),s=e.slice(96,128),h=r.getBigUint64(128,!0),c=r.getBigInt64(136,!0),a=new ee.PublicKey(e.slice(144,176)),m=r.getUint32(176,!0),l=r.getBigUint64(180,!0);if(!n.equals(K.DEXTER_VAULT_PROGRAM_ID))throw new E("wrong_program",`${n.toBase58()} is not ${K.DEXTER_VAULT_PROGRAM_ID.toBase58()}`);if(h===0n)throw new E("cap_zero");let o=BigInt(Math.floor(Date.now()/1e3));if(c<=o)throw new E("expiry_in_past",`expires_at=${c}, now=${o}`);return{programId:n,vaultPda:i,sessionPubkey:new Uint8Array(s),maxAmount:h,expiresAt:c,allowedCounterparty:a,nonce:m,maxRevolvingCapacity:l}}var I=class extends Error{constructor(r,n){super(`On-chain verification failed: ${r}${n?` (${n})`:""}`);this.reason=r;this.name="OnChainVerificationError"}};async function ne(e,t){let r=await(0,te.fetchSessionAccount)(e,t.vaultPda,t.allowedCounterparty);if(!r||r.version===0)throw new I("session_not_active","no live SessionAccount PDA for this (vault, counterparty) \u2014 revoked, expiry-swept, or never registered");if(!(0,te.isSessionLive)(r))throw new I("session_not_active","SessionAccount PDA is present but expired");if(!wt(r.session.sessionPubkey,t.sessionPubkey))throw new I("session_pubkey_mismatch",`on-chain ${ze(r.session.sessionPubkey)} != registration ${ze(t.sessionPubkey)}`);let n=r.session.spent,i=r.session.crystallizedCumulative;return{frontierAtomic:(n>i?n:i).toString(),spentAtomic:n.toString(),crystallizedCumulativeAtomic:i.toString()}}var _=class extends Error{constructor(t){super(`Invalid voucher signature${t?`: ${t}`:""}`),this.name="InvalidVoucherSignatureError"}};function ie(e,t){if(t.length!==32)throw new _(`channelIdBytes must be 32 bytes, got ${t.length}`);if(e.sessionPublicKey.length!==32)throw new _(`sessionPublicKey must be 32 bytes, got ${e.sessionPublicKey.length}`);if(e.sessionSignature.length!==64)throw new _(`sessionSignature must be 64 bytes, got ${e.sessionSignature.length}`);let r=(0,$.voucherPayloadMessage)({channelId:t,cumulativeAmount:BigInt(e.payload.cumulativeAmount),sequenceNumber:e.payload.sequenceNumber});if(!He.default.sign.detached.verify(r,e.sessionSignature,e.sessionPublicKey))throw new _("ed25519 verify rejected")}var S=class extends Error{constructor(r,n){super(`Scope violation: ${r}${n?` (${n})`:""}`);this.reason=r;this.name="ScopeViolationError"}};function se(e){let t=BigInt(e.voucher.payload.cumulativeAmount);if(t>e.registration.maxAmount)throw new S("cumulative_exceeds_cap",`${t} > ${e.registration.maxAmount}`);let r=BigInt(Math.floor(Date.now()/1e3));if(r>=e.registration.expiresAt)throw new S("session_expired",`now=${r} >= expiresAt=${e.registration.expiresAt}`);if(!e.registration.allowedCounterparty.equals(e.expectedCounterparty))throw new S("wrong_counterparty",`${e.registration.allowedCounterparty.toBase58()} != ${e.expectedCounterparty.toBase58()}`);if(e.previousCumulativeAtomic!==void 0){let n=BigInt(e.previousCumulativeAtomic);if(t<=n)throw new S("non_monotonic",`cumulative=${t} not > previous=${n}`)}}function wt(e,t){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(e[r]!==t[r])return!1;return!0}function ze(e){let t="";for(let r of e)t+=r.toString(16).padStart(2,"0");return t}var Ct=require("@solana/web3.js"),Et=require("@noble/hashes/utils");var St=Me(require("tweetnacl"),1);var xt=require("@noble/hashes/sha256");var qe="https://x402.dexter.cash",Fe=6;function R(e,t=Fe){if(!/^\d+(\.\d+)?$/.test(e))throw new Error(`amount must be a non-negative decimal string, got "${e}"`);let[r,n=""]=e.split(".");if(n.length>t)throw new Error(`amount "${e}" has more than ${t} decimals`);let i=n.padEnd(t,"0"),s=`${r}${i}`.replace(/^0+(?=\d)/,"");return s===""?"0":s}function V(e,t=Fe){if(!/^\d+$/.test(e))throw new Error(`atomic must be a non-negative integer string, got "${e}"`);let r=e.padStart(t+1,"0"),n=r.slice(0,-t).replace(/^0+(?=\d)/,"")||"0",i=r.slice(-t).replace(/0+$/,"");return i?`${n}.${i}`:n}var oe=require("@noble/hashes/utils"),Rt=15e3,Pt=["claim_already_exists","non_monotonic_cumulative"],Tt="below_lock_cadence";function ae(e){return typeof e=="string"&&e.includes(Tt)}function _t(e){return`${e.slice(0,16)}\u2026`}function We(e,t,r,n){let i=_t(e);if(n.crystallized){console.info(`[tab/seller] crystallize OK channel=${i} cumulative=${t} seq=${r}${n.claimPda?` claimPda=${n.claimPda}`:""} \u2014 LockVoucher landed`);return}if(n.error){if(ae(n.error)){console.warn(`[tab/seller] crystallize refused below the facilitator's cadence gate channel=${i} cumulative=${t} seq=${r}: ${n.error} \u2014 benign: this facilitator's cadence engine owns the protection cadence for the span; gate-refused watermark advanced, re-attempts after the next signed voucher`);return}if(Pt.some(s=>n.error.includes(s))){console.warn(`[tab/seller] crystallize duplicate channel=${i} cumulative=${t} seq=${r}: ${n.error} \u2014 voucher already secured on-chain (benign, no action)`);return}console.error(`[tab/seller] crystallize FAILED channel=${i} cumulative=${t} seq=${r}: ${n.error} \u2014 lock NOT secured (degraded: best-effort path, no watermark advance, retries at next threshold/close; seller exposure stays unsecured until it lands)`)}}async function ke(e,t,r,n,i=fetch){let s=e.lastVoucher;if(!s)return{crystallized:!1};if(s.payload.channelId!==t){let c={crystallized:!1,error:"channel_id_mismatch"};return We(t,s.payload.cumulativeAmount,s.payload.sequenceNumber,c),c}let h=c=>(We(t,s.payload.cumulativeAmount,s.payload.sequenceNumber,c),c);try{let c=`${r.replace(/\/$/,"")}/tab/lock`,a={channelId:s.payload.channelId,cumulativeAmount:s.payload.cumulativeAmount,sequenceNumber:s.payload.sequenceNumber,sessionPublicKey:(0,oe.bytesToHex)(s.sessionPublicKey),sessionSignature:(0,oe.bytesToHex)(s.sessionSignature),sessionRegistration:(0,oe.bytesToHex)(s.sessionRegistration),network:n},m=await i(c,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(a),signal:AbortSignal.timeout(Rt)}),l=await m.text();if(!m.ok)return h({crystallized:!1,error:`tab lock ${m.status}: ${l.slice(0,200)}`});let o;try{let y=JSON.parse(l);y&&typeof y.claimPda=="string"&&(o=y.claimPda)}catch{}return h({crystallized:!0,claimPda:o})}catch(c){return h({crystallized:!1,error:String(c?.message??c)})}}async function je(e,t,r,n,i,s={}){let h=BigInt(e.deliveredCumulativeAtomic),c=BigInt(e.lastCrystallizedCumulativeAtomic??"0"),a=BigInt(i.thresholdAtomic);if(h-c<a)return{crystallized:!1};let m=e.lastVoucher?.payload.cumulativeAmount,l=e.gateRefusedCumulativeAtomic;if(m!==void 0&&l!==void 0&&BigInt(m)<=BigInt(l))return{crystallized:!1};let o=await ke(e,t,r,n,s.fetchImpl);return o.crystallized&&m!==void 0?(e.lastCrystallizedCumulativeAtomic=m,e.gateRefusedCumulativeAtomic=void 0):ae(o.error)&&m!==void 0&&(e.gateRefusedCumulativeAtomic=m),o}var k="x-tab-voucher",Ue=class{map=new Map;get(t){return this.map.get(t)}set(t,r){this.map.set(t,r)}update(t,r){let n=this.map.get(t);n&&(n.lastCumulativeAtomic=r)}delete(t){this.map.delete(t)}},Oe=class{constructor(t,r,n,i,s,h){this.recordDeliveredImpl=s;this.chargeImpl=h;this.channelId=t,this.network=r,this.cumulativeAtomic=n,this.deliveredBaselineAtomic=i}channelId;network;sessionPublicKey=null;cumulativeAtomic;deliveredBaselineAtomic;cumulative(){return V(this.cumulativeAtomic.toString())}deliveredCumulative(){return V(this.deliveredBaselineAtomic.toString())}async recordDelivered(t){return this.recordDeliveredImpl(t)}bumpCumulative(t){this.cumulativeAtomic=t}setSessionPublicKey(t){this.sessionPublicKey=t}async charge(t){return this.chargeImpl(t)}};function It(e){if(typeof e!="string"||e.length===0)throw new w("signature_invalid",`missing ${k} header`);let t;try{t=Buffer.from(e,"base64").toString("utf8")}catch{throw new w("signature_invalid","malformed base64")}let r;try{r=JSON.parse(t)}catch{throw new w("signature_invalid","malformed JSON")}if(!r||typeof r!="object"||!r.payload||!r.sessionPublicKey)throw new w("signature_invalid","missing required fields");return{payload:r.payload,sessionPublicKey:ce(r.sessionPublicKey),sessionRegistration:ce(r.sessionRegistration),sessionSignature:ce(r.sessionSignature)}}function ce(e){if(typeof e!="string"||e.length%2!==0)throw new w("signature_invalid",`bad hex: ${typeof e}`);let t=new Uint8Array(e.length/2);for(let r=0;r<t.length;r++)t[r]=parseInt(e.substr(r*2,2),16);return t}function Nt(e){if(!/^[0-9a-f]{64}$/i.test(e))throw new w("signature_invalid",`channelId must be 64-char hex, got "${e}"`);return ce(e)}function le(e){let t=e.ledger??new L,r=new Ue,n=typeof e.sellerPubkey=="string"?new Xe.PublicKey(e.sellerPubkey):e.sellerPubkey,i=e.maxPerVoucherAtomic?BigInt(e.maxPerVoucherAtomic):BigInt(R(e.perUnit))*100n,s=e.facilitatorUrl??qe,h={thresholdAtomic:e.lockCadence?.thresholdAtomic??R("0.10"),onClose:e.lockCadence?.onClose??!0};return async(c,a,m)=>{try{let l=It(c.headers[k]),o=l.payload.channelId,y=Nt(o),d=r.get(o),f=null;if(!d){let p=re(l.sessionRegistration);f=(await ne(e.connection,p))?.frontierAtomic??null,d={registration:p,lastCumulativeAtomic:"0"},r.set(o,d)}ie(l,y),se({registration:d.registration,voucher:l,expectedCounterparty:n,previousCumulativeAtomic:d.lastCumulativeAtomic});let b=BigInt(l.payload.cumulativeAmount),D=BigInt(d.lastCumulativeAtomic),z=b-D;if(z>i)throw new S("cumulative_exceeds_cap",`single voucher increment ${z} exceeds maxPerVoucherAtomic ${i}`);let Re=e.leaseTtlMs??3e5;if(!await t.tryAcquireLease(o,Re))throw new w("channel_busy","another stream is live on this channel; tabs serve one stream at a time");let H=!1,J=()=>{H||(H=!0,t.releaseLease(o).catch(p=>{console.error("[tab/seller] failed to release channel lease:",p)}))};a.on("close",J),a.on("finish",J);let Pe=async p=>{let P=p.lastCrystallizedCumulativeAtomic??"0",C=p.gateRefusedCumulativeAtomic;await je(p,o,s,e.network,h),(p.lastCrystallizedCumulativeAtomic!==P||p.gateRefusedCumulativeAtomic!==C)&&await T(o,async()=>{let v=await t.get(o);v&&await t.set(o,{...v,lastCrystallizedCumulativeAtomic:p.lastCrystallizedCumulativeAtomic,gateRefusedCumulativeAtomic:p.gateRefusedCumulativeAtomic})}).catch(v=>{console.error(`[tab/seller] crystallize watermark persist FAILED channel=${o.slice(0,16)}\u2026 (expect a benign duplicate/below-cadence warn on the next request):`,v)})},G=!1,u=()=>{!h.onClose||G||(G=!0,(async()=>{let p=await t.get(o);if(!p||!p.lastVoucher)return;let P=p.lastVoucher.payload.cumulativeAmount;if(p.gateRefusedCumulativeAtomic!==void 0&&BigInt(P)<=BigInt(p.gateRefusedCumulativeAtomic))return;let C=await ke(p,o,s,e.network);C.crystallized?await T(o,async()=>{let v=await t.get(o);v&&await t.set(o,{...v,lastCrystallizedCumulativeAtomic:P,gateRefusedCumulativeAtomic:void 0})}):ae(C.error)&&await T(o,async()=>{let v=await t.get(o);v&&await t.set(o,{...v,gateRefusedCumulativeAtomic:P})})})().catch(p=>{console.error(`[tab/seller] close-path crystallize CRASHED channel=${o.slice(0,16)}\u2026 (final voucher NOT locked; exposure unsecured until the next request retries):`,p)}))};a.on("close",u),a.on("finish",u);let g=await t.get(o),A=!g?.lastVoucher&&BigInt(g?.deliveredCumulativeAtomic??"0")===0n,x=f!==null&&A&&BigInt(f)>0n?f:null;x!==null&&console.info(`[tab/seller] channel ${o.slice(0,16)}\u2026 resumed: seeding delivered baseline from chain frontier ${x} (session already settled/locked up to it \u2014 that span is not deliverable budget)`);let U=x!==null?BigInt(x):g?BigInt(g.deliveredCumulativeAtomic):0n;await T(o,async()=>{let p=await t.get(o);await t.set(o,{...p,lastVoucher:l,deliveredCumulativeAtomic:x??(p?p.deliveredCumulativeAtomic:"0"),lastCrystallizedCumulativeAtomic:x??p?.lastCrystallizedCumulativeAtomic??"0"})}),r.update(o,l.payload.cumulativeAmount);let O=new Oe(o,e.network,b,U,async p=>{let P=null;await T(o,async()=>{let C=await t.get(o),v=C?BigInt(C.deliveredCumulativeAtomic):0n,Be=BigInt(p),ct=Be>0n?v+Be:v,Le={...C,lastVoucher:C?.lastVoucher??l,deliveredCumulativeAtomic:ct.toString(),lastCrystallizedCumulativeAtomic:C?.lastCrystallizedCumulativeAtomic??"0"};await t.set(o,Le),P=Le}),P&&Pe(P).catch(C=>{console.error(`[tab/seller] crystallize cadence CRASHED channel=${o.slice(0,16)}\u2026 (threshold lock not attempted; retries at the next delivery):`,C)})},async p=>{throw new Error("SellerTab.charge() is not driven by the route handler; the buyer presents a fresh voucher per chunk. Use openSse(res, tab) for the metered-stream pattern.")});O.setSessionPublicKey(l.sessionPublicKey),c.tab=O,m()}catch(l){if(l instanceof w||l instanceof E||l instanceof I||l instanceof _||l instanceof S){a.status(402).json({error:"invalid_voucher",reason:l.reason??"unknown",detail:l.message});return}m(l)}}}function Je(e){if(!e.tab)throw new Error("req.tab is missing \u2014 did tabMiddleware run on this route?");return e.tab}function Ge(e,t){if(!t.tab)throw new Error("openSse requires options.tab");e.headersSent||(e.setHeader("Content-Type","text/event-stream"),e.setHeader("Cache-Control","no-cache"),e.setHeader("Connection","keep-alive"),typeof e.flushHeaders=="function"&&e.flushHeaders());let r=t.tab,n=BigInt(R(r.cumulative())),i=BigInt(R(r.deliveredCumulative())),s=n-i;s<0n&&(s=0n);let h=t.perUnit?BigInt(R(t.perUnit)):null,c=0n,a=!1;async function m(){await r.recordDelivered(c.toString())}e.on("close",()=>{a||(a=!0,m().catch(d=>{console.error("[tab/seller] terminal persist failed on disconnect:",d)}))});async function l(d=1){if(a)throw new Error("meter ended");if(h===null)throw new Error("charge() needs options.perUnit");let f=h*BigInt(d),b=c+f;if(b>s)throw a=!0,await m(),new S("cumulative_exceeds_cap",`chunk would push delivered to ${V((i+b).toString())} beyond signed cumulative ${V(n.toString())} (per-request budget ${V(s.toString())})`);c=b}function o(d){if(a)throw new Error("meter ended");let b=(typeof d=="string"?d:Buffer.from(d).toString("utf8")).replace(/\n/g,"\\n");e.write(`data: ${b}
|
|
2
2
|
|
|
3
|
-
`)}async function
|
|
3
|
+
`)}async function y(){a||(a=!0,await m(),e.write(`event: end
|
|
4
4
|
data: {"chargedAtomic":"${c}"}
|
|
5
5
|
|
|
6
|
-
`),e.end())}return{charge:l,send:a,end:g}}var M=require("fs"),me=require("path");function Nt(e){return{payload:e.payload,sessionPublicKey:Oe(e.sessionPublicKey),sessionRegistration:Oe(e.sessionRegistration),sessionSignature:Oe(e.sessionSignature)}}function It(e){return{payload:e.payload,sessionPublicKey:$e(e.sessionPublicKey),sessionRegistration:$e(e.sessionRegistration),sessionSignature:$e(e.sessionSignature)}}function Oe(e){let t="";for(let r of e)t+=r.toString(16).padStart(2,"0");return t}function $e(e){if(e.length%2!==0)throw new Error(`hex length must be even, got ${e.length}`);let t=new Uint8Array(e.length/2);for(let r=0;r<t.length;r++)t[r]=parseInt(e.substr(r*2,2),16);return t}var le=class{map=new Map;async get(t){return this.map.get(t)??null}async set(t,r){this.map.set(t,r)}async delete(t){this.map.delete(t)}},ue=class{constructor(t){this.dir=t}pathFor(t){if(!/^[a-z0-9_-]+$/i.test(t))throw new Error(`unsafe channelId for filesystem: ${t}`);return(0,me.join)(this.dir,`${t}.json`)}async get(t){try{let r=await M.promises.readFile(this.pathFor(t),"utf8");return It(JSON.parse(r))}catch(r){if(r?.code==="ENOENT")return null;throw r}}async set(t,r){let n=this.pathFor(t);await M.promises.mkdir((0,me.dirname)(n),{recursive:!0});let i=`${n}.tmp`;await M.promises.writeFile(i,JSON.stringify(Nt(r))),await M.promises.rename(i,n)}async delete(t){try{await M.promises.unlink(this.pathFor(t))}catch(r){if(r?.code!=="ENOENT")throw r}}};var nt=require("@solana/web3.js");var R="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",Ye="solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",Ge="solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z";var H="eip155:8453",de="eip155:84532",pe="eip155:42161",he="eip155:137",ge="eip155:10",ye="eip155:43114",fe="eip155:56",Ae="eip155:1187947933",be="eip155:324705682",we="eip155:1";var ve="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";var Ze="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",kt="0x55d398326f99059fF775485246999027B3197955",Qe="0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",et={[fe]:Qe,[H]:Ze,[de]:"0x036CbD53842c5426634e7929541eC2318f3dCF7e",[pe]:"0xaf88d065e77c8cC2239327C5EDb3A432268e5831",[he]:"0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",[ge]:"0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",[ye]:"0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",[Ae]:"0x85889c8c714505E0c94b30fcfcF64fE3Ac8FCb20",[be]:"0x2e08028E3C4c2356572E096d8EF835cD5C6030bD",[we]:"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"},tt={[kt]:{symbol:"USDT",decimals:18},[Qe]:{symbol:"USDC",decimals:18}};var fr={[fe]:56,[H]:8453,[de]:84532,[pe]:42161,[he]:137,[ge]:10,[ye]:43114,[Ae]:1187947933,[be]:324705682,[we]:1},Ar={[R]:"https://api.dexter.cash/api/solana/rpc",[Ye]:"https://api.devnet.solana.com",[Ge]:"https://api.testnet.solana.com"},br={[fe]:"https://api.dexter.cash/api/evm/bsc/rpc",[H]:"https://api.dexter.cash/api/base/rpc",[de]:"https://sepolia.base.org",[pe]:"https://api.dexter.cash/api/evm/arbitrum/rpc",[he]:"https://api.dexter.cash/api/evm/polygon/rpc",[ge]:"https://api.dexter.cash/api/evm/optimism/rpc",[ye]:"https://api.dexter.cash/api/evm/avalanche/rpc",[Ae]:"https://skale-base.skalenodes.com/v1/base",[be]:"https://base-sepolia-testnet.skalenodes.com/v1/jubilant-horrible-ancha",[we]:"https://eth.llamarpc.com"},F="https://x402.dexter.cash";function Se(e){return e.startsWith("solana:")||e==="solana"}function Ut(e){if(typeof Buffer<"u")return Buffer.from(e,"utf-8").toString("base64");let t=new TextEncoder().encode(e),r="";for(let n=0;n<t.length;n++)r+=String.fromCharCode(t[n]);return btoa(r)}function Ot(e){if(typeof Buffer<"u")return Buffer.from(e,"base64").toString("utf-8");let t=atob(e),r=new Uint8Array(t.length);for(let n=0;n<t.length;n++)r[n]=t.charCodeAt(n);return new TextDecoder().decode(r)}function xe(e){return Ut(JSON.stringify(e))}function W(e){return JSON.parse(Ot(e))}function $t(e){if(e instanceof TypeError)return!0;if(e&&typeof e=="object"&&"status"in e){let t=e.status;return t>=500&&t<600}return!1}var j=class extends Error{status;body;constructor(t,r){super(`HTTP ${t}`),this.status=t,this.body=r}},Ce=class{facilitatorUrl;cachedSupported=null;cacheTime=0;CACHE_TTL_MS=6e4;timeoutMs;maxRetries;retryBaseMs;constructor(t=F,r){this.facilitatorUrl=t.replace(/\/$/,""),this.timeoutMs=r?.timeoutMs??1e4,this.maxRetries=r?.maxRetries??3,this.retryBaseMs=r?.retryBaseMs??500}async fetchWithTimeout(t,r){let n=new AbortController,i=setTimeout(()=>n.abort(),this.timeoutMs);try{return await fetch(t,{...r,signal:n.signal})}finally{clearTimeout(i)}}async fetchWithRetry(t,r){let n;for(let i=0;i<this.maxRetries;i++)try{let s=await this.fetchWithTimeout(t,r);if(!s.ok&&s.status>=500)throw new j(s.status,await s.text());return s}catch(s){if(n=s,i<this.maxRetries-1&&$t(s)){let h=this.retryBaseMs*Math.pow(2,i);await new Promise(c=>setTimeout(c,h));continue}throw s}throw n}async getSupported(){let t=Date.now();if(this.cachedSupported&&t-this.cacheTime<this.CACHE_TTL_MS)return this.cachedSupported;let r=await this.fetchWithTimeout(`${this.facilitatorUrl}/supported`);if(!r.ok)throw new Error(`Facilitator /supported returned ${r.status}`);return this.cachedSupported=await r.json(),this.cacheTime=t,this.cachedSupported}async getFeePayer(t){let n=(await this.getSupported()).kinds.find(i=>i.x402Version===2&&(i.scheme==="exact"||i.scheme==="exact-approval")&&i.network===t);if(!n)throw new Error(`Facilitator does not support network "${t}" with a recognized scheme`);return n.extra?.feePayer}async getNetworkExtra(t){return(await this.getSupported()).kinds.find(i=>i.x402Version===2&&(i.scheme==="exact"||i.scheme==="exact-approval"||i.scheme==="batch-settlement")&&i.network===t)?.extra}async verifyPayment(t,r){try{let n=W(t),i=await this.fetchWithRetry(`${this.facilitatorUrl}/verify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({x402Version:2,paymentPayload:n,paymentRequirements:r})});return i.ok?await i.json():{isValid:!1,invalidReason:`facilitator_error_${i.status}`}}catch(n){return{isValid:!1,invalidReason:n instanceof j?`facilitator_error_${n.status}`:n instanceof Error&&n.name==="AbortError"?"facilitator_timeout":n instanceof Error?n.message:"unexpected_verify_error"}}}async settlePayment(t,r){try{let n=W(t),i=await this.fetchWithRetry(`${this.facilitatorUrl}/settle`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({x402Version:2,paymentPayload:n,paymentRequirements:r})});return i.ok?{...await i.json(),network:r.network}:{success:!1,network:r.network,errorReason:`facilitator_error_${i.status}`}}catch(n){let i=n instanceof j?`facilitator_error_${n.status}`:n instanceof Error&&n.name==="AbortError"?"facilitator_timeout":n instanceof Error?n.message:"unexpected_settle_error";return{success:!1,network:r.network,errorReason:i}}}};function Vt(e){if(Se(e))return{address:ve,decimals:6};let t=et[e];if(t){let r=tt[t]?.decimals??6;return{address:t,decimals:r}}return{address:ve,decimals:6}}function rt(e){try{let t=W(e);return t?.accepted?.amount??t?.accepted?.maxAmountRequired}catch{return}}function X(e){let{payTo:t,facilitatorUrl:r=F,network:n=R,defaultTimeoutSeconds:i=60}=e,s=e.asset??Vt(n),h=e.scheme??"exact";if(h==="tab"&&!Se(n))throw new Error(`scheme 'tab' is SVM-only; got network "${n}"`);let c=new Ce(r),o=null,d=new Map,l=3e4,a=Date.now();function g(u){let p=(u.maxTimeoutSeconds||i)*1e3;if(d.set(u.payTo,{accept:u,expiresAt:Date.now()+p}),Date.now()-a>l){let A=Date.now();for(let[x,k]of d)k.expiresAt<A&&d.delete(x);a=A}}function m(u){let p=d.get(u);if(p){if(p.expiresAt<Date.now()){d.delete(u);return}return p.accept}}async function f(u){return typeof t=="string"?t:t(u||{})}async function b(){if(o||(o=await c.getNetworkExtra(n)),Se(n)&&!o?.feePayer)throw new Error(`Facilitator does not provide feePayer for network "${n}"`);return{...o?.feePayer?{feePayer:o.feePayer}:{},decimals:o?.decimals??s.decimals,name:o?.name,version:o?.version,...h==="batch-settlement"&&o?.receiverAuthorizer?{receiverAuthorizer:o.receiverAuthorizer}:{},...h==="tab"?{voucherHeader:"x-tab-voucher",registrationEncoding:"base64(188-byte sessionRegisterMessage)"}:{}}}async function L(u,p){let{amountAtomic:A,timeoutSeconds:x=i}=p,k=await b(),U={scheme:h,network:n,amount:A,maxAmountRequired:A,asset:s.address,payTo:u,maxTimeoutSeconds:x,extra:k};return g(U),U}async function D(u){let p=await f({amountAtomic:u.amountAtomic,resourceUrl:u.resourceUrl});return L(p,u)}async function Ee(u){let{resourceUrl:p,description:A,mimeType:x="application/json"}=u,k={url:p,description:A,mimeType:x},U=await D(u);return{x402Version:2,resource:k,accepts:[U],error:"Payment required"}}function q(u){return xe(u)}function J(u){return{status:402,headers:{"PAYMENT-REQUIRED":q(u)},body:{}}}async function Pe(u,p){if(!p){let A=await f({paymentHeader:u});p=m(A),p||(p=await L(A,{amountAtomic:rt(u)??"0",resourceUrl:""}))}return c.verifyPayment(u,p)}async function Y(u,p){if(!p){let A=await f({paymentHeader:u});p=m(A),p||(p=await L(A,{amountAtomic:rt(u)??"0",resourceUrl:""}))}return c.settlePayment(u,p)}return{buildRequirements:Ee,encodeRequirements:q,create402Response:J,verifyPayment:Pe,settlePayment:Y,getPaymentAccept:D,network:n,assetDecimals:s.decimals,facilitator:c}}var Bt={"solana:mainnet":R};function it(e){let t=Bt[e.network];if(!t)throw new Error(`tabChallengeMiddleware: unsupported network "${e.network}"`);let r=typeof e.sellerPubkey=="string"?e.sellerPubkey:e.sellerPubkey.toBase58();new nt.PublicKey(r);let n=X({payTo:r,network:t,scheme:"tab",facilitatorUrl:e.facilitatorUrl}),i=E(e.perUnit);return async(s,h,c)=>{if(s.headers[I])return c();try{let o=`${s.protocol}://${s.get("host")}${s.originalUrl}`,d=await n.buildRequirements({amountAtomic:i,resourceUrl:o,description:e.description}),l=n.create402Response(d);h.set(l.headers).status(l.status).json(l.body)}catch(o){let d=o?.message??String(o);h.status(503).set({"Retry-After":"5"}).json({error:"challenge_unavailable",detail:d})}}}var st=require("@solana/web3.js");var Kt={"solana:mainnet":R};function ot(e){let t=Kt[e.network];if(!t)throw new Error(`tabOrExactMiddleware: unsupported network "${e.network}"`);let r=typeof e.sellerPubkey=="string"?e.sellerPubkey:e.sellerPubkey.toBase58();new st.PublicKey(r);let n=X({payTo:r,network:t,scheme:"tab",facilitatorUrl:e.facilitatorUrl}),i=X({payTo:r,network:t,scheme:"exact",facilitatorUrl:e.facilitatorUrl}),s=E(e.perUnit),h=ce({connection:e.connection,sellerPubkey:r,network:e.network,perUnit:e.perUnit,settle:"on-close",facilitatorUrl:e.facilitatorUrl,lockCadence:e.lockCadence});return async(c,o,d)=>{if(c.headers[I])return h(c,o,d);let l=`${c.protocol}://${c.get("host")}${c.originalUrl}`,a=c.headers["payment-signature"];if(a){let g;try{g=await i.getPaymentAccept({amountAtomic:s,resourceUrl:l,description:e.description})}catch(m){let f=m?.message??String(m);o.status(503).set({"Retry-After":"5"}).json({error:"challenge_unavailable",detail:f});return}try{let m=await i.verifyPayment(a,g);if(!m.isValid){o.status(402).json({error:"Payment verification failed",reason:m.invalidReason});return}let f=await i.settlePayment(a,g);if(!f.success){o.status(402).json({error:"Payment settlement failed",reason:f.errorReason});return}return c.x402={transaction:f.transaction,payer:m.payer??"",network:f.network||t},o.setHeader("PAYMENT-RESPONSE",xe({success:!0,transaction:f.transaction,network:f.network||t,payer:m.payer??""})),d()}catch{o.status(500).json({error:"Payment processing error"});return}}try{let g={amountAtomic:s,resourceUrl:l,description:e.description},[m,f]=await Promise.all([n.buildRequirements(g),i.buildRequirements(g)]),b={...m,accepts:[...m.accepts,...f.accepts]};o.set({"PAYMENT-REQUIRED":n.encodeRequirements(b)}).status(402).json({error:"Payment required",accepts:b.accepts,resource:b.resource})}catch(g){let m=g?.message??String(g);o.status(503).set({"Retry-After":"5"}).json({error:"challenge_unavailable",detail:m})}}}0&&(module.exports={FileChannelLedger,FileVoucherStore,InMemoryChannelLedger,InMemoryVoucherStore,InvalidRegistrationError,InvalidVoucherError,InvalidVoucherSignatureError,OnChainVerificationError,ScopeViolationError,TAB_VOUCHER_HEADER,enforceScope,openSse,parseRegistration,requireTab,tabChallengeMiddleware,tabMiddleware,tabOrExactMiddleware,verifyRegistrationOnChain,verifyVoucherSignature});
|
|
6
|
+
`),e.end())}return{charge:l,send:o,end:y}}var M=require("fs"),de=require("path");function kt(e){return{payload:e.payload,sessionPublicKey:$e(e.sessionPublicKey),sessionRegistration:$e(e.sessionRegistration),sessionSignature:$e(e.sessionSignature)}}function Ut(e){return{payload:e.payload,sessionPublicKey:Ve(e.sessionPublicKey),sessionRegistration:Ve(e.sessionRegistration),sessionSignature:Ve(e.sessionSignature)}}function $e(e){let t="";for(let r of e)t+=r.toString(16).padStart(2,"0");return t}function Ve(e){if(e.length%2!==0)throw new Error(`hex length must be even, got ${e.length}`);let t=new Uint8Array(e.length/2);for(let r=0;r<t.length;r++)t[r]=parseInt(e.substr(r*2,2),16);return t}var ue=class{map=new Map;async get(t){return this.map.get(t)??null}async set(t,r){this.map.set(t,r)}async delete(t){this.map.delete(t)}},me=class{constructor(t){this.dir=t}pathFor(t){if(!/^[a-z0-9_-]+$/i.test(t))throw new Error(`unsafe channelId for filesystem: ${t}`);return(0,de.join)(this.dir,`${t}.json`)}async get(t){try{let r=await M.promises.readFile(this.pathFor(t),"utf8");return Ut(JSON.parse(r))}catch(r){if(r?.code==="ENOENT")return null;throw r}}async set(t,r){let n=this.pathFor(t);await M.promises.mkdir((0,de.dirname)(n),{recursive:!0});let i=`${n}.tmp`;await M.promises.writeFile(i,JSON.stringify(kt(r))),await M.promises.rename(i,n)}async delete(t){try{await M.promises.unlink(this.pathFor(t))}catch(r){if(r?.code!=="ENOENT")throw r}}};var it=require("@solana/web3.js");var N="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",Ye="solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",Ze="solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z";var q="eip155:8453",pe="eip155:84532",ge="eip155:42161",he="eip155:137",ye="eip155:10",fe="eip155:43114",Ae="eip155:56",be="eip155:1187947933",ve="eip155:324705682",we="eip155:1";var Se="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";var Qe="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",Ot="0x55d398326f99059fF775485246999027B3197955",et="0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",tt={[Ae]:et,[q]:Qe,[pe]:"0x036CbD53842c5426634e7929541eC2318f3dCF7e",[ge]:"0xaf88d065e77c8cC2239327C5EDb3A432268e5831",[he]:"0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",[ye]:"0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",[fe]:"0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",[be]:"0x85889c8c714505E0c94b30fcfcF64fE3Ac8FCb20",[ve]:"0x2e08028E3C4c2356572E096d8EF835cD5C6030bD",[we]:"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"},rt={[Ot]:{symbol:"USDT",decimals:18},[et]:{symbol:"USDC",decimals:18}};var br={[Ae]:56,[q]:8453,[pe]:84532,[ge]:42161,[he]:137,[ye]:10,[fe]:43114,[be]:1187947933,[ve]:324705682,[we]:1},vr={[N]:"https://api.dexter.cash/api/solana/rpc",[Ye]:"https://api.devnet.solana.com",[Ze]:"https://api.testnet.solana.com"},wr={[Ae]:"https://api.dexter.cash/api/evm/bsc/rpc",[q]:"https://api.dexter.cash/api/base/rpc",[pe]:"https://sepolia.base.org",[ge]:"https://api.dexter.cash/api/evm/arbitrum/rpc",[he]:"https://api.dexter.cash/api/evm/polygon/rpc",[ye]:"https://api.dexter.cash/api/evm/optimism/rpc",[fe]:"https://api.dexter.cash/api/evm/avalanche/rpc",[be]:"https://skale-base.skalenodes.com/v1/base",[ve]:"https://base-sepolia-testnet.skalenodes.com/v1/jubilant-horrible-ancha",[we]:"https://eth.llamarpc.com"},F="https://x402.dexter.cash";function xe(e){return e.startsWith("solana:")||e==="solana"}function $t(e){if(typeof Buffer<"u")return Buffer.from(e,"utf-8").toString("base64");let t=new TextEncoder().encode(e),r="";for(let n=0;n<t.length;n++)r+=String.fromCharCode(t[n]);return btoa(r)}function Vt(e){if(typeof Buffer<"u")return Buffer.from(e,"base64").toString("utf-8");let t=atob(e),r=new Uint8Array(t.length);for(let n=0;n<t.length;n++)r[n]=t.charCodeAt(n);return new TextDecoder().decode(r)}function Ce(e){return $t(JSON.stringify(e))}function W(e){return JSON.parse(Vt(e))}function Bt(e){if(e instanceof TypeError)return!0;if(e&&typeof e=="object"&&"status"in e){let t=e.status;return t>=500&&t<600}return!1}var j=class extends Error{status;body;constructor(t,r){super(`HTTP ${t}`),this.status=t,this.body=r}},Ee=class{facilitatorUrl;cachedSupported=null;cacheTime=0;CACHE_TTL_MS=6e4;timeoutMs;maxRetries;retryBaseMs;constructor(t=F,r){this.facilitatorUrl=t.replace(/\/$/,""),this.timeoutMs=r?.timeoutMs??1e4,this.maxRetries=r?.maxRetries??3,this.retryBaseMs=r?.retryBaseMs??500}async fetchWithTimeout(t,r){let n=new AbortController,i=setTimeout(()=>n.abort(),this.timeoutMs);try{return await fetch(t,{...r,signal:n.signal})}finally{clearTimeout(i)}}async fetchWithRetry(t,r){let n;for(let i=0;i<this.maxRetries;i++)try{let s=await this.fetchWithTimeout(t,r);if(!s.ok&&s.status>=500)throw new j(s.status,await s.text());return s}catch(s){if(n=s,i<this.maxRetries-1&&Bt(s)){let h=this.retryBaseMs*Math.pow(2,i);await new Promise(c=>setTimeout(c,h));continue}throw s}throw n}async getSupported(){let t=Date.now();if(this.cachedSupported&&t-this.cacheTime<this.CACHE_TTL_MS)return this.cachedSupported;let r=await this.fetchWithTimeout(`${this.facilitatorUrl}/supported`);if(!r.ok)throw new Error(`Facilitator /supported returned ${r.status}`);return this.cachedSupported=await r.json(),this.cacheTime=t,this.cachedSupported}async getFeePayer(t){let n=(await this.getSupported()).kinds.find(i=>i.x402Version===2&&(i.scheme==="exact"||i.scheme==="exact-approval")&&i.network===t);if(!n)throw new Error(`Facilitator does not support network "${t}" with a recognized scheme`);return n.extra?.feePayer}async getNetworkExtra(t){return(await this.getSupported()).kinds.find(i=>i.x402Version===2&&(i.scheme==="exact"||i.scheme==="exact-approval"||i.scheme==="batch-settlement")&&i.network===t)?.extra}async verifyPayment(t,r){try{let n=W(t),i=await this.fetchWithRetry(`${this.facilitatorUrl}/verify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({x402Version:2,paymentPayload:n,paymentRequirements:r})});return i.ok?await i.json():{isValid:!1,invalidReason:`facilitator_error_${i.status}`}}catch(n){return{isValid:!1,invalidReason:n instanceof j?`facilitator_error_${n.status}`:n instanceof Error&&n.name==="AbortError"?"facilitator_timeout":n instanceof Error?n.message:"unexpected_verify_error"}}}async settlePayment(t,r){try{let n=W(t),i=await this.fetchWithRetry(`${this.facilitatorUrl}/settle`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({x402Version:2,paymentPayload:n,paymentRequirements:r})});return i.ok?{...await i.json(),network:r.network}:{success:!1,network:r.network,errorReason:`facilitator_error_${i.status}`}}catch(n){let i=n instanceof j?`facilitator_error_${n.status}`:n instanceof Error&&n.name==="AbortError"?"facilitator_timeout":n instanceof Error?n.message:"unexpected_settle_error";return{success:!1,network:r.network,errorReason:i}}}};function Lt(e){if(xe(e))return{address:Se,decimals:6};let t=tt[e];if(t){let r=rt[t]?.decimals??6;return{address:t,decimals:r}}return{address:Se,decimals:6}}function nt(e){try{let t=W(e);return t?.accepted?.amount??t?.accepted?.maxAmountRequired}catch{return}}function X(e){let{payTo:t,facilitatorUrl:r=F,network:n=N,defaultTimeoutSeconds:i=60}=e,s=e.asset??Lt(n),h=e.scheme??"exact";if(h==="tab"&&!xe(n))throw new Error(`scheme 'tab' is SVM-only; got network "${n}"`);let c=new Ee(r),a=null,m=new Map,l=3e4,o=Date.now();function y(u){let g=(u.maxTimeoutSeconds||i)*1e3;if(m.set(u.payTo,{accept:u,expiresAt:Date.now()+g}),Date.now()-o>l){let A=Date.now();for(let[x,U]of m)U.expiresAt<A&&m.delete(x);o=A}}function d(u){let g=m.get(u);if(g){if(g.expiresAt<Date.now()){m.delete(u);return}return g.accept}}async function f(u){return typeof t=="string"?t:t(u||{})}async function b(){if(a||(a=await c.getNetworkExtra(n)),xe(n)&&!a?.feePayer)throw new Error(`Facilitator does not provide feePayer for network "${n}"`);return{...a?.feePayer?{feePayer:a.feePayer}:{},decimals:a?.decimals??s.decimals,name:a?.name,version:a?.version,...h==="batch-settlement"&&a?.receiverAuthorizer?{receiverAuthorizer:a.receiverAuthorizer}:{},...h==="tab"?{voucherHeader:"x-tab-voucher",registrationEncoding:"base64(188-byte sessionRegisterMessage)"}:{}}}async function D(u,g){let{amountAtomic:A,timeoutSeconds:x=i}=g,U=await b(),O={scheme:h,network:n,amount:A,maxAmountRequired:A,asset:s.address,payTo:u,maxTimeoutSeconds:x,extra:U};return y(O),O}async function z(u){let g=await f({amountAtomic:u.amountAtomic,resourceUrl:u.resourceUrl});return D(g,u)}async function Re(u){let{resourceUrl:g,description:A,mimeType:x="application/json"}=u,U={url:g,description:A,mimeType:x},O=await z(u);return{x402Version:2,resource:U,accepts:[O],error:"Payment required"}}function H(u){return Ce(u)}function J(u){return{status:402,headers:{"PAYMENT-REQUIRED":H(u)},body:{}}}async function Pe(u,g){if(!g){let A=await f({paymentHeader:u});g=d(A),g||(g=await D(A,{amountAtomic:nt(u)??"0",resourceUrl:""}))}return c.verifyPayment(u,g)}async function G(u,g){if(!g){let A=await f({paymentHeader:u});g=d(A),g||(g=await D(A,{amountAtomic:nt(u)??"0",resourceUrl:""}))}return c.settlePayment(u,g)}return{buildRequirements:Re,encodeRequirements:H,create402Response:J,verifyPayment:Pe,settlePayment:G,getPaymentAccept:z,network:n,assetDecimals:s.decimals,facilitator:c}}var Kt={"solana:mainnet":N};function st(e){let t=Kt[e.network];if(!t)throw new Error(`tabChallengeMiddleware: unsupported network "${e.network}"`);let r=typeof e.sellerPubkey=="string"?e.sellerPubkey:e.sellerPubkey.toBase58();new it.PublicKey(r);let n=X({payTo:r,network:t,scheme:"tab",facilitatorUrl:e.facilitatorUrl}),i=R(e.perUnit);return async(s,h,c)=>{if(s.headers[k])return c();try{let a=`${s.protocol}://${s.get("host")}${s.originalUrl}`,m=await n.buildRequirements({amountAtomic:i,resourceUrl:a,description:e.description}),l=n.create402Response(m);h.set(l.headers).status(l.status).json(l.body)}catch(a){let m=a?.message??String(a);h.status(503).set({"Retry-After":"5"}).json({error:"challenge_unavailable",detail:m})}}}var ot=require("@solana/web3.js");var Mt={"solana:mainnet":N};function at(e){let t=Mt[e.network];if(!t)throw new Error(`tabOrExactMiddleware: unsupported network "${e.network}"`);let r=typeof e.sellerPubkey=="string"?e.sellerPubkey:e.sellerPubkey.toBase58();new ot.PublicKey(r);let n=X({payTo:r,network:t,scheme:"tab",facilitatorUrl:e.facilitatorUrl}),i=X({payTo:r,network:t,scheme:"exact",facilitatorUrl:e.facilitatorUrl}),s=R(e.perUnit),h=le({connection:e.connection,sellerPubkey:r,network:e.network,perUnit:e.perUnit,settle:"on-close",facilitatorUrl:e.facilitatorUrl,lockCadence:e.lockCadence});return async(c,a,m)=>{if(c.headers[k])return h(c,a,m);let l=`${c.protocol}://${c.get("host")}${c.originalUrl}`,o=c.headers["payment-signature"];if(o){let y;try{y=await i.getPaymentAccept({amountAtomic:s,resourceUrl:l,description:e.description})}catch(d){let f=d?.message??String(d);a.status(503).set({"Retry-After":"5"}).json({error:"challenge_unavailable",detail:f});return}try{let d=await i.verifyPayment(o,y);if(!d.isValid){a.status(402).json({error:"Payment verification failed",reason:d.invalidReason});return}let f=await i.settlePayment(o,y);if(!f.success){a.status(402).json({error:"Payment settlement failed",reason:f.errorReason});return}return c.x402={transaction:f.transaction,payer:d.payer??"",network:f.network||t},a.setHeader("PAYMENT-RESPONSE",Ce({success:!0,transaction:f.transaction,network:f.network||t,payer:d.payer??""})),m()}catch{a.status(500).json({error:"Payment processing error"});return}}try{let y={amountAtomic:s,resourceUrl:l,description:e.description},[d,f]=await Promise.all([n.buildRequirements(y),i.buildRequirements(y)]),b={...d,accepts:[...d.accepts,...f.accepts]};a.set({"PAYMENT-REQUIRED":n.encodeRequirements(b)}).status(402).json({error:"Payment required",accepts:b.accepts,resource:b.resource})}catch(y){let d=y?.message??String(y);a.status(503).set({"Retry-After":"5"}).json({error:"challenge_unavailable",detail:d})}}}0&&(module.exports={FileChannelLedger,FileVoucherStore,InMemoryChannelLedger,InMemoryVoucherStore,InvalidRegistrationError,InvalidVoucherError,InvalidVoucherSignatureError,OnChainVerificationError,ScopeViolationError,TAB_VOUCHER_HEADER,enforceScope,openSse,parseRegistration,requireTab,tabChallengeMiddleware,tabMiddleware,tabOrExactMiddleware,verifyRegistrationOnChain,verifyVoucherSignature});
|
|
@@ -71,6 +71,17 @@ interface ChannelLedgerEntry {
|
|
|
71
71
|
* pre-Step-4 ledger constructors remain valid without a breaking change.
|
|
72
72
|
*/
|
|
73
73
|
lastCrystallizedCumulativeAtomic?: AtomicAmount;
|
|
74
|
+
/**
|
|
75
|
+
* Gate-refused watermark (cadence spec §5 [A12]): the highest voucher
|
|
76
|
+
* cumulative (atomic) the facilitator's router gate REFUSED with
|
|
77
|
+
* `below_lock_cadence`. The crystallization cadence skips re-attempting
|
|
78
|
+
* any span at or below it — the facilitator's server-side engine already
|
|
79
|
+
* guarantees the protection cadence, so re-asking about the identical
|
|
80
|
+
* refused span on every delivery/close is a retry storm, not protection.
|
|
81
|
+
* A NEW signed voucher (higher cumulative) always re-attempts; a
|
|
82
|
+
* successful lock clears it. Absent on entries that were never refused.
|
|
83
|
+
*/
|
|
84
|
+
gateRefusedCumulativeAtomic?: AtomicAmount;
|
|
74
85
|
/** RESERVED (Step 4): on-chain money ledger snapshot. Unset today. */
|
|
75
86
|
onChain?: OnChainLedgerSnapshot;
|
|
76
87
|
/**
|
|
@@ -205,6 +216,18 @@ interface TabMiddlewareOptions {
|
|
|
205
216
|
* crystallize never blocks or errors the seller's response; a missed lock
|
|
206
217
|
* just widens the seller's unsecured window (their risk dial).
|
|
207
218
|
*
|
|
219
|
+
* ADVISORY as of 5.3.1: the protection cadence is becoming
|
|
220
|
+
* FACILITATOR-owned — a server-side engine that fires locks at the
|
|
221
|
+
* operator's on-chain intent knob (a penny) for every seller, whatever
|
|
222
|
+
* this client-side setting says. This cadence defers to that engine as it
|
|
223
|
+
* rolls out; until it is live on your facilitator, this setting is still
|
|
224
|
+
* your mid-stream protection, and it remains the seller's own
|
|
225
|
+
* lock-more-aggressively dial either way. A cadence-gated
|
|
226
|
+
* facilitator may refuse sub-threshold seller-initiated locks with
|
|
227
|
+
* `below_lock_cadence` — benign (the engine already protected you); the
|
|
228
|
+
* SDK records a gate-refused watermark so the same span is never
|
|
229
|
+
* re-attempted (see `ChannelLedgerEntry.gateRefusedCumulativeAtomic`).
|
|
230
|
+
*
|
|
208
231
|
* Defaults when omitted: `{ thresholdAtomic: humanToAtomic('0.10'),
|
|
209
232
|
* onClose: true }`. Set `thresholdAtomic` higher to crystallize less often
|
|
210
233
|
* (cheaper, wider window) or lower to lock more aggressively. Set
|
|
@@ -518,8 +541,11 @@ interface TabOrExactConfig {
|
|
|
518
541
|
/**
|
|
519
542
|
* Keyless crystallization cadence for the tab rail — forwarded verbatim to
|
|
520
543
|
* `tabMiddleware` (see `TabMiddlewareOptions.lockCadence`). This is the
|
|
521
|
-
* seller's on-chain LockVoucher dial (mid-stream protection)
|
|
522
|
-
*
|
|
544
|
+
* seller's own on-chain LockVoucher dial (mid-stream protection); ADVISORY
|
|
545
|
+
* as of 5.3.1 — it defers to the facilitator-owned cadence engine as that
|
|
546
|
+
* engine rolls out server-side, and stays your mid-stream protection until
|
|
547
|
+
* it is live on your facilitator. Omitted → armed by default
|
|
548
|
+
* (`thresholdAtomic: humanToAtomic('0.10'), onClose: true`). Pass
|
|
523
549
|
* `{ onClose: false }` to disarm the close-time lock, or `{ thresholdAtomic }`
|
|
524
550
|
* to change the threshold cadence.
|
|
525
551
|
*/
|
|
@@ -71,6 +71,17 @@ interface ChannelLedgerEntry {
|
|
|
71
71
|
* pre-Step-4 ledger constructors remain valid without a breaking change.
|
|
72
72
|
*/
|
|
73
73
|
lastCrystallizedCumulativeAtomic?: AtomicAmount;
|
|
74
|
+
/**
|
|
75
|
+
* Gate-refused watermark (cadence spec §5 [A12]): the highest voucher
|
|
76
|
+
* cumulative (atomic) the facilitator's router gate REFUSED with
|
|
77
|
+
* `below_lock_cadence`. The crystallization cadence skips re-attempting
|
|
78
|
+
* any span at or below it — the facilitator's server-side engine already
|
|
79
|
+
* guarantees the protection cadence, so re-asking about the identical
|
|
80
|
+
* refused span on every delivery/close is a retry storm, not protection.
|
|
81
|
+
* A NEW signed voucher (higher cumulative) always re-attempts; a
|
|
82
|
+
* successful lock clears it. Absent on entries that were never refused.
|
|
83
|
+
*/
|
|
84
|
+
gateRefusedCumulativeAtomic?: AtomicAmount;
|
|
74
85
|
/** RESERVED (Step 4): on-chain money ledger snapshot. Unset today. */
|
|
75
86
|
onChain?: OnChainLedgerSnapshot;
|
|
76
87
|
/**
|
|
@@ -205,6 +216,18 @@ interface TabMiddlewareOptions {
|
|
|
205
216
|
* crystallize never blocks or errors the seller's response; a missed lock
|
|
206
217
|
* just widens the seller's unsecured window (their risk dial).
|
|
207
218
|
*
|
|
219
|
+
* ADVISORY as of 5.3.1: the protection cadence is becoming
|
|
220
|
+
* FACILITATOR-owned — a server-side engine that fires locks at the
|
|
221
|
+
* operator's on-chain intent knob (a penny) for every seller, whatever
|
|
222
|
+
* this client-side setting says. This cadence defers to that engine as it
|
|
223
|
+
* rolls out; until it is live on your facilitator, this setting is still
|
|
224
|
+
* your mid-stream protection, and it remains the seller's own
|
|
225
|
+
* lock-more-aggressively dial either way. A cadence-gated
|
|
226
|
+
* facilitator may refuse sub-threshold seller-initiated locks with
|
|
227
|
+
* `below_lock_cadence` — benign (the engine already protected you); the
|
|
228
|
+
* SDK records a gate-refused watermark so the same span is never
|
|
229
|
+
* re-attempted (see `ChannelLedgerEntry.gateRefusedCumulativeAtomic`).
|
|
230
|
+
*
|
|
208
231
|
* Defaults when omitted: `{ thresholdAtomic: humanToAtomic('0.10'),
|
|
209
232
|
* onClose: true }`. Set `thresholdAtomic` higher to crystallize less often
|
|
210
233
|
* (cheaper, wider window) or lower to lock more aggressively. Set
|
|
@@ -518,8 +541,11 @@ interface TabOrExactConfig {
|
|
|
518
541
|
/**
|
|
519
542
|
* Keyless crystallization cadence for the tab rail — forwarded verbatim to
|
|
520
543
|
* `tabMiddleware` (see `TabMiddlewareOptions.lockCadence`). This is the
|
|
521
|
-
* seller's on-chain LockVoucher dial (mid-stream protection)
|
|
522
|
-
*
|
|
544
|
+
* seller's own on-chain LockVoucher dial (mid-stream protection); ADVISORY
|
|
545
|
+
* as of 5.3.1 — it defers to the facilitator-owned cadence engine as that
|
|
546
|
+
* engine rolls out server-side, and stays your mid-stream protection until
|
|
547
|
+
* it is live on your facilitator. Omitted → armed by default
|
|
548
|
+
* (`thresholdAtomic: humanToAtomic('0.10'), onClose: true`). Pass
|
|
523
549
|
* `{ onClose: false }` to disarm the close-time lock, or `{ thresholdAtomic }`
|
|
524
550
|
* to change the threshold cadence.
|
|
525
551
|
*/
|