@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.d.cts CHANGED
@@ -4,3 +4,81 @@ export { RelayFeedbackConfig, submitRelayFeedback } from './relay-feedback.cjs';
4
4
  export { A as AcceptsExtra, B as BASE_MAINNET_NETWORK, C as CAIP2_TO_NETWORK, b as CHAIN_IDS, E as EXPLORER_TX_URL, l as EvmWallet, N as NETWORK_CAIP2, e as NETWORK_LABELS, d as NETWORK_TOKENS, c as NetworkToken, P as PaymentAccept, o as PaymentRequired, R as RELAI_FACILITATOR_URL, h as RELAI_NETWORKS, a as RelaiNetwork, m as ResourceInfo, S as SOLANA_MAINNET_NETWORK, k as SolanaWallet, U as USDC_ADDRESSES, g as USDC_BASE, f as USDC_SOLANA, W as WalletSet, j as isEvm, i as isSolana, n as normalizeNetwork, r as resolveToken } from './types-Y9ni5XwY.cjs';
5
5
  export { BridgeBalances, BridgeQuoteResult, BridgeResult } from './management.cjs';
6
6
  export { NETWORK_V1_TO_V2, NETWORK_V2_TO_V1, convertPayloadToVersion, convertV1ToV2, convertV2ToV1, detectPayloadVersion, formatUsd, fromAtomicUnits, isEvmNetwork, isSolanaNetwork, networkV1ToV2, networkV2ToV1, normalizePaymentHeader, toAtomicUnits } from './utils/index.cjs';
7
+
8
+ /**
9
+ * Payment Code API — BLIK-style x402 payment codes
10
+ *
11
+ * generatePaymentCode() — sign EIP-3009 authorization and register on SKALE L3
12
+ * redeemPaymentCode() — redeem a code (payee calls this, triggers Base L2 settlement)
13
+ * getPaymentCode() — check code status
14
+ */
15
+ interface PaymentCodeConfig {
16
+ facilitatorUrl?: string;
17
+ }
18
+ interface GeneratePaymentCodeParams {
19
+ /** EIP-3009 signer — must implement signTypedData */
20
+ signer: {
21
+ getAddress(): Promise<string>;
22
+ signTypedData(domain: object, types: object, value: object): Promise<string>;
23
+ };
24
+ /** Payee wallet address */
25
+ to: string;
26
+ /** Amount in USDC micro-units (6 decimals), e.g. 1000 = $0.001 */
27
+ value: string | bigint;
28
+ /** USDC contract address on Base L2 (defaults to Base mainnet USDC) */
29
+ usdcContract?: string;
30
+ /** TTL in seconds (default: 120) */
31
+ ttl?: number;
32
+ }
33
+ interface PaymentCode {
34
+ code: string;
35
+ validBefore: number;
36
+ expiresIn: number;
37
+ }
38
+ interface RedeemResult {
39
+ success: boolean;
40
+ code: string;
41
+ l3TxHash: string;
42
+ l2TxHash: string;
43
+ explorerUrl: string;
44
+ amount: string;
45
+ from: string;
46
+ to: string;
47
+ }
48
+ interface CodeStatus {
49
+ code: string;
50
+ from: string;
51
+ to: string;
52
+ value: string;
53
+ validBefore: number;
54
+ redeemed: boolean;
55
+ expired: boolean;
56
+ redeemable: boolean;
57
+ }
58
+ /**
59
+ * Generate a BLIK-style x402 payment code backed by EIP-3009.
60
+ *
61
+ * @example
62
+ * const { code } = await generatePaymentCode(config, {
63
+ * signer: walletClient,
64
+ * to: "0xMerchant...",
65
+ * value: "1000", // $0.001 USDC
66
+ * ttl: 120,
67
+ * });
68
+ * // code = "X7K9P2AB"
69
+ */
70
+ declare function generatePaymentCode(config: PaymentCodeConfig, params: GeneratePaymentCodeParams): Promise<PaymentCode>;
71
+ /**
72
+ * Redeem a payment code. Triggers Base L2 settlement via relayer.
73
+ *
74
+ * @example
75
+ * const result = await redeemPaymentCode(config, "X7K9P2AB");
76
+ * // result.l2TxHash = "0x..."
77
+ */
78
+ declare function redeemPaymentCode(config: PaymentCodeConfig, code: string): Promise<RedeemResult>;
79
+ /**
80
+ * Get the status of a payment code.
81
+ */
82
+ declare function getPaymentCode(config: PaymentCodeConfig, code: string): Promise<CodeStatus>;
83
+
84
+ export { type CodeStatus, type GeneratePaymentCodeParams, type PaymentCode, type PaymentCodeConfig, type RedeemResult, generatePaymentCode, getPaymentCode, redeemPaymentCode };
package/dist/index.d.ts CHANGED
@@ -4,3 +4,81 @@ export { RelayFeedbackConfig, submitRelayFeedback } from './relay-feedback.js';
4
4
  export { A as AcceptsExtra, B as BASE_MAINNET_NETWORK, C as CAIP2_TO_NETWORK, b as CHAIN_IDS, E as EXPLORER_TX_URL, l as EvmWallet, N as NETWORK_CAIP2, e as NETWORK_LABELS, d as NETWORK_TOKENS, c as NetworkToken, P as PaymentAccept, o as PaymentRequired, R as RELAI_FACILITATOR_URL, h as RELAI_NETWORKS, a as RelaiNetwork, m as ResourceInfo, S as SOLANA_MAINNET_NETWORK, k as SolanaWallet, U as USDC_ADDRESSES, g as USDC_BASE, f as USDC_SOLANA, W as WalletSet, j as isEvm, i as isSolana, n as normalizeNetwork, r as resolveToken } from './types-Y9ni5XwY.js';
