@x402/evm 2.2.0 → 2.3.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/cjs/exact/client/index.d.ts +2 -2
- package/dist/cjs/exact/client/index.js +264 -119
- package/dist/cjs/exact/client/index.js.map +1 -1
- package/dist/cjs/exact/facilitator/index.d.ts +3 -0
- package/dist/cjs/exact/facilitator/index.js +691 -281
- package/dist/cjs/exact/facilitator/index.js.map +1 -1
- package/dist/cjs/exact/v1/client/index.js +43 -17
- package/dist/cjs/exact/v1/client/index.js.map +1 -1
- package/dist/cjs/exact/v1/facilitator/index.js +59 -26
- package/dist/cjs/exact/v1/facilitator/index.js.map +1 -1
- package/dist/cjs/index.d.ts +458 -31
- package/dist/cjs/index.js +438 -63
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/permit2-BYv82va2.d.ts +103 -0
- package/dist/cjs/v1/index.d.ts +21 -1
- package/dist/cjs/v1/index.js +33 -29
- package/dist/cjs/v1/index.js.map +1 -1
- package/dist/esm/chunk-DSSJHWGT.mjs +658 -0
- package/dist/esm/chunk-DSSJHWGT.mjs.map +1 -0
- package/dist/esm/chunk-PFULIQAE.mjs +13 -0
- package/dist/esm/chunk-PFULIQAE.mjs.map +1 -0
- package/dist/esm/chunk-U4H6Q62Q.mjs +229 -0
- package/dist/esm/chunk-U4H6Q62Q.mjs.map +1 -0
- package/dist/esm/exact/client/index.d.mts +2 -2
- package/dist/esm/exact/client/index.mjs +10 -30
- package/dist/esm/exact/client/index.mjs.map +1 -1
- package/dist/esm/exact/facilitator/index.d.mts +3 -0
- package/dist/esm/exact/facilitator/index.mjs +491 -241
- package/dist/esm/exact/facilitator/index.mjs.map +1 -1
- package/dist/esm/exact/v1/client/index.mjs +1 -2
- package/dist/esm/exact/v1/facilitator/index.mjs +2 -3
- package/dist/esm/index.d.mts +458 -31
- package/dist/esm/index.mjs +31 -4
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/permit2-BsAoJiWD.d.mts +103 -0
- package/dist/esm/v1/index.d.mts +21 -1
- package/dist/esm/v1/index.mjs +4 -6
- package/package.json +2 -2
- package/dist/esm/chunk-FOUXRQAV.mjs +0 -88
- package/dist/esm/chunk-FOUXRQAV.mjs.map +0 -1
- package/dist/esm/chunk-JYZWCLMP.mjs +0 -305
- package/dist/esm/chunk-JYZWCLMP.mjs.map +0 -1
- package/dist/esm/chunk-PSA4YVU2.mjs +0 -92
- package/dist/esm/chunk-PSA4YVU2.mjs.map +0 -1
- package/dist/esm/chunk-QLXM7BIB.mjs +0 -23
- package/dist/esm/chunk-QLXM7BIB.mjs.map +0 -1
- package/dist/esm/chunk-ZYXTTU74.mjs +0 -88
- package/dist/esm/chunk-ZYXTTU74.mjs.map +0 -1
package/dist/cjs/index.js
CHANGED
|
@@ -21,13 +21,25 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
ExactEvmScheme: () => ExactEvmScheme,
|
|
24
|
+
PERMIT2_ADDRESS: () => PERMIT2_ADDRESS,
|
|
25
|
+
authorizationTypes: () => authorizationTypes,
|
|
26
|
+
createPermit2ApprovalTx: () => createPermit2ApprovalTx,
|
|
27
|
+
eip3009ABI: () => eip3009ABI,
|
|
28
|
+
erc20AllowanceAbi: () => erc20AllowanceAbi,
|
|
29
|
+
getPermit2AllowanceReadParams: () => getPermit2AllowanceReadParams,
|
|
30
|
+
isEIP3009Payload: () => isEIP3009Payload,
|
|
31
|
+
isPermit2Payload: () => isPermit2Payload,
|
|
32
|
+
permit2WitnessTypes: () => permit2WitnessTypes,
|
|
24
33
|
toClientEvmSigner: () => toClientEvmSigner,
|
|
25
|
-
toFacilitatorEvmSigner: () => toFacilitatorEvmSigner
|
|
34
|
+
toFacilitatorEvmSigner: () => toFacilitatorEvmSigner,
|
|
35
|
+
x402ExactPermit2ProxyABI: () => x402ExactPermit2ProxyABI,
|
|
36
|
+
x402ExactPermit2ProxyAddress: () => x402ExactPermit2ProxyAddress,
|
|
37
|
+
x402UptoPermit2ProxyAddress: () => x402UptoPermit2ProxyAddress
|
|
26
38
|
});
|
|
27
39
|
module.exports = __toCommonJS(src_exports);
|
|
28
40
|
|
|
29
|
-
// src/exact/client/
|
|
30
|
-
var
|
|
41
|
+
// src/exact/client/eip3009.ts
|
|
42
|
+
var import_viem4 = require("viem");
|
|
31
43
|
|
|
32
44
|
// src/constants.ts
|
|
33
45
|
var authorizationTypes = {
|
|
@@ -40,15 +52,408 @@ var authorizationTypes = {
|
|
|
40
52
|
{ name: "nonce", type: "bytes32" }
|
|
41
53
|
]
|
|
42
54
|
};
|
|
55
|
+
var permit2WitnessTypes = {
|
|
56
|
+
PermitWitnessTransferFrom: [
|
|
57
|
+
{ name: "permitted", type: "TokenPermissions" },
|
|
58
|
+
{ name: "spender", type: "address" },
|
|
59
|
+
{ name: "nonce", type: "uint256" },
|
|
60
|
+
{ name: "deadline", type: "uint256" },
|
|
61
|
+
{ name: "witness", type: "Witness" }
|
|
62
|
+
],
|
|
63
|
+
TokenPermissions: [
|
|
64
|
+
{ name: "token", type: "address" },
|
|
65
|
+
{ name: "amount", type: "uint256" }
|
|
66
|
+
],
|
|
67
|
+
Witness: [
|
|
68
|
+
{ name: "to", type: "address" },
|
|
69
|
+
{ name: "validAfter", type: "uint256" },
|
|
70
|
+
{ name: "extra", type: "bytes" }
|
|
71
|
+
]
|
|
72
|
+
};
|
|
73
|
+
var eip3009ABI = [
|
|
74
|
+
{
|
|
75
|
+
inputs: [
|
|
76
|
+
{ name: "from", type: "address" },
|
|
77
|
+
{ name: "to", type: "address" },
|
|
78
|
+
{ name: "value", type: "uint256" },
|
|
79
|
+
{ name: "validAfter", type: "uint256" },
|
|
80
|
+
{ name: "validBefore", type: "uint256" },
|
|
81
|
+
{ name: "nonce", type: "bytes32" },
|
|
82
|
+
{ name: "v", type: "uint8" },
|
|
83
|
+
{ name: "r", type: "bytes32" },
|
|
84
|
+
{ name: "s", type: "bytes32" }
|
|
85
|
+
],
|
|
86
|
+
name: "transferWithAuthorization",
|
|
87
|
+
outputs: [],
|
|
88
|
+
stateMutability: "nonpayable",
|
|
89
|
+
type: "function"
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
inputs: [
|
|
93
|
+
{ name: "from", type: "address" },
|
|
94
|
+
{ name: "to", type: "address" },
|
|
95
|
+
{ name: "value", type: "uint256" },
|
|
96
|
+
{ name: "validAfter", type: "uint256" },
|
|
97
|
+
{ name: "validBefore", type: "uint256" },
|
|
98
|
+
{ name: "nonce", type: "bytes32" },
|
|
99
|
+
{ name: "signature", type: "bytes" }
|
|
100
|
+
],
|
|
101
|
+
name: "transferWithAuthorization",
|
|
102
|
+
outputs: [],
|
|
103
|
+
stateMutability: "nonpayable",
|
|
104
|
+
type: "function"
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
inputs: [{ name: "account", type: "address" }],
|
|
108
|
+
name: "balanceOf",
|
|
109
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
110
|
+
stateMutability: "view",
|
|
111
|
+
type: "function"
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
inputs: [],
|
|
115
|
+
name: "version",
|
|
116
|
+
outputs: [{ name: "", type: "string" }],
|
|
117
|
+
stateMutability: "view",
|
|
118
|
+
type: "function"
|
|
119
|
+
}
|
|
120
|
+
];
|
|
121
|
+
var PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
|
|
122
|
+
var x402ExactPermit2ProxyAddress = "0x4020615294c913F045dc10f0a5cdEbd86c280001";
|
|
123
|
+
var x402UptoPermit2ProxyAddress = "0x4020633461b2895a48930Ff97eE8fCdE8E520002";
|
|
124
|
+
var x402ExactPermit2ProxyABI = [
|
|
125
|
+
{
|
|
126
|
+
type: "function",
|
|
127
|
+
name: "PERMIT2",
|
|
128
|
+
inputs: [],
|
|
129
|
+
outputs: [{ name: "", type: "address", internalType: "contract ISignatureTransfer" }],
|
|
130
|
+
stateMutability: "view"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
type: "function",
|
|
134
|
+
name: "WITNESS_TYPEHASH",
|
|
135
|
+
inputs: [],
|
|
136
|
+
outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
|
|
137
|
+
stateMutability: "view"
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
type: "function",
|
|
141
|
+
name: "WITNESS_TYPE_STRING",
|
|
142
|
+
inputs: [],
|
|
143
|
+
outputs: [{ name: "", type: "string", internalType: "string" }],
|
|
144
|
+
stateMutability: "view"
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
type: "function",
|
|
148
|
+
name: "initialize",
|
|
149
|
+
inputs: [{ name: "_permit2", type: "address", internalType: "address" }],
|
|
150
|
+
outputs: [],
|
|
151
|
+
stateMutability: "nonpayable"
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
type: "function",
|
|
155
|
+
name: "settle",
|
|
156
|
+
inputs: [
|
|
157
|
+
{
|
|
158
|
+
name: "permit",
|
|
159
|
+
type: "tuple",
|
|
160
|
+
internalType: "struct ISignatureTransfer.PermitTransferFrom",
|
|
161
|
+
components: [
|
|
162
|
+
{
|
|
163
|
+
name: "permitted",
|
|
164
|
+
type: "tuple",
|
|
165
|
+
internalType: "struct ISignatureTransfer.TokenPermissions",
|
|
166
|
+
components: [
|
|
167
|
+
{ name: "token", type: "address", internalType: "address" },
|
|
168
|
+
{ name: "amount", type: "uint256", internalType: "uint256" }
|
|
169
|
+
]
|
|
170
|
+
},
|
|
171
|
+
{ name: "nonce", type: "uint256", internalType: "uint256" },
|
|
172
|
+
{ name: "deadline", type: "uint256", internalType: "uint256" }
|
|
173
|
+
]
|
|
174
|
+
},
|
|
175
|
+
{ name: "owner", type: "address", internalType: "address" },
|
|
176
|
+
{
|
|
177
|
+
name: "witness",
|
|
178
|
+
type: "tuple",
|
|
179
|
+
internalType: "struct x402BasePermit2Proxy.Witness",
|
|
180
|
+
components: [
|
|
181
|
+
{ name: "to", type: "address", internalType: "address" },
|
|
182
|
+
{ name: "validAfter", type: "uint256", internalType: "uint256" },
|
|
183
|
+
{ name: "extra", type: "bytes", internalType: "bytes" }
|
|
184
|
+
]
|
|
185
|
+
},
|
|
186
|
+
{ name: "signature", type: "bytes", internalType: "bytes" }
|
|
187
|
+
],
|
|
188
|
+
outputs: [],
|
|
189
|
+
stateMutability: "nonpayable"
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
type: "function",
|
|
193
|
+
name: "settleWithPermit",
|
|
194
|
+
inputs: [
|
|
195
|
+
{
|
|
196
|
+
name: "permit2612",
|
|
197
|
+
type: "tuple",
|
|
198
|
+
internalType: "struct x402BasePermit2Proxy.EIP2612Permit",
|
|
199
|
+
components: [
|
|
200
|
+
{ name: "value", type: "uint256", internalType: "uint256" },
|
|
201
|
+
{ name: "deadline", type: "uint256", internalType: "uint256" },
|
|
202
|
+
{ name: "r", type: "bytes32", internalType: "bytes32" },
|
|
203
|
+
{ name: "s", type: "bytes32", internalType: "bytes32" },
|
|
204
|
+
{ name: "v", type: "uint8", internalType: "uint8" }
|
|
205
|
+
]
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "permit",
|
|
209
|
+
type: "tuple",
|
|
210
|
+
internalType: "struct ISignatureTransfer.PermitTransferFrom",
|
|
211
|
+
components: [
|
|
212
|
+
{
|
|
213
|
+
name: "permitted",
|
|
214
|
+
type: "tuple",
|
|
215
|
+
internalType: "struct ISignatureTransfer.TokenPermissions",
|
|
216
|
+
components: [
|
|
217
|
+
{ name: "token", type: "address", internalType: "address" },
|
|
218
|
+
{ name: "amount", type: "uint256", internalType: "uint256" }
|
|
219
|
+
]
|
|
220
|
+
},
|
|
221
|
+
{ name: "nonce", type: "uint256", internalType: "uint256" },
|
|
222
|
+
{ name: "deadline", type: "uint256", internalType: "uint256" }
|
|
223
|
+
]
|
|
224
|
+
},
|
|
225
|
+
{ name: "owner", type: "address", internalType: "address" },
|
|
226
|
+
{
|
|
227
|
+
name: "witness",
|
|
228
|
+
type: "tuple",
|
|
229
|
+
internalType: "struct x402BasePermit2Proxy.Witness",
|
|
230
|
+
components: [
|
|
231
|
+
{ name: "to", type: "address", internalType: "address" },
|
|
232
|
+
{ name: "validAfter", type: "uint256", internalType: "uint256" },
|
|
233
|
+
{ name: "extra", type: "bytes", internalType: "bytes" }
|
|
234
|
+
]
|
|
235
|
+
},
|
|
236
|
+
{ name: "signature", type: "bytes", internalType: "bytes" }
|
|
237
|
+
],
|
|
238
|
+
outputs: [],
|
|
239
|
+
stateMutability: "nonpayable"
|
|
240
|
+
},
|
|
241
|
+
{ type: "event", name: "Settled", inputs: [], anonymous: false },
|
|
242
|
+
{ type: "event", name: "SettledWithPermit", inputs: [], anonymous: false },
|
|
243
|
+
{ type: "error", name: "AlreadyInitialized", inputs: [] },
|
|
244
|
+
{ type: "error", name: "InvalidDestination", inputs: [] },
|
|
245
|
+
{ type: "error", name: "InvalidOwner", inputs: [] },
|
|
246
|
+
{ type: "error", name: "InvalidPermit2Address", inputs: [] },
|
|
247
|
+
{ type: "error", name: "PaymentTooEarly", inputs: [] },
|
|
248
|
+
{ type: "error", name: "ReentrancyGuardReentrantCall", inputs: [] }
|
|
249
|
+
];
|
|
43
250
|
|
|
44
251
|
// src/utils.ts
|
|
252
|
+
var import_viem3 = require("viem");
|
|
253
|
+
|
|
254
|
+
// src/exact/v1/client/scheme.ts
|
|
45
255
|
var import_viem = require("viem");
|
|
46
|
-
|
|
47
|
-
|
|
256
|
+
|
|
257
|
+
// src/exact/v1/facilitator/scheme.ts
|
|
258
|
+
var import_viem2 = require("viem");
|
|
259
|
+
|
|
260
|
+
// src/v1/index.ts
|
|
261
|
+
var EVM_NETWORK_CHAIN_ID_MAP = {
|
|
262
|
+
ethereum: 1,
|
|
263
|
+
sepolia: 11155111,
|
|
264
|
+
abstract: 2741,
|
|
265
|
+
"abstract-testnet": 11124,
|
|
266
|
+
"base-sepolia": 84532,
|
|
267
|
+
base: 8453,
|
|
268
|
+
"avalanche-fuji": 43113,
|
|
269
|
+
avalanche: 43114,
|
|
270
|
+
iotex: 4689,
|
|
271
|
+
sei: 1329,
|
|
272
|
+
"sei-testnet": 1328,
|
|
273
|
+
polygon: 137,
|
|
274
|
+
"polygon-amoy": 80002,
|
|
275
|
+
peaq: 3338,
|
|
276
|
+
story: 1514,
|
|
277
|
+
educhain: 41923,
|
|
278
|
+
"skale-base-sepolia": 324705682
|
|
279
|
+
};
|
|
280
|
+
var NETWORKS = Object.keys(EVM_NETWORK_CHAIN_ID_MAP);
|
|
281
|
+
|
|
282
|
+
// src/utils.ts
|
|
283
|
+
function getCrypto() {
|
|
284
|
+
const cryptoObj = globalThis.crypto;
|
|
48
285
|
if (!cryptoObj) {
|
|
49
286
|
throw new Error("Crypto API not available");
|
|
50
287
|
}
|
|
51
|
-
return
|
|
288
|
+
return cryptoObj;
|
|
289
|
+
}
|
|
290
|
+
function createNonce() {
|
|
291
|
+
return (0, import_viem3.toHex)(getCrypto().getRandomValues(new Uint8Array(32)));
|
|
292
|
+
}
|
|
293
|
+
function createPermit2Nonce() {
|
|
294
|
+
const randomBytes = getCrypto().getRandomValues(new Uint8Array(32));
|
|
295
|
+
return BigInt((0, import_viem3.toHex)(randomBytes)).toString();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// src/exact/client/eip3009.ts
|
|
299
|
+
async function createEIP3009Payload(signer, x402Version, paymentRequirements) {
|
|
300
|
+
const nonce = createNonce();
|
|
301
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
302
|
+
const authorization = {
|
|
303
|
+
from: signer.address,
|
|
304
|
+
to: (0, import_viem4.getAddress)(paymentRequirements.payTo),
|
|
305
|
+
value: paymentRequirements.amount,
|
|
306
|
+
validAfter: (now - 600).toString(),
|
|
307
|
+
validBefore: (now + paymentRequirements.maxTimeoutSeconds).toString(),
|
|
308
|
+
nonce
|
|
309
|
+
};
|
|
310
|
+
const signature = await signEIP3009Authorization(signer, authorization, paymentRequirements);
|
|
311
|
+
const payload = {
|
|
312
|
+
authorization,
|
|
313
|
+
signature
|
|
314
|
+
};
|
|
315
|
+
return {
|
|
316
|
+
x402Version,
|
|
317
|
+
payload
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
async function signEIP3009Authorization(signer, authorization, requirements) {
|
|
321
|
+
const chainId = parseInt(requirements.network.split(":")[1]);
|
|
322
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
323
|
+
throw new Error(
|
|
324
|
+
`EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
const { name, version } = requirements.extra;
|
|
328
|
+
const domain = {
|
|
329
|
+
name,
|
|
330
|
+
version,
|
|
331
|
+
chainId,
|
|
332
|
+
verifyingContract: (0, import_viem4.getAddress)(requirements.asset)
|
|
333
|
+
};
|
|
334
|
+
const message = {
|
|
335
|
+
from: (0, import_viem4.getAddress)(authorization.from),
|
|
336
|
+
to: (0, import_viem4.getAddress)(authorization.to),
|
|
337
|
+
value: BigInt(authorization.value),
|
|
338
|
+
validAfter: BigInt(authorization.validAfter),
|
|
339
|
+
validBefore: BigInt(authorization.validBefore),
|
|
340
|
+
nonce: authorization.nonce
|
|
341
|
+
};
|
|
342
|
+
return await signer.signTypedData({
|
|
343
|
+
domain,
|
|
344
|
+
types: authorizationTypes,
|
|
345
|
+
primaryType: "TransferWithAuthorization",
|
|
346
|
+
message
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/exact/client/permit2.ts
|
|
351
|
+
var import_viem5 = require("viem");
|
|
352
|
+
var MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
353
|
+
async function createPermit2Payload(signer, x402Version, paymentRequirements) {
|
|
354
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
355
|
+
const nonce = createPermit2Nonce();
|
|
356
|
+
const validAfter = (now - 600).toString();
|
|
357
|
+
const deadline = (now + paymentRequirements.maxTimeoutSeconds).toString();
|
|
358
|
+
const permit2Authorization = {
|
|
359
|
+
from: signer.address,
|
|
360
|
+
permitted: {
|
|
361
|
+
token: (0, import_viem5.getAddress)(paymentRequirements.asset),
|
|
362
|
+
amount: paymentRequirements.amount
|
|
363
|
+
},
|
|
364
|
+
spender: x402ExactPermit2ProxyAddress,
|
|
365
|
+
nonce,
|
|
366
|
+
deadline,
|
|
367
|
+
witness: {
|
|
368
|
+
to: (0, import_viem5.getAddress)(paymentRequirements.payTo),
|
|
369
|
+
validAfter,
|
|
370
|
+
extra: "0x"
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
const signature = await signPermit2Authorization(
|
|
374
|
+
signer,
|
|
375
|
+
permit2Authorization,
|
|
376
|
+
paymentRequirements
|
|
377
|
+
);
|
|
378
|
+
const payload = {
|
|
379
|
+
signature,
|
|
380
|
+
permit2Authorization
|
|
381
|
+
};
|
|
382
|
+
return {
|
|
383
|
+
x402Version,
|
|
384
|
+
payload
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
async function signPermit2Authorization(signer, permit2Authorization, requirements) {
|
|
388
|
+
const chainId = parseInt(requirements.network.split(":")[1]);
|
|
389
|
+
const domain = {
|
|
390
|
+
name: "Permit2",
|
|
391
|
+
chainId,
|
|
392
|
+
verifyingContract: PERMIT2_ADDRESS
|
|
393
|
+
};
|
|
394
|
+
const message = {
|
|
395
|
+
permitted: {
|
|
396
|
+
token: (0, import_viem5.getAddress)(permit2Authorization.permitted.token),
|
|
397
|
+
amount: BigInt(permit2Authorization.permitted.amount)
|
|
398
|
+
},
|
|
399
|
+
spender: (0, import_viem5.getAddress)(permit2Authorization.spender),
|
|
400
|
+
nonce: BigInt(permit2Authorization.nonce),
|
|
401
|
+
deadline: BigInt(permit2Authorization.deadline),
|
|
402
|
+
witness: {
|
|
403
|
+
to: (0, import_viem5.getAddress)(permit2Authorization.witness.to),
|
|
404
|
+
validAfter: BigInt(permit2Authorization.witness.validAfter),
|
|
405
|
+
extra: permit2Authorization.witness.extra
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
return await signer.signTypedData({
|
|
409
|
+
domain,
|
|
410
|
+
types: permit2WitnessTypes,
|
|
411
|
+
primaryType: "PermitWitnessTransferFrom",
|
|
412
|
+
message
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
var erc20ApproveAbi = [
|
|
416
|
+
{
|
|
417
|
+
type: "function",
|
|
418
|
+
name: "approve",
|
|
419
|
+
inputs: [
|
|
420
|
+
{ name: "spender", type: "address" },
|
|
421
|
+
{ name: "amount", type: "uint256" }
|
|
422
|
+
],
|
|
423
|
+
outputs: [{ type: "bool" }],
|
|
424
|
+
stateMutability: "nonpayable"
|
|
425
|
+
}
|
|
426
|
+
];
|
|
427
|
+
var erc20AllowanceAbi = [
|
|
428
|
+
{
|
|
429
|
+
type: "function",
|
|
430
|
+
name: "allowance",
|
|
431
|
+
inputs: [
|
|
432
|
+
{ name: "owner", type: "address" },
|
|
433
|
+
{ name: "spender", type: "address" }
|
|
434
|
+
],
|
|
435
|
+
outputs: [{ type: "uint256" }],
|
|
436
|
+
stateMutability: "view"
|
|
437
|
+
}
|
|
438
|
+
];
|
|
439
|
+
function createPermit2ApprovalTx(tokenAddress) {
|
|
440
|
+
const data = (0, import_viem5.encodeFunctionData)({
|
|
441
|
+
abi: erc20ApproveAbi,
|
|
442
|
+
functionName: "approve",
|
|
443
|
+
args: [PERMIT2_ADDRESS, MAX_UINT256]
|
|
444
|
+
});
|
|
445
|
+
return {
|
|
446
|
+
to: (0, import_viem5.getAddress)(tokenAddress),
|
|
447
|
+
data
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
function getPermit2AllowanceReadParams(params) {
|
|
451
|
+
return {
|
|
452
|
+
address: (0, import_viem5.getAddress)(params.tokenAddress),
|
|
453
|
+
abi: erc20AllowanceAbi,
|
|
454
|
+
functionName: "allowance",
|
|
455
|
+
args: [(0, import_viem5.getAddress)(params.ownerAddress), PERMIT2_ADDRESS]
|
|
456
|
+
};
|
|
52
457
|
}
|
|
53
458
|
|
|
54
459
|
// src/exact/client/scheme.ts
|
|
@@ -64,68 +469,18 @@ var ExactEvmScheme = class {
|
|
|
64
469
|
}
|
|
65
470
|
/**
|
|
66
471
|
* Creates a payment payload for the Exact scheme.
|
|
472
|
+
* Routes to EIP-3009 or Permit2 based on requirements.extra.assetTransferMethod.
|
|
67
473
|
*
|
|
68
474
|
* @param x402Version - The x402 protocol version
|
|
69
475
|
* @param paymentRequirements - The payment requirements
|
|
70
|
-
* @returns Promise resolving to a payment payload
|
|
476
|
+
* @returns Promise resolving to a payment payload result
|
|
71
477
|
*/
|
|
72
478
|
async createPaymentPayload(x402Version, paymentRequirements) {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
from: this.signer.address,
|
|
77
|
-
to: (0, import_viem2.getAddress)(paymentRequirements.payTo),
|
|
78
|
-
value: paymentRequirements.amount,
|
|
79
|
-
validAfter: (now - 600).toString(),
|
|
80
|
-
// 10 minutes before
|
|
81
|
-
validBefore: (now + paymentRequirements.maxTimeoutSeconds).toString(),
|
|
82
|
-
nonce
|
|
83
|
-
};
|
|
84
|
-
const signature = await this.signAuthorization(authorization, paymentRequirements);
|
|
85
|
-
const payload = {
|
|
86
|
-
authorization,
|
|
87
|
-
signature
|
|
88
|
-
};
|
|
89
|
-
return {
|
|
90
|
-
x402Version,
|
|
91
|
-
payload
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Sign the EIP-3009 authorization using EIP-712
|
|
96
|
-
*
|
|
97
|
-
* @param authorization - The authorization to sign
|
|
98
|
-
* @param requirements - The payment requirements
|
|
99
|
-
* @returns Promise resolving to the signature
|
|
100
|
-
*/
|
|
101
|
-
async signAuthorization(authorization, requirements) {
|
|
102
|
-
const chainId = parseInt(requirements.network.split(":")[1]);
|
|
103
|
-
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
104
|
-
throw new Error(
|
|
105
|
-
`EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
|
|
106
|
-
);
|
|
479
|
+
const assetTransferMethod = paymentRequirements.extra?.assetTransferMethod ?? "eip3009";
|
|
480
|
+
if (assetTransferMethod === "permit2") {
|
|
481
|
+
return createPermit2Payload(this.signer, x402Version, paymentRequirements);
|
|
107
482
|
}
|
|
108
|
-
|
|
109
|
-
const domain = {
|
|
110
|
-
name,
|
|
111
|
-
version,
|
|
112
|
-
chainId,
|
|
113
|
-
verifyingContract: (0, import_viem2.getAddress)(requirements.asset)
|
|
114
|
-
};
|
|
115
|
-
const message = {
|
|
116
|
-
from: (0, import_viem2.getAddress)(authorization.from),
|
|
117
|
-
to: (0, import_viem2.getAddress)(authorization.to),
|
|
118
|
-
value: BigInt(authorization.value),
|
|
119
|
-
validAfter: BigInt(authorization.validAfter),
|
|
120
|
-
validBefore: BigInt(authorization.validBefore),
|
|
121
|
-
nonce: authorization.nonce
|
|
122
|
-
};
|
|
123
|
-
return await this.signer.signTypedData({
|
|
124
|
-
domain,
|
|
125
|
-
types: authorizationTypes,
|
|
126
|
-
primaryType: "TransferWithAuthorization",
|
|
127
|
-
message
|
|
128
|
-
});
|
|
483
|
+
return createEIP3009Payload(this.signer, x402Version, paymentRequirements);
|
|
129
484
|
}
|
|
130
485
|
};
|
|
131
486
|
|
|
@@ -139,10 +494,30 @@ function toFacilitatorEvmSigner(client) {
|
|
|
139
494
|
getAddresses: () => [client.address]
|
|
140
495
|
};
|
|
141
496
|
}
|
|
497
|
+
|
|
498
|
+
// src/types.ts
|
|
499
|
+
function isPermit2Payload(payload) {
|
|
500
|
+
return "permit2Authorization" in payload;
|
|
501
|
+
}
|
|
502
|
+
function isEIP3009Payload(payload) {
|
|
503
|
+
return "authorization" in payload;
|
|
504
|
+
}
|
|
142
505
|
// Annotate the CommonJS export names for ESM import in node:
|
|
143
506
|
0 && (module.exports = {
|
|
144
507
|
ExactEvmScheme,
|
|
508
|
+
PERMIT2_ADDRESS,
|
|
509
|
+
authorizationTypes,
|
|
510
|
+
createPermit2ApprovalTx,
|
|
511
|
+
eip3009ABI,
|
|
512
|
+
erc20AllowanceAbi,
|
|
513
|
+
getPermit2AllowanceReadParams,
|
|
514
|
+
isEIP3009Payload,
|
|
515
|
+
isPermit2Payload,
|
|
516
|
+
permit2WitnessTypes,
|
|
145
517
|
toClientEvmSigner,
|
|
146
|
-
toFacilitatorEvmSigner
|
|
518
|
+
toFacilitatorEvmSigner,
|
|
519
|
+
x402ExactPermit2ProxyABI,
|
|
520
|
+
x402ExactPermit2ProxyAddress,
|
|
521
|
+
x402UptoPermit2ProxyAddress
|
|
147
522
|
});
|
|
148
523
|
//# sourceMappingURL=index.js.map
|