@relai-fi/x402 0.6.2 → 0.6.3

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.cjs CHANGED
@@ -44,6 +44,8 @@ __export(index_exports, {
44
44
  detectPayloadVersion: () => detectPayloadVersion,
45
45
  formatUsd: () => formatUsd,
46
46
  fromAtomicUnits: () => fromAtomicUnits,
47
+ generatePaymentCode: () => generatePaymentCode,
48
+ getPaymentCode: () => getPaymentCode,
47
49
  isEvm: () => isEvm,
48
50
  isEvmNetwork: () => isEvmNetwork,
49
51
  isSolana: () => isSolana,
@@ -52,6 +54,7 @@ __export(index_exports, {
52
54
  networkV2ToV1: () => networkV2ToV1,
53
55
  normalizeNetwork: () => normalizeNetwork,
54
56
  normalizePaymentHeader: () => normalizePaymentHeader,
57
+ redeemPaymentCode: () => redeemPaymentCode,
55
58
  resolveToken: () => resolveToken,
56
59
  stripePayTo: () => stripePayTo,
57
60
  submitRelayFeedback: () => submitRelayFeedback,
@@ -2254,6 +2257,96 @@ function submitRelayFeedback(config) {
2254
2257
  })();
2255
2258
  }
2256
2259
 
2260
+ // src/payment-codes.ts
2261
+ var DEFAULT_FACILITATOR = "https://relai.fi/facilitator";
2262
+ var DEFAULT_USDC_BASE = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
2263
+ var EIP3009_TYPES = {
2264
+ TransferWithAuthorization: [
2265
+ { name: "from", type: "address" },
2266
+ { name: "to", type: "address" },
2267
+ { name: "value", type: "uint256" },
2268
+ { name: "validAfter", type: "uint256" },
2269
+ { name: "validBefore", type: "uint256" },
2270
+ { name: "nonce", type: "bytes32" }
2271
+ ]
2272
+ };
2273
+ function randomBytes32() {
2274
+ const bytes = new Uint8Array(32);
2275
+ if (typeof globalThis.crypto !== "undefined") {
2276
+ globalThis.crypto.getRandomValues(bytes);
2277
+ } else {
2278
+ const { randomBytes } = require("crypto");
2279
+ randomBytes(32).copy(Buffer.from(bytes.buffer));
2280
+ }
2281
+ return "0x" + Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
2282
+ }
2283
+ async function generatePaymentCode(config, params) {
2284
+ const { signer, to, value, usdcContract, ttl = 120 } = params;
2285
+ const facilitatorUrl = config.facilitatorUrl || DEFAULT_FACILITATOR;
2286
+ const usdc = usdcContract || DEFAULT_USDC_BASE;
2287
+ const from = await signer.getAddress();
2288
+ const now = Math.floor(Date.now() / 1e3);
2289
+ const validAfter = 0;
2290
+ const validBefore = now + ttl;
2291
+ const nonce = randomBytes32();
2292
+ const domain = {
2293
+ name: "USD Coin",
2294
+ version: "2",
2295
+ chainId: 8453,
2296
+ // Base mainnet
2297
+ verifyingContract: usdc
2298
+ };
2299
+ const message = {
2300
+ from,
2301
+ to,
2302
+ value: BigInt(value).toString(),
2303
+ validAfter,
2304
+ validBefore,
2305
+ nonce
2306
+ };
2307
+ const signature = await signer.signTypedData(domain, EIP3009_TYPES, message);
2308
+ const res = await fetch(`${facilitatorUrl}/payment-codes`, {
2309
+ method: "POST",
2310
+ headers: { "Content-Type": "application/json" },
2311
+ body: JSON.stringify({
2312
+ from,
2313
+ to,
2314
+ value: BigInt(value).toString(),
2315
+ validAfter,
2316
+ validBefore,
2317
+ nonce,
2318
+ signature,
2319
+ usdcContract: usdc
2320
+ })
2321
+ });
2322
+ if (!res.ok) {
2323
+ const err = await res.json().catch(() => ({}));
2324
+ throw new Error(`Failed to register payment code: ${err.error || res.status}`);
2325
+ }
2326
+ return res.json();
2327
+ }
2328
+ async function redeemPaymentCode(config, code) {
2329
+ const facilitatorUrl = config.facilitatorUrl || DEFAULT_FACILITATOR;
2330
+ const res = await fetch(`${facilitatorUrl}/payment-codes/${code.toUpperCase()}/redeem`, {
2331
+ method: "POST",
2332
+ headers: { "Content-Type": "application/json" }
2333
+ });
2334
+ if (!res.ok) {
2335
+ const err = await res.json().catch(() => ({}));
2336
+ throw new Error(`Failed to redeem payment code: ${err.error || res.status}`);
2337
+ }
2338
+ return res.json();
2339
+ }
2340
+ async function getPaymentCode(config, code) {
2341
+ const facilitatorUrl = config.facilitatorUrl || DEFAULT_FACILITATOR;
2342
+ const res = await fetch(`${facilitatorUrl}/payment-codes/${code.toUpperCase()}`);
2343
+ if (!res.ok) {
2344
+ const err = await res.json().catch(() => ({}));
2345
+ throw new Error(`Payment code not found: ${err.error || res.status}`);
2346
+ }
2347
+ return res.json();
2348
+ }
2349
+
2257
2350
  // src/utils/payload-converter.ts
2258
2351
  var NETWORK_V1_TO_V2 = {
2259
2352
  "solana": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
@@ -2418,6 +2511,8 @@ function formatUsd(usd, maxDecimals = 4) {
2418
2511
  detectPayloadVersion,
2419
2512
  formatUsd,
2420
2513
  fromAtomicUnits,
2514
+ generatePaymentCode,
2515
+ getPaymentCode,
2421
2516
  isEvm,
2422
2517
  isEvmNetwork,
2423
2518
  isSolana,
@@ -2426,6 +2521,7 @@ function formatUsd(usd, maxDecimals = 4) {
2426
2521
  networkV2ToV1,
2427
2522
  normalizeNetwork,
2428
2523
  normalizePaymentHeader,
2524
+ redeemPaymentCode,
2429
2525
  resolveToken,
2430
2526
  stripePayTo,
2431
2527
  submitRelayFeedback,