x402-evm-mantle 2.1.1-mantle
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/README.md +183 -0
- package/dist/cjs/exact/client/index.d.ts +53 -0
- package/dist/cjs/exact/client/index.js +270 -0
- package/dist/cjs/exact/client/index.js.map +1 -0
- package/dist/cjs/exact/facilitator/index.d.ts +118 -0
- package/dist/cjs/exact/facilitator/index.js +735 -0
- package/dist/cjs/exact/facilitator/index.js.map +1 -0
- package/dist/cjs/exact/server/index.d.ts +140 -0
- package/dist/cjs/exact/server/index.js +247 -0
- package/dist/cjs/exact/server/index.js.map +1 -0
- package/dist/cjs/exact/v1/client/index.d.ts +37 -0
- package/dist/cjs/exact/v1/client/index.js +147 -0
- package/dist/cjs/exact/v1/client/index.js.map +1 -0
- package/dist/cjs/exact/v1/facilitator/index.d.ts +62 -0
- package/dist/cjs/exact/v1/facilitator/index.js +401 -0
- package/dist/cjs/exact/v1/facilitator/index.js.map +1 -0
- package/dist/cjs/index.d.ts +36 -0
- package/dist/cjs/index.js +148 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/signer-5OVDxViv.d.ts +79 -0
- package/dist/cjs/v1/index.d.ts +7 -0
- package/dist/cjs/v1/index.js +171 -0
- package/dist/cjs/v1/index.js.map +1 -0
- package/dist/esm/chunk-CYGMVQCG.mjs +88 -0
- package/dist/esm/chunk-CYGMVQCG.mjs.map +1 -0
- package/dist/esm/chunk-DI3EK7PR.mjs +305 -0
- package/dist/esm/chunk-DI3EK7PR.mjs.map +1 -0
- package/dist/esm/chunk-QLXM7BIB.mjs +23 -0
- package/dist/esm/chunk-QLXM7BIB.mjs.map +1 -0
- package/dist/esm/chunk-S5OEANGR.mjs +92 -0
- package/dist/esm/chunk-S5OEANGR.mjs.map +1 -0
- package/dist/esm/chunk-T3FPOH7F.mjs +88 -0
- package/dist/esm/chunk-T3FPOH7F.mjs.map +1 -0
- package/dist/esm/exact/client/index.d.mts +53 -0
- package/dist/esm/exact/client/index.mjs +36 -0
- package/dist/esm/exact/client/index.mjs.map +1 -0
- package/dist/esm/exact/facilitator/index.d.mts +118 -0
- package/dist/esm/exact/facilitator/index.mjs +324 -0
- package/dist/esm/exact/facilitator/index.mjs.map +1 -0
- package/dist/esm/exact/server/index.d.mts +140 -0
- package/dist/esm/exact/server/index.mjs +219 -0
- package/dist/esm/exact/server/index.mjs.map +1 -0
- package/dist/esm/exact/v1/client/index.d.mts +37 -0
- package/dist/esm/exact/v1/client/index.mjs +8 -0
- package/dist/esm/exact/v1/client/index.mjs.map +1 -0
- package/dist/esm/exact/v1/facilitator/index.d.mts +62 -0
- package/dist/esm/exact/v1/facilitator/index.mjs +8 -0
- package/dist/esm/exact/v1/facilitator/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +36 -0
- package/dist/esm/index.mjs +21 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/signer-5OVDxViv.d.mts +79 -0
- package/dist/esm/v1/index.d.mts +7 -0
- package/dist/esm/v1/index.mjs +13 -0
- package/dist/esm/v1/index.mjs.map +1 -0
- package/package.json +127 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/exact/v1/client/index.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
ExactEvmSchemeV1: () => ExactEvmSchemeV1
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(client_exports);
|
|
26
|
+
|
|
27
|
+
// src/exact/v1/client/scheme.ts
|
|
28
|
+
var import_viem2 = require("viem");
|
|
29
|
+
|
|
30
|
+
// src/constants.ts
|
|
31
|
+
var authorizationTypes = {
|
|
32
|
+
TransferWithAuthorization: [
|
|
33
|
+
{ name: "from", type: "address" },
|
|
34
|
+
{ name: "to", type: "address" },
|
|
35
|
+
{ name: "value", type: "uint256" },
|
|
36
|
+
{ name: "validAfter", type: "uint256" },
|
|
37
|
+
{ name: "validBefore", type: "uint256" },
|
|
38
|
+
{ name: "nonce", type: "bytes32" }
|
|
39
|
+
]
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/utils.ts
|
|
43
|
+
var import_viem = require("viem");
|
|
44
|
+
function getEvmChainId(network) {
|
|
45
|
+
const networkMap = {
|
|
46
|
+
base: 8453,
|
|
47
|
+
"base-sepolia": 84532,
|
|
48
|
+
ethereum: 1,
|
|
49
|
+
sepolia: 11155111,
|
|
50
|
+
polygon: 137,
|
|
51
|
+
"polygon-amoy": 80002
|
|
52
|
+
};
|
|
53
|
+
return networkMap[network] || 1;
|
|
54
|
+
}
|
|
55
|
+
function createNonce() {
|
|
56
|
+
const cryptoObj = typeof globalThis.crypto !== "undefined" ? globalThis.crypto : globalThis.crypto;
|
|
57
|
+
if (!cryptoObj) {
|
|
58
|
+
throw new Error("Crypto API not available");
|
|
59
|
+
}
|
|
60
|
+
return (0, import_viem.toHex)(cryptoObj.getRandomValues(new Uint8Array(32)));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/exact/v1/client/scheme.ts
|
|
64
|
+
var ExactEvmSchemeV1 = class {
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new ExactEvmClientV1 instance.
|
|
67
|
+
*
|
|
68
|
+
* @param signer - The EVM signer for client operations
|
|
69
|
+
*/
|
|
70
|
+
constructor(signer) {
|
|
71
|
+
this.signer = signer;
|
|
72
|
+
this.scheme = "exact";
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a payment payload for the Exact scheme (V1).
|
|
76
|
+
*
|
|
77
|
+
* @param x402Version - The x402 protocol version
|
|
78
|
+
* @param paymentRequirements - The payment requirements
|
|
79
|
+
* @returns Promise resolving to a payment payload
|
|
80
|
+
*/
|
|
81
|
+
async createPaymentPayload(x402Version, paymentRequirements) {
|
|
82
|
+
const selectedV1 = paymentRequirements;
|
|
83
|
+
const nonce = createNonce();
|
|
84
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
85
|
+
const authorization = {
|
|
86
|
+
from: this.signer.address,
|
|
87
|
+
to: (0, import_viem2.getAddress)(selectedV1.payTo),
|
|
88
|
+
value: selectedV1.maxAmountRequired,
|
|
89
|
+
validAfter: (now - 600).toString(),
|
|
90
|
+
// 10 minutes before
|
|
91
|
+
validBefore: (now + selectedV1.maxTimeoutSeconds).toString(),
|
|
92
|
+
nonce
|
|
93
|
+
};
|
|
94
|
+
const signature = await this.signAuthorization(authorization, selectedV1);
|
|
95
|
+
const payload = {
|
|
96
|
+
authorization,
|
|
97
|
+
signature
|
|
98
|
+
};
|
|
99
|
+
return {
|
|
100
|
+
x402Version,
|
|
101
|
+
scheme: selectedV1.scheme,
|
|
102
|
+
network: selectedV1.network,
|
|
103
|
+
payload
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Sign the EIP-3009 authorization using EIP-712
|
|
108
|
+
*
|
|
109
|
+
* @param authorization - The authorization to sign
|
|
110
|
+
* @param requirements - The payment requirements
|
|
111
|
+
* @returns Promise resolving to the signature
|
|
112
|
+
*/
|
|
113
|
+
async signAuthorization(authorization, requirements) {
|
|
114
|
+
const chainId = getEvmChainId(requirements.network);
|
|
115
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
`EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const { name, version } = requirements.extra;
|
|
121
|
+
const domain = {
|
|
122
|
+
name,
|
|
123
|
+
version,
|
|
124
|
+
chainId,
|
|
125
|
+
verifyingContract: (0, import_viem2.getAddress)(requirements.asset)
|
|
126
|
+
};
|
|
127
|
+
const message = {
|
|
128
|
+
from: (0, import_viem2.getAddress)(authorization.from),
|
|
129
|
+
to: (0, import_viem2.getAddress)(authorization.to),
|
|
130
|
+
value: BigInt(authorization.value),
|
|
131
|
+
validAfter: BigInt(authorization.validAfter),
|
|
132
|
+
validBefore: BigInt(authorization.validBefore),
|
|
133
|
+
nonce: authorization.nonce
|
|
134
|
+
};
|
|
135
|
+
return await this.signer.signTypedData({
|
|
136
|
+
domain,
|
|
137
|
+
types: authorizationTypes,
|
|
138
|
+
primaryType: "TransferWithAuthorization",
|
|
139
|
+
message
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
144
|
+
0 && (module.exports = {
|
|
145
|
+
ExactEvmSchemeV1
|
|
146
|
+
});
|
|
147
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/exact/v1/client/index.ts","../../../../../src/exact/v1/client/scheme.ts","../../../../../src/constants.ts","../../../../../src/utils.ts"],"sourcesContent":["export { ExactEvmSchemeV1 } from \"./scheme\";\n","import {\n Network,\n PaymentPayload,\n PaymentRequirements,\n SchemeNetworkClient,\n} from \"x402-core-mantle/types\";\nimport { PaymentRequirementsV1 } from \"x402-core-mantle/types/v1\";\nimport { getAddress } from \"viem\";\nimport { authorizationTypes } from \"../../../constants\";\nimport { ClientEvmSigner } from \"../../../signer\";\nimport { ExactEvmPayloadV1 } from \"../../../types\";\nimport { createNonce, getEvmChainId } from \"../../../utils\";\n\n/**\n * EVM client implementation for the Exact payment scheme (V1).\n */\nexport class ExactEvmSchemeV1 implements SchemeNetworkClient {\n readonly scheme = \"exact\";\n\n /**\n * Creates a new ExactEvmClientV1 instance.\n *\n * @param signer - The EVM signer for client operations\n */\n constructor(private readonly signer: ClientEvmSigner) {}\n\n /**\n * Creates a payment payload for the Exact scheme (V1).\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - The payment requirements\n * @returns Promise resolving to a payment payload\n */\n async createPaymentPayload(\n x402Version: number,\n paymentRequirements: PaymentRequirements,\n ): Promise<\n Pick<PaymentPayload, \"x402Version\" | \"payload\"> & { scheme: string; network: Network }\n > {\n const selectedV1 = paymentRequirements as unknown as PaymentRequirementsV1;\n const nonce = createNonce();\n const now = Math.floor(Date.now() / 1000);\n\n const authorization: ExactEvmPayloadV1[\"authorization\"] = {\n from: this.signer.address,\n to: getAddress(selectedV1.payTo),\n value: selectedV1.maxAmountRequired,\n validAfter: (now - 600).toString(), // 10 minutes before\n validBefore: (now + selectedV1.maxTimeoutSeconds).toString(),\n nonce,\n };\n\n // Sign the authorization\n const signature = await this.signAuthorization(authorization, selectedV1);\n\n const payload: ExactEvmPayloadV1 = {\n authorization,\n signature,\n };\n\n return {\n x402Version,\n scheme: selectedV1.scheme,\n network: selectedV1.network,\n payload,\n };\n }\n\n /**\n * Sign the EIP-3009 authorization using EIP-712\n *\n * @param authorization - The authorization to sign\n * @param requirements - The payment requirements\n * @returns Promise resolving to the signature\n */\n private async signAuthorization(\n authorization: ExactEvmPayloadV1[\"authorization\"],\n requirements: PaymentRequirementsV1,\n ): Promise<`0x${string}`> {\n const chainId = getEvmChainId(requirements.network);\n\n if (!requirements.extra?.name || !requirements.extra?.version) {\n throw new Error(\n `EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`,\n );\n }\n\n const { name, version } = requirements.extra;\n\n const domain = {\n name,\n version,\n chainId,\n verifyingContract: getAddress(requirements.asset),\n };\n\n const message = {\n from: getAddress(authorization.from),\n to: getAddress(authorization.to),\n value: BigInt(authorization.value),\n validAfter: BigInt(authorization.validAfter),\n validBefore: BigInt(authorization.validBefore),\n nonce: authorization.nonce,\n };\n\n return await this.signer.signTypedData({\n domain,\n types: authorizationTypes,\n primaryType: \"TransferWithAuthorization\",\n message,\n });\n }\n}\n","// EIP-3009 TransferWithAuthorization types for EIP-712 signing\nexport const authorizationTypes = {\n TransferWithAuthorization: [\n { name: \"from\", type: \"address\" },\n { name: \"to\", type: \"address\" },\n { name: \"value\", type: \"uint256\" },\n { name: \"validAfter\", type: \"uint256\" },\n { name: \"validBefore\", type: \"uint256\" },\n { name: \"nonce\", type: \"bytes32\" },\n ],\n} as const;\n\n// EIP3009 ABI for transferWithAuthorization function\nexport const eip3009ABI = [\n {\n inputs: [\n { name: \"from\", type: \"address\" },\n { name: \"to\", type: \"address\" },\n { name: \"value\", type: \"uint256\" },\n { name: \"validAfter\", type: \"uint256\" },\n { name: \"validBefore\", type: \"uint256\" },\n { name: \"nonce\", type: \"bytes32\" },\n { name: \"v\", type: \"uint8\" },\n { name: \"r\", type: \"bytes32\" },\n { name: \"s\", type: \"bytes32\" },\n ],\n name: \"transferWithAuthorization\",\n outputs: [],\n stateMutability: \"nonpayable\",\n type: \"function\",\n },\n {\n inputs: [\n { name: \"from\", type: \"address\" },\n { name: \"to\", type: \"address\" },\n { name: \"value\", type: \"uint256\" },\n { name: \"validAfter\", type: \"uint256\" },\n { name: \"validBefore\", type: \"uint256\" },\n { name: \"nonce\", type: \"bytes32\" },\n { name: \"signature\", type: \"bytes\" },\n ],\n name: \"transferWithAuthorization\",\n outputs: [],\n stateMutability: \"nonpayable\",\n type: \"function\",\n },\n {\n inputs: [{ name: \"account\", type: \"address\" }],\n name: \"balanceOf\",\n outputs: [{ name: \"\", type: \"uint256\" }],\n stateMutability: \"view\",\n type: \"function\",\n },\n {\n inputs: [],\n name: \"version\",\n outputs: [{ name: \"\", type: \"string\" }],\n stateMutability: \"view\",\n type: \"function\",\n },\n] as const;\n","import { toHex } from \"viem\";\nimport { Network } from \"x402-core-mantle/types\";\n\n/**\n * Extract chain ID from network string (e.g., \"base-sepolia\" -> 84532)\n * Used by v1 implementations\n *\n * @param network - The network identifier\n * @returns The numeric chain ID\n */\nexport function getEvmChainId(network: Network): number {\n const networkMap: Record<string, number> = {\n base: 8453,\n \"base-sepolia\": 84532,\n ethereum: 1,\n sepolia: 11155111,\n polygon: 137,\n \"polygon-amoy\": 80002,\n };\n return networkMap[network] || 1;\n}\n\n/**\n * Create a random 32-byte nonce for authorization\n *\n * @returns A hex-encoded 32-byte nonce\n */\nexport function createNonce(): `0x${string}` {\n // Use dynamic import to avoid require() in ESM context\n const cryptoObj =\n typeof globalThis.crypto !== \"undefined\"\n ? globalThis.crypto\n : (globalThis as { crypto?: Crypto }).crypto;\n\n if (!cryptoObj) {\n throw new Error(\"Crypto API not available\");\n }\n\n return toHex(cryptoObj.getRandomValues(new Uint8Array(32)));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOA,IAAAA,eAA2B;;;ACNpB,IAAM,qBAAqB;AAAA,EAChC,2BAA2B;AAAA,IACzB,EAAE,MAAM,QAAQ,MAAM,UAAU;AAAA,IAChC,EAAE,MAAM,MAAM,MAAM,UAAU;AAAA,IAC9B,EAAE,MAAM,SAAS,MAAM,UAAU;AAAA,IACjC,EAAE,MAAM,cAAc,MAAM,UAAU;AAAA,IACtC,EAAE,MAAM,eAAe,MAAM,UAAU;AAAA,IACvC,EAAE,MAAM,SAAS,MAAM,UAAU;AAAA,EACnC;AACF;;;ACVA,kBAAsB;AAUf,SAAS,cAAc,SAA0B;AACtD,QAAM,aAAqC;AAAA,IACzC,MAAM;AAAA,IACN,gBAAgB;AAAA,IAChB,UAAU;AAAA,IACV,SAAS;AAAA,IACT,SAAS;AAAA,IACT,gBAAgB;AAAA,EAClB;AACA,SAAO,WAAW,OAAO,KAAK;AAChC;AAOO,SAAS,cAA6B;AAE3C,QAAM,YACJ,OAAO,WAAW,WAAW,cACzB,WAAW,SACV,WAAmC;AAE1C,MAAI,CAAC,WAAW;AACd,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,aAAO,mBAAM,UAAU,gBAAgB,IAAI,WAAW,EAAE,CAAC,CAAC;AAC5D;;;AFvBO,IAAM,mBAAN,MAAsD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ3D,YAA6B,QAAyB;AAAzB;AAP7B,SAAS,SAAS;AAAA,EAOqC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASvD,MAAM,qBACJ,aACA,qBAGA;AACA,UAAM,aAAa;AACnB,UAAM,QAAQ,YAAY;AAC1B,UAAM,MAAM,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAExC,UAAM,gBAAoD;AAAA,MACxD,MAAM,KAAK,OAAO;AAAA,MAClB,QAAI,yBAAW,WAAW,KAAK;AAAA,MAC/B,OAAO,WAAW;AAAA,MAClB,aAAa,MAAM,KAAK,SAAS;AAAA;AAAA,MACjC,cAAc,MAAM,WAAW,mBAAmB,SAAS;AAAA,MAC3D;AAAA,IACF;AAGA,UAAM,YAAY,MAAM,KAAK,kBAAkB,eAAe,UAAU;AAExE,UAAM,UAA6B;AAAA,MACjC;AAAA,MACA;AAAA,IACF;AAEA,WAAO;AAAA,MACL;AAAA,MACA,QAAQ,WAAW;AAAA,MACnB,SAAS,WAAW;AAAA,MACpB;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAc,kBACZ,eACA,cACwB;AACxB,UAAM,UAAU,cAAc,aAAa,OAAO;AAElD,QAAI,CAAC,aAAa,OAAO,QAAQ,CAAC,aAAa,OAAO,SAAS;AAC7D,YAAM,IAAI;AAAA,QACR,4FAA4F,aAAa,KAAK;AAAA,MAChH;AAAA,IACF;AAEA,UAAM,EAAE,MAAM,QAAQ,IAAI,aAAa;AAEvC,UAAM,SAAS;AAAA,MACb;AAAA,MACA;AAAA,MACA;AAAA,MACA,uBAAmB,yBAAW,aAAa,KAAK;AAAA,IAClD;AAEA,UAAM,UAAU;AAAA,MACd,UAAM,yBAAW,cAAc,IAAI;AAAA,MACnC,QAAI,yBAAW,cAAc,EAAE;AAAA,MAC/B,OAAO,OAAO,cAAc,KAAK;AAAA,MACjC,YAAY,OAAO,cAAc,UAAU;AAAA,MAC3C,aAAa,OAAO,cAAc,WAAW;AAAA,MAC7C,OAAO,cAAc;AAAA,IACvB;AAEA,WAAO,MAAM,KAAK,OAAO,cAAc;AAAA,MACrC;AAAA,MACA,OAAO;AAAA,MACP,aAAa;AAAA,MACb;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":["import_viem"]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { SchemeNetworkFacilitator, PaymentPayload, PaymentRequirements, VerifyResponse, SettleResponse } from 'x402-core-mantle/types';
|
|
2
|
+
import { F as FacilitatorEvmSigner } from '../../../signer-5OVDxViv.js';
|
|
3
|
+
|
|
4
|
+
interface ExactEvmSchemeV1Config {
|
|
5
|
+
/**
|
|
6
|
+
* If enabled, the facilitator will deploy ERC-4337 smart wallets
|
|
7
|
+
* via EIP-6492 when encountering undeployed contract signatures.
|
|
8
|
+
*
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
deployERC4337WithEIP6492?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* EVM facilitator implementation for the Exact payment scheme (V1).
|
|
15
|
+
*/
|
|
16
|
+
declare class ExactEvmSchemeV1 implements SchemeNetworkFacilitator {
|
|
17
|
+
private readonly signer;
|
|
18
|
+
readonly scheme = "exact";
|
|
19
|
+
readonly caipFamily = "eip155:*";
|
|
20
|
+
private readonly config;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new ExactEvmFacilitatorV1 instance.
|
|
23
|
+
*
|
|
24
|
+
* @param signer - The EVM signer for facilitator operations
|
|
25
|
+
* @param config - Optional configuration for the facilitator
|
|
26
|
+
*/
|
|
27
|
+
constructor(signer: FacilitatorEvmSigner, config?: ExactEvmSchemeV1Config);
|
|
28
|
+
/**
|
|
29
|
+
* Get mechanism-specific extra data for the supported kinds endpoint.
|
|
30
|
+
* For EVM, no extra data is needed.
|
|
31
|
+
*
|
|
32
|
+
* @param _ - The network identifier (unused for EVM)
|
|
33
|
+
* @returns undefined (EVM has no extra data)
|
|
34
|
+
*/
|
|
35
|
+
getExtra(_: string): Record<string, unknown> | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Get signer addresses used by this facilitator.
|
|
38
|
+
* Returns all addresses this facilitator can use for signing/settling transactions.
|
|
39
|
+
*
|
|
40
|
+
* @param _ - The network identifier (unused for EVM, addresses are network-agnostic)
|
|
41
|
+
* @returns Array of facilitator wallet addresses
|
|
42
|
+
*/
|
|
43
|
+
getSigners(_: string): string[];
|
|
44
|
+
/**
|
|
45
|
+
* Verifies a payment payload (V1).
|
|
46
|
+
*
|
|
47
|
+
* @param payload - The payment payload to verify
|
|
48
|
+
* @param requirements - The payment requirements
|
|
49
|
+
* @returns Promise resolving to verification response
|
|
50
|
+
*/
|
|
51
|
+
verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
52
|
+
/**
|
|
53
|
+
* Settles a payment by executing the transfer (V1).
|
|
54
|
+
*
|
|
55
|
+
* @param payload - The payment payload to settle
|
|
56
|
+
* @param requirements - The payment requirements
|
|
57
|
+
* @returns Promise resolving to settlement response
|
|
58
|
+
*/
|
|
59
|
+
settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { ExactEvmSchemeV1, type ExactEvmSchemeV1Config };
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/exact/v1/facilitator/index.ts
|
|
21
|
+
var facilitator_exports = {};
|
|
22
|
+
__export(facilitator_exports, {
|
|
23
|
+
ExactEvmSchemeV1: () => ExactEvmSchemeV1
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(facilitator_exports);
|
|
26
|
+
|
|
27
|
+
// src/exact/v1/facilitator/scheme.ts
|
|
28
|
+
var import_viem2 = require("viem");
|
|
29
|
+
|
|
30
|
+
// src/constants.ts
|
|
31
|
+
var authorizationTypes = {
|
|
32
|
+
TransferWithAuthorization: [
|
|
33
|
+
{ name: "from", type: "address" },
|
|
34
|
+
{ name: "to", type: "address" },
|
|
35
|
+
{ name: "value", type: "uint256" },
|
|
36
|
+
{ name: "validAfter", type: "uint256" },
|
|
37
|
+
{ name: "validBefore", type: "uint256" },
|
|
38
|
+
{ name: "nonce", type: "bytes32" }
|
|
39
|
+
]
|
|
40
|
+
};
|
|
41
|
+
var eip3009ABI = [
|
|
42
|
+
{
|
|
43
|
+
inputs: [
|
|
44
|
+
{ name: "from", type: "address" },
|
|
45
|
+
{ name: "to", type: "address" },
|
|
46
|
+
{ name: "value", type: "uint256" },
|
|
47
|
+
{ name: "validAfter", type: "uint256" },
|
|
48
|
+
{ name: "validBefore", type: "uint256" },
|
|
49
|
+
{ name: "nonce", type: "bytes32" },
|
|
50
|
+
{ name: "v", type: "uint8" },
|
|
51
|
+
{ name: "r", type: "bytes32" },
|
|
52
|
+
{ name: "s", type: "bytes32" }
|
|
53
|
+
],
|
|
54
|
+
name: "transferWithAuthorization",
|
|
55
|
+
outputs: [],
|
|
56
|
+
stateMutability: "nonpayable",
|
|
57
|
+
type: "function"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
inputs: [
|
|
61
|
+
{ name: "from", type: "address" },
|
|
62
|
+
{ name: "to", type: "address" },
|
|
63
|
+
{ name: "value", type: "uint256" },
|
|
64
|
+
{ name: "validAfter", type: "uint256" },
|
|
65
|
+
{ name: "validBefore", type: "uint256" },
|
|
66
|
+
{ name: "nonce", type: "bytes32" },
|
|
67
|
+
{ name: "signature", type: "bytes" }
|
|
68
|
+
],
|
|
69
|
+
name: "transferWithAuthorization",
|
|
70
|
+
outputs: [],
|
|
71
|
+
stateMutability: "nonpayable",
|
|
72
|
+
type: "function"
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
inputs: [{ name: "account", type: "address" }],
|
|
76
|
+
name: "balanceOf",
|
|
77
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
78
|
+
stateMutability: "view",
|
|
79
|
+
type: "function"
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
inputs: [],
|
|
83
|
+
name: "version",
|
|
84
|
+
outputs: [{ name: "", type: "string" }],
|
|
85
|
+
stateMutability: "view",
|
|
86
|
+
type: "function"
|
|
87
|
+
}
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
// src/utils.ts
|
|
91
|
+
var import_viem = require("viem");
|
|
92
|
+
function getEvmChainId(network) {
|
|
93
|
+
const networkMap = {
|
|
94
|
+
base: 8453,
|
|
95
|
+
"base-sepolia": 84532,
|
|
96
|
+
ethereum: 1,
|
|
97
|
+
sepolia: 11155111,
|
|
98
|
+
polygon: 137,
|
|
99
|
+
"polygon-amoy": 80002
|
|
100
|
+
};
|
|
101
|
+
return networkMap[network] || 1;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/exact/v1/facilitator/scheme.ts
|
|
105
|
+
var ExactEvmSchemeV1 = class {
|
|
106
|
+
/**
|
|
107
|
+
* Creates a new ExactEvmFacilitatorV1 instance.
|
|
108
|
+
*
|
|
109
|
+
* @param signer - The EVM signer for facilitator operations
|
|
110
|
+
* @param config - Optional configuration for the facilitator
|
|
111
|
+
*/
|
|
112
|
+
constructor(signer, config) {
|
|
113
|
+
this.signer = signer;
|
|
114
|
+
this.scheme = "exact";
|
|
115
|
+
this.caipFamily = "eip155:*";
|
|
116
|
+
this.config = {
|
|
117
|
+
deployERC4337WithEIP6492: config?.deployERC4337WithEIP6492 ?? false
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get mechanism-specific extra data for the supported kinds endpoint.
|
|
122
|
+
* For EVM, no extra data is needed.
|
|
123
|
+
*
|
|
124
|
+
* @param _ - The network identifier (unused for EVM)
|
|
125
|
+
* @returns undefined (EVM has no extra data)
|
|
126
|
+
*/
|
|
127
|
+
getExtra(_) {
|
|
128
|
+
return void 0;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get signer addresses used by this facilitator.
|
|
132
|
+
* Returns all addresses this facilitator can use for signing/settling transactions.
|
|
133
|
+
*
|
|
134
|
+
* @param _ - The network identifier (unused for EVM, addresses are network-agnostic)
|
|
135
|
+
* @returns Array of facilitator wallet addresses
|
|
136
|
+
*/
|
|
137
|
+
getSigners(_) {
|
|
138
|
+
return [...this.signer.getAddresses()];
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Verifies a payment payload (V1).
|
|
142
|
+
*
|
|
143
|
+
* @param payload - The payment payload to verify
|
|
144
|
+
* @param requirements - The payment requirements
|
|
145
|
+
* @returns Promise resolving to verification response
|
|
146
|
+
*/
|
|
147
|
+
async verify(payload, requirements) {
|
|
148
|
+
const requirementsV1 = requirements;
|
|
149
|
+
const payloadV1 = payload;
|
|
150
|
+
const exactEvmPayload = payload.payload;
|
|
151
|
+
if (payloadV1.scheme !== "exact" || requirements.scheme !== "exact") {
|
|
152
|
+
return {
|
|
153
|
+
isValid: false,
|
|
154
|
+
invalidReason: "unsupported_scheme",
|
|
155
|
+
payer: exactEvmPayload.authorization.from
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
const chainId = getEvmChainId(payloadV1.network);
|
|
159
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
160
|
+
return {
|
|
161
|
+
isValid: false,
|
|
162
|
+
invalidReason: "missing_eip712_domain",
|
|
163
|
+
payer: exactEvmPayload.authorization.from
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const { name, version } = requirements.extra;
|
|
167
|
+
const erc20Address = (0, import_viem2.getAddress)(requirements.asset);
|
|
168
|
+
if (payloadV1.network !== requirements.network) {
|
|
169
|
+
return {
|
|
170
|
+
isValid: false,
|
|
171
|
+
invalidReason: "network_mismatch",
|
|
172
|
+
payer: exactEvmPayload.authorization.from
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
const permitTypedData = {
|
|
176
|
+
types: authorizationTypes,
|
|
177
|
+
primaryType: "TransferWithAuthorization",
|
|
178
|
+
domain: {
|
|
179
|
+
name,
|
|
180
|
+
version,
|
|
181
|
+
chainId,
|
|
182
|
+
verifyingContract: erc20Address
|
|
183
|
+
},
|
|
184
|
+
message: {
|
|
185
|
+
from: exactEvmPayload.authorization.from,
|
|
186
|
+
to: exactEvmPayload.authorization.to,
|
|
187
|
+
value: BigInt(exactEvmPayload.authorization.value),
|
|
188
|
+
validAfter: BigInt(exactEvmPayload.authorization.validAfter),
|
|
189
|
+
validBefore: BigInt(exactEvmPayload.authorization.validBefore),
|
|
190
|
+
nonce: exactEvmPayload.authorization.nonce
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
try {
|
|
194
|
+
const recoveredAddress = await this.signer.verifyTypedData({
|
|
195
|
+
address: exactEvmPayload.authorization.from,
|
|
196
|
+
...permitTypedData,
|
|
197
|
+
signature: exactEvmPayload.signature
|
|
198
|
+
});
|
|
199
|
+
if (!recoveredAddress) {
|
|
200
|
+
return {
|
|
201
|
+
isValid: false,
|
|
202
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
203
|
+
payer: exactEvmPayload.authorization.from
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
} catch {
|
|
207
|
+
const signature = exactEvmPayload.signature;
|
|
208
|
+
const signatureLength = signature.startsWith("0x") ? signature.length - 2 : signature.length;
|
|
209
|
+
const isSmartWallet = signatureLength > 130;
|
|
210
|
+
if (isSmartWallet) {
|
|
211
|
+
const payerAddress = exactEvmPayload.authorization.from;
|
|
212
|
+
const bytecode = await this.signer.getCode({ address: payerAddress });
|
|
213
|
+
if (!bytecode || bytecode === "0x") {
|
|
214
|
+
const erc6492Data = (0, import_viem2.parseErc6492Signature)(signature);
|
|
215
|
+
const hasDeploymentInfo = erc6492Data.address && erc6492Data.data && !(0, import_viem2.isAddressEqual)(erc6492Data.address, "0x0000000000000000000000000000000000000000");
|
|
216
|
+
if (!hasDeploymentInfo) {
|
|
217
|
+
return {
|
|
218
|
+
isValid: false,
|
|
219
|
+
invalidReason: "invalid_exact_evm_payload_undeployed_smart_wallet",
|
|
220
|
+
payer: payerAddress
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
} else {
|
|
224
|
+
return {
|
|
225
|
+
isValid: false,
|
|
226
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
227
|
+
payer: exactEvmPayload.authorization.from
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
} else {
|
|
231
|
+
return {
|
|
232
|
+
isValid: false,
|
|
233
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
234
|
+
payer: exactEvmPayload.authorization.from
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if ((0, import_viem2.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem2.getAddress)(requirements.payTo)) {
|
|
239
|
+
return {
|
|
240
|
+
isValid: false,
|
|
241
|
+
invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
|
|
242
|
+
payer: exactEvmPayload.authorization.from
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
246
|
+
if (BigInt(exactEvmPayload.authorization.validBefore) < BigInt(now + 6)) {
|
|
247
|
+
return {
|
|
248
|
+
isValid: false,
|
|
249
|
+
invalidReason: "invalid_exact_evm_payload_authorization_valid_before",
|
|
250
|
+
payer: exactEvmPayload.authorization.from
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
if (BigInt(exactEvmPayload.authorization.validAfter) > BigInt(now)) {
|
|
254
|
+
return {
|
|
255
|
+
isValid: false,
|
|
256
|
+
invalidReason: "invalid_exact_evm_payload_authorization_valid_after",
|
|
257
|
+
payer: exactEvmPayload.authorization.from
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
const balance = await this.signer.readContract({
|
|
262
|
+
address: erc20Address,
|
|
263
|
+
abi: eip3009ABI,
|
|
264
|
+
functionName: "balanceOf",
|
|
265
|
+
args: [exactEvmPayload.authorization.from]
|
|
266
|
+
});
|
|
267
|
+
if (BigInt(balance) < BigInt(requirementsV1.maxAmountRequired)) {
|
|
268
|
+
return {
|
|
269
|
+
isValid: false,
|
|
270
|
+
invalidReason: "insufficient_funds",
|
|
271
|
+
payer: exactEvmPayload.authorization.from
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
} catch {
|
|
275
|
+
}
|
|
276
|
+
if (BigInt(exactEvmPayload.authorization.value) < BigInt(requirementsV1.maxAmountRequired)) {
|
|
277
|
+
return {
|
|
278
|
+
isValid: false,
|
|
279
|
+
invalidReason: "invalid_exact_evm_payload_authorization_value",
|
|
280
|
+
payer: exactEvmPayload.authorization.from
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
isValid: true,
|
|
285
|
+
invalidReason: void 0,
|
|
286
|
+
payer: exactEvmPayload.authorization.from
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Settles a payment by executing the transfer (V1).
|
|
291
|
+
*
|
|
292
|
+
* @param payload - The payment payload to settle
|
|
293
|
+
* @param requirements - The payment requirements
|
|
294
|
+
* @returns Promise resolving to settlement response
|
|
295
|
+
*/
|
|
296
|
+
async settle(payload, requirements) {
|
|
297
|
+
const payloadV1 = payload;
|
|
298
|
+
const exactEvmPayload = payload.payload;
|
|
299
|
+
const valid = await this.verify(payload, requirements);
|
|
300
|
+
if (!valid.isValid) {
|
|
301
|
+
return {
|
|
302
|
+
success: false,
|
|
303
|
+
network: payloadV1.network,
|
|
304
|
+
transaction: "",
|
|
305
|
+
errorReason: valid.invalidReason ?? "invalid_scheme",
|
|
306
|
+
payer: exactEvmPayload.authorization.from
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
const parseResult = (0, import_viem2.parseErc6492Signature)(exactEvmPayload.signature);
|
|
311
|
+
const { signature, address: factoryAddress, data: factoryCalldata } = parseResult;
|
|
312
|
+
if (this.config.deployERC4337WithEIP6492 && factoryAddress && factoryCalldata && !(0, import_viem2.isAddressEqual)(factoryAddress, "0x0000000000000000000000000000000000000000")) {
|
|
313
|
+
const payerAddress = exactEvmPayload.authorization.from;
|
|
314
|
+
const bytecode = await this.signer.getCode({ address: payerAddress });
|
|
315
|
+
if (!bytecode || bytecode === "0x") {
|
|
316
|
+
try {
|
|
317
|
+
console.log(`Deploying ERC-4337 smart wallet for ${payerAddress} via EIP-6492`);
|
|
318
|
+
const deployTx = await this.signer.sendTransaction({
|
|
319
|
+
to: factoryAddress,
|
|
320
|
+
data: factoryCalldata
|
|
321
|
+
});
|
|
322
|
+
await this.signer.waitForTransactionReceipt({ hash: deployTx });
|
|
323
|
+
console.log(`Successfully deployed smart wallet for ${payerAddress}`);
|
|
324
|
+
} catch (deployError) {
|
|
325
|
+
console.error("Smart wallet deployment failed:", deployError);
|
|
326
|
+
throw deployError;
|
|
327
|
+
}
|
|
328
|
+
} else {
|
|
329
|
+
console.log(`Smart wallet for ${payerAddress} already deployed, skipping deployment`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const signatureLength = signature.startsWith("0x") ? signature.length - 2 : signature.length;
|
|
333
|
+
const isECDSA = signatureLength === 130;
|
|
334
|
+
let tx;
|
|
335
|
+
if (isECDSA) {
|
|
336
|
+
const parsedSig = (0, import_viem2.parseSignature)(signature);
|
|
337
|
+
tx = await this.signer.writeContract({
|
|
338
|
+
address: (0, import_viem2.getAddress)(requirements.asset),
|
|
339
|
+
abi: eip3009ABI,
|
|
340
|
+
functionName: "transferWithAuthorization",
|
|
341
|
+
args: [
|
|
342
|
+
(0, import_viem2.getAddress)(exactEvmPayload.authorization.from),
|
|
343
|
+
(0, import_viem2.getAddress)(exactEvmPayload.authorization.to),
|
|
344
|
+
BigInt(exactEvmPayload.authorization.value),
|
|
345
|
+
BigInt(exactEvmPayload.authorization.validAfter),
|
|
346
|
+
BigInt(exactEvmPayload.authorization.validBefore),
|
|
347
|
+
exactEvmPayload.authorization.nonce,
|
|
348
|
+
parsedSig.v || parsedSig.yParity,
|
|
349
|
+
parsedSig.r,
|
|
350
|
+
parsedSig.s
|
|
351
|
+
]
|
|
352
|
+
});
|
|
353
|
+
} else {
|
|
354
|
+
tx = await this.signer.writeContract({
|
|
355
|
+
address: (0, import_viem2.getAddress)(requirements.asset),
|
|
356
|
+
abi: eip3009ABI,
|
|
357
|
+
functionName: "transferWithAuthorization",
|
|
358
|
+
args: [
|
|
359
|
+
(0, import_viem2.getAddress)(exactEvmPayload.authorization.from),
|
|
360
|
+
(0, import_viem2.getAddress)(exactEvmPayload.authorization.to),
|
|
361
|
+
BigInt(exactEvmPayload.authorization.value),
|
|
362
|
+
BigInt(exactEvmPayload.authorization.validAfter),
|
|
363
|
+
BigInt(exactEvmPayload.authorization.validBefore),
|
|
364
|
+
exactEvmPayload.authorization.nonce,
|
|
365
|
+
signature
|
|
366
|
+
]
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
const receipt = await this.signer.waitForTransactionReceipt({ hash: tx });
|
|
370
|
+
if (receipt.status !== "success") {
|
|
371
|
+
return {
|
|
372
|
+
success: false,
|
|
373
|
+
errorReason: "invalid_transaction_state",
|
|
374
|
+
transaction: tx,
|
|
375
|
+
network: payloadV1.network,
|
|
376
|
+
payer: exactEvmPayload.authorization.from
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
return {
|
|
380
|
+
success: true,
|
|
381
|
+
transaction: tx,
|
|
382
|
+
network: payloadV1.network,
|
|
383
|
+
payer: exactEvmPayload.authorization.from
|
|
384
|
+
};
|
|
385
|
+
} catch (error) {
|
|
386
|
+
console.error("Failed to settle transaction:", error);
|
|
387
|
+
return {
|
|
388
|
+
success: false,
|
|
389
|
+
errorReason: "transaction_failed",
|
|
390
|
+
transaction: "",
|
|
391
|
+
network: payloadV1.network,
|
|
392
|
+
payer: exactEvmPayload.authorization.from
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
398
|
+
0 && (module.exports = {
|
|
399
|
+
ExactEvmSchemeV1
|
|
400
|
+
});
|
|
401
|
+
//# sourceMappingURL=index.js.map
|