@wtflabs/x402 0.0.1-beta.5 → 0.0.1-beta.7

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/cjs/index.js CHANGED
@@ -1692,7 +1692,7 @@ function preparePaymentHeader(from, x402Version2, paymentRequirements) {
1692
1692
  signature: void 0,
1693
1693
  authorization: {
1694
1694
  from,
1695
- to: paymentRequirements.payTo,
1695
+ to: paymentRequirements.extra?.relayer || paymentRequirements.payTo,
1696
1696
  value: paymentRequirements.maxAmountRequired,
1697
1697
  validAfter: validAfter.toString(),
1698
1698
  validBefore: validBefore.toString(),
@@ -2290,6 +2290,264 @@ async function sendAndConfirmSignedTransaction(signedTransaction, rpc, rpcSubscr
2290
2290
  // src/schemes/exact/index.ts
2291
2291
  var SCHEME = "exact";
2292
2292
 
2293
+ // src/types/shared/evm/permitProxyABI.ts
2294
+ var permitProxyContractABI = [
2295
+ // settleWithPermit - 使用 EIP-2612 Permit 进行结算
2296
+ {
2297
+ inputs: [
2298
+ {
2299
+ internalType: "address",
2300
+ name: "token",
2301
+ type: "address"
2302
+ },
2303
+ {
2304
+ internalType: "address",
2305
+ name: "payer",
2306
+ type: "address"
2307
+ },
2308
+ {
2309
+ internalType: "address",
2310
+ name: "seller",
2311
+ type: "address"
2312
+ },
2313
+ {
2314
+ internalType: "uint256",
2315
+ name: "amount",
2316
+ type: "uint256"
2317
+ },
2318
+ {
2319
+ internalType: "uint256",
2320
+ name: "deadline",
2321
+ type: "uint256"
2322
+ },
2323
+ {
2324
+ internalType: "uint8",
2325
+ name: "v",
2326
+ type: "uint8"
2327
+ },
2328
+ {
2329
+ internalType: "bytes32",
2330
+ name: "r",
2331
+ type: "bytes32"
2332
+ },
2333
+ {
2334
+ internalType: "bytes32",
2335
+ name: "s",
2336
+ type: "bytes32"
2337
+ }
2338
+ ],
2339
+ name: "settleWithPermit",
2340
+ outputs: [],
2341
+ stateMutability: "nonpayable",
2342
+ type: "function"
2343
+ },
2344
+ // settleWithERC3009 - 使用 EIP-3009 TransferWithAuthorization 进行结算
2345
+ {
2346
+ inputs: [
2347
+ {
2348
+ internalType: "address",
2349
+ name: "token",
2350
+ type: "address"
2351
+ },
2352
+ {
2353
+ internalType: "address",
2354
+ name: "payer",
2355
+ type: "address"
2356
+ },
2357
+ {
2358
+ internalType: "address",
2359
+ name: "seller",
2360
+ type: "address"
2361
+ },
2362
+ {
2363
+ internalType: "uint256",
2364
+ name: "amount",
2365
+ type: "uint256"
2366
+ },
2367
+ {
2368
+ internalType: "uint256",
2369
+ name: "validAfter",
2370
+ type: "uint256"
2371
+ },
2372
+ {
2373
+ internalType: "uint256",
2374
+ name: "validBefore",
2375
+ type: "uint256"
2376
+ },
2377
+ {
2378
+ internalType: "bytes32",
2379
+ name: "nonce",
2380
+ type: "bytes32"
2381
+ },
2382
+ {
2383
+ internalType: "uint8",
2384
+ name: "v",
2385
+ type: "uint8"
2386
+ },
2387
+ {
2388
+ internalType: "bytes32",
2389
+ name: "r",
2390
+ type: "bytes32"
2391
+ },
2392
+ {
2393
+ internalType: "bytes32",
2394
+ name: "s",
2395
+ type: "bytes32"
2396
+ }
2397
+ ],
2398
+ name: "settleWithERC3009",
2399
+ outputs: [],
2400
+ stateMutability: "nonpayable",
2401
+ type: "function"
2402
+ },
2403
+ // settleWithERC3009Direct - 直接结算(无手续费)
2404
+ {
2405
+ inputs: [
2406
+ {
2407
+ internalType: "address",
2408
+ name: "token",
2409
+ type: "address"
2410
+ },
2411
+ {
2412
+ internalType: "address",
2413
+ name: "payer",
2414
+ type: "address"
2415
+ },
2416
+ {
2417
+ internalType: "address",
2418
+ name: "seller",
2419
+ type: "address"
2420
+ },
2421
+ {
2422
+ internalType: "uint256",
2423
+ name: "amount",
2424
+ type: "uint256"
2425
+ },
2426
+ {
2427
+ internalType: "uint256",
2428
+ name: "validAfter",
2429
+ type: "uint256"
2430
+ },
2431
+ {
2432
+ internalType: "uint256",
2433
+ name: "validBefore",
2434
+ type: "uint256"
2435
+ },
2436
+ {
2437
+ internalType: "bytes32",
2438
+ name: "nonce",
2439
+ type: "bytes32"
2440
+ },
2441
+ {
2442
+ internalType: "uint8",
2443
+ name: "v",
2444
+ type: "uint8"
2445
+ },
2446
+ {
2447
+ internalType: "bytes32",
2448
+ name: "r",
2449
+ type: "bytes32"
2450
+ },
2451
+ {
2452
+ internalType: "bytes32",
2453
+ name: "s",
2454
+ type: "bytes32"
2455
+ }
2456
+ ],
2457
+ name: "settleWithERC3009Direct",
2458
+ outputs: [],
2459
+ stateMutability: "nonpayable",
2460
+ type: "function"
2461
+ },
2462
+ // Events
2463
+ {
2464
+ anonymous: false,
2465
+ inputs: [
2466
+ {
2467
+ indexed: true,
2468
+ internalType: "address",
2469
+ name: "token",
2470
+ type: "address"
2471
+ },
2472
+ {
2473
+ indexed: true,
2474
+ internalType: "address",
2475
+ name: "payer",
2476
+ type: "address"
2477
+ },
2478
+ {
2479
+ indexed: true,
2480
+ internalType: "address",
2481
+ name: "seller",
2482
+ type: "address"
2483
+ },
2484
+ {
2485
+ indexed: false,
2486
+ internalType: "uint256",
2487
+ name: "amount",
2488
+ type: "uint256"
2489
+ },
2490
+ {
2491
+ indexed: false,
2492
+ internalType: "uint256",
2493
+ name: "sellerAmount",
2494
+ type: "uint256"
2495
+ },
2496
+ {
2497
+ indexed: false,
2498
+ internalType: "uint256",
2499
+ name: "feeAmount",
2500
+ type: "uint256"
2501
+ },
2502
+ {
2503
+ indexed: false,
2504
+ internalType: "string",
2505
+ name: "path",
2506
+ type: "string"
2507
+ }
2508
+ ],
2509
+ name: "SettledSplit",
2510
+ type: "event"
2511
+ },
2512
+ {
2513
+ anonymous: false,
2514
+ inputs: [
2515
+ {
2516
+ indexed: true,
2517
+ internalType: "address",
2518
+ name: "token",
2519
+ type: "address"
2520
+ },
2521
+ {
2522
+ indexed: true,
2523
+ internalType: "address",
2524
+ name: "payer",
2525
+ type: "address"
2526
+ },
2527
+ {
2528
+ indexed: true,
2529
+ internalType: "address",
2530
+ name: "seller",
2531
+ type: "address"
2532
+ },
2533
+ {
2534
+ indexed: false,
2535
+ internalType: "uint256",
2536
+ name: "amount",
2537
+ type: "uint256"
2538
+ },
2539
+ {
2540
+ indexed: false,
2541
+ internalType: "string",
2542
+ name: "path",
2543
+ type: "string"
2544
+ }
2545
+ ],
2546
+ name: "SettledDirect",
2547
+ type: "event"
2548
+ }
2549
+ ];
2550
+
2293
2551
  // src/schemes/exact/evm/eip3009/facilitator.ts
2294
2552
  async function verify2(client, payload, paymentRequirements) {
2295
2553
  const exactEvmPayload = payload.payload;
@@ -2354,7 +2612,7 @@ async function verify2(client, payload, paymentRequirements) {
2354
2612
  payer: exactEvmPayload.authorization.from
2355
2613
  };
2356
2614
  }
2357
- if ((0, import_viem3.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem3.getAddress)(paymentRequirements.payTo)) {
2615
+ if ((0, import_viem3.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem3.getAddress)(paymentRequirements.payTo) && (0, import_viem3.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem3.getAddress)(paymentRequirements.extra?.relayer)) {
2358
2616
  return {
2359
2617
  isValid: false,
2360
2618
  invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
@@ -2427,21 +2685,57 @@ async function settle2(wallet, paymentPayload, paymentRequirements) {
2427
2685
  };
2428
2686
  }
2429
2687
  const { signature } = (0, import_viem3.parseErc6492Signature)(payload.signature);
2430
- const tx = await wallet.writeContract({
2431
- address: paymentRequirements.asset,
2432
- abi: usdcABI,
2433
- functionName: "transferWithAuthorization",
2434
- args: [
2435
- payload.authorization.from,
2436
- payload.authorization.to,
2437
- BigInt(payload.authorization.value),
2438
- BigInt(payload.authorization.validAfter),
2439
- BigInt(payload.authorization.validBefore),
2440
- payload.authorization.nonce,
2441
- signature
2442
- ],
2443
- chain: wallet.chain
2444
- });
2688
+ let tx;
2689
+ if (paymentRequirements.extra?.relayer) {
2690
+ const sig = (0, import_viem3.hexToSignature)(signature);
2691
+ const v = Number(sig.v);
2692
+ const r = sig.r;
2693
+ const s = sig.s;
2694
+ tx = await wallet.writeContract({
2695
+ address: paymentRequirements.extra.relayer,
2696
+ abi: permitProxyContractABI,
2697
+ functionName: "settleWithERC3009",
2698
+ args: [
2699
+ paymentRequirements.asset,
2700
+ // token
2701
+ payload.authorization.from,
2702
+ // payer
2703
+ paymentRequirements.payTo,
2704
+ // seller
2705
+ BigInt(payload.authorization.value),
2706
+ // amount
2707
+ BigInt(payload.authorization.validAfter),
2708
+ // validAfter
2709
+ BigInt(payload.authorization.validBefore),
2710
+ // validBefore
2711
+ payload.authorization.nonce,
2712
+ // nonce
2713
+ v,
2714
+ // v (uint8)
2715
+ r,
2716
+ // r (bytes32)
2717
+ s
2718
+ // s (bytes32)
2719
+ ],
2720
+ chain: wallet.chain
2721
+ });
2722
+ } else {
2723
+ tx = await wallet.writeContract({
2724
+ address: paymentRequirements.asset,
2725
+ abi: usdcABI,
2726
+ functionName: "transferWithAuthorization",
2727
+ args: [
2728
+ payload.authorization.from,
2729
+ payload.authorization.to,
2730
+ BigInt(payload.authorization.value),
2731
+ BigInt(payload.authorization.validAfter),
2732
+ BigInt(payload.authorization.validBefore),
2733
+ payload.authorization.nonce,
2734
+ signature
2735
+ ],
2736
+ chain: wallet.chain
2737
+ });
2738
+ }
2445
2739
  const receipt = await wallet.waitForTransactionReceipt({ hash: tx });
2446
2740
  if (receipt.status !== "success") {
2447
2741
  return {
@@ -2477,63 +2771,6 @@ function splitSignature(signature) {
2477
2771
  return { v, r, s };
2478
2772
  }
2479
2773
 
2480
- // src/types/shared/evm/permitProxyABI.ts
2481
- var permitProxyContractABI = [
2482
- {
2483
- inputs: [
2484
- {
2485
- internalType: "address",
2486
- name: "token",
2487
- type: "address"
2488
- },
2489
- {
2490
- internalType: "address",
2491
- name: "owner",
2492
- type: "address"
2493
- },
2494
- {
2495
- internalType: "address",
2496
- name: "spender",
2497
- type: "address"
2498
- },
2499
- {
2500
- internalType: "uint256",
2501
- name: "value",
2502
- type: "uint256"
2503
- },
2504
- {
2505
- internalType: "uint256",
2506
- name: "deadline",
2507
- type: "uint256"
2508
- },
2509
- {
2510
- internalType: "uint8",
2511
- name: "v",
2512
- type: "uint8"
2513
- },
2514
- {
2515
- internalType: "bytes32",
2516
- name: "r",
2517
- type: "bytes32"
2518
- },
2519
- {
2520
- internalType: "bytes32",
2521
- name: "s",
2522
- type: "bytes32"
2523
- },
2524
- {
2525
- internalType: "address",
2526
- name: "to",
2527
- type: "address"
2528
- }
2529
- ],
2530
- name: "permitAndTransfer",
2531
- outputs: [],
2532
- stateMutability: "nonpayable",
2533
- type: "function"
2534
- }
2535
- ];
2536
-
2537
2774
  // src/schemes/exact/evm/permit/facilitator.ts
2538
2775
  async function verify3(client, payload, paymentRequirements) {
2539
2776
  if (payload.payload.authorizationType !== "permit" || payload.scheme !== SCHEME || paymentRequirements.scheme !== SCHEME) {
@@ -2601,8 +2838,8 @@ async function verify3(client, payload, paymentRequirements) {
2601
2838
  payer: owner
2602
2839
  };
2603
2840
  }
2604
- if (paymentRequirements.extra?.proxyAddress) {
2605
- if ((0, import_viem5.getAddress)(spender) !== (0, import_viem5.getAddress)(paymentRequirements.extra?.proxyAddress)) {
2841
+ if (paymentRequirements.extra?.relayer) {
2842
+ if ((0, import_viem5.getAddress)(spender) !== (0, import_viem5.getAddress)(paymentRequirements.extra?.relayer)) {
2606
2843
  return {
2607
2844
  isValid: false,
2608
2845
  invalidReason: "invalid_spender_address",
@@ -2666,21 +2903,28 @@ async function settle3(wallet, paymentPayload, paymentRequirements) {
2666
2903
  address: wallet.account.address
2667
2904
  });
2668
2905
  let transactionHash;
2669
- if (paymentRequirements.extra?.proxyAddress) {
2906
+ if (paymentRequirements.extra?.relayer) {
2670
2907
  transactionHash = await wallet.writeContract({
2671
- address: paymentRequirements.extra.proxyAddress,
2908
+ address: paymentRequirements.extra.relayer,
2672
2909
  abi: permitProxyContractABI,
2673
- functionName: "permitAndTransfer",
2910
+ functionName: "settleWithPermit",
2674
2911
  args: [
2675
2912
  tokenAddress,
2913
+ // token
2676
2914
  owner,
2677
- spender,
2915
+ // payer
2916
+ paymentRequirements.payTo,
2917
+ // seller
2678
2918
  BigInt(value),
2919
+ // amount
2679
2920
  BigInt(deadline),
2921
+ // deadline
2680
2922
  v,
2923
+ // v
2681
2924
  r,
2682
- s,
2683
- paymentRequirements.payTo
2925
+ // r
2926
+ s
2927
+ // s
2684
2928
  ],
2685
2929
  chain: wallet.chain,
2686
2930
  nonce: txNonce