@x402/evm 2.2.0 → 2.3.1
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 +265 -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 +692 -281
- package/dist/cjs/exact/facilitator/index.js.map +1 -1
- package/dist/cjs/exact/server/index.js +8 -1
- package/dist/cjs/exact/server/index.js.map +1 -1
- package/dist/cjs/exact/v1/client/index.js +44 -17
- package/dist/cjs/exact/v1/client/index.js.map +1 -1
- package/dist/cjs/exact/v1/facilitator/index.js +60 -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 +439 -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 +22 -1
- package/dist/cjs/v1/index.js +34 -29
- package/dist/cjs/v1/index.js.map +1 -1
- package/dist/esm/chunk-E2YMUI3X.mjs +229 -0
- package/dist/esm/chunk-E2YMUI3X.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-RPL6OFJL.mjs +659 -0
- package/dist/esm/chunk-RPL6OFJL.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/server/index.mjs +8 -1
- package/dist/esm/exact/server/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 +22 -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,409 @@ 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
|
+
megaeth: 4326
|
|
280
|
+
};
|
|
281
|
+
var NETWORKS = Object.keys(EVM_NETWORK_CHAIN_ID_MAP);
|
|
282
|
+
|
|
283
|
+
// src/utils.ts
|
|
284
|
+
function getCrypto() {
|
|
285
|
+
const cryptoObj = globalThis.crypto;
|
|
48
286
|
if (!cryptoObj) {
|
|
49
287
|
throw new Error("Crypto API not available");
|
|
50
288
|
}
|
|
51
|
-
return
|
|
289
|
+
return cryptoObj;
|
|
290
|
+
}
|
|
291
|
+
function createNonce() {
|
|
292
|
+
return (0, import_viem3.toHex)(getCrypto().getRandomValues(new Uint8Array(32)));
|
|
293
|
+
}
|
|
294
|
+
function createPermit2Nonce() {
|
|
295
|
+
const randomBytes = getCrypto().getRandomValues(new Uint8Array(32));
|
|
296
|
+
return BigInt((0, import_viem3.toHex)(randomBytes)).toString();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/exact/client/eip3009.ts
|
|
300
|
+
async function createEIP3009Payload(signer, x402Version, paymentRequirements) {
|
|
301
|
+
const nonce = createNonce();
|
|
302
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
303
|
+
const authorization = {
|
|
304
|
+
from: signer.address,
|
|
305
|
+
to: (0, import_viem4.getAddress)(paymentRequirements.payTo),
|
|
306
|
+
value: paymentRequirements.amount,
|
|
307
|
+
validAfter: (now - 600).toString(),
|
|
308
|
+
validBefore: (now + paymentRequirements.maxTimeoutSeconds).toString(),
|
|
309
|
+
nonce
|
|
310
|
+
};
|
|
311
|
+
const signature = await signEIP3009Authorization(signer, authorization, paymentRequirements);
|
|
312
|
+
const payload = {
|
|
313
|
+
authorization,
|
|
314
|
+
signature
|
|
315
|
+
};
|
|
316
|
+
return {
|
|
317
|
+
x402Version,
|
|
318
|
+
payload
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
async function signEIP3009Authorization(signer, authorization, requirements) {
|
|
322
|
+
const chainId = parseInt(requirements.network.split(":")[1]);
|
|
323
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
324
|
+
throw new Error(
|
|
325
|
+
`EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
const { name, version } = requirements.extra;
|
|
329
|
+
const domain = {
|
|
330
|
+
name,
|
|
331
|
+
version,
|
|
332
|
+
chainId,
|
|
333
|
+
verifyingContract: (0, import_viem4.getAddress)(requirements.asset)
|
|
334
|
+
};
|
|
335
|
+
const message = {
|
|
336
|
+
from: (0, import_viem4.getAddress)(authorization.from),
|
|
337
|
+
to: (0, import_viem4.getAddress)(authorization.to),
|
|
338
|
+
value: BigInt(authorization.value),
|
|
339
|
+
validAfter: BigInt(authorization.validAfter),
|
|
340
|
+
validBefore: BigInt(authorization.validBefore),
|
|
341
|
+
nonce: authorization.nonce
|
|
342
|
+
};
|
|
343
|
+
return await signer.signTypedData({
|
|
344
|
+
domain,
|
|
345
|
+
types: authorizationTypes,
|
|
346
|
+
primaryType: "TransferWithAuthorization",
|
|
347
|
+
message
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// src/exact/client/permit2.ts
|
|
352
|
+
var import_viem5 = require("viem");
|
|
353
|
+
var MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
354
|
+
async function createPermit2Payload(signer, x402Version, paymentRequirements) {
|
|
355
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
356
|
+
const nonce = createPermit2Nonce();
|
|
357
|
+
const validAfter = (now - 600).toString();
|
|
358
|
+
const deadline = (now + paymentRequirements.maxTimeoutSeconds).toString();
|
|
359
|
+
const permit2Authorization = {
|
|
360
|
+
from: signer.address,
|
|
361
|
+
permitted: {
|
|
362
|
+
token: (0, import_viem5.getAddress)(paymentRequirements.asset),
|
|
363
|
+
amount: paymentRequirements.amount
|
|
364
|
+
},
|
|
365
|
+
spender: x402ExactPermit2ProxyAddress,
|
|
366
|
+
nonce,
|
|
367
|
+
deadline,
|
|
368
|
+
witness: {
|
|
369
|
+
to: (0, import_viem5.getAddress)(paymentRequirements.payTo),
|
|
370
|
+
validAfter,
|
|
371
|
+
extra: "0x"
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
const signature = await signPermit2Authorization(
|
|
375
|
+
signer,
|
|
376
|
+
permit2Authorization,
|
|
377
|
+
paymentRequirements
|
|
378
|
+
);
|
|
379
|
+
const payload = {
|
|
380
|
+
signature,
|
|
381
|
+
permit2Authorization
|
|
382
|
+
};
|
|
383
|
+
return {
|
|
384
|
+
x402Version,
|
|
385
|
+
payload
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
async function signPermit2Authorization(signer, permit2Authorization, requirements) {
|
|
389
|
+
const chainId = parseInt(requirements.network.split(":")[1]);
|
|
390
|
+
const domain = {
|
|
391
|
+
name: "Permit2",
|
|
392
|
+
chainId,
|
|
393
|
+
verifyingContract: PERMIT2_ADDRESS
|
|
394
|
+
};
|
|
395
|
+
const message = {
|
|
396
|
+
permitted: {
|
|
397
|
+
token: (0, import_viem5.getAddress)(permit2Authorization.permitted.token),
|
|
398
|
+
amount: BigInt(permit2Authorization.permitted.amount)
|
|
399
|
+
},
|
|
400
|
+
spender: (0, import_viem5.getAddress)(permit2Authorization.spender),
|
|
401
|
+
nonce: BigInt(permit2Authorization.nonce),
|
|
402
|
+
deadline: BigInt(permit2Authorization.deadline),
|
|
403
|
+
witness: {
|
|
404
|
+
to: (0, import_viem5.getAddress)(permit2Authorization.witness.to),
|
|
405
|
+
validAfter: BigInt(permit2Authorization.witness.validAfter),
|
|
406
|
+
extra: permit2Authorization.witness.extra
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
return await signer.signTypedData({
|
|
410
|
+
domain,
|
|
411
|
+
types: permit2WitnessTypes,
|
|
412
|
+
primaryType: "PermitWitnessTransferFrom",
|
|
413
|
+
message
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
var erc20ApproveAbi = [
|
|
417
|
+
{
|
|
418
|
+
type: "function",
|
|
419
|
+
name: "approve",
|
|
420
|
+
inputs: [
|
|
421
|
+
{ name: "spender", type: "address" },
|
|
422
|
+
{ name: "amount", type: "uint256" }
|
|
423
|
+
],
|
|
424
|
+
outputs: [{ type: "bool" }],
|
|
425
|
+
stateMutability: "nonpayable"
|
|
426
|
+
}
|
|
427
|
+
];
|
|
428
|
+
var erc20AllowanceAbi = [
|
|
429
|
+
{
|
|
430
|
+
type: "function",
|
|
431
|
+
name: "allowance",
|
|
432
|
+
inputs: [
|
|
433
|
+
{ name: "owner", type: "address" },
|
|
434
|
+
{ name: "spender", type: "address" }
|
|
435
|
+
],
|
|
436
|
+
outputs: [{ type: "uint256" }],
|
|
437
|
+
stateMutability: "view"
|
|
438
|
+
}
|
|
439
|
+
];
|
|
440
|
+
function createPermit2ApprovalTx(tokenAddress) {
|
|
441
|
+
const data = (0, import_viem5.encodeFunctionData)({
|
|
442
|
+
abi: erc20ApproveAbi,
|
|
443
|
+
functionName: "approve",
|
|
444
|
+
args: [PERMIT2_ADDRESS, MAX_UINT256]
|
|
445
|
+
});
|
|
446
|
+
return {
|
|
447
|
+
to: (0, import_viem5.getAddress)(tokenAddress),
|
|
448
|
+
data
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
function getPermit2AllowanceReadParams(params) {
|
|
452
|
+
return {
|
|
453
|
+
address: (0, import_viem5.getAddress)(params.tokenAddress),
|
|
454
|
+
abi: erc20AllowanceAbi,
|
|
455
|
+
functionName: "allowance",
|
|
456
|
+
args: [(0, import_viem5.getAddress)(params.ownerAddress), PERMIT2_ADDRESS]
|
|
457
|
+
};
|
|
52
458
|
}
|
|
53
459
|
|
|
54
460
|
// src/exact/client/scheme.ts
|
|
@@ -64,68 +470,18 @@ var ExactEvmScheme = class {
|
|
|
64
470
|
}
|
|
65
471
|
/**
|
|
66
472
|
* Creates a payment payload for the Exact scheme.
|
|
473
|
+
* Routes to EIP-3009 or Permit2 based on requirements.extra.assetTransferMethod.
|
|
67
474
|
*
|
|
68
475
|
* @param x402Version - The x402 protocol version
|
|
69
476
|
* @param paymentRequirements - The payment requirements
|
|
70
|
-
* @returns Promise resolving to a payment payload
|
|
477
|
+
* @returns Promise resolving to a payment payload result
|
|
71
478
|
*/
|
|
72
479
|
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
|
-
);
|
|
480
|
+
const assetTransferMethod = paymentRequirements.extra?.assetTransferMethod ?? "eip3009";
|
|
481
|
+
if (assetTransferMethod === "permit2") {
|
|
482
|
+
return createPermit2Payload(this.signer, x402Version, paymentRequirements);
|
|
107
483
|
}
|
|
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
|
-
});
|
|
484
|
+
return createEIP3009Payload(this.signer, x402Version, paymentRequirements);
|
|
129
485
|
}
|
|
130
486
|
};
|
|
131
487
|
|
|
@@ -139,10 +495,30 @@ function toFacilitatorEvmSigner(client) {
|
|
|
139
495
|
getAddresses: () => [client.address]
|
|
140
496
|
};
|
|
141
497
|
}
|
|
498
|
+
|
|
499
|
+
// src/types.ts
|
|
500
|
+
function isPermit2Payload(payload) {
|
|
501
|
+
return "permit2Authorization" in payload;
|
|
502
|
+
}
|
|
503
|
+
function isEIP3009Payload(payload) {
|
|
504
|
+
return "authorization" in payload;
|
|
505
|
+
}
|
|
142
506
|
// Annotate the CommonJS export names for ESM import in node:
|
|
143
507
|
0 && (module.exports = {
|
|
144
508
|
ExactEvmScheme,
|
|
509
|
+
PERMIT2_ADDRESS,
|
|
510
|
+
authorizationTypes,
|
|
511
|
+
createPermit2ApprovalTx,
|
|
512
|
+
eip3009ABI,
|
|
513
|
+
erc20AllowanceAbi,
|
|
514
|
+
getPermit2AllowanceReadParams,
|
|
515
|
+
isEIP3009Payload,
|
|
516
|
+
isPermit2Payload,
|
|
517
|
+
permit2WitnessTypes,
|
|
145
518
|
toClientEvmSigner,
|
|
146
|
-
toFacilitatorEvmSigner
|
|
519
|
+
toFacilitatorEvmSigner,
|
|
520
|
+
x402ExactPermit2ProxyABI,
|
|
521
|
+
x402ExactPermit2ProxyAddress,
|
|
522
|
+
x402UptoPermit2ProxyAddress
|
|
147
523
|
});
|
|
148
524
|
//# sourceMappingURL=index.js.map
|