5
5
  export { BridgeBalances, BridgeQuoteResult, BridgeResult } from './management.js';
6
6
  export { NETWORK_V1_TO_V2, NETWORK_V2_TO_V1, convertPayloadToVersion, convertV1ToV2, convertV2ToV1, detectPayloadVersion, formatUsd, fromAtomicUnits, isEvmNetwork, isSolanaNetwork, networkV1ToV2, networkV2ToV1, normalizePaymentHeader, toAtomicUnits } from './utils/index.js';
7
+
8
+ /**
9
+ * Payment Code API — BLIK-style x402 payment codes
10
+ *
11
+ * generatePaymentCode() — sign EIP-3009 authorization and register on SKALE L3
12
+ * redeemPaymentCode() — redeem a code (payee calls this, triggers Base L2 settlement)
13
+ * getPaymentCode() — check code status
14
+ */
15
+ interface PaymentCodeConfig {
16
+ facilitatorUrl?: string;
17
+ }
18
+ interface GeneratePaymentCodeParams {
19
+ /** EIP-3009 signer — must implement signTypedData */
20
+ signer: {
21
+ getAddress(): Promise<string>;
22
+ signTypedData(domain: object, types: object, value: object): Promise<string>;
23
+ };
24
+ /** Payee wallet address */
25
+ to: string;
26
+ /** Amount in USDC micro-units (6 decimals), e.g. 1000 = $0.001 */
27
+ value: string | bigint;
28
+ /** USDC contract address on Base L2 (defaults to Base mainnet USDC) */
29
+ usdcContract?: string;
30
+ /** TTL in seconds (default: 120) */
31
+ ttl?: number;
32
+ }
33
+ interface PaymentCode {
34
+ code: string;
35
+ validBefore: number;
36
+ expiresIn: number;
37
+ }
38
+ interface RedeemResult {
39
+ success: boolean;
40
+ code: string;
41
+ l3TxHash: string;
42
+ l2TxHash: string;
43
+ explorerUrl: string;
44
+ amount: string;
45
+ from: string;
46
+ to: string;
47
+ }
48
+ interface CodeStatus {
49
+ code: string;
50
+ from: string;
51
+ to: string;
52
+ value: string;
53
+ validBefore: number;
54
+ redeemed: boolean;
55
+ expired: boolean;
56
+ redeemable: boolean;
57
+ }
58
+ /**
59
+ * Generate a BLIK-style x402 payment code backed by EIP-3009.
60
+ *
61
+ * @example
62
+ * const { code } = await generatePaymentCode(config, {
63
+ * signer: walletClient,
64
+ * to: "0xMerchant...",
65
+ * value: "1000", // $0.001 USDC
66
+ * ttl: 120,
67
+ * });
68
+ * // code = "X7K9P2AB"
69
+ */
70
+ declare function generatePaymentCode(config: PaymentCodeConfig, params: GeneratePaymentCodeParams): Promise<PaymentCode>;
71
+ /**
72
+ * Redeem a payment code. Triggers Base L2 settlement via relayer.
73
+ *
74
+ * @example
75
+ * const result = await redeemPaymentCode(config, "X7K9P2AB");
76
+ * // result.l2TxHash = "0x..."
77
+ */
78
+ declare function redeemPaymentCode(config: PaymentCodeConfig, code: string): Promise<RedeemResult>;
79
+ /**
80
+ * Get the status of a payment code.
81
+ */
82
+ declare function getPaymentCode(config: PaymentCodeConfig, code: string): Promise<CodeStatus>;
83
+
84
+ export { type CodeStatus, type GeneratePaymentCodeParams, type PaymentCode, type PaymentCodeConfig, type RedeemResult, generatePaymentCode, getPaymentCode, redeemPaymentCode };
package/dist/index.js CHANGED
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/types.ts
2
9
  var RELAI_FACILITATOR_URL = "https://facilitator.x402.fi";
3
10
  var NETWORK_CAIP2 = {
@@ -2204,6 +2211,96 @@ function submitRelayFeedback(config) {
2204
2211
  })();
2205
2212
  }
2206
2213
 
2214
+ // src/payment-codes.ts
2215
+ var DEFAULT_FACILITATOR = "https://relai.fi/facilitator";
2216
+ var DEFAULT_USDC_BASE = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
2217
+ var EIP3009_TYPES = {
2218
+ TransferWithAuthorization: [
2219
+ { name: "from", type: "address" },
2220
+ { name: "to", type: "address" },
2221
+ { name: "value", type: "uint256" },
2222
+ { name: "validAfter", type: "uint256" },
2223
+ { name: "validBefore", type: "uint256" },
2224
+ { name: "nonce", type: "bytes32" }
2225
+ ]
2226
+ };
2227
+ function randomBytes32() {
2228
+ const bytes = new Uint8Array(32);
2229
+ if (typeof globalThis.crypto !== "undefined") {
2230
+ globalThis.crypto.getRandomValues(bytes);
2231
+ } else {
2232
+ const { randomBytes } = __require("crypto");
2233
+ randomBytes(32).copy(Buffer.from(bytes.buffer));
2234
+ }
2235
+ return "0x" + Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
2236
+ }
2237
+ async function generatePaymentCode(config, params) {
2238
+ const { signer, to, value, usdcContract, ttl = 120 } = params;
2239
+ const facilitatorUrl = config.facilitatorUrl || DEFAULT_FACILITATOR;
2240
+ const usdc = usdcContract || DEFAULT_USDC_BASE;
2241
+ const from = await signer.getAddress();
2242
+ const now = Math.floor(Date.now() / 1e3);
2243
+ const validAfter = 0;
2244
+ const validBefore = now + ttl;
2245
+ const nonce = randomBytes32();
2246
+ const domain = {
2247
+ name: "USD Coin",
2248
+ version: "2",
2249
+ chainId: 8453,
2250
+ // Base mainnet
2251
+ verifyingContract: usdc
2252
+ };
2253
+ const message = {
2254
+ from,
2255
+ to,
2256
+ value: BigInt(value).toString(),
2257
+ validAfter,
2258
+ validBefore,
2259
+ nonce
2260
+ };
2261
+ const signature = await signer.signTypedData(domain, EIP3009_TYPES, message);
2262
+ const res = await fetch(`${facilitatorUrl}/payment-codes`, {
2263
+ method: "POST",
2264
+ headers: { "Content-Type": "application/json" },
2265
+ body: JSON.stringify({
2266
+ from,
2267
+ to,
2268
+ value: BigInt(value).toString(),
2269
+ validAfter,
2270
+ validBefore,
2271
+ nonce,
2272
+ signature,
2273
+ usdcContract: usdc
2274
+ })
2275
+ });
2276
+ if (!res.ok) {
2277
+ const err = await res.json().catch(() => ({}));
2278
+ throw new Error(`Failed to register payment code: ${err.error || res.status}`);
2279
+ }
2280
+ return res.json();
2281
+ }
2282
+ async function redeemPaymentCode(config, code) {
2283
+ const facilitatorUrl = config.facilitatorUrl || DEFAULT_FACILITATOR;
2284
+ const res = await fetch(`${facilitatorUrl}/payment-codes/${code.toUpperCase()}/redeem`, {
2285
+ method: "POST",
2286
+ headers: { "Content-Type": "application/json" }
2287
+ });
2288
+ if (!res.ok) {
2289
+ const err = await res.json().catch(() => ({}));
2290
+ throw new Error(`Failed to redeem payment code: ${err.error || res.status}`);
2291
+ }
2292
+ return res.json();
2293
+ }
2294
+ async function getPaymentCode(config, code) {
2295
+ const facilitatorUrl = config.facilitatorUrl || DEFAULT_FACILITATOR;
2296
+ const res = await fetch(`${facilitatorUrl}/payment-codes/${code.toUpperCase()}`);
2297
+ if (!res.ok) {
2298
+ const err = await res.json().catch(() => ({}));
2299
+ throw new Error(`Payment code not found: ${err.error || res.status}`);
2300
+ }
2301
+ return res.json();
2302
+ }
2303
+
2207
2304
  // src/utils/payload-converter.ts
2208
2305
  var NETWORK_V1_TO_V2 = {
2209
2306
  "solana": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
@@ -2368,6 +2465,8 @@ export {
2368
2465
  detectPayloadVersion,
2369
2466
  formatUsd,
2370
2467
  fromAtomicUnits,
2468
+ generatePaymentCode,
2469
+ getPaymentCode,
2371
2470
  isEvm,
2372
2471
  isEvmNetwork,
2373
2472
  isSolana,
@@ -2376,6 +2475,7 @@ export {
2376
2475
  networkV2ToV1,
2377
2476
  normalizeNetwork,
2378
2477
  normalizePaymentHeader,
2478
+ redeemPaymentCode,
2379
2479
  resolveToken,
2380
2480
  stripePayTo,
2381
2481
  submitRelayFeedback,