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,88 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var authorizationTypes = {
|
|
3
|
+
TransferWithAuthorization: [
|
|
4
|
+
{ name: "from", type: "address" },
|
|
5
|
+
{ name: "to", type: "address" },
|
|
6
|
+
{ name: "value", type: "uint256" },
|
|
7
|
+
{ name: "validAfter", type: "uint256" },
|
|
8
|
+
{ name: "validBefore", type: "uint256" },
|
|
9
|
+
{ name: "nonce", type: "bytes32" }
|
|
10
|
+
]
|
|
11
|
+
};
|
|
12
|
+
var eip3009ABI = [
|
|
13
|
+
{
|
|
14
|
+
inputs: [
|
|
15
|
+
{ name: "from", type: "address" },
|
|
16
|
+
{ name: "to", type: "address" },
|
|
17
|
+
{ name: "value", type: "uint256" },
|
|
18
|
+
{ name: "validAfter", type: "uint256" },
|
|
19
|
+
{ name: "validBefore", type: "uint256" },
|
|
20
|
+
{ name: "nonce", type: "bytes32" },
|
|
21
|
+
{ name: "v", type: "uint8" },
|
|
22
|
+
{ name: "r", type: "bytes32" },
|
|
23
|
+
{ name: "s", type: "bytes32" }
|
|
24
|
+
],
|
|
25
|
+
name: "transferWithAuthorization",
|
|
26
|
+
outputs: [],
|
|
27
|
+
stateMutability: "nonpayable",
|
|
28
|
+
type: "function"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
inputs: [
|
|
32
|
+
{ name: "from", type: "address" },
|
|
33
|
+
{ name: "to", type: "address" },
|
|
34
|
+
{ name: "value", type: "uint256" },
|
|
35
|
+
{ name: "validAfter", type: "uint256" },
|
|
36
|
+
{ name: "validBefore", type: "uint256" },
|
|
37
|
+
{ name: "nonce", type: "bytes32" },
|
|
38
|
+
{ name: "signature", type: "bytes" }
|
|
39
|
+
],
|
|
40
|
+
name: "transferWithAuthorization",
|
|
41
|
+
outputs: [],
|
|
42
|
+
stateMutability: "nonpayable",
|
|
43
|
+
type: "function"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
inputs: [{ name: "account", type: "address" }],
|
|
47
|
+
name: "balanceOf",
|
|
48
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
49
|
+
stateMutability: "view",
|
|
50
|
+
type: "function"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
inputs: [],
|
|
54
|
+
name: "version",
|
|
55
|
+
outputs: [{ name: "", type: "string" }],
|
|
56
|
+
stateMutability: "view",
|
|
57
|
+
type: "function"
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
// src/utils.ts
|
|
62
|
+
import { toHex } from "viem";
|
|
63
|
+
function getEvmChainId(network) {
|
|
64
|
+
const networkMap = {
|
|
65
|
+
base: 8453,
|
|
66
|
+
"base-sepolia": 84532,
|
|
67
|
+
ethereum: 1,
|
|
68
|
+
sepolia: 11155111,
|
|
69
|
+
polygon: 137,
|
|
70
|
+
"polygon-amoy": 80002
|
|
71
|
+
};
|
|
72
|
+
return networkMap[network] || 1;
|
|
73
|
+
}
|
|
74
|
+
function createNonce() {
|
|
75
|
+
const cryptoObj = typeof globalThis.crypto !== "undefined" ? globalThis.crypto : globalThis.crypto;
|
|
76
|
+
if (!cryptoObj) {
|
|
77
|
+
throw new Error("Crypto API not available");
|
|
78
|
+
}
|
|
79
|
+
return toHex(cryptoObj.getRandomValues(new Uint8Array(32)));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export {
|
|
83
|
+
authorizationTypes,
|
|
84
|
+
eip3009ABI,
|
|
85
|
+
getEvmChainId,
|
|
86
|
+
createNonce
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=chunk-CYGMVQCG.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/constants.ts","../../src/utils.ts"],"sourcesContent":["// 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":";AACO,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,M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AAAA,IACT,MAAM;AAAA,IACN,SAAS,CAAC,EAAE,MAAM,IAAI,MAAM,SAAS,CAAC;AAAA,IACtC,iBAAiB;AAAA,IACjB,MAAM;AAAA,EACR;AACF;;;AC5DA,SAAS,aAAa;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,SAAO,MAAM,UAAU,gBAAgB,IAAI,WAAW,EAAE,CAAC,CAAC;AAC5D;","names":[]}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import {
|
|
2
|
+
authorizationTypes,
|
|
3
|
+
eip3009ABI,
|
|
4
|
+
getEvmChainId
|
|
5
|
+
} from "./chunk-CYGMVQCG.mjs";
|
|
6
|
+
|
|
7
|
+
// src/exact/v1/facilitator/scheme.ts
|
|
8
|
+
import { getAddress, isAddressEqual, parseErc6492Signature, parseSignature } from "viem";
|
|
9
|
+
var ExactEvmSchemeV1 = class {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new ExactEvmFacilitatorV1 instance.
|
|
12
|
+
*
|
|
13
|
+
* @param signer - The EVM signer for facilitator operations
|
|
14
|
+
* @param config - Optional configuration for the facilitator
|
|
15
|
+
*/
|
|
16
|
+
constructor(signer, config) {
|
|
17
|
+
this.signer = signer;
|
|
18
|
+
this.scheme = "exact";
|
|
19
|
+
this.caipFamily = "eip155:*";
|
|
20
|
+
this.config = {
|
|
21
|
+
deployERC4337WithEIP6492: config?.deployERC4337WithEIP6492 ?? false
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get mechanism-specific extra data for the supported kinds endpoint.
|
|
26
|
+
* For EVM, no extra data is needed.
|
|
27
|
+
*
|
|
28
|
+
* @param _ - The network identifier (unused for EVM)
|
|
29
|
+
* @returns undefined (EVM has no extra data)
|
|
30
|
+
*/
|
|
31
|
+
getExtra(_) {
|
|
32
|
+
return void 0;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get signer addresses used by this facilitator.
|
|
36
|
+
* Returns all addresses this facilitator can use for signing/settling transactions.
|
|
37
|
+
*
|
|
38
|
+
* @param _ - The network identifier (unused for EVM, addresses are network-agnostic)
|
|
39
|
+
* @returns Array of facilitator wallet addresses
|
|
40
|
+
*/
|
|
41
|
+
getSigners(_) {
|
|
42
|
+
return [...this.signer.getAddresses()];
|
|
43
|
+
}
|
|
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
|
+
async verify(payload, requirements) {
|
|
52
|
+
const requirementsV1 = requirements;
|
|
53
|
+
const payloadV1 = payload;
|
|
54
|
+
const exactEvmPayload = payload.payload;
|
|
55
|
+
if (payloadV1.scheme !== "exact" || requirements.scheme !== "exact") {
|
|
56
|
+
return {
|
|
57
|
+
isValid: false,
|
|
58
|
+
invalidReason: "unsupported_scheme",
|
|
59
|
+
payer: exactEvmPayload.authorization.from
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const chainId = getEvmChainId(payloadV1.network);
|
|
63
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
64
|
+
return {
|
|
65
|
+
isValid: false,
|
|
66
|
+
invalidReason: "missing_eip712_domain",
|
|
67
|
+
payer: exactEvmPayload.authorization.from
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
const { name, version } = requirements.extra;
|
|
71
|
+
const erc20Address = getAddress(requirements.asset);
|
|
72
|
+
if (payloadV1.network !== requirements.network) {
|
|
73
|
+
return {
|
|
74
|
+
isValid: false,
|
|
75
|
+
invalidReason: "network_mismatch",
|
|
76
|
+
payer: exactEvmPayload.authorization.from
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const permitTypedData = {
|
|
80
|
+
types: authorizationTypes,
|
|
81
|
+
primaryType: "TransferWithAuthorization",
|
|
82
|
+
domain: {
|
|
83
|
+
name,
|
|
84
|
+
version,
|
|
85
|
+
chainId,
|
|
86
|
+
verifyingContract: erc20Address
|
|
87
|
+
},
|
|
88
|
+
message: {
|
|
89
|
+
from: exactEvmPayload.authorization.from,
|
|
90
|
+
to: exactEvmPayload.authorization.to,
|
|
91
|
+
value: BigInt(exactEvmPayload.authorization.value),
|
|
92
|
+
validAfter: BigInt(exactEvmPayload.authorization.validAfter),
|
|
93
|
+
validBefore: BigInt(exactEvmPayload.authorization.validBefore),
|
|
94
|
+
nonce: exactEvmPayload.authorization.nonce
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
try {
|
|
98
|
+
const recoveredAddress = await this.signer.verifyTypedData({
|
|
99
|
+
address: exactEvmPayload.authorization.from,
|
|
100
|
+
...permitTypedData,
|
|
101
|
+
signature: exactEvmPayload.signature
|
|
102
|
+
});
|
|
103
|
+
if (!recoveredAddress) {
|
|
104
|
+
return {
|
|
105
|
+
isValid: false,
|
|
106
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
107
|
+
payer: exactEvmPayload.authorization.from
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
} catch {
|
|
111
|
+
const signature = exactEvmPayload.signature;
|
|
112
|
+
const signatureLength = signature.startsWith("0x") ? signature.length - 2 : signature.length;
|
|
113
|
+
const isSmartWallet = signatureLength > 130;
|
|
114
|
+
if (isSmartWallet) {
|
|
115
|
+
const payerAddress = exactEvmPayload.authorization.from;
|
|
116
|
+
const bytecode = await this.signer.getCode({ address: payerAddress });
|
|
117
|
+
if (!bytecode || bytecode === "0x") {
|
|
118
|
+
const erc6492Data = parseErc6492Signature(signature);
|
|
119
|
+
const hasDeploymentInfo = erc6492Data.address && erc6492Data.data && !isAddressEqual(erc6492Data.address, "0x0000000000000000000000000000000000000000");
|
|
120
|
+
if (!hasDeploymentInfo) {
|
|
121
|
+
return {
|
|
122
|
+
isValid: false,
|
|
123
|
+
invalidReason: "invalid_exact_evm_payload_undeployed_smart_wallet",
|
|
124
|
+
payer: payerAddress
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
return {
|
|
129
|
+
isValid: false,
|
|
130
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
131
|
+
payer: exactEvmPayload.authorization.from
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
return {
|
|
136
|
+
isValid: false,
|
|
137
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
138
|
+
payer: exactEvmPayload.authorization.from
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (getAddress(exactEvmPayload.authorization.to) !== getAddress(requirements.payTo)) {
|
|
143
|
+
return {
|
|
144
|
+
isValid: false,
|
|
145
|
+
invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
|
|
146
|
+
payer: exactEvmPayload.authorization.from
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
150
|
+
if (BigInt(exactEvmPayload.authorization.validBefore) < BigInt(now + 6)) {
|
|
151
|
+
return {
|
|
152
|
+
isValid: false,
|
|
153
|
+
invalidReason: "invalid_exact_evm_payload_authorization_valid_before",
|
|
154
|
+
payer: exactEvmPayload.authorization.from
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
if (BigInt(exactEvmPayload.authorization.validAfter) > BigInt(now)) {
|
|
158
|
+
return {
|
|
159
|
+
isValid: false,
|
|
160
|
+
invalidReason: "invalid_exact_evm_payload_authorization_valid_after",
|
|
161
|
+
payer: exactEvmPayload.authorization.from
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const balance = await this.signer.readContract({
|
|
166
|
+
address: erc20Address,
|
|
167
|
+
abi: eip3009ABI,
|
|
168
|
+
functionName: "balanceOf",
|
|
169
|
+
args: [exactEvmPayload.authorization.from]
|
|
170
|
+
});
|
|
171
|
+
if (BigInt(balance) < BigInt(requirementsV1.maxAmountRequired)) {
|
|
172
|
+
return {
|
|
173
|
+
isValid: false,
|
|
174
|
+
invalidReason: "insufficient_funds",
|
|
175
|
+
payer: exactEvmPayload.authorization.from
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
} catch {
|
|
179
|
+
}
|
|
180
|
+
if (BigInt(exactEvmPayload.authorization.value) < BigInt(requirementsV1.maxAmountRequired)) {
|
|
181
|
+
return {
|
|
182
|
+
isValid: false,
|
|
183
|
+
invalidReason: "invalid_exact_evm_payload_authorization_value",
|
|
184
|
+
payer: exactEvmPayload.authorization.from
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
isValid: true,
|
|
189
|
+
invalidReason: void 0,
|
|
190
|
+
payer: exactEvmPayload.authorization.from
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Settles a payment by executing the transfer (V1).
|
|
195
|
+
*
|
|
196
|
+
* @param payload - The payment payload to settle
|
|
197
|
+
* @param requirements - The payment requirements
|
|
198
|
+
* @returns Promise resolving to settlement response
|
|
199
|
+
*/
|
|
200
|
+
async settle(payload, requirements) {
|
|
201
|
+
const payloadV1 = payload;
|
|
202
|
+
const exactEvmPayload = payload.payload;
|
|
203
|
+
const valid = await this.verify(payload, requirements);
|
|
204
|
+
if (!valid.isValid) {
|
|
205
|
+
return {
|
|
206
|
+
success: false,
|
|
207
|
+
network: payloadV1.network,
|
|
208
|
+
transaction: "",
|
|
209
|
+
errorReason: valid.invalidReason ?? "invalid_scheme",
|
|
210
|
+
payer: exactEvmPayload.authorization.from
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const parseResult = parseErc6492Signature(exactEvmPayload.signature);
|
|
215
|
+
const { signature, address: factoryAddress, data: factoryCalldata } = parseResult;
|
|
216
|
+
if (this.config.deployERC4337WithEIP6492 && factoryAddress && factoryCalldata && !isAddressEqual(factoryAddress, "0x0000000000000000000000000000000000000000")) {
|
|
217
|
+
const payerAddress = exactEvmPayload.authorization.from;
|
|
218
|
+
const bytecode = await this.signer.getCode({ address: payerAddress });
|
|
219
|
+
if (!bytecode || bytecode === "0x") {
|
|
220
|
+
try {
|
|
221
|
+
console.log(`Deploying ERC-4337 smart wallet for ${payerAddress} via EIP-6492`);
|
|
222
|
+
const deployTx = await this.signer.sendTransaction({
|
|
223
|
+
to: factoryAddress,
|
|
224
|
+
data: factoryCalldata
|
|
225
|
+
});
|
|
226
|
+
await this.signer.waitForTransactionReceipt({ hash: deployTx });
|
|
227
|
+
console.log(`Successfully deployed smart wallet for ${payerAddress}`);
|
|
228
|
+
} catch (deployError) {
|
|
229
|
+
console.error("Smart wallet deployment failed:", deployError);
|
|
230
|
+
throw deployError;
|
|
231
|
+
}
|
|
232
|
+
} else {
|
|
233
|
+
console.log(`Smart wallet for ${payerAddress} already deployed, skipping deployment`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const signatureLength = signature.startsWith("0x") ? signature.length - 2 : signature.length;
|
|
237
|
+
const isECDSA = signatureLength === 130;
|
|
238
|
+
let tx;
|
|
239
|
+
if (isECDSA) {
|
|
240
|
+
const parsedSig = parseSignature(signature);
|
|
241
|
+
tx = await this.signer.writeContract({
|
|
242
|
+
address: getAddress(requirements.asset),
|
|
243
|
+
abi: eip3009ABI,
|
|
244
|
+
functionName: "transferWithAuthorization",
|
|
245
|
+
args: [
|
|
246
|
+
getAddress(exactEvmPayload.authorization.from),
|
|
247
|
+
getAddress(exactEvmPayload.authorization.to),
|
|
248
|
+
BigInt(exactEvmPayload.authorization.value),
|
|
249
|
+
BigInt(exactEvmPayload.authorization.validAfter),
|
|
250
|
+
BigInt(exactEvmPayload.authorization.validBefore),
|
|
251
|
+
exactEvmPayload.authorization.nonce,
|
|
252
|
+
parsedSig.v || parsedSig.yParity,
|
|
253
|
+
parsedSig.r,
|
|
254
|
+
parsedSig.s
|
|
255
|
+
]
|
|
256
|
+
});
|
|
257
|
+
} else {
|
|
258
|
+
tx = await this.signer.writeContract({
|
|
259
|
+
address: getAddress(requirements.asset),
|
|
260
|
+
abi: eip3009ABI,
|
|
261
|
+
functionName: "transferWithAuthorization",
|
|
262
|
+
args: [
|
|
263
|
+
getAddress(exactEvmPayload.authorization.from),
|
|
264
|
+
getAddress(exactEvmPayload.authorization.to),
|
|
265
|
+
BigInt(exactEvmPayload.authorization.value),
|
|
266
|
+
BigInt(exactEvmPayload.authorization.validAfter),
|
|
267
|
+
BigInt(exactEvmPayload.authorization.validBefore),
|
|
268
|
+
exactEvmPayload.authorization.nonce,
|
|
269
|
+
signature
|
|
270
|
+
]
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
const receipt = await this.signer.waitForTransactionReceipt({ hash: tx });
|
|
274
|
+
if (receipt.status !== "success") {
|
|
275
|
+
return {
|
|
276
|
+
success: false,
|
|
277
|
+
errorReason: "invalid_transaction_state",
|
|
278
|
+
transaction: tx,
|
|
279
|
+
network: payloadV1.network,
|
|
280
|
+
payer: exactEvmPayload.authorization.from
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
success: true,
|
|
285
|
+
transaction: tx,
|
|
286
|
+
network: payloadV1.network,
|
|
287
|
+
payer: exactEvmPayload.authorization.from
|
|
288
|
+
};
|
|
289
|
+
} catch (error) {
|
|
290
|
+
console.error("Failed to settle transaction:", error);
|
|
291
|
+
return {
|
|
292
|
+
success: false,
|
|
293
|
+
errorReason: "transaction_failed",
|
|
294
|
+
transaction: "",
|
|
295
|
+
network: payloadV1.network,
|
|
296
|
+
payer: exactEvmPayload.authorization.from
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
export {
|
|
303
|
+
ExactEvmSchemeV1
|
|
304
|
+
};
|
|
305
|
+
//# sourceMappingURL=chunk-DI3EK7PR.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exact/v1/facilitator/scheme.ts"],"sourcesContent":["import {\n PaymentPayload,\n PaymentPayloadV1,\n PaymentRequirements,\n SchemeNetworkFacilitator,\n SettleResponse,\n VerifyResponse,\n} from \"x402-core-mantle/types\";\nimport { PaymentRequirementsV1 } from \"x402-core-mantle/types/v1\";\nimport { getAddress, Hex, isAddressEqual, parseErc6492Signature, parseSignature } from \"viem\";\nimport { authorizationTypes, eip3009ABI } from \"../../../constants\";\nimport { FacilitatorEvmSigner } from \"../../../signer\";\nimport { ExactEvmPayloadV1 } from \"../../../types\";\nimport { getEvmChainId } from \"../../../utils\";\n\nexport interface ExactEvmSchemeV1Config {\n /**\n * If enabled, the facilitator will deploy ERC-4337 smart wallets\n * via EIP-6492 when encountering undeployed contract signatures.\n *\n * @default false\n */\n deployERC4337WithEIP6492?: boolean;\n}\n\n/**\n * EVM facilitator implementation for the Exact payment scheme (V1).\n */\nexport class ExactEvmSchemeV1 implements SchemeNetworkFacilitator {\n readonly scheme = \"exact\";\n readonly caipFamily = \"eip155:*\";\n private readonly config: Required<ExactEvmSchemeV1Config>;\n\n /**\n * Creates a new ExactEvmFacilitatorV1 instance.\n *\n * @param signer - The EVM signer for facilitator operations\n * @param config - Optional configuration for the facilitator\n */\n constructor(\n private readonly signer: FacilitatorEvmSigner,\n config?: ExactEvmSchemeV1Config,\n ) {\n this.config = {\n deployERC4337WithEIP6492: config?.deployERC4337WithEIP6492 ?? false,\n };\n }\n\n /**\n * Get mechanism-specific extra data for the supported kinds endpoint.\n * For EVM, no extra data is needed.\n *\n * @param _ - The network identifier (unused for EVM)\n * @returns undefined (EVM has no extra data)\n */\n getExtra(_: string): Record<string, unknown> | undefined {\n return undefined;\n }\n\n /**\n * Get signer addresses used by this facilitator.\n * Returns all addresses this facilitator can use for signing/settling transactions.\n *\n * @param _ - The network identifier (unused for EVM, addresses are network-agnostic)\n * @returns Array of facilitator wallet addresses\n */\n getSigners(_: string): string[] {\n return [...this.signer.getAddresses()];\n }\n\n /**\n * Verifies a payment payload (V1).\n *\n * @param payload - The payment payload to verify\n * @param requirements - The payment requirements\n * @returns Promise resolving to verification response\n */\n async verify(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n const requirementsV1 = requirements as unknown as PaymentRequirementsV1;\n const payloadV1 = payload as unknown as PaymentPayloadV1;\n const exactEvmPayload = payload.payload as ExactEvmPayloadV1;\n\n // Verify scheme matches\n if (payloadV1.scheme !== \"exact\" || requirements.scheme !== \"exact\") {\n return {\n isValid: false,\n invalidReason: \"unsupported_scheme\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Get chain configuration\n const chainId = getEvmChainId(payloadV1.network);\n\n if (!requirements.extra?.name || !requirements.extra?.version) {\n return {\n isValid: false,\n invalidReason: \"missing_eip712_domain\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n const { name, version } = requirements.extra;\n const erc20Address = getAddress(requirements.asset);\n\n // Verify network matches\n if (payloadV1.network !== requirements.network) {\n return {\n isValid: false,\n invalidReason: \"network_mismatch\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Build typed data for signature verification\n const permitTypedData = {\n types: authorizationTypes,\n primaryType: \"TransferWithAuthorization\" as const,\n domain: {\n name,\n version,\n chainId,\n verifyingContract: erc20Address,\n },\n message: {\n from: exactEvmPayload.authorization.from,\n to: exactEvmPayload.authorization.to,\n value: BigInt(exactEvmPayload.authorization.value),\n validAfter: BigInt(exactEvmPayload.authorization.validAfter),\n validBefore: BigInt(exactEvmPayload.authorization.validBefore),\n nonce: exactEvmPayload.authorization.nonce,\n },\n };\n\n // Verify signature\n try {\n const recoveredAddress = await this.signer.verifyTypedData({\n address: exactEvmPayload.authorization.from,\n ...permitTypedData,\n signature: exactEvmPayload.signature!,\n });\n\n if (!recoveredAddress) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_signature\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n } catch {\n // Signature verification failed - could be an undeployed smart wallet\n // Check if smart wallet is deployed\n const signature = exactEvmPayload.signature!;\n const signatureLength = signature.startsWith(\"0x\") ? signature.length - 2 : signature.length;\n const isSmartWallet = signatureLength > 130; // 65 bytes = 130 hex chars for EOA\n\n if (isSmartWallet) {\n const payerAddress = exactEvmPayload.authorization.from;\n const bytecode = await this.signer.getCode({ address: payerAddress });\n\n if (!bytecode || bytecode === \"0x\") {\n // Wallet is not deployed. Check if it's EIP-6492 with deployment info.\n // EIP-6492 signatures contain factory address and calldata needed for deployment.\n // Non-EIP-6492 undeployed wallets cannot succeed (no way to deploy them).\n const erc6492Data = parseErc6492Signature(signature);\n const hasDeploymentInfo =\n erc6492Data.address &&\n erc6492Data.data &&\n !isAddressEqual(erc6492Data.address, \"0x0000000000000000000000000000000000000000\");\n\n if (!hasDeploymentInfo) {\n // Non-EIP-6492 undeployed smart wallet - will always fail at settlement\n // since EIP-3009 requires on-chain EIP-1271 validation\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_undeployed_smart_wallet\",\n payer: payerAddress,\n };\n }\n // EIP-6492 signature with deployment info - allow through\n // Facilitators with sponsored deployment support can handle this in settle()\n } else {\n // Wallet is deployed but signature still failed - invalid signature\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_signature\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n } else {\n // EOA signature failed\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_signature\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n }\n\n // Verify payment recipient matches\n if (getAddress(exactEvmPayload.authorization.to) !== getAddress(requirements.payTo)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_recipient_mismatch\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Verify validBefore is in the future (with 6 second buffer for block time)\n const now = Math.floor(Date.now() / 1000);\n if (BigInt(exactEvmPayload.authorization.validBefore) < BigInt(now + 6)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_authorization_valid_before\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Verify validAfter is not in the future\n if (BigInt(exactEvmPayload.authorization.validAfter) > BigInt(now)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_authorization_valid_after\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Check balance\n try {\n const balance = (await this.signer.readContract({\n address: erc20Address,\n abi: eip3009ABI,\n functionName: \"balanceOf\",\n args: [exactEvmPayload.authorization.from],\n })) as bigint;\n\n if (BigInt(balance) < BigInt(requirementsV1.maxAmountRequired)) {\n return {\n isValid: false,\n invalidReason: \"insufficient_funds\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n } catch {\n // If we can't check balance, continue with other validations\n }\n\n // Verify amount is sufficient\n if (BigInt(exactEvmPayload.authorization.value) < BigInt(requirementsV1.maxAmountRequired)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_authorization_value\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n return {\n isValid: true,\n invalidReason: undefined,\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n /**\n * Settles a payment by executing the transfer (V1).\n *\n * @param payload - The payment payload to settle\n * @param requirements - The payment requirements\n * @returns Promise resolving to settlement response\n */\n async settle(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n const payloadV1 = payload as unknown as PaymentPayloadV1;\n const exactEvmPayload = payload.payload as ExactEvmPayloadV1;\n\n // Re-verify before settling\n const valid = await this.verify(payload, requirements);\n if (!valid.isValid) {\n return {\n success: false,\n network: payloadV1.network,\n transaction: \"\",\n errorReason: valid.invalidReason ?? \"invalid_scheme\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n try {\n // Parse ERC-6492 signature if applicable\n const parseResult = parseErc6492Signature(exactEvmPayload.signature!);\n const { signature, address: factoryAddress, data: factoryCalldata } = parseResult;\n\n // Deploy ERC-4337 smart wallet via EIP-6492 if configured and needed\n if (\n this.config.deployERC4337WithEIP6492 &&\n factoryAddress &&\n factoryCalldata &&\n !isAddressEqual(factoryAddress, \"0x0000000000000000000000000000000000000000\")\n ) {\n // Check if smart wallet is already deployed\n const payerAddress = exactEvmPayload.authorization.from;\n const bytecode = await this.signer.getCode({ address: payerAddress });\n\n if (!bytecode || bytecode === \"0x\") {\n // Wallet not deployed - attempt deployment\n try {\n console.log(`Deploying ERC-4337 smart wallet for ${payerAddress} via EIP-6492`);\n\n // Send the factory calldata directly as a transaction\n // The factoryCalldata already contains the complete encoded function call\n const deployTx = await this.signer.sendTransaction({\n to: factoryAddress as Hex,\n data: factoryCalldata as Hex,\n });\n\n // Wait for deployment transaction\n await this.signer.waitForTransactionReceipt({ hash: deployTx });\n console.log(`Successfully deployed smart wallet for ${payerAddress}`);\n } catch (deployError) {\n console.error(\"Smart wallet deployment failed:\", deployError);\n // Deployment failed - cannot proceed\n throw deployError;\n }\n } else {\n console.log(`Smart wallet for ${payerAddress} already deployed, skipping deployment`);\n }\n }\n\n // Determine if this is an ECDSA signature (EOA) or smart wallet signature\n // ECDSA signatures are exactly 65 bytes (130 hex chars without 0x)\n const signatureLength = signature.startsWith(\"0x\") ? signature.length - 2 : signature.length;\n const isECDSA = signatureLength === 130;\n\n let tx: Hex;\n if (isECDSA) {\n // For EOA wallets, parse signature into v, r, s and use that overload\n const parsedSig = parseSignature(signature);\n\n tx = await this.signer.writeContract({\n address: getAddress(requirements.asset),\n abi: eip3009ABI,\n functionName: \"transferWithAuthorization\",\n args: [\n getAddress(exactEvmPayload.authorization.from),\n getAddress(exactEvmPayload.authorization.to),\n BigInt(exactEvmPayload.authorization.value),\n BigInt(exactEvmPayload.authorization.validAfter),\n BigInt(exactEvmPayload.authorization.validBefore),\n exactEvmPayload.authorization.nonce,\n (parsedSig.v as number | undefined) || parsedSig.yParity,\n parsedSig.r,\n parsedSig.s,\n ],\n });\n } else {\n // For smart wallets, use the bytes signature overload\n // The signature contains WebAuthn/P256 or other ERC-1271 compatible signature data\n tx = await this.signer.writeContract({\n address: getAddress(requirements.asset),\n abi: eip3009ABI,\n functionName: \"transferWithAuthorization\",\n args: [\n getAddress(exactEvmPayload.authorization.from),\n getAddress(exactEvmPayload.authorization.to),\n BigInt(exactEvmPayload.authorization.value),\n BigInt(exactEvmPayload.authorization.validAfter),\n BigInt(exactEvmPayload.authorization.validBefore),\n exactEvmPayload.authorization.nonce,\n signature,\n ],\n });\n }\n\n // Wait for transaction confirmation\n const receipt = await this.signer.waitForTransactionReceipt({ hash: tx });\n\n if (receipt.status !== \"success\") {\n return {\n success: false,\n errorReason: \"invalid_transaction_state\",\n transaction: tx,\n network: payloadV1.network,\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n return {\n success: true,\n transaction: tx,\n network: payloadV1.network,\n payer: exactEvmPayload.authorization.from,\n };\n } catch (error) {\n console.error(\"Failed to settle transaction:\", error);\n return {\n success: false,\n errorReason: \"transaction_failed\",\n transaction: \"\",\n network: payloadV1.network,\n payer: exactEvmPayload.authorization.from,\n };\n }\n }\n}\n"],"mappings":";;;;;;;AASA,SAAS,YAAiB,gBAAgB,uBAAuB,sBAAsB;AAmBhF,IAAM,mBAAN,MAA2D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWhE,YACmB,QACjB,QACA;AAFiB;AAXnB,SAAS,SAAS;AAClB,SAAS,aAAa;AAapB,SAAK,SAAS;AAAA,MACZ,0BAA0B,QAAQ,4BAA4B;AAAA,IAChE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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Ac;AAAA,MACvC;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/v1/index.ts
|
|
2
|
+
var NETWORKS = [
|
|
3
|
+
"abstract",
|
|
4
|
+
"abstract-testnet",
|
|
5
|
+
"base-sepolia",
|
|
6
|
+
"base",
|
|
7
|
+
"avalanche-fuji",
|
|
8
|
+
"avalanche",
|
|
9
|
+
"iotex",
|
|
10
|
+
"sei",
|
|
11
|
+
"sei-testnet",
|
|
12
|
+
"polygon",
|
|
13
|
+
"polygon-amoy",
|
|
14
|
+
"peaq",
|
|
15
|
+
"story",
|
|
16
|
+
"educhain",
|
|
17
|
+
"skale-base-sepolia"
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
NETWORKS
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=chunk-QLXM7BIB.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/v1/index.ts"],"sourcesContent":["export { ExactEvmSchemeV1 } from \"../exact/v1\";\n\nexport const NETWORKS: string[] = [\n \"abstract\",\n \"abstract-testnet\",\n \"base-sepolia\",\n \"base\",\n \"avalanche-fuji\",\n \"avalanche\",\n \"iotex\",\n \"sei\",\n \"sei-testnet\",\n \"polygon\",\n \"polygon-amoy\",\n \"peaq\",\n \"story\",\n \"educhain\",\n \"skale-base-sepolia\",\n];\n"],"mappings":";AAEO,IAAM,WAAqB;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;","names":[]}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
authorizationTypes,
|
|
3
|
+
createNonce,
|
|
4
|
+
getEvmChainId
|
|
5
|
+
} from "./chunk-CYGMVQCG.mjs";
|
|
6
|
+
|
|
7
|
+
// src/exact/v1/client/scheme.ts
|
|
8
|
+
import { getAddress } from "viem";
|
|
9
|
+
var ExactEvmSchemeV1 = class {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new ExactEvmClientV1 instance.
|
|
12
|
+
*
|
|
13
|
+
* @param signer - The EVM signer for client operations
|
|
14
|
+
*/
|
|
15
|
+
constructor(signer) {
|
|
16
|
+
this.signer = signer;
|
|
17
|
+
this.scheme = "exact";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a payment payload for the Exact scheme (V1).
|
|
21
|
+
*
|
|
22
|
+
* @param x402Version - The x402 protocol version
|
|
23
|
+
* @param paymentRequirements - The payment requirements
|
|
24
|
+
* @returns Promise resolving to a payment payload
|
|
25
|
+
*/
|
|
26
|
+
async createPaymentPayload(x402Version, paymentRequirements) {
|
|
27
|
+
const selectedV1 = paymentRequirements;
|
|
28
|
+
const nonce = createNonce();
|
|
29
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
30
|
+
const authorization = {
|
|
31
|
+
from: this.signer.address,
|
|
32
|
+
to: getAddress(selectedV1.payTo),
|
|
33
|
+
value: selectedV1.maxAmountRequired,
|
|
34
|
+
validAfter: (now - 600).toString(),
|
|
35
|
+
// 10 minutes before
|
|
36
|
+
validBefore: (now + selectedV1.maxTimeoutSeconds).toString(),
|
|
37
|
+
nonce
|
|
38
|
+
};
|
|
39
|
+
const signature = await this.signAuthorization(authorization, selectedV1);
|
|
40
|
+
const payload = {
|
|
41
|
+
authorization,
|
|
42
|
+
signature
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
x402Version,
|
|
46
|
+
scheme: selectedV1.scheme,
|
|
47
|
+
network: selectedV1.network,
|
|
48
|
+
payload
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sign the EIP-3009 authorization using EIP-712
|
|
53
|
+
*
|
|
54
|
+
* @param authorization - The authorization to sign
|
|
55
|
+
* @param requirements - The payment requirements
|
|
56
|
+
* @returns Promise resolving to the signature
|
|
57
|
+
*/
|
|
58
|
+
async signAuthorization(authorization, requirements) {
|
|
59
|
+
const chainId = getEvmChainId(requirements.network);
|
|
60
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
const { name, version } = requirements.extra;
|
|
66
|
+
const domain = {
|
|
67
|
+
name,
|
|
68
|
+
version,
|
|
69
|
+
chainId,
|
|
70
|
+
verifyingContract: getAddress(requirements.asset)
|
|
71
|
+
};
|
|
72
|
+
const message = {
|
|
73
|
+
from: getAddress(authorization.from),
|
|
74
|
+
to: getAddress(authorization.to),
|
|
75
|
+
value: BigInt(authorization.value),
|
|
76
|
+
validAfter: BigInt(authorization.validAfter),
|
|
77
|
+
validBefore: BigInt(authorization.validBefore),
|
|
78
|
+
nonce: authorization.nonce
|
|
79
|
+
};
|
|
80
|
+
return await this.signer.signTypedData({
|
|
81
|
+
domain,
|
|
82
|
+
types: authorizationTypes,
|
|
83
|
+
primaryType: "TransferWithAuthorization",
|
|
84
|
+
message
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export {
|
|
90
|
+
ExactEvmSchemeV1
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=chunk-S5OEANGR.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exact/v1/client/scheme.ts"],"sourcesContent":["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"],"mappings":";;;;;;;AAOA,SAAS,kBAAkB;AASpB,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,IAAI,WAAW,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,mBAAmB,WAAW,aAAa,KAAK;AAAA,IAClD;AAEA,UAAM,UAAU;AAAA,MACd,MAAM,WAAW,cAAc,IAAI;AAAA,MACnC,IAAI,WAAW,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":[]}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
authorizationTypes,
|
|
3
|
+
createNonce
|
|
4
|
+
} from "./chunk-CYGMVQCG.mjs";
|
|
5
|
+
|
|
6
|
+
// src/exact/client/scheme.ts
|
|
7
|
+
import { getAddress } from "viem";
|
|
8
|
+
var ExactEvmScheme = class {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new ExactEvmClient instance.
|
|
11
|
+
*
|
|
12
|
+
* @param signer - The EVM signer for client operations
|
|
13
|
+
*/
|
|
14
|
+
constructor(signer) {
|
|
15
|
+
this.signer = signer;
|
|
16
|
+
this.scheme = "exact";
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a payment payload for the Exact scheme.
|
|
20
|
+
*
|
|
21
|
+
* @param x402Version - The x402 protocol version
|
|
22
|
+
* @param paymentRequirements - The payment requirements
|
|
23
|
+
* @returns Promise resolving to a payment payload
|
|
24
|
+
*/
|
|
25
|
+
async createPaymentPayload(x402Version, paymentRequirements) {
|
|
26
|
+
const nonce = createNonce();
|
|
27
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
28
|
+
const authorization = {
|
|
29
|
+
from: this.signer.address,
|
|
30
|
+
to: getAddress(paymentRequirements.payTo),
|
|
31
|
+
value: paymentRequirements.amount,
|
|
32
|
+
validAfter: (now - 600).toString(),
|
|
33
|
+
// 10 minutes before
|
|
34
|
+
validBefore: (now + paymentRequirements.maxTimeoutSeconds).toString(),
|
|
35
|
+
nonce
|
|
36
|
+
};
|
|
37
|
+
const signature = await this.signAuthorization(authorization, paymentRequirements);
|
|
38
|
+
const payload = {
|
|
39
|
+
authorization,
|
|
40
|
+
signature
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
x402Version,
|
|
44
|
+
payload
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Sign the EIP-3009 authorization using EIP-712
|
|
49
|
+
*
|
|
50
|
+
* @param authorization - The authorization to sign
|
|
51
|
+
* @param requirements - The payment requirements
|
|
52
|
+
* @returns Promise resolving to the signature
|
|
53
|
+
*/
|
|
54
|
+
async signAuthorization(authorization, requirements) {
|
|
55
|
+
const chainId = parseInt(requirements.network.split(":")[1]);
|
|
56
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const { name, version } = requirements.extra;
|
|
62
|
+
const domain = {
|
|
63
|
+
name,
|
|
64
|
+
version,
|
|
65
|
+
chainId,
|
|
66
|
+
verifyingContract: getAddress(requirements.asset)
|
|
67
|
+
};
|
|
68
|
+
const message = {
|
|
69
|
+
from: getAddress(authorization.from),
|
|
70
|
+
to: getAddress(authorization.to),
|
|
71
|
+
value: BigInt(authorization.value),
|
|
72
|
+
validAfter: BigInt(authorization.validAfter),
|
|
73
|
+
validBefore: BigInt(authorization.validBefore),
|
|
74
|
+
nonce: authorization.nonce
|
|
75
|
+
};
|
|
76
|
+
return await this.signer.signTypedData({
|
|
77
|
+
domain,
|
|
78
|
+
types: authorizationTypes,
|
|
79
|
+
primaryType: "TransferWithAuthorization",
|
|
80
|
+
message
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export {
|
|
86
|
+
ExactEvmScheme
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=chunk-T3FPOH7F.mjs.map
|