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

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.
@@ -9,7 +9,7 @@ import {
9
9
  preparePaymentHeader,
10
10
  signAuthorization,
11
11
  signPaymentHeader
12
- } from "./chunk-J3FMDNC3.mjs";
12
+ } from "./chunk-VTLJOZXM.mjs";
13
13
  import {
14
14
  ErrorReasons,
15
15
  PERMIT2_ADDRESS,
@@ -61,7 +61,271 @@ __export(evm_exports, {
61
61
  });
62
62
 
63
63
  // src/schemes/exact/evm/eip3009/facilitator.ts
64
- import { getAddress, parseErc6492Signature } from "viem";
64
+ import {
65
+ getAddress,
66
+ parseErc6492Signature,
67
+ hexToSignature
68
+ } from "viem";
69
+
70
+ // src/types/shared/evm/permitProxyABI.ts
71
+ var permitProxyContractABI = [
72
+ // settleWithPermit - 使用 EIP-2612 Permit 进行结算
73
+ {
74
+ inputs: [
75
+ {
76
+ internalType: "address",
77
+ name: "token",
78
+ type: "address"
79
+ },
80
+ {
81
+ internalType: "address",
82
+ name: "payer",
83
+ type: "address"
84
+ },
85
+ {
86
+ internalType: "address",
87
+ name: "seller",
88
+ type: "address"
89
+ },
90
+ {
91
+ internalType: "uint256",
92
+ name: "amount",
93
+ type: "uint256"
94
+ },
95
+ {
96
+ internalType: "uint256",
97
+ name: "deadline",
98
+ type: "uint256"
99
+ },
100
+ {
101
+ internalType: "uint8",
102
+ name: "v",
103
+ type: "uint8"
104
+ },
105
+ {
106
+ internalType: "bytes32",
107
+ name: "r",
108
+ type: "bytes32"
109
+ },
110
+ {
111
+ internalType: "bytes32",
112
+ name: "s",
113
+ type: "bytes32"
114
+ }
115
+ ],
116
+ name: "settleWithPermit",
117
+ outputs: [],
118
+ stateMutability: "nonpayable",
119
+ type: "function"
120
+ },
121
+ // settleWithERC3009 - 使用 EIP-3009 TransferWithAuthorization 进行结算
122
+ {
123
+ inputs: [
124
+ {
125
+ internalType: "address",
126
+ name: "token",
127
+ type: "address"
128
+ },
129
+ {
130
+ internalType: "address",
131
+ name: "payer",
132
+ type: "address"
133
+ },
134
+ {
135
+ internalType: "address",
136
+ name: "seller",
137
+ type: "address"
138
+ },
139
+ {
140
+ internalType: "uint256",
141
+ name: "amount",
142
+ type: "uint256"
143
+ },
144
+ {
145
+ internalType: "uint256",
146
+ name: "validAfter",
147
+ type: "uint256"
148
+ },
149
+ {
150
+ internalType: "uint256",
151
+ name: "validBefore",
152
+ type: "uint256"
153
+ },
154
+ {
155
+ internalType: "bytes32",
156
+ name: "nonce",
157
+ type: "bytes32"
158
+ },
159
+ {
160
+ internalType: "uint8",
161
+ name: "v",
162
+ type: "uint8"
163
+ },
164
+ {
165
+ internalType: "bytes32",
166
+ name: "r",
167
+ type: "bytes32"
168
+ },
169
+ {
170
+ internalType: "bytes32",
171
+ name: "s",
172
+ type: "bytes32"
173
+ }
174
+ ],
175
+ name: "settleWithERC3009",
176
+ outputs: [],
177
+ stateMutability: "nonpayable",
178
+ type: "function"
179
+ },
180
+ // settleWithERC3009Direct - 直接结算(无手续费)
181
+ {
182
+ inputs: [
183
+ {
184
+ internalType: "address",
185
+ name: "token",
186
+ type: "address"
187
+ },
188
+ {
189
+ internalType: "address",
190
+ name: "payer",
191
+ type: "address"
192
+ },
193
+ {
194
+ internalType: "address",
195
+ name: "seller",
196
+ type: "address"
197
+ },
198
+ {
199
+ internalType: "uint256",
200
+ name: "amount",
201
+ type: "uint256"
202
+ },
203
+ {
204
+ internalType: "uint256",
205
+ name: "validAfter",
206
+ type: "uint256"
207
+ },
208
+ {
209
+ internalType: "uint256",
210
+ name: "validBefore",
211
+ type: "uint256"
212
+ },
213
+ {
214
+ internalType: "bytes32",
215
+ name: "nonce",
216
+ type: "bytes32"
217
+ },
218
+ {
219
+ internalType: "uint8",
220
+ name: "v",
221
+ type: "uint8"
222
+ },
223
+ {
224
+ internalType: "bytes32",
225
+ name: "r",
226
+ type: "bytes32"
227
+ },
228
+ {
229
+ internalType: "bytes32",
230
+ name: "s",
231
+ type: "bytes32"
232
+ }
233
+ ],
234
+ name: "settleWithERC3009Direct",
235
+ outputs: [],
236
+ stateMutability: "nonpayable",
237
+ type: "function"
238
+ },
239
+ // Events
240
+ {
241
+ anonymous: false,
242
+ inputs: [
243
+ {
244
+ indexed: true,
245
+ internalType: "address",
246
+ name: "token",
247
+ type: "address"
248
+ },
249
+ {
250
+ indexed: true,
251
+ internalType: "address",
252
+ name: "payer",
253
+ type: "address"
254
+ },
255
+ {
256
+ indexed: true,
257
+ internalType: "address",
258
+ name: "seller",
259
+ type: "address"
260
+ },
261
+ {
262
+ indexed: false,
263
+ internalType: "uint256",
264
+ name: "amount",
265
+ type: "uint256"
266
+ },
267
+ {
268
+ indexed: false,
269
+ internalType: "uint256",
270
+ name: "sellerAmount",
271
+ type: "uint256"
272
+ },
273
+ {
274
+ indexed: false,
275
+ internalType: "uint256",
276
+ name: "feeAmount",
277
+ type: "uint256"
278
+ },
279
+ {
280
+ indexed: false,
281
+ internalType: "string",
282
+ name: "path",
283
+ type: "string"
284
+ }
285
+ ],
286
+ name: "SettledSplit",
287
+ type: "event"
288
+ },
289
+ {
290
+ anonymous: false,
291
+ inputs: [
292
+ {
293
+ indexed: true,
294
+ internalType: "address",
295
+ name: "token",
296
+ type: "address"
297
+ },
298
+ {
299
+ indexed: true,
300
+ internalType: "address",
301
+ name: "payer",
302
+ type: "address"
303
+ },
304
+ {
305
+ indexed: true,
306
+ internalType: "address",
307
+ name: "seller",
308
+ type: "address"
309
+ },
310
+ {
311
+ indexed: false,
312
+ internalType: "uint256",
313
+ name: "amount",
314
+ type: "uint256"
315
+ },
316
+ {
317
+ indexed: false,
318
+ internalType: "string",
319
+ name: "path",
320
+ type: "string"
321
+ }
322
+ ],
323
+ name: "SettledDirect",
324
+ type: "event"
325
+ }
326
+ ];
327
+
328
+ // src/schemes/exact/evm/eip3009/facilitator.ts
65
329
  async function verify(client, payload, paymentRequirements) {
66
330
  const exactEvmPayload = payload.payload;
67
331
  if (exactEvmPayload.authorizationType !== "eip3009") {
@@ -125,7 +389,7 @@ async function verify(client, payload, paymentRequirements) {
125
389
  payer: exactEvmPayload.authorization.from
126
390
  };
127
391
  }
128
- if (getAddress(exactEvmPayload.authorization.to) !== getAddress(paymentRequirements.payTo)) {
392
+ if (getAddress(exactEvmPayload.authorization.to) !== getAddress(paymentRequirements.payTo) && getAddress(exactEvmPayload.authorization.to) !== getAddress(paymentRequirements.extra?.relayer)) {
129
393
  return {
130
394
  isValid: false,
131
395
  invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
@@ -198,21 +462,57 @@ async function settle(wallet, paymentPayload, paymentRequirements) {
198
462
  };
199
463
  }
200
464
  const { signature } = parseErc6492Signature(payload.signature);
201
- const tx = await wallet.writeContract({
202
- address: paymentRequirements.asset,
203
- abi: usdcABI,
204
- functionName: "transferWithAuthorization",
205
- args: [
206
- payload.authorization.from,
207
- payload.authorization.to,
208
- BigInt(payload.authorization.value),
209
- BigInt(payload.authorization.validAfter),
210
- BigInt(payload.authorization.validBefore),
211
- payload.authorization.nonce,
212
- signature
213
- ],
214
- chain: wallet.chain
215
- });
465
+ let tx;
466
+ if (paymentRequirements.extra?.relayer) {
467
+ const sig = hexToSignature(signature);
468
+ const v = Number(sig.v);
469
+ const r = sig.r;
470
+ const s = sig.s;
471
+ tx = await wallet.writeContract({
472
+ address: paymentRequirements.extra.relayer,
473
+ abi: permitProxyContractABI,
474
+ functionName: "settleWithERC3009",
475
+ args: [
476
+ paymentRequirements.asset,
477
+ // token
478
+ payload.authorization.from,
479
+ // payer
480
+ payload.authorization.to,
481
+ // seller
482
+ BigInt(payload.authorization.value),
483
+ // amount
484
+ BigInt(payload.authorization.validAfter),
485
+ // validAfter
486
+ BigInt(payload.authorization.validBefore),
487
+ // validBefore
488
+ payload.authorization.nonce,
489
+ // nonce
490
+ v,
491
+ // v (uint8)
492
+ r,
493
+ // r (bytes32)
494
+ s
495
+ // s (bytes32)
496
+ ],
497
+ chain: wallet.chain
498
+ });
499
+ } else {
500
+ tx = await wallet.writeContract({
501
+ address: paymentRequirements.asset,
502
+ abi: usdcABI,
503
+ functionName: "transferWithAuthorization",
504
+ args: [
505
+ payload.authorization.from,
506
+ payload.authorization.to,
507
+ BigInt(payload.authorization.value),
508
+ BigInt(payload.authorization.validAfter),
509
+ BigInt(payload.authorization.validBefore),
510
+ payload.authorization.nonce,
511
+ signature
512
+ ],
513
+ chain: wallet.chain
514
+ });
515
+ }
216
516
  const receipt = await wallet.waitForTransactionReceipt({ hash: tx });
217
517
  if (receipt.status !== "success") {
218
518
  return {
@@ -306,63 +606,6 @@ function splitSignature(signature) {
306
606
  return { v, r, s };
307
607
  }
308
608
 
309
- // src/types/shared/evm/permitProxyABI.ts
310
- var permitProxyContractABI = [
311
- {
312
- inputs: [
313
- {
314
- internalType: "address",
315
- name: "token",
316
- type: "address"
317
- },
318
- {
319
- internalType: "address",
320
- name: "owner",
321
- type: "address"
322
- },
323
- {
324
- internalType: "address",
325
- name: "spender",
326
- type: "address"
327
- },
328
- {
329
- internalType: "uint256",
330
- name: "value",
331
- type: "uint256"
332
- },
333
- {
334
- internalType: "uint256",
335
- name: "deadline",
336
- type: "uint256"
337
- },
338
- {
339
- internalType: "uint8",
340
- name: "v",
341
- type: "uint8"
342
- },
343
- {
344
- internalType: "bytes32",
345
- name: "r",
346
- type: "bytes32"
347
- },
348
- {
349
- internalType: "bytes32",
350
- name: "s",
351
- type: "bytes32"
352
- },
353
- {
354
- internalType: "address",
355
- name: "to",
356
- type: "address"
357
- }
358
- ],
359
- name: "permitAndTransfer",
360
- outputs: [],
361
- stateMutability: "nonpayable",
362
- type: "function"
363
- }
364
- ];
365
-
366
609
  // src/schemes/exact/evm/permit/facilitator.ts
367
610
  async function verify2(client, payload, paymentRequirements) {
368
611
  if (payload.payload.authorizationType !== "permit" || payload.scheme !== SCHEME || paymentRequirements.scheme !== SCHEME) {
@@ -430,8 +673,8 @@ async function verify2(client, payload, paymentRequirements) {
430
673
  payer: owner
431
674
  };
432
675
  }
433
- if (paymentRequirements.extra?.proxyAddress) {
434
- if (getAddress3(spender) !== getAddress3(paymentRequirements.extra?.proxyAddress)) {
676
+ if (paymentRequirements.extra?.relayer) {
677
+ if (getAddress3(spender) !== getAddress3(paymentRequirements.extra?.relayer)) {
435
678
  return {
436
679
  isValid: false,
437
680
  invalidReason: "invalid_spender_address",
@@ -495,21 +738,28 @@ async function settle2(wallet, paymentPayload, paymentRequirements) {
495
738
  address: wallet.account.address
496
739
  });
497
740
  let transactionHash;
498
- if (paymentRequirements.extra?.proxyAddress) {
741
+ if (paymentRequirements.extra?.relayer) {
499
742
  transactionHash = await wallet.writeContract({
500
- address: paymentRequirements.extra.proxyAddress,
743
+ address: paymentRequirements.extra.relayer,
501
744
  abi: permitProxyContractABI,
502
- functionName: "permitAndTransfer",
745
+ functionName: "settleWithPermit",
503
746
  args: [
504
747
  tokenAddress,
748
+ // token
505
749
  owner,
506
- spender,
750
+ // payer
751
+ paymentRequirements.payTo,
752
+ // seller
507
753
  BigInt(value),
754
+ // amount
508
755
  BigInt(deadline),
756
+ // deadline
509
757
  v,
758
+ // v
510
759
  r,
511
- s,
512
- paymentRequirements.payTo
760
+ // r
761
+ s
762
+ // s
513
763
  ],
514
764
  chain: wallet.chain,
515
765
  nonce: txNonce
@@ -824,7 +1074,7 @@ function preparePaymentHeader2(from, x402Version, paymentRequirements) {
824
1074
  signature: void 0,
825
1075
  authorization: {
826
1076
  owner: from,
827
- spender: paymentRequirements.extra?.relayer,
1077
+ spender: paymentRequirements.extra?.relayer || paymentRequirements.payTo,
828
1078
  value: paymentRequirements.maxAmountRequired,
829
1079
  deadline
830
1080
  }
@@ -1494,4 +1744,4 @@ export {
1494
1744
  verify4 as verify2,
1495
1745
  settle4 as settle2
1496
1746
  };
1497
- //# sourceMappingURL=chunk-TAYXZJPR.mjs.map
1747
+ //# sourceMappingURL=chunk-3SCH5JL2.mjs.map