@piprail/sdk 1.20.0 → 1.21.0

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/index.js CHANGED
@@ -533,6 +533,37 @@ var EXACT_NETWORK_SLUGS = {
533
533
  function chainIdForExactNetwork(slug) {
534
534
  return EXACT_NETWORK_SLUGS[slug] ?? null;
535
535
  }
536
+ async function resolveExactRailEvm(input) {
537
+ const { asset, method, readDomain, permit2Supported } = input;
538
+ if (asset === "native") return null;
539
+ const want = method === "eip3009" || method === "permit2" ? method : "auto";
540
+ let chosen;
541
+ let extra = {};
542
+ if (want === "permit2") {
543
+ chosen = "permit2";
544
+ } else {
545
+ const d = await readDomain(asset);
546
+ if (d) {
547
+ chosen = "eip3009";
548
+ extra = { name: d.name, version: d.version };
549
+ } else if (want === "eip3009") {
550
+ throw new Error(
551
+ `requirePayment: exact \`method: 'eip3009'\` requested for ${asset}, but it isn't an EIP-3009 token (no name()/version()/authorizationState). Use \`method: 'permit2'\` (any ERC-20, e.g. Binance-Peg USDC on BNB) or \`'auto'\`. (Or check your rpcUrl is reachable.)`
552
+ );
553
+ } else {
554
+ chosen = "permit2";
555
+ }
556
+ }
557
+ if (chosen === "permit2" && !permit2Supported()) {
558
+ if (method === "permit2") {
559
+ throw new Error(
560
+ `requirePayment: exact \`method: 'permit2'\` needs the x402 Permit2 proxy deployed on this chain, but it isn't there. Offer an EIP-3009 token (gasless, no proxy), or drop \`exact\` on this chain. (See PERMIT2_PROXY_CHAIN_IDS.)`
561
+ );
562
+ }
563
+ return null;
564
+ }
565
+ return { method: chosen, extra };
566
+ }
536
567
  var EIP3009_TYPES = {
537
568
  TransferWithAuthorization: [
538
569
  { name: "from", type: "address" },
@@ -1417,11 +1448,14 @@ function parseExactPaymentHeader(value) {
1417
1448
  if (typeof network !== "string") return null;
1418
1449
  const payload = v.payload;
1419
1450
  if (!payload || typeof payload !== "object") return null;
1420
- const signature = payload.signature;
1421
- if (typeof signature !== "string") return null;
1422
1451
  const x402Version = typeof v.x402Version === "number" ? v.x402Version : 2;
1423
1452
  const asset = accepted && typeof accepted.asset === "string" ? accepted.asset : void 0;
1424
1453
  const base2 = { x402Version, network, ...asset ? { asset } : {}, raw: v };
1454
+ if (typeof payload.transaction === "string") {
1455
+ return { ...base2, method: "svm", payload: { transaction: payload.transaction } };
1456
+ }
1457
+ const signature = payload.signature;
1458
+ if (typeof signature !== "string") return null;
1425
1459
  const authorization = payload.authorization;
1426
1460
  if (authorization && typeof authorization === "object") {
1427
1461
  for (const k of ["from", "to", "value", "validAfter", "validBefore", "nonce"]) {
@@ -1708,6 +1742,16 @@ function makeEvmNetwork(resolved) {
1708
1742
  exactPermit2Supported() {
1709
1743
  return isPermit2ProxyChain(resolved.chainId);
1710
1744
  },
1745
+ // The gate's rail-advertisement SPI — EIP-3009 vs Permit2 selection (the pure helper),
1746
+ // injecting the on-chain domain read + the Permit2 proxy-presence check.
1747
+ async resolveExactRail({ asset, method }) {
1748
+ return resolveExactRailEvm({
1749
+ asset,
1750
+ method,
1751
+ readDomain: (a) => readExactDomain(publicClient, a),
1752
+ permit2Supported: () => isPermit2ProxyChain(resolved.chainId)
1753
+ });
1754
+ },
1711
1755
  async settleExactSelf({ relayer, payload, accept }) {
1712
1756
  const a = relayer._native;
1713
1757
  if ("permit2Authorization" in payload) {
@@ -1720,6 +1764,9 @@ function makeEvmNetwork(resolved) {
1720
1764
  accept
1721
1765
  });
1722
1766
  }
1767
+ if ("transaction" in payload) {
1768
+ return { ok: false, error: "signature_invalid", detail: "An SVM (Solana) payload was submitted to an EVM exact rail." };
1769
+ }
1723
1770
  return verifyAndSettleExactEvm({
1724
1771
  publicClient,
1725
1772
  walletClient: a.walletClient,
@@ -1744,7 +1791,7 @@ var loaders = {
1744
1791
  solana: async () => {
1745
1792
  let mod;
1746
1793
  try {
1747
- mod = await import("./solana-WDKWWF33.js");
1794
+ mod = await import("./solana-4EMMGGDR.js");
1748
1795
  } catch (cause) {
1749
1796
  throw new MissingDriverError(
1750
1797
  `Solana selected, but its packages aren't installed. Run: npm install @solana/web3.js @solana/spl-token bs58`,
@@ -2770,7 +2817,7 @@ var PipRailClient = class {
2770
2817
  * before publishing, so retry with a brief backoff if a fresh listing is missing.
2771
2818
  * - Results are cross-scheme (mostly the mainstream `exact` scheme); `fetch()` pays
2772
2819
  * `onchain-proof` rails by default, and standard `exact` rails too once you opt in
2773
- * with `schemes: ['onchain-proof', 'exact']` (EVM + EIP-3009 USDC/EURC).
2820
+ * with `schemes: ['onchain-proof', 'exact']` (EVM EIP-3009/Permit2 + Solana SVM).
2774
2821
  */
2775
2822
  async discover(opts = {}) {
2776
2823
  const found = await searchOpenIndexes({
@@ -2955,7 +3002,7 @@ var PipRailClient = class {
2955
3002
  );
2956
3003
  if (schemes.includes("exact") && exactOnNet && typeof net.payExact !== "function") {
2957
3004
  throw new UnsupportedSchemeError(
2958
- `This 402 offers a standard 'exact' rail on ${net.network}, but the ${net.family} family can't pay 'exact' (EVM + EIP-3009 only), and no 'onchain-proof' rail was offered.`
3005
+ `This 402 offers a standard 'exact' rail on ${net.network}, but the ${net.family} family can't pay 'exact' (supported on EVM + Solana today), and no 'onchain-proof' rail was offered.`
2959
3006
  );
2960
3007
  }
2961
3008
  if (!schemes.includes("exact") && exactOnNet && typeof net.payExact === "function") {
@@ -3335,7 +3382,7 @@ var PipRailClient = class {
3335
3382
  async payExactRail(net, wallet, accept, url, init, quote) {
3336
3383
  if (!net.payExact) {
3337
3384
  throw new UnsupportedSchemeError(
3338
- `the ${net.family} family can't pay a standard 'exact' rail (EVM + EIP-3009 only).`
3385
+ `the ${net.family} family can't pay a standard 'exact' rail (supported on EVM + Solana today).`
3339
3386
  );
3340
3387
  }
3341
3388
  throwIfAborted(init?.signal);
@@ -4012,6 +4059,24 @@ function buildX402DnsTxt(input) {
4012
4059
  }
4013
4060
 
4014
4061
  // src/facilitator.ts
4062
+ async function fetchFacilitatorFeePayer(url, network, timeoutMs = 8e3) {
4063
+ const base2 = url.replace(/\/+$/, "");
4064
+ const ctrl = new AbortController();
4065
+ const timer = setTimeout(() => ctrl.abort(), timeoutMs);
4066
+ try {
4067
+ const res = await fetch(`${base2}/supported`, { signal: ctrl.signal });
4068
+ if (!res.ok) return void 0;
4069
+ const body = await res.json();
4070
+ const kinds = Array.isArray(body?.kinds) ? body.kinds : [];
4071
+ const kind = kinds.find((k) => k?.scheme === "exact" && k?.network === network);
4072
+ const fp = kind?.extra?.feePayer;
4073
+ return typeof fp === "string" ? fp : void 0;
4074
+ } catch {
4075
+ return void 0;
4076
+ } finally {
4077
+ clearTimeout(timer);
4078
+ }
4079
+ }
4015
4080
  function safeStringify(value) {
4016
4081
  return JSON.stringify(value, (_k, v) => typeof v === "bigint" ? v.toString() : v);
4017
4082
  }
@@ -4153,7 +4218,7 @@ function createPaymentGate(options) {
4153
4218
  );
4154
4219
  if (options.exact && !specs.some((s) => s.exact)) {
4155
4220
  throw new Error(
4156
- "requirePayment: `exact` was requested but none of the offered rails support it. The standard `exact` rail is EVM ERC-20 only \u2014 EIP-3009 (USDC / EURC) or Permit2 (any ERC-20, e.g. Binance-Peg USDC on BNB) \u2014 NOT native coins, NOT non-EVM chains. Offer an EVM ERC-20 token, or drop `exact`."
4221
+ "requirePayment: `exact` was requested but none of the offered rails support it. The standard `exact` rail is EVM ERC-20 (EIP-3009 \u2014 USDC / EURC \u2014 or Permit2, e.g. Binance-Peg USDC on BNB) or a Solana SPL token (SVM) \u2014 NOT native coins, NOT families without a standard `exact` scheme. Offer an EVM ERC-20 / Solana SPL token, or drop `exact`."
4157
4222
  );
4158
4223
  }
4159
4224
  return specs;
@@ -4166,63 +4231,51 @@ function createPaymentGate(options) {
4166
4231
  }
4167
4232
  async function resolveExactRail(net, asset) {
4168
4233
  const cfg = options.exact;
4169
- if (net.family !== "evm" || asset === "native" || !net.settleExactSelf) {
4170
- return void 0;
4171
- }
4172
- const want = cfg.method ?? "auto";
4173
- let method;
4174
- let domain;
4175
- if (want === "permit2") {
4176
- method = "permit2";
4177
- } else {
4178
- const d = net.exactDomain ? await net.exactDomain(asset) : null;
4179
- if (d) {
4180
- method = "eip3009";
4181
- domain = d;
4182
- } else if (want === "eip3009") {
4183
- throw new Error(
4184
- `requirePayment: exact \`method: 'eip3009'\` requested for ${asset} on ${net.network}, but it isn't an EIP-3009 token (no name()/version()/authorizationState). Use \`method: 'permit2'\` (any ERC-20, e.g. Binance-Peg USDC on BNB) or \`'auto'\`. (Or check your rpcUrl is reachable.)`
4185
- );
4186
- } else {
4187
- method = "permit2";
4188
- }
4189
- }
4190
- if (method === "permit2" && !(net.exactPermit2Supported?.() ?? false)) {
4191
- if (cfg.method === "permit2") {
4192
- throw new Error(
4193
- `requirePayment: exact \`method: 'permit2'\` needs the x402 Permit2 proxy deployed on ${net.network}, but it isn't there. Offer an EIP-3009 token (gasless, no proxy), or drop \`exact\` on this chain. (See PERMIT2_PROXY_CHAIN_IDS.)`
4194
- );
4195
- }
4196
- return void 0;
4197
- }
4234
+ if (!net.resolveExactRail) return void 0;
4235
+ let relayer;
4236
+ let feePayer;
4198
4237
  if (cfg.settle === "self") {
4199
4238
  if (cfg.relayer === void 0) {
4200
4239
  throw new Error(
4201
4240
  "requirePayment: exact `settle: 'self'` needs a `relayer` wallet (the gas-paying key that broadcasts the settle), e.g. exact: { settle: 'self', relayer: { privateKey } }."
4202
4241
  );
4203
4242
  }
4204
- const relayer = net.bindWallet(cfg.relayer);
4205
- return { method, ...domain ? { domain } : {}, mode: { kind: "self", relayer } };
4206
- }
4207
- return {
4208
- method,
4209
- ...domain ? { domain } : {},
4210
- mode: {
4211
- kind: "facilitator",
4212
- url: cfg.settle.facilitator,
4213
- ...cfg.settle.authHeaders ? { authHeaders: cfg.settle.authHeaders } : {}
4214
- }
4243
+ relayer = net.bindWallet(cfg.relayer);
4244
+ } else {
4245
+ feePayer = cfg.settle.feePayer;
4246
+ }
4247
+ const method = cfg.method ?? "auto";
4248
+ let info = await net.resolveExactRail({ asset, method, relayer, feePayer });
4249
+ if (!info && cfg.settle !== "self" && !feePayer) {
4250
+ const discovered = await fetchFacilitatorFeePayer(cfg.settle.facilitator, net.network);
4251
+ if (discovered) info = await net.resolveExactRail({ asset, method, relayer, feePayer: discovered });
4252
+ }
4253
+ if (!info) return void 0;
4254
+ const mode = cfg.settle === "self" ? { kind: "self", relayer } : {
4255
+ kind: "facilitator",
4256
+ url: cfg.settle.facilitator,
4257
+ ...cfg.settle.authHeaders ? { authHeaders: cfg.settle.authHeaders } : {}
4215
4258
  };
4259
+ return { method: info.method, ...info.extra ? { extra: info.extra } : {}, mode };
4216
4260
  }
4217
4261
  const hasCustomStore = Boolean(options.isUsed || options.markUsed);
4218
- const localUsed = /* @__PURE__ */ new Set();
4262
+ const localUsed = /* @__PURE__ */ new Map();
4263
+ const replayWindowMs = maxTimeoutSeconds * 1e3;
4264
+ function pruneUsed(now) {
4265
+ for (const [key, expiry] of localUsed) {
4266
+ if (expiry > now) break;
4267
+ localUsed.delete(key);
4268
+ }
4269
+ }
4219
4270
  async function claimTx(ref) {
4220
4271
  if (hasCustomStore) {
4221
4272
  return options.isUsed ? Boolean(await options.isUsed(ref)) : false;
4222
4273
  }
4223
4274
  const key = ref.toLowerCase();
4275
+ const now = Date.now();
4276
+ pruneUsed(now);
4224
4277
  if (localUsed.has(key)) return true;
4225
- localUsed.add(key);
4278
+ localUsed.set(key, now + replayWindowMs);
4226
4279
  return false;
4227
4280
  }
4228
4281
  async function settleTx(ref, ok) {
@@ -4260,11 +4313,11 @@ function createPaymentGate(options) {
4260
4313
  maxTimeoutSeconds,
4261
4314
  extra: {
4262
4315
  assetTransferMethod: rail.method,
4263
- ...rail.domain ? { name: rail.domain.name, version: rail.domain.version } : {},
4264
4316
  minConfirmations,
4265
4317
  decimals: s.decimals,
4266
4318
  amountFormatted: s.amountFormatted,
4267
- ...s.symbol ? { symbol: s.symbol } : {}
4319
+ ...s.symbol ? { symbol: s.symbol } : {},
4320
+ ...rail.extra
4268
4321
  }
4269
4322
  };
4270
4323
  }
@@ -4382,7 +4435,13 @@ function createPaymentGate(options) {
4382
4435
  }
4383
4436
  const ref = sig.payload.txHash;
4384
4437
  if (await claimTx(ref)) return rejection("tx_already_used", `Proof ${ref} was already redeemed.`);
4385
- const result = await spec.net.verify(ref, buildAccept(spec, sig.payload.nonce));
4438
+ let result;
4439
+ try {
4440
+ result = await spec.net.verify(ref, buildAccept(spec, sig.payload.nonce));
4441
+ } catch (err) {
4442
+ await settleTx(ref, false);
4443
+ throw err;
4444
+ }
4386
4445
  if (!result.ok) {
4387
4446
  await settleTx(ref, false);
4388
4447
  return rejection(result.error, result.detail);
@@ -4411,10 +4470,23 @@ function createPaymentGate(options) {
4411
4470
  `No \`exact\` rail offered for ${exact.network}${exact.asset ? `/${exact.asset}` : ""} (offered: ${exactSpecs.map((s) => `${s.asset}@${s.net.network}`).join(", ")}).`
4412
4471
  );
4413
4472
  }
4414
- const auth = "permit2Authorization" in exact.payload ? exact.payload.permit2Authorization : exact.payload.authorization;
4415
- const nonce = auth.nonce;
4473
+ let nonce;
4474
+ let evmAuth = null;
4475
+ if ("transaction" in exact.payload) {
4476
+ try {
4477
+ nonce = Buffer.from(exact.payload.transaction, "base64").toString("base64");
4478
+ } catch {
4479
+ nonce = exact.payload.transaction;
4480
+ }
4481
+ } else if ("permit2Authorization" in exact.payload) {
4482
+ evmAuth = exact.payload.permit2Authorization;
4483
+ nonce = evmAuth.nonce;
4484
+ } else {
4485
+ evmAuth = exact.payload.authorization;
4486
+ nonce = evmAuth.nonce;
4487
+ }
4416
4488
  if (await claimTx(nonce)) {
4417
- return rejection("tx_already_used", `Authorization nonce ${nonce} was already redeemed.`);
4489
+ return rejection("tx_already_used", `Authorization ${evmAuth ? `nonce ${nonce}` : "transaction"} was already redeemed.`);
4418
4490
  }
4419
4491
  const accept = buildExactAccept(spec);
4420
4492
  const mode = spec.exact.mode;
@@ -4439,12 +4511,14 @@ function createPaymentGate(options) {
4439
4511
  amount: accept.amount,
4440
4512
  payTo: accept.payTo,
4441
4513
  maxTimeoutSeconds: accept.maxTimeoutSeconds,
4442
- // name/version are OPTIONAL on the wire type (a foreign rail may omit them), but the
4443
- // gate's OWN exact rail always read them on-chain at resolution — so they're present here.
4444
- extra: { name: accept.extra.name ?? "", version: accept.extra.version ?? "" }
4514
+ // The scheme's chain-specific `extra`, from the gate's OWN trusted rail: SVM forwards the
4515
+ // facilitator's `feePayer` (the gas sponsor); EVM forwards the token's EIP-712 domain.
4516
+ extra: accept.extra.assetTransferMethod === "svm" ? { feePayer: accept.extra.feePayer ?? "" } : { name: accept.extra.name ?? "", version: accept.extra.version ?? "" }
4445
4517
  },
4446
4518
  receipt: { network: accept.network, asset: accept.asset, payTo: accept.payTo, amount: accept.amount },
4447
- payerHint: auth.from
4519
+ // The buyer address, for the receipt's `payer` fallback. EVM carries it in the
4520
+ // authorization; SVM doesn't (the facilitator returns the settled payer) → omit it.
4521
+ ...evmAuth ? { payerHint: evmAuth.from } : {}
4448
4522
  });
4449
4523
  }
4450
4524
  } catch (err) {