@payai/x402-evm 2.4.1 → 2.4.3
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/batch-settlement/client/file-storage.d.ts +47 -0
- package/dist/cjs/batch-settlement/client/file-storage.js +116 -0
- package/dist/cjs/batch-settlement/client/file-storage.js.map +1 -0
- package/dist/cjs/batch-settlement/client/index.d.ts +111 -0
- package/dist/cjs/batch-settlement/client/index.js +1565 -0
- package/dist/cjs/batch-settlement/client/index.js.map +1 -0
- package/dist/cjs/batch-settlement/facilitator/index.d.ts +71 -0
- package/dist/cjs/batch-settlement/facilitator/index.js +2032 -0
- package/dist/cjs/batch-settlement/facilitator/index.js.map +1 -0
- package/dist/cjs/batch-settlement/server/file-storage.d.ts +53 -0
- package/dist/cjs/batch-settlement/server/file-storage.js +181 -0
- package/dist/cjs/batch-settlement/server/file-storage.js.map +1 -0
- package/dist/cjs/batch-settlement/server/index.d.ts +491 -0
- package/dist/cjs/batch-settlement/server/index.js +1960 -0
- package/dist/cjs/batch-settlement/server/index.js.map +1 -0
- package/dist/cjs/batch-settlement/server/redis-storage.d.ts +87 -0
- package/dist/cjs/batch-settlement/server/redis-storage.js +181 -0
- package/dist/cjs/batch-settlement/server/redis-storage.js.map +1 -0
- package/dist/cjs/exact/client/index.d.ts +6 -4
- package/dist/cjs/exact/client/index.js +7 -5
- package/dist/cjs/exact/client/index.js.map +1 -1
- package/dist/cjs/exact/facilitator/index.d.ts +16 -9
- package/dist/cjs/exact/facilitator/index.js +35 -7
- package/dist/cjs/exact/facilitator/index.js.map +1 -1
- package/dist/cjs/exact/server/index.js +40 -1
- package/dist/cjs/exact/server/index.js.map +1 -1
- package/dist/cjs/exact/v1/client/index.d.ts +2 -1
- package/dist/cjs/exact/v1/client/index.js.map +1 -1
- package/dist/cjs/exact/v1/facilitator/index.d.ts +11 -5
- package/dist/cjs/exact/v1/facilitator/index.js +16 -2
- package/dist/cjs/exact/v1/facilitator/index.js.map +1 -1
- package/dist/cjs/index.d.ts +113 -7
- package/dist/cjs/index.js +1353 -5
- package/dist/cjs/index.js.map +1 -1
- package/dist/{esm/permit2-CyZxwngN.d.mts → cjs/permit2-DhJRUcgY.d.ts} +1 -13
- package/dist/cjs/rpc-DULZzRne.d.ts +13 -0
- package/dist/cjs/scheme-CvkPJXBD.d.ts +307 -0
- package/dist/{esm/scheme-DCR7hsa3.d.mts → cjs/scheme-DTQFE9xp.d.ts} +2 -2
- package/dist/{esm/signer-D912R4mq.d.mts → cjs/signer-tYS6Y46X.d.ts} +3 -0
- package/dist/cjs/storage-6W5MO46W.d.ts +50 -0
- package/dist/cjs/storage-Bl6aD0Xg.d.ts +81 -0
- package/dist/cjs/types-CF8P2-NM.d.ts +180 -0
- package/dist/cjs/upto/client/index.d.ts +5 -3
- package/dist/cjs/upto/client/index.js +7 -5
- package/dist/cjs/upto/client/index.js.map +1 -1
- package/dist/cjs/upto/facilitator/index.d.ts +2 -1
- package/dist/cjs/upto/facilitator/index.js +2 -1
- package/dist/cjs/upto/facilitator/index.js.map +1 -1
- package/dist/cjs/upto/server/index.js +40 -1
- package/dist/cjs/upto/server/index.js.map +1 -1
- package/dist/cjs/v1/index.d.ts +2 -1
- package/dist/cjs/v1/index.js.map +1 -1
- package/dist/esm/batch-settlement/client/file-storage.d.mts +47 -0
- package/dist/esm/batch-settlement/client/file-storage.mjs +63 -0
- package/dist/esm/batch-settlement/client/file-storage.mjs.map +1 -0
- package/dist/esm/batch-settlement/client/index.d.mts +111 -0
- package/dist/esm/batch-settlement/client/index.mjs +59 -0
- package/dist/esm/batch-settlement/client/index.mjs.map +1 -0
- package/dist/esm/batch-settlement/facilitator/index.d.mts +71 -0
- package/dist/esm/batch-settlement/facilitator/index.mjs +1235 -0
- package/dist/esm/batch-settlement/facilitator/index.mjs.map +1 -0
- package/dist/esm/batch-settlement/server/file-storage.d.mts +53 -0
- package/dist/esm/batch-settlement/server/file-storage.mjs +128 -0
- package/dist/esm/batch-settlement/server/file-storage.mjs.map +1 -0
- package/dist/esm/batch-settlement/server/index.d.mts +491 -0
- package/dist/esm/batch-settlement/server/index.mjs +1645 -0
- package/dist/esm/batch-settlement/server/index.mjs.map +1 -0
- package/dist/esm/batch-settlement/server/redis-storage.d.mts +87 -0
- package/dist/esm/batch-settlement/server/redis-storage.mjs +156 -0
- package/dist/esm/batch-settlement/server/redis-storage.mjs.map +1 -0
- package/dist/esm/chunk-2EUQTNJO.mjs +38 -0
- package/dist/esm/chunk-2EUQTNJO.mjs.map +1 -0
- package/dist/esm/chunk-53USC5VE.mjs +47 -0
- package/dist/esm/chunk-53USC5VE.mjs.map +1 -0
- package/dist/esm/{chunk-GJ57SZGI.mjs → chunk-6WQOGWBE.mjs} +7 -5
- package/dist/esm/{chunk-GJ57SZGI.mjs.map → chunk-6WQOGWBE.mjs.map} +1 -1
- package/dist/esm/{chunk-F3OOHBAW.mjs → chunk-BTYNCDNS.mjs} +42 -2
- package/dist/esm/{chunk-F3OOHBAW.mjs.map → chunk-BTYNCDNS.mjs.map} +1 -1
- package/dist/esm/{chunk-ERK2ZPOY.mjs → chunk-CSQS7ZON.mjs} +27 -7
- package/dist/esm/chunk-CSQS7ZON.mjs.map +1 -0
- package/dist/esm/chunk-GD4MKCN7.mjs +57 -0
- package/dist/esm/chunk-GD4MKCN7.mjs.map +1 -0
- package/dist/esm/chunk-HYABYUBD.mjs +432 -0
- package/dist/esm/chunk-HYABYUBD.mjs.map +1 -0
- package/dist/esm/chunk-IN5YIT5C.mjs +159 -0
- package/dist/esm/chunk-IN5YIT5C.mjs.map +1 -0
- package/dist/esm/{chunk-JII456TS.mjs → chunk-JK7SLLF7.mjs} +1 -1
- package/dist/esm/chunk-JK7SLLF7.mjs.map +1 -0
- package/dist/esm/{chunk-C4ZQMS77.mjs → chunk-MACPBXCT.mjs} +2 -216
- package/dist/esm/chunk-MACPBXCT.mjs.map +1 -0
- package/dist/esm/chunk-NKYVYGRA.mjs +911 -0
- package/dist/esm/chunk-NKYVYGRA.mjs.map +1 -0
- package/dist/esm/{chunk-FQJR4RCF.mjs → chunk-R7I3RZFF.mjs} +10 -6
- package/dist/esm/{chunk-FQJR4RCF.mjs.map → chunk-R7I3RZFF.mjs.map} +1 -1
- package/dist/esm/{chunk-CRT6YNY5.mjs → chunk-RWLVVO3B.mjs} +21 -61
- package/dist/esm/chunk-RWLVVO3B.mjs.map +1 -0
- package/dist/esm/chunk-TGFAVNUD.mjs +111 -0
- package/dist/esm/chunk-TGFAVNUD.mjs.map +1 -0
- package/dist/esm/chunk-TW7Z65AO.mjs +34 -0
- package/dist/esm/chunk-TW7Z65AO.mjs.map +1 -0
- package/dist/esm/chunk-U4HCGTLU.mjs +35 -0
- package/dist/esm/chunk-U4HCGTLU.mjs.map +1 -0
- package/dist/esm/chunk-VS3RYAYE.mjs +80 -0
- package/dist/esm/chunk-VS3RYAYE.mjs.map +1 -0
- package/dist/esm/chunk-W6ON4LG2.mjs +39 -0
- package/dist/esm/chunk-W6ON4LG2.mjs.map +1 -0
- package/dist/esm/{chunk-WKBC5YMI.mjs → chunk-YMQCTKDU.mjs} +23 -55
- package/dist/esm/chunk-YMQCTKDU.mjs.map +1 -0
- package/dist/esm/exact/client/index.d.mts +6 -4
- package/dist/esm/exact/client/index.mjs +10 -5
- package/dist/esm/exact/facilitator/index.d.mts +16 -9
- package/dist/esm/exact/facilitator/index.mjs +36 -14
- package/dist/esm/exact/facilitator/index.mjs.map +1 -1
- package/dist/esm/exact/server/index.mjs +1 -1
- package/dist/esm/exact/v1/client/index.d.mts +2 -1
- package/dist/esm/exact/v1/client/index.mjs +5 -2
- package/dist/esm/exact/v1/facilitator/index.d.mts +11 -5
- package/dist/esm/exact/v1/facilitator/index.mjs +5 -2
- package/dist/esm/index.d.mts +113 -7
- package/dist/esm/index.mjs +53 -7
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/permit2-DhJRUcgY.d.mts +729 -0
- package/dist/esm/rpc-DULZzRne.d.mts +13 -0
- package/dist/esm/scheme-DtbSS4Fk.d.mts +307 -0
- package/dist/esm/scheme-gtqAIYPJ.d.mts +47 -0
- package/dist/esm/signer-tYS6Y46X.d.mts +170 -0
- package/dist/esm/storage-6W5MO46W.d.mts +50 -0
- package/dist/esm/storage-sZ1CDS4P.d.mts +81 -0
- package/dist/esm/types-CF8P2-NM.d.mts +180 -0
- package/dist/esm/upto/client/index.d.mts +5 -3
- package/dist/esm/upto/client/index.mjs +9 -4
- package/dist/esm/upto/facilitator/index.d.mts +2 -1
- package/dist/esm/upto/facilitator/index.mjs +17 -9
- package/dist/esm/upto/facilitator/index.mjs.map +1 -1
- package/dist/esm/upto/server/index.mjs +1 -1
- package/dist/esm/v1/index.d.mts +2 -1
- package/dist/esm/v1/index.mjs +5 -2
- package/package.json +5 -5
- package/dist/esm/chunk-C4ZQMS77.mjs.map +0 -1
- package/dist/esm/chunk-CRT6YNY5.mjs.map +0 -1
- package/dist/esm/chunk-ERK2ZPOY.mjs.map +0 -1
- package/dist/esm/chunk-JII456TS.mjs.map +0 -1
- package/dist/esm/chunk-WKBC5YMI.mjs.map +0 -1
|
@@ -0,0 +1,1235 @@
|
|
|
1
|
+
import {
|
|
2
|
+
erc3009AuthorizationTimeInvalidReason,
|
|
3
|
+
readChannelState,
|
|
4
|
+
signClaimBatch,
|
|
5
|
+
signRefund,
|
|
6
|
+
toContractChannelConfig,
|
|
7
|
+
validateChannelConfig,
|
|
8
|
+
verifyBatchSettlementVoucherTypedData
|
|
9
|
+
} from "../../chunk-IN5YIT5C.mjs";
|
|
10
|
+
import {
|
|
11
|
+
buildEip2612PermitData,
|
|
12
|
+
buildErc3009CollectorData,
|
|
13
|
+
buildErc3009DepositNonce,
|
|
14
|
+
buildPermit2CollectorData
|
|
15
|
+
} from "../../chunk-W6ON4LG2.mjs";
|
|
16
|
+
import {
|
|
17
|
+
BATCH_SETTLEMENT_ADDRESS,
|
|
18
|
+
BATCH_SETTLEMENT_SCHEME,
|
|
19
|
+
ERC3009_DEPOSIT_COLLECTOR_ADDRESS,
|
|
20
|
+
ErrAuthorizerAddressMismatch,
|
|
21
|
+
ErrChannelIdMismatch,
|
|
22
|
+
ErrChannelNotFound,
|
|
23
|
+
ErrClaimSimulationFailed,
|
|
24
|
+
ErrClaimTransactionFailed,
|
|
25
|
+
ErrCumulativeAmountBelowClaimed,
|
|
26
|
+
ErrCumulativeExceedsBalance,
|
|
27
|
+
ErrDepositSimulationFailed,
|
|
28
|
+
ErrDepositTransactionFailed,
|
|
29
|
+
ErrEip2612AmountMismatch,
|
|
30
|
+
ErrErc20ApprovalUnavailable,
|
|
31
|
+
ErrErc3009AuthorizationRequired,
|
|
32
|
+
ErrInsufficientBalance,
|
|
33
|
+
ErrInvalidPayloadType,
|
|
34
|
+
ErrInvalidReceiveAuthorizationSignature,
|
|
35
|
+
ErrInvalidScheme,
|
|
36
|
+
ErrInvalidVoucherSignature,
|
|
37
|
+
ErrMissingEip712Domain,
|
|
38
|
+
ErrNetworkMismatch,
|
|
39
|
+
ErrNothingToSettle,
|
|
40
|
+
ErrPermit2AllowanceRequired,
|
|
41
|
+
ErrPermit2AmountMismatch,
|
|
42
|
+
ErrPermit2AuthorizationRequired,
|
|
43
|
+
ErrPermit2DeadlineExpired,
|
|
44
|
+
ErrPermit2InvalidSignature,
|
|
45
|
+
ErrPermit2InvalidSpender,
|
|
46
|
+
ErrRefundNoBalance,
|
|
47
|
+
ErrRefundSimulationFailed,
|
|
48
|
+
ErrRefundTransactionFailed,
|
|
49
|
+
ErrRpcReadFailed,
|
|
50
|
+
ErrSettleSimulationFailed,
|
|
51
|
+
ErrSettleTransactionFailed,
|
|
52
|
+
ErrTokenMismatch,
|
|
53
|
+
PERMIT2_DEPOSIT_COLLECTOR_ADDRESS,
|
|
54
|
+
batchPermit2WitnessTypes,
|
|
55
|
+
batchSettlementABI,
|
|
56
|
+
computeChannelId,
|
|
57
|
+
erc20BalanceOfABI,
|
|
58
|
+
receiveAuthorizationTypes
|
|
59
|
+
} from "../../chunk-HYABYUBD.mjs";
|
|
60
|
+
import {
|
|
61
|
+
isBatchSettlementClaimPayload,
|
|
62
|
+
isBatchSettlementDepositPayload,
|
|
63
|
+
isBatchSettlementEnrichedRefundPayload,
|
|
64
|
+
isBatchSettlementRefundPayload,
|
|
65
|
+
isBatchSettlementSettlePayload,
|
|
66
|
+
isBatchSettlementVoucherPayload
|
|
67
|
+
} from "../../chunk-U4HCGTLU.mjs";
|
|
68
|
+
import {
|
|
69
|
+
splitEip2612Signature,
|
|
70
|
+
validateEip2612PermitForPayment,
|
|
71
|
+
validateErc20ApprovalForPayment
|
|
72
|
+
} from "../../chunk-RWLVVO3B.mjs";
|
|
73
|
+
import {
|
|
74
|
+
ERC20_APPROVAL_GAS_SPONSORING_KEY,
|
|
75
|
+
extractEip2612GasSponsoringInfo,
|
|
76
|
+
extractErc20ApprovalGasSponsoringInfo,
|
|
77
|
+
resolveErc20ApprovalExtensionSigner
|
|
78
|
+
} from "../../chunk-GD4MKCN7.mjs";
|
|
79
|
+
import "../../chunk-TGFAVNUD.mjs";
|
|
80
|
+
import {
|
|
81
|
+
PERMIT2_ADDRESS,
|
|
82
|
+
erc20AllowanceAbi
|
|
83
|
+
} from "../../chunk-MACPBXCT.mjs";
|
|
84
|
+
import {
|
|
85
|
+
multicall
|
|
86
|
+
} from "../../chunk-VS3RYAYE.mjs";
|
|
87
|
+
import {
|
|
88
|
+
getEvmChainId
|
|
89
|
+
} from "../../chunk-TW7Z65AO.mjs";
|
|
90
|
+
|
|
91
|
+
// src/batch-settlement/facilitator/deposit.ts
|
|
92
|
+
import { getAddress as getAddress3 } from "viem";
|
|
93
|
+
|
|
94
|
+
// src/batch-settlement/facilitator/deposit-eip3009.ts
|
|
95
|
+
import { getAddress, parseErc6492Signature } from "viem";
|
|
96
|
+
function getEip3009DepositCollectorAddress() {
|
|
97
|
+
return getAddress(ERC3009_DEPOSIT_COLLECTOR_ADDRESS);
|
|
98
|
+
}
|
|
99
|
+
function buildEip3009DepositCollectorData(payload) {
|
|
100
|
+
const auth = payload.deposit.authorization.erc3009Authorization;
|
|
101
|
+
if (!auth) {
|
|
102
|
+
throw new Error(ErrErc3009AuthorizationRequired);
|
|
103
|
+
}
|
|
104
|
+
const { signature } = parseErc6492Signature(auth.signature);
|
|
105
|
+
return buildErc3009CollectorData(auth.validAfter, auth.validBefore, auth.salt, signature);
|
|
106
|
+
}
|
|
107
|
+
async function verifyEip3009DepositAuthorization(signer, payload, requirements, chainId) {
|
|
108
|
+
const { deposit, voucher } = payload;
|
|
109
|
+
const payer = payload.channelConfig.payer;
|
|
110
|
+
const auth = deposit.authorization.erc3009Authorization;
|
|
111
|
+
if (!auth) {
|
|
112
|
+
return { isValid: false, invalidReason: ErrErc3009AuthorizationRequired, payer };
|
|
113
|
+
}
|
|
114
|
+
const extra = requirements.extra;
|
|
115
|
+
if (!extra?.name || !extra?.version) {
|
|
116
|
+
return { isValid: false, invalidReason: ErrMissingEip712Domain, payer };
|
|
117
|
+
}
|
|
118
|
+
const validAfter = BigInt(auth.validAfter);
|
|
119
|
+
const validBefore = BigInt(auth.validBefore);
|
|
120
|
+
const timeInvalid = erc3009AuthorizationTimeInvalidReason(validAfter, validBefore);
|
|
121
|
+
if (timeInvalid) {
|
|
122
|
+
return { isValid: false, invalidReason: timeInvalid, payer };
|
|
123
|
+
}
|
|
124
|
+
const erc3009Nonce = buildErc3009DepositNonce(voucher.channelId, auth.salt);
|
|
125
|
+
const receiveAuthOk = await verifyReceiveAuth(signer, {
|
|
126
|
+
payer,
|
|
127
|
+
asset: requirements.asset,
|
|
128
|
+
name: extra.name,
|
|
129
|
+
version: extra.version,
|
|
130
|
+
chainId,
|
|
131
|
+
amount: deposit.amount,
|
|
132
|
+
validAfter,
|
|
133
|
+
validBefore,
|
|
134
|
+
nonce: erc3009Nonce,
|
|
135
|
+
signature: auth.signature
|
|
136
|
+
});
|
|
137
|
+
if (!receiveAuthOk) {
|
|
138
|
+
return { isValid: false, invalidReason: ErrInvalidReceiveAuthorizationSignature, payer };
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
async function verifyReceiveAuth(signer, params) {
|
|
143
|
+
try {
|
|
144
|
+
return await signer.verifyTypedData({
|
|
145
|
+
address: getAddress(params.payer),
|
|
146
|
+
domain: {
|
|
147
|
+
name: params.name,
|
|
148
|
+
version: params.version,
|
|
149
|
+
chainId: params.chainId,
|
|
150
|
+
verifyingContract: getAddress(params.asset)
|
|
151
|
+
},
|
|
152
|
+
types: receiveAuthorizationTypes,
|
|
153
|
+
primaryType: "ReceiveWithAuthorization",
|
|
154
|
+
message: {
|
|
155
|
+
from: getAddress(params.payer),
|
|
156
|
+
to: getAddress(ERC3009_DEPOSIT_COLLECTOR_ADDRESS),
|
|
157
|
+
value: BigInt(params.amount),
|
|
158
|
+
validAfter: params.validAfter,
|
|
159
|
+
validBefore: params.validBefore,
|
|
160
|
+
nonce: params.nonce
|
|
161
|
+
},
|
|
162
|
+
signature: params.signature
|
|
163
|
+
});
|
|
164
|
+
} catch {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/batch-settlement/facilitator/deposit-permit2.ts
|
|
170
|
+
import { encodeFunctionData, getAddress as getAddress2, parseErc6492Signature as parseErc6492Signature2 } from "viem";
|
|
171
|
+
function getPermit2DepositCollectorAddress() {
|
|
172
|
+
return getAddress2(PERMIT2_DEPOSIT_COLLECTOR_ADDRESS);
|
|
173
|
+
}
|
|
174
|
+
function buildPermit2DepositCollectorData(payload, eip2612PermitData = "0x") {
|
|
175
|
+
const auth = payload.deposit.authorization.permit2Authorization;
|
|
176
|
+
if (!auth) {
|
|
177
|
+
throw new Error(ErrPermit2AuthorizationRequired);
|
|
178
|
+
}
|
|
179
|
+
const { signature } = parseErc6492Signature2(auth.signature);
|
|
180
|
+
return buildPermit2CollectorData(auth.nonce, auth.deadline, signature, eip2612PermitData);
|
|
181
|
+
}
|
|
182
|
+
async function verifyPermit2DepositAuthorization(signer, payment, payload, requirements, chainId, context) {
|
|
183
|
+
const authResult = await verifyPermit2TypedData(signer, payload, requirements, chainId);
|
|
184
|
+
if (authResult) {
|
|
185
|
+
return authResult;
|
|
186
|
+
}
|
|
187
|
+
const branchResult = await resolvePermit2DepositBranch(
|
|
188
|
+
signer,
|
|
189
|
+
payment,
|
|
190
|
+
payload,
|
|
191
|
+
requirements,
|
|
192
|
+
context
|
|
193
|
+
);
|
|
194
|
+
if ("isValid" in branchResult) {
|
|
195
|
+
return branchResult;
|
|
196
|
+
}
|
|
197
|
+
if (branchResult.kind !== "erc20Approval" || !branchResult.extensionSigner.simulateTransactions) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
const ok = await branchResult.extensionSigner.simulateTransactions([
|
|
201
|
+
branchResult.signedTransaction,
|
|
202
|
+
buildDepositTransaction(payload, branchResult.collectorData)
|
|
203
|
+
]);
|
|
204
|
+
if (!ok) {
|
|
205
|
+
return {
|
|
206
|
+
isValid: false,
|
|
207
|
+
invalidReason: ErrDepositSimulationFailed,
|
|
208
|
+
payer: payload.channelConfig.payer
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
async function resolvePermit2DepositBranch(signer, payment, payload, requirements, context) {
|
|
214
|
+
const payer = payload.channelConfig.payer;
|
|
215
|
+
const tokenAddress = getAddress2(requirements.asset);
|
|
216
|
+
const eip2612Info = extractEip2612GasSponsoringInfo(payment);
|
|
217
|
+
if (eip2612Info) {
|
|
218
|
+
const result = validateBatchEip2612Permit(
|
|
219
|
+
eip2612Info,
|
|
220
|
+
payer,
|
|
221
|
+
tokenAddress,
|
|
222
|
+
payload.deposit.amount
|
|
223
|
+
);
|
|
224
|
+
if (!result.isValid) {
|
|
225
|
+
return { isValid: false, invalidReason: result.invalidReason, payer };
|
|
226
|
+
}
|
|
227
|
+
const { v, r, s } = splitEip2612Signature(eip2612Info.signature);
|
|
228
|
+
return {
|
|
229
|
+
kind: "eip2612",
|
|
230
|
+
collectorData: buildPermit2DepositCollectorData(
|
|
231
|
+
payload,
|
|
232
|
+
buildEip2612PermitData({
|
|
233
|
+
value: eip2612Info.amount,
|
|
234
|
+
deadline: eip2612Info.deadline,
|
|
235
|
+
v,
|
|
236
|
+
r,
|
|
237
|
+
s
|
|
238
|
+
})
|
|
239
|
+
)
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
const erc20Info = extractErc20ApprovalGasSponsoringInfo(payment);
|
|
243
|
+
if (erc20Info) {
|
|
244
|
+
const extensionSigner = resolveErc20ApprovalExtensionSigner(
|
|
245
|
+
context?.getExtension(
|
|
246
|
+
ERC20_APPROVAL_GAS_SPONSORING_KEY
|
|
247
|
+
),
|
|
248
|
+
requirements.network
|
|
249
|
+
);
|
|
250
|
+
if (!extensionSigner) {
|
|
251
|
+
return { isValid: false, invalidReason: ErrErc20ApprovalUnavailable, payer };
|
|
252
|
+
}
|
|
253
|
+
const result = await validateErc20ApprovalForPayment(erc20Info, payer, tokenAddress);
|
|
254
|
+
if (!result.isValid) {
|
|
255
|
+
return {
|
|
256
|
+
isValid: false,
|
|
257
|
+
invalidReason: result.invalidReason,
|
|
258
|
+
invalidMessage: result.invalidMessage,
|
|
259
|
+
payer
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
kind: "erc20Approval",
|
|
264
|
+
collectorData: buildPermit2DepositCollectorData(payload),
|
|
265
|
+
signedTransaction: erc20Info.signedTransaction,
|
|
266
|
+
extensionSigner
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
try {
|
|
270
|
+
const allowance = await signer.readContract({
|
|
271
|
+
address: tokenAddress,
|
|
272
|
+
abi: erc20AllowanceAbi,
|
|
273
|
+
functionName: "allowance",
|
|
274
|
+
args: [payer, PERMIT2_ADDRESS]
|
|
275
|
+
});
|
|
276
|
+
if (allowance < BigInt(payload.deposit.amount)) {
|
|
277
|
+
return { isValid: false, invalidReason: ErrPermit2AllowanceRequired, payer };
|
|
278
|
+
}
|
|
279
|
+
} catch {
|
|
280
|
+
return { isValid: false, invalidReason: ErrPermit2AllowanceRequired, payer };
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
kind: "standard",
|
|
284
|
+
collectorData: buildPermit2DepositCollectorData(payload)
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
function buildDepositTransaction(payload, collectorData) {
|
|
288
|
+
return {
|
|
289
|
+
to: getAddress2(BATCH_SETTLEMENT_ADDRESS),
|
|
290
|
+
data: encodeFunctionData({
|
|
291
|
+
abi: batchSettlementABI,
|
|
292
|
+
functionName: "deposit",
|
|
293
|
+
args: [
|
|
294
|
+
toContractChannelConfig(payload.channelConfig),
|
|
295
|
+
BigInt(payload.deposit.amount),
|
|
296
|
+
getPermit2DepositCollectorAddress(),
|
|
297
|
+
collectorData
|
|
298
|
+
]
|
|
299
|
+
}),
|
|
300
|
+
gas: 300000n
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
async function verifyPermit2TypedData(signer, payload, requirements, chainId) {
|
|
304
|
+
const auth = payload.deposit.authorization.permit2Authorization;
|
|
305
|
+
const payer = payload.channelConfig.payer;
|
|
306
|
+
if (!auth) {
|
|
307
|
+
return { isValid: false, invalidReason: ErrPermit2AuthorizationRequired, payer };
|
|
308
|
+
}
|
|
309
|
+
if (getAddress2(auth.from) !== getAddress2(payer)) {
|
|
310
|
+
return { isValid: false, invalidReason: ErrPermit2InvalidSignature, payer };
|
|
311
|
+
}
|
|
312
|
+
if (getAddress2(auth.spender) !== getPermit2DepositCollectorAddress()) {
|
|
313
|
+
return { isValid: false, invalidReason: ErrPermit2InvalidSpender, payer };
|
|
314
|
+
}
|
|
315
|
+
if (getAddress2(auth.permitted.token) !== getAddress2(requirements.asset)) {
|
|
316
|
+
return { isValid: false, invalidReason: ErrTokenMismatch, payer };
|
|
317
|
+
}
|
|
318
|
+
if (BigInt(auth.permitted.amount) !== BigInt(payload.deposit.amount)) {
|
|
319
|
+
return { isValid: false, invalidReason: ErrPermit2AmountMismatch, payer };
|
|
320
|
+
}
|
|
321
|
+
if (auth.witness.channelId !== payload.voucher.channelId) {
|
|
322
|
+
return { isValid: false, invalidReason: ErrChannelIdMismatch, payer };
|
|
323
|
+
}
|
|
324
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
325
|
+
if (BigInt(auth.deadline) < BigInt(now + 6)) {
|
|
326
|
+
return { isValid: false, invalidReason: ErrPermit2DeadlineExpired, payer };
|
|
327
|
+
}
|
|
328
|
+
try {
|
|
329
|
+
const ok = await signer.verifyTypedData({
|
|
330
|
+
address: getAddress2(auth.from),
|
|
331
|
+
domain: { name: "Permit2", chainId, verifyingContract: PERMIT2_ADDRESS },
|
|
332
|
+
types: batchPermit2WitnessTypes,
|
|
333
|
+
primaryType: "PermitWitnessTransferFrom",
|
|
334
|
+
message: {
|
|
335
|
+
permitted: {
|
|
336
|
+
token: getAddress2(auth.permitted.token),
|
|
337
|
+
amount: BigInt(auth.permitted.amount)
|
|
338
|
+
},
|
|
339
|
+
spender: getAddress2(auth.spender),
|
|
340
|
+
nonce: BigInt(auth.nonce),
|
|
341
|
+
deadline: BigInt(auth.deadline),
|
|
342
|
+
witness: {
|
|
343
|
+
channelId: auth.witness.channelId
|
|
344
|
+
}
|
|
345
|
+
},
|
|
346
|
+
signature: auth.signature
|
|
347
|
+
});
|
|
348
|
+
if (!ok) {
|
|
349
|
+
return { isValid: false, invalidReason: ErrPermit2InvalidSignature, payer };
|
|
350
|
+
}
|
|
351
|
+
} catch {
|
|
352
|
+
return { isValid: false, invalidReason: ErrPermit2InvalidSignature, payer };
|
|
353
|
+
}
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
function validateBatchEip2612Permit(info, payer, tokenAddress, depositAmount) {
|
|
357
|
+
const baseline = validateEip2612PermitForPayment(info, payer, tokenAddress);
|
|
358
|
+
if (!baseline.isValid) {
|
|
359
|
+
return {
|
|
360
|
+
isValid: false,
|
|
361
|
+
invalidReason: baseline.invalidReason ?? ErrInvalidPayloadType
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
if (BigInt(info.amount) !== BigInt(depositAmount)) {
|
|
365
|
+
return { isValid: false, invalidReason: ErrEip2612AmountMismatch };
|
|
366
|
+
}
|
|
367
|
+
return { isValid: true };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// src/batch-settlement/facilitator/deposit.ts
|
|
371
|
+
async function verifyDeposit(signer, payment, payload, requirements, context) {
|
|
372
|
+
const payer = payload.channelConfig.payer;
|
|
373
|
+
const chainId = getEvmChainId(requirements.network);
|
|
374
|
+
const configErr = validateChannelConfig(
|
|
375
|
+
payload.channelConfig,
|
|
376
|
+
payload.voucher.channelId,
|
|
377
|
+
requirements
|
|
378
|
+
);
|
|
379
|
+
if (configErr) {
|
|
380
|
+
return { isValid: false, invalidReason: configErr, payer };
|
|
381
|
+
}
|
|
382
|
+
const transferMethod = resolveDepositTransferMethod(payload, requirements);
|
|
383
|
+
if (transferMethod === "permit2" && !payload.deposit.authorization.permit2Authorization) {
|
|
384
|
+
return { isValid: false, invalidReason: ErrInvalidPayloadType, payer };
|
|
385
|
+
}
|
|
386
|
+
const methodErr = transferMethod === "permit2" ? await verifyPermit2DepositAuthorization(
|
|
387
|
+
signer,
|
|
388
|
+
payment,
|
|
389
|
+
payload,
|
|
390
|
+
requirements,
|
|
391
|
+
chainId,
|
|
392
|
+
context
|
|
393
|
+
) : await verifyEip3009DepositAuthorization(signer, payload, requirements, chainId);
|
|
394
|
+
if (methodErr) {
|
|
395
|
+
return methodErr;
|
|
396
|
+
}
|
|
397
|
+
const shared = await verifySharedDepositState(signer, payload, requirements);
|
|
398
|
+
if (!shared.ok) {
|
|
399
|
+
return shared.response;
|
|
400
|
+
}
|
|
401
|
+
const { depositAmount, chBalance, chTotalClaimed, wdInitiatedAt, refundNonceVal } = shared;
|
|
402
|
+
const execution = await resolveDepositExecution(signer, payment, payload, requirements, context);
|
|
403
|
+
if ("isValid" in execution) {
|
|
404
|
+
return execution;
|
|
405
|
+
}
|
|
406
|
+
if (!execution.skipDirectSimulation) {
|
|
407
|
+
try {
|
|
408
|
+
await signer.readContract({
|
|
409
|
+
address: getAddress3(BATCH_SETTLEMENT_ADDRESS),
|
|
410
|
+
abi: batchSettlementABI,
|
|
411
|
+
functionName: "deposit",
|
|
412
|
+
args: [
|
|
413
|
+
toContractChannelConfig(payload.channelConfig),
|
|
414
|
+
depositAmount,
|
|
415
|
+
execution.collector,
|
|
416
|
+
execution.collectorData
|
|
417
|
+
]
|
|
418
|
+
});
|
|
419
|
+
} catch (e) {
|
|
420
|
+
return {
|
|
421
|
+
isValid: false,
|
|
422
|
+
invalidReason: ErrDepositSimulationFailed,
|
|
423
|
+
invalidMessage: e instanceof Error ? e.message : String(e),
|
|
424
|
+
payer
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return {
|
|
429
|
+
isValid: true,
|
|
430
|
+
payer,
|
|
431
|
+
extra: {
|
|
432
|
+
channelId: payload.voucher.channelId,
|
|
433
|
+
balance: chBalance.toString(),
|
|
434
|
+
totalClaimed: chTotalClaimed.toString(),
|
|
435
|
+
withdrawRequestedAt: Number(wdInitiatedAt),
|
|
436
|
+
refundNonce: refundNonceVal.toString()
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
async function verifySharedDepositState(signer, payload, requirements) {
|
|
441
|
+
const { deposit, voucher } = payload;
|
|
442
|
+
const config = payload.channelConfig;
|
|
443
|
+
const payer = config.payer;
|
|
444
|
+
const chainId = getEvmChainId(requirements.network);
|
|
445
|
+
const configErr = validateChannelConfig(config, voucher.channelId, requirements);
|
|
446
|
+
if (configErr) {
|
|
447
|
+
return { ok: false, response: { isValid: false, invalidReason: configErr, payer } };
|
|
448
|
+
}
|
|
449
|
+
const voucherOk = await verifyBatchSettlementVoucherTypedData(
|
|
450
|
+
signer,
|
|
451
|
+
{
|
|
452
|
+
channelId: voucher.channelId,
|
|
453
|
+
maxClaimableAmount: voucher.maxClaimableAmount,
|
|
454
|
+
payerAuthorizer: config.payerAuthorizer,
|
|
455
|
+
payer: config.payer,
|
|
456
|
+
signature: voucher.signature
|
|
457
|
+
},
|
|
458
|
+
chainId
|
|
459
|
+
);
|
|
460
|
+
if (!voucherOk) {
|
|
461
|
+
return {
|
|
462
|
+
ok: false,
|
|
463
|
+
response: { isValid: false, invalidReason: ErrInvalidVoucherSignature, payer }
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
const mcResults = await multicall(signer.readContract.bind(signer), [
|
|
467
|
+
{
|
|
468
|
+
address: getAddress3(BATCH_SETTLEMENT_ADDRESS),
|
|
469
|
+
abi: batchSettlementABI,
|
|
470
|
+
functionName: "channels",
|
|
471
|
+
args: [voucher.channelId]
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
address: getAddress3(requirements.asset),
|
|
475
|
+
abi: erc20BalanceOfABI,
|
|
476
|
+
functionName: "balanceOf",
|
|
477
|
+
args: [getAddress3(payer)]
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
address: getAddress3(BATCH_SETTLEMENT_ADDRESS),
|
|
481
|
+
abi: batchSettlementABI,
|
|
482
|
+
functionName: "pendingWithdrawals",
|
|
483
|
+
args: [voucher.channelId]
|
|
484
|
+
},
|
|
485
|
+
{
|
|
486
|
+
address: getAddress3(BATCH_SETTLEMENT_ADDRESS),
|
|
487
|
+
abi: batchSettlementABI,
|
|
488
|
+
functionName: "refundNonce",
|
|
489
|
+
args: [voucher.channelId]
|
|
490
|
+
}
|
|
491
|
+
]);
|
|
492
|
+
const [chRes, balRes, wdRes, rnRes] = mcResults;
|
|
493
|
+
if (chRes.status === "failure" || balRes.status === "failure" || wdRes.status === "failure" || rnRes.status === "failure") {
|
|
494
|
+
return {
|
|
495
|
+
ok: false,
|
|
496
|
+
response: { isValid: false, invalidReason: ErrRpcReadFailed, payer }
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
const [chBalance, chTotalClaimed] = chRes.result;
|
|
500
|
+
const payerBalance = balRes.result;
|
|
501
|
+
const [, wdInitiatedAt] = wdRes.result;
|
|
502
|
+
const refundNonceVal = rnRes.result;
|
|
503
|
+
const depositAmount = BigInt(deposit.amount);
|
|
504
|
+
if (payerBalance < depositAmount) {
|
|
505
|
+
return {
|
|
506
|
+
ok: false,
|
|
507
|
+
response: { isValid: false, invalidReason: ErrInsufficientBalance, payer }
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
const effectiveBalance = chBalance + depositAmount;
|
|
511
|
+
const maxClaimableAmount = BigInt(voucher.maxClaimableAmount);
|
|
512
|
+
if (maxClaimableAmount > effectiveBalance) {
|
|
513
|
+
return {
|
|
514
|
+
ok: false,
|
|
515
|
+
response: { isValid: false, invalidReason: ErrCumulativeExceedsBalance, payer }
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
if (maxClaimableAmount <= chTotalClaimed) {
|
|
519
|
+
return {
|
|
520
|
+
ok: false,
|
|
521
|
+
response: { isValid: false, invalidReason: ErrCumulativeAmountBelowClaimed, payer }
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
return {
|
|
525
|
+
ok: true,
|
|
526
|
+
chainId,
|
|
527
|
+
depositAmount,
|
|
528
|
+
payer,
|
|
529
|
+
chBalance,
|
|
530
|
+
chTotalClaimed,
|
|
531
|
+
wdInitiatedAt,
|
|
532
|
+
refundNonceVal
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
async function settleDeposit(signer, payment, payload, requirements, context) {
|
|
536
|
+
const { deposit, voucher } = payload;
|
|
537
|
+
const config = payload.channelConfig;
|
|
538
|
+
const payer = config.payer;
|
|
539
|
+
const verified = await verifyDeposit(signer, payment, payload, requirements, context);
|
|
540
|
+
if (!verified.isValid) {
|
|
541
|
+
const reason = verified.invalidReason ?? ErrInvalidPayloadType;
|
|
542
|
+
return {
|
|
543
|
+
success: false,
|
|
544
|
+
errorReason: reason,
|
|
545
|
+
errorMessage: verified.invalidMessage ?? reason,
|
|
546
|
+
transaction: "",
|
|
547
|
+
network: requirements.network,
|
|
548
|
+
payer: verified.payer
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
try {
|
|
552
|
+
const execution = await resolveDepositExecution(
|
|
553
|
+
signer,
|
|
554
|
+
payment,
|
|
555
|
+
payload,
|
|
556
|
+
requirements,
|
|
557
|
+
context
|
|
558
|
+
);
|
|
559
|
+
if ("isValid" in execution) {
|
|
560
|
+
const reason = execution.invalidReason ?? ErrInvalidPayloadType;
|
|
561
|
+
return {
|
|
562
|
+
success: false,
|
|
563
|
+
errorReason: reason,
|
|
564
|
+
errorMessage: execution.invalidMessage ?? reason,
|
|
565
|
+
transaction: "",
|
|
566
|
+
network: requirements.network,
|
|
567
|
+
payer: execution.payer
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
const depositTx = buildDepositTransaction(payload, execution.collectorData);
|
|
571
|
+
const tx = execution.kind === "erc20Approval" ? (await execution.extensionSigner.sendTransactions([
|
|
572
|
+
execution.signedTransaction,
|
|
573
|
+
depositTx
|
|
574
|
+
]))[1] : await signer.writeContract({
|
|
575
|
+
address: getAddress3(BATCH_SETTLEMENT_ADDRESS),
|
|
576
|
+
abi: batchSettlementABI,
|
|
577
|
+
functionName: "deposit",
|
|
578
|
+
args: [
|
|
579
|
+
toContractChannelConfig(config),
|
|
580
|
+
BigInt(deposit.amount),
|
|
581
|
+
execution.collector,
|
|
582
|
+
execution.collectorData
|
|
583
|
+
]
|
|
584
|
+
});
|
|
585
|
+
const receipt = await signer.waitForTransactionReceipt({ hash: tx });
|
|
586
|
+
if (receipt.status !== "success") {
|
|
587
|
+
return {
|
|
588
|
+
success: false,
|
|
589
|
+
errorReason: ErrDepositTransactionFailed,
|
|
590
|
+
errorMessage: `transaction reverted (receipt status ${receipt.status})`,
|
|
591
|
+
transaction: tx,
|
|
592
|
+
network: requirements.network,
|
|
593
|
+
payer
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
const optimisticExtra = {
|
|
597
|
+
channelState: {
|
|
598
|
+
channelId: voucher.channelId,
|
|
599
|
+
balance: (BigInt(String(verified.extra?.balance ?? "0")) + BigInt(deposit.amount)).toString(),
|
|
600
|
+
totalClaimed: String(verified.extra?.totalClaimed ?? "0"),
|
|
601
|
+
withdrawRequestedAt: Number(verified.extra?.withdrawRequestedAt ?? 0),
|
|
602
|
+
refundNonce: String(verified.extra?.refundNonce ?? "0")
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
const expectedMinBalance = BigInt(optimisticExtra.channelState.balance);
|
|
606
|
+
const rpcDeadline = Date.now() + 2e3;
|
|
607
|
+
let postState = await readChannelState(signer, voucher.channelId);
|
|
608
|
+
while (postState.balance < expectedMinBalance && Date.now() < rpcDeadline) {
|
|
609
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
610
|
+
postState = await readChannelState(signer, voucher.channelId);
|
|
611
|
+
}
|
|
612
|
+
const rpcCaughtUp = postState.balance >= expectedMinBalance;
|
|
613
|
+
return {
|
|
614
|
+
success: true,
|
|
615
|
+
transaction: tx,
|
|
616
|
+
network: requirements.network,
|
|
617
|
+
payer,
|
|
618
|
+
amount: deposit.amount,
|
|
619
|
+
extra: rpcCaughtUp ? {
|
|
620
|
+
...optimisticExtra,
|
|
621
|
+
channelState: {
|
|
622
|
+
channelId: voucher.channelId,
|
|
623
|
+
balance: postState.balance.toString(),
|
|
624
|
+
totalClaimed: postState.totalClaimed.toString(),
|
|
625
|
+
withdrawRequestedAt: postState.withdrawRequestedAt,
|
|
626
|
+
refundNonce: postState.refundNonce.toString()
|
|
627
|
+
}
|
|
628
|
+
} : optimisticExtra
|
|
629
|
+
};
|
|
630
|
+
} catch (e) {
|
|
631
|
+
return {
|
|
632
|
+
success: false,
|
|
633
|
+
errorReason: ErrDepositTransactionFailed,
|
|
634
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
635
|
+
transaction: "",
|
|
636
|
+
network: requirements.network,
|
|
637
|
+
payer
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
async function resolveDepositExecution(signer, payment, payload, requirements, context) {
|
|
642
|
+
const transferMethod = resolveDepositTransferMethod(payload, requirements);
|
|
643
|
+
if (transferMethod === "eip3009") {
|
|
644
|
+
return {
|
|
645
|
+
kind: "direct",
|
|
646
|
+
collector: getEip3009DepositCollectorAddress(),
|
|
647
|
+
collectorData: buildEip3009DepositCollectorData(payload)
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
const branch = await resolvePermit2DepositBranch(signer, payment, payload, requirements, context);
|
|
651
|
+
if ("isValid" in branch) {
|
|
652
|
+
return branch;
|
|
653
|
+
}
|
|
654
|
+
if (branch.kind === "erc20Approval") {
|
|
655
|
+
return {
|
|
656
|
+
kind: "erc20Approval",
|
|
657
|
+
collector: getPermit2DepositCollectorAddress(),
|
|
658
|
+
collectorData: branch.collectorData,
|
|
659
|
+
signedTransaction: branch.signedTransaction,
|
|
660
|
+
extensionSigner: branch.extensionSigner,
|
|
661
|
+
skipDirectSimulation: true
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
return {
|
|
665
|
+
kind: "direct",
|
|
666
|
+
collector: getPermit2DepositCollectorAddress(),
|
|
667
|
+
collectorData: branch.collectorData
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
function resolveDepositTransferMethod(payload, requirements) {
|
|
671
|
+
const hinted = requirements.extra?.assetTransferMethod;
|
|
672
|
+
if (hinted) {
|
|
673
|
+
return hinted;
|
|
674
|
+
}
|
|
675
|
+
return payload.deposit.authorization.permit2Authorization ? "permit2" : "eip3009";
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// src/batch-settlement/facilitator/voucher.ts
|
|
679
|
+
async function verifyVoucher(signer, payload, requirements, channelConfig) {
|
|
680
|
+
const { voucher } = payload;
|
|
681
|
+
const channelId = voucher.channelId;
|
|
682
|
+
const chainId = getEvmChainId(requirements.network);
|
|
683
|
+
const configErr = validateChannelConfig(channelConfig, channelId, requirements);
|
|
684
|
+
if (configErr) {
|
|
685
|
+
return { isValid: false, invalidReason: configErr, payer: channelConfig.payer };
|
|
686
|
+
}
|
|
687
|
+
const voucherOk = await verifyBatchSettlementVoucherTypedData(
|
|
688
|
+
signer,
|
|
689
|
+
{
|
|
690
|
+
channelId,
|
|
691
|
+
maxClaimableAmount: voucher.maxClaimableAmount,
|
|
692
|
+
payerAuthorizer: channelConfig.payerAuthorizer,
|
|
693
|
+
payer: channelConfig.payer,
|
|
694
|
+
signature: voucher.signature
|
|
695
|
+
},
|
|
696
|
+
chainId
|
|
697
|
+
);
|
|
698
|
+
if (!voucherOk) {
|
|
699
|
+
return {
|
|
700
|
+
isValid: false,
|
|
701
|
+
invalidReason: ErrInvalidVoucherSignature,
|
|
702
|
+
payer: channelConfig.payer
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
const state = await readChannelState(signer, channelId);
|
|
706
|
+
if (state.balance === 0n) {
|
|
707
|
+
return { isValid: false, invalidReason: ErrChannelNotFound, payer: channelConfig.payer };
|
|
708
|
+
}
|
|
709
|
+
const maxClaimableAmount = BigInt(voucher.maxClaimableAmount);
|
|
710
|
+
if (maxClaimableAmount > state.balance) {
|
|
711
|
+
return {
|
|
712
|
+
isValid: false,
|
|
713
|
+
invalidReason: ErrCumulativeExceedsBalance,
|
|
714
|
+
payer: channelConfig.payer
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
const belowClaimed = payload.type === "refund" ? maxClaimableAmount < state.totalClaimed : maxClaimableAmount <= state.totalClaimed;
|
|
718
|
+
if (belowClaimed) {
|
|
719
|
+
return {
|
|
720
|
+
isValid: false,
|
|
721
|
+
invalidReason: ErrCumulativeAmountBelowClaimed,
|
|
722
|
+
payer: channelConfig.payer
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
return {
|
|
726
|
+
isValid: true,
|
|
727
|
+
payer: channelConfig.payer,
|
|
728
|
+
extra: {
|
|
729
|
+
channelId,
|
|
730
|
+
balance: state.balance.toString(),
|
|
731
|
+
totalClaimed: state.totalClaimed.toString(),
|
|
732
|
+
withdrawRequestedAt: state.withdrawRequestedAt,
|
|
733
|
+
refundNonce: state.refundNonce.toString()
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// src/batch-settlement/facilitator/claim.ts
|
|
739
|
+
import { getAddress as getAddress4 } from "viem";
|
|
740
|
+
function buildVoucherClaimArgs(claims) {
|
|
741
|
+
return claims.map((c) => ({
|
|
742
|
+
voucher: {
|
|
743
|
+
channel: toContractChannelConfig(c.voucher.channel),
|
|
744
|
+
maxClaimableAmount: BigInt(c.voucher.maxClaimableAmount)
|
|
745
|
+
},
|
|
746
|
+
signature: c.signature,
|
|
747
|
+
totalClaimed: BigInt(c.totalClaimed)
|
|
748
|
+
}));
|
|
749
|
+
}
|
|
750
|
+
async function executeClaimWithSignature(signer, payload, requirements, authorizerSigner) {
|
|
751
|
+
const network = requirements.network;
|
|
752
|
+
const claimArgs = buildVoucherClaimArgs(payload.claims);
|
|
753
|
+
let sig = payload.claimAuthorizerSignature;
|
|
754
|
+
if (!sig) {
|
|
755
|
+
for (const claim of payload.claims) {
|
|
756
|
+
if (getAddress4(claim.voucher.channel.receiverAuthorizer) !== getAddress4(authorizerSigner.address)) {
|
|
757
|
+
return {
|
|
758
|
+
success: false,
|
|
759
|
+
errorReason: ErrAuthorizerAddressMismatch,
|
|
760
|
+
transaction: "",
|
|
761
|
+
network
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
sig = await signClaimBatch(authorizerSigner, payload.claims, network);
|
|
766
|
+
}
|
|
767
|
+
try {
|
|
768
|
+
await signer.readContract({
|
|
769
|
+
address: getAddress4(BATCH_SETTLEMENT_ADDRESS),
|
|
770
|
+
abi: batchSettlementABI,
|
|
771
|
+
functionName: "claimWithSignature",
|
|
772
|
+
args: [claimArgs, sig]
|
|
773
|
+
});
|
|
774
|
+
} catch (e) {
|
|
775
|
+
return {
|
|
776
|
+
success: false,
|
|
777
|
+
errorReason: ErrClaimSimulationFailed,
|
|
778
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
779
|
+
transaction: "",
|
|
780
|
+
network
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
try {
|
|
784
|
+
const tx = await signer.writeContract({
|
|
785
|
+
address: getAddress4(BATCH_SETTLEMENT_ADDRESS),
|
|
786
|
+
abi: batchSettlementABI,
|
|
787
|
+
functionName: "claimWithSignature",
|
|
788
|
+
args: [claimArgs, sig]
|
|
789
|
+
});
|
|
790
|
+
const receipt = await signer.waitForTransactionReceipt({ hash: tx });
|
|
791
|
+
if (receipt.status !== "success") {
|
|
792
|
+
return {
|
|
793
|
+
success: false,
|
|
794
|
+
errorReason: ErrClaimTransactionFailed,
|
|
795
|
+
errorMessage: `transaction reverted (receipt status ${receipt.status})`,
|
|
796
|
+
transaction: tx,
|
|
797
|
+
network
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
return {
|
|
801
|
+
success: true,
|
|
802
|
+
transaction: tx,
|
|
803
|
+
network,
|
|
804
|
+
amount: ""
|
|
805
|
+
};
|
|
806
|
+
} catch (e) {
|
|
807
|
+
return {
|
|
808
|
+
success: false,
|
|
809
|
+
errorReason: ErrClaimTransactionFailed,
|
|
810
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
811
|
+
transaction: "",
|
|
812
|
+
network
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
// src/batch-settlement/facilitator/settle.ts
|
|
818
|
+
import { getAddress as getAddress5, isAddressEqual, parseEventLogs } from "viem";
|
|
819
|
+
async function executeSettle(signer, payload, requirements) {
|
|
820
|
+
const network = requirements.network;
|
|
821
|
+
const contractAddr = getAddress5(BATCH_SETTLEMENT_ADDRESS);
|
|
822
|
+
const receiver = getAddress5(payload.receiver);
|
|
823
|
+
const token = getAddress5(payload.token);
|
|
824
|
+
try {
|
|
825
|
+
const [totalClaimed, totalSettled] = await signer.readContract({
|
|
826
|
+
address: contractAddr,
|
|
827
|
+
abi: batchSettlementABI,
|
|
828
|
+
functionName: "receivers",
|
|
829
|
+
args: [receiver, token]
|
|
830
|
+
});
|
|
831
|
+
if (totalClaimed <= totalSettled) {
|
|
832
|
+
return {
|
|
833
|
+
success: false,
|
|
834
|
+
errorReason: ErrNothingToSettle,
|
|
835
|
+
errorMessage: "nothing to settle for receiver and token",
|
|
836
|
+
transaction: "",
|
|
837
|
+
network
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
} catch (e) {
|
|
841
|
+
return {
|
|
842
|
+
success: false,
|
|
843
|
+
errorReason: ErrRpcReadFailed,
|
|
844
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
845
|
+
transaction: "",
|
|
846
|
+
network
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
try {
|
|
850
|
+
await signer.readContract({
|
|
851
|
+
address: contractAddr,
|
|
852
|
+
abi: batchSettlementABI,
|
|
853
|
+
functionName: "settle",
|
|
854
|
+
args: [receiver, token]
|
|
855
|
+
});
|
|
856
|
+
} catch (e) {
|
|
857
|
+
return {
|
|
858
|
+
success: false,
|
|
859
|
+
errorReason: ErrSettleSimulationFailed,
|
|
860
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
861
|
+
transaction: "",
|
|
862
|
+
network
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
try {
|
|
866
|
+
const tx = await signer.writeContract({
|
|
867
|
+
address: contractAddr,
|
|
868
|
+
abi: batchSettlementABI,
|
|
869
|
+
functionName: "settle",
|
|
870
|
+
args: [receiver, token]
|
|
871
|
+
});
|
|
872
|
+
const receipt = await signer.waitForTransactionReceipt({ hash: tx });
|
|
873
|
+
if (receipt.status !== "success") {
|
|
874
|
+
return {
|
|
875
|
+
success: false,
|
|
876
|
+
errorReason: ErrSettleTransactionFailed,
|
|
877
|
+
errorMessage: `transaction reverted (receipt status ${receipt.status})`,
|
|
878
|
+
transaction: tx,
|
|
879
|
+
network
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
let amount = "";
|
|
883
|
+
if (receipt.logs) {
|
|
884
|
+
const logs = parseEventLogs({
|
|
885
|
+
abi: batchSettlementABI,
|
|
886
|
+
eventName: "Settled",
|
|
887
|
+
logs: receipt.logs.filter((log) => isAddressEqual(log.address, contractAddr))
|
|
888
|
+
});
|
|
889
|
+
const settledLog = logs.find(
|
|
890
|
+
(log) => isAddressEqual(log.args.receiver, receiver) && isAddressEqual(log.args.token, token)
|
|
891
|
+
);
|
|
892
|
+
amount = settledLog?.args.amount.toString() ?? "0";
|
|
893
|
+
}
|
|
894
|
+
return {
|
|
895
|
+
success: true,
|
|
896
|
+
transaction: tx,
|
|
897
|
+
network,
|
|
898
|
+
amount
|
|
899
|
+
};
|
|
900
|
+
} catch (e) {
|
|
901
|
+
return {
|
|
902
|
+
success: false,
|
|
903
|
+
errorReason: ErrSettleTransactionFailed,
|
|
904
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
905
|
+
transaction: "",
|
|
906
|
+
network
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// src/batch-settlement/facilitator/refund.ts
|
|
912
|
+
import { encodeFunctionData as encodeFunctionData2, getAddress as getAddress6 } from "viem";
|
|
913
|
+
var REFUND_STATE_POLL_MS = 2e3;
|
|
914
|
+
var REFUND_STATE_POLL_INTERVAL_MS = 150;
|
|
915
|
+
function getRefundableAmount(payload, preState, channelId, network) {
|
|
916
|
+
const postClaimTotalClaimed = payload.claims.reduce((max, claim) => {
|
|
917
|
+
const claimChannelId = computeChannelId(claim.voucher.channel, network);
|
|
918
|
+
if (claimChannelId.toLowerCase() !== channelId.toLowerCase()) {
|
|
919
|
+
return max;
|
|
920
|
+
}
|
|
921
|
+
const totalClaimed = BigInt(claim.totalClaimed);
|
|
922
|
+
return totalClaimed > max ? totalClaimed : max;
|
|
923
|
+
}, preState.totalClaimed);
|
|
924
|
+
if (postClaimTotalClaimed > preState.balance) {
|
|
925
|
+
return null;
|
|
926
|
+
}
|
|
927
|
+
const requestedAmount = BigInt(payload.amount);
|
|
928
|
+
if (requestedAmount === 0n) {
|
|
929
|
+
return null;
|
|
930
|
+
}
|
|
931
|
+
const available = preState.balance - postClaimTotalClaimed;
|
|
932
|
+
return requestedAmount > available ? available : requestedAmount;
|
|
933
|
+
}
|
|
934
|
+
function buildRefundExtra(payload, channelId, preState) {
|
|
935
|
+
const preTotalClaimed = preState?.totalClaimed ?? 0n;
|
|
936
|
+
const preBalance = preState?.balance ?? 0n;
|
|
937
|
+
const lastClaimTotal = payload.claims.length > 0 ? BigInt(payload.claims[payload.claims.length - 1].totalClaimed) : preTotalClaimed;
|
|
938
|
+
const postClaimTotalClaimed = lastClaimTotal > preTotalClaimed ? lastClaimTotal : preTotalClaimed;
|
|
939
|
+
const available = preBalance - postClaimTotalClaimed;
|
|
940
|
+
const requestedAmount = BigInt(payload.amount);
|
|
941
|
+
const actualRefund = requestedAmount > available ? available : requestedAmount;
|
|
942
|
+
return {
|
|
943
|
+
amount: actualRefund.toString(),
|
|
944
|
+
extra: {
|
|
945
|
+
channelState: {
|
|
946
|
+
channelId,
|
|
947
|
+
balance: (preBalance - actualRefund).toString(),
|
|
948
|
+
totalClaimed: postClaimTotalClaimed.toString(),
|
|
949
|
+
withdrawRequestedAt: 0,
|
|
950
|
+
refundNonce: String((preState?.refundNonce ?? 0n) + 1n)
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
}
|
|
955
|
+
async function readPostRefundState(signer, channelId, submittedNonce) {
|
|
956
|
+
const expectedNonce = BigInt(submittedNonce) + 1n;
|
|
957
|
+
const deadline = Date.now() + REFUND_STATE_POLL_MS;
|
|
958
|
+
do {
|
|
959
|
+
let state;
|
|
960
|
+
try {
|
|
961
|
+
state = await readChannelState(signer, channelId);
|
|
962
|
+
} catch {
|
|
963
|
+
return null;
|
|
964
|
+
}
|
|
965
|
+
if (state.refundNonce >= expectedNonce) {
|
|
966
|
+
return state;
|
|
967
|
+
}
|
|
968
|
+
await new Promise((resolve) => setTimeout(resolve, REFUND_STATE_POLL_INTERVAL_MS));
|
|
969
|
+
} while (Date.now() < deadline);
|
|
970
|
+
return null;
|
|
971
|
+
}
|
|
972
|
+
function buildRefundExtraFromPostState(channelId, preState, postState) {
|
|
973
|
+
const actualRefund = preState.balance > postState.balance ? preState.balance - postState.balance : 0n;
|
|
974
|
+
return {
|
|
975
|
+
amount: actualRefund.toString(),
|
|
976
|
+
extra: {
|
|
977
|
+
channelState: {
|
|
978
|
+
channelId,
|
|
979
|
+
balance: postState.balance.toString(),
|
|
980
|
+
totalClaimed: postState.totalClaimed.toString(),
|
|
981
|
+
withdrawRequestedAt: postState.withdrawRequestedAt,
|
|
982
|
+
refundNonce: postState.refundNonce.toString()
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
async function executeRefundWithSignature(signer, payload, requirements, authorizerSigner) {
|
|
988
|
+
const network = requirements.network;
|
|
989
|
+
try {
|
|
990
|
+
const channelId = computeChannelId(payload.channelConfig, network);
|
|
991
|
+
const preState = await readChannelState(signer, channelId);
|
|
992
|
+
const contractAddr = getAddress6(BATCH_SETTLEMENT_ADDRESS);
|
|
993
|
+
const refundableAmount = getRefundableAmount(payload, preState, channelId, network);
|
|
994
|
+
if (refundableAmount === 0n) {
|
|
995
|
+
return {
|
|
996
|
+
success: false,
|
|
997
|
+
errorReason: ErrRefundNoBalance,
|
|
998
|
+
errorMessage: "Nothing to refund",
|
|
999
|
+
transaction: "",
|
|
1000
|
+
network
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
const hasClientSig = payload.refundAuthorizerSignature !== void 0;
|
|
1004
|
+
const authorizerMismatch = getAddress6(payload.channelConfig.receiverAuthorizer) !== getAddress6(authorizerSigner.address);
|
|
1005
|
+
if (!hasClientSig && authorizerMismatch) {
|
|
1006
|
+
return {
|
|
1007
|
+
success: false,
|
|
1008
|
+
errorReason: ErrAuthorizerAddressMismatch,
|
|
1009
|
+
transaction: "",
|
|
1010
|
+
network
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
const refundSig = payload.refundAuthorizerSignature ?? await signRefund(authorizerSigner, channelId, payload.amount, payload.refundNonce, network);
|
|
1014
|
+
const refundCalldata = encodeFunctionData2({
|
|
1015
|
+
abi: batchSettlementABI,
|
|
1016
|
+
functionName: "refundWithSignature",
|
|
1017
|
+
args: [
|
|
1018
|
+
toContractChannelConfig(payload.channelConfig),
|
|
1019
|
+
BigInt(payload.amount),
|
|
1020
|
+
BigInt(payload.refundNonce),
|
|
1021
|
+
refundSig
|
|
1022
|
+
]
|
|
1023
|
+
});
|
|
1024
|
+
let tx;
|
|
1025
|
+
if (payload.claims.length > 0) {
|
|
1026
|
+
let claimSig = payload.claimAuthorizerSignature;
|
|
1027
|
+
if (!claimSig) {
|
|
1028
|
+
claimSig = await signClaimBatch(authorizerSigner, payload.claims, network);
|
|
1029
|
+
}
|
|
1030
|
+
const claimCalldata = encodeFunctionData2({
|
|
1031
|
+
abi: batchSettlementABI,
|
|
1032
|
+
functionName: "claimWithSignature",
|
|
1033
|
+
args: [buildVoucherClaimArgs(payload.claims), claimSig]
|
|
1034
|
+
});
|
|
1035
|
+
try {
|
|
1036
|
+
await signer.readContract({
|
|
1037
|
+
address: contractAddr,
|
|
1038
|
+
abi: batchSettlementABI,
|
|
1039
|
+
functionName: "multicall",
|
|
1040
|
+
args: [[claimCalldata, refundCalldata]]
|
|
1041
|
+
});
|
|
1042
|
+
} catch (e) {
|
|
1043
|
+
return {
|
|
1044
|
+
success: false,
|
|
1045
|
+
errorReason: ErrRefundSimulationFailed,
|
|
1046
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
1047
|
+
transaction: "",
|
|
1048
|
+
network
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
tx = await signer.writeContract({
|
|
1052
|
+
address: contractAddr,
|
|
1053
|
+
abi: batchSettlementABI,
|
|
1054
|
+
functionName: "multicall",
|
|
1055
|
+
args: [[claimCalldata, refundCalldata]]
|
|
1056
|
+
});
|
|
1057
|
+
} else {
|
|
1058
|
+
try {
|
|
1059
|
+
await signer.readContract({
|
|
1060
|
+
address: contractAddr,
|
|
1061
|
+
abi: batchSettlementABI,
|
|
1062
|
+
functionName: "refundWithSignature",
|
|
1063
|
+
args: [
|
|
1064
|
+
toContractChannelConfig(payload.channelConfig),
|
|
1065
|
+
BigInt(payload.amount),
|
|
1066
|
+
BigInt(payload.refundNonce),
|
|
1067
|
+
refundSig
|
|
1068
|
+
]
|
|
1069
|
+
});
|
|
1070
|
+
} catch (e) {
|
|
1071
|
+
return {
|
|
1072
|
+
success: false,
|
|
1073
|
+
errorReason: ErrRefundSimulationFailed,
|
|
1074
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
1075
|
+
transaction: "",
|
|
1076
|
+
network
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
tx = await signer.writeContract({
|
|
1080
|
+
address: contractAddr,
|
|
1081
|
+
abi: batchSettlementABI,
|
|
1082
|
+
functionName: "refundWithSignature",
|
|
1083
|
+
args: [
|
|
1084
|
+
toContractChannelConfig(payload.channelConfig),
|
|
1085
|
+
BigInt(payload.amount),
|
|
1086
|
+
BigInt(payload.refundNonce),
|
|
1087
|
+
refundSig
|
|
1088
|
+
]
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
const receipt = await signer.waitForTransactionReceipt({ hash: tx });
|
|
1092
|
+
if (receipt.status !== "success") {
|
|
1093
|
+
return {
|
|
1094
|
+
success: false,
|
|
1095
|
+
errorReason: ErrRefundTransactionFailed,
|
|
1096
|
+
errorMessage: `transaction reverted (receipt status ${receipt.status})`,
|
|
1097
|
+
transaction: tx,
|
|
1098
|
+
network
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
const postState = preState && preState.withdrawRequestedAt !== 0 ? await readPostRefundState(signer, channelId, payload.refundNonce) : null;
|
|
1102
|
+
const refundDetails = preState && postState ? buildRefundExtraFromPostState(channelId, preState, postState) : buildRefundExtra(payload, channelId, preState);
|
|
1103
|
+
return {
|
|
1104
|
+
success: true,
|
|
1105
|
+
transaction: tx,
|
|
1106
|
+
network,
|
|
1107
|
+
payer: payload.channelConfig.payer,
|
|
1108
|
+
amount: refundDetails.amount,
|
|
1109
|
+
extra: refundDetails.extra
|
|
1110
|
+
};
|
|
1111
|
+
} catch (e) {
|
|
1112
|
+
return {
|
|
1113
|
+
success: false,
|
|
1114
|
+
errorReason: ErrRefundTransactionFailed,
|
|
1115
|
+
errorMessage: e instanceof Error ? e.message : String(e),
|
|
1116
|
+
transaction: "",
|
|
1117
|
+
network
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
// src/batch-settlement/facilitator/scheme.ts
|
|
1123
|
+
var BatchSettlementEvmScheme = class {
|
|
1124
|
+
/**
|
|
1125
|
+
* Creates a facilitator scheme for verifying and settling batch-settlement payments.
|
|
1126
|
+
*
|
|
1127
|
+
* @param signer - Facilitator EVM signer(s) used for tx submission and onchain reads.
|
|
1128
|
+
* @param authorizerSigner - Dedicated key that provides EIP-712 signatures for
|
|
1129
|
+
* `claimWithSignature` / `refundWithSignature`. The facilitator will sign missing
|
|
1130
|
+
* authorizer signatures using this key when the server omits them.
|
|
1131
|
+
*/
|
|
1132
|
+
constructor(signer, authorizerSigner) {
|
|
1133
|
+
this.signer = signer;
|
|
1134
|
+
this.authorizerSigner = authorizerSigner;
|
|
1135
|
+
this.scheme = BATCH_SETTLEMENT_SCHEME;
|
|
1136
|
+
this.caipFamily = "eip155:*";
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* Returns facilitator-specific extra fields to be merged into payment requirements.
|
|
1140
|
+
*
|
|
1141
|
+
* Exposes the configured `receiverAuthorizer` address so the server and client can
|
|
1142
|
+
* embed it in `ChannelConfig`.
|
|
1143
|
+
*
|
|
1144
|
+
* @param _ - Network identifier (unused).
|
|
1145
|
+
* @returns Extra fields containing `receiverAuthorizer`.
|
|
1146
|
+
*/
|
|
1147
|
+
getExtra(_) {
|
|
1148
|
+
return { receiverAuthorizer: this.authorizerSigner.address };
|
|
1149
|
+
}
|
|
1150
|
+
/**
|
|
1151
|
+
* Returns all facilitator signer addresses available for the given network.
|
|
1152
|
+
*
|
|
1153
|
+
* @param _ - Network identifier (unused).
|
|
1154
|
+
* @returns Array of hex addresses.
|
|
1155
|
+
*/
|
|
1156
|
+
getSigners(_) {
|
|
1157
|
+
return [...this.signer.getAddresses()];
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Verifies a payment payload (deposit or voucher) without executing settlement.
|
|
1161
|
+
*
|
|
1162
|
+
* @param payload - The x402 payment payload envelope.
|
|
1163
|
+
* @param requirements - Server payment requirements (scheme, network, asset, amount).
|
|
1164
|
+
* @param context - Optional facilitator extension context.
|
|
1165
|
+
* @returns A {@link VerifyResponse} indicating validity with payer and channel state in `extra`.
|
|
1166
|
+
*/
|
|
1167
|
+
async verify(payload, requirements, context) {
|
|
1168
|
+
const rawPayload = payload.payload;
|
|
1169
|
+
if (payload.accepted.scheme !== BATCH_SETTLEMENT_SCHEME || requirements.scheme !== BATCH_SETTLEMENT_SCHEME) {
|
|
1170
|
+
return { isValid: false, invalidReason: ErrInvalidScheme };
|
|
1171
|
+
}
|
|
1172
|
+
if (payload.accepted.network !== requirements.network) {
|
|
1173
|
+
return { isValid: false, invalidReason: ErrNetworkMismatch };
|
|
1174
|
+
}
|
|
1175
|
+
if (isBatchSettlementDepositPayload(rawPayload)) {
|
|
1176
|
+
return verifyDeposit(this.signer, payload, rawPayload, requirements, context);
|
|
1177
|
+
}
|
|
1178
|
+
if (isBatchSettlementVoucherPayload(rawPayload)) {
|
|
1179
|
+
return verifyVoucher(this.signer, rawPayload, requirements, rawPayload.channelConfig);
|
|
1180
|
+
}
|
|
1181
|
+
if (isBatchSettlementRefundPayload(rawPayload)) {
|
|
1182
|
+
return verifyVoucher(this.signer, rawPayload, requirements, rawPayload.channelConfig);
|
|
1183
|
+
}
|
|
1184
|
+
return { isValid: false, invalidReason: ErrInvalidPayloadType };
|
|
1185
|
+
}
|
|
1186
|
+
/**
|
|
1187
|
+
* Executes settlement for a payment payload.
|
|
1188
|
+
*
|
|
1189
|
+
* Dispatches to the correct handler based on payload settle action:
|
|
1190
|
+
* - `deposit` → onchain `deposit(config, amount, collector, collectorData)`
|
|
1191
|
+
* - `claim` → onchain `claimWithSignature(VoucherClaim[], bytes)`
|
|
1192
|
+
* - `settle` → onchain `settle(receiver, token)`
|
|
1193
|
+
* - `refund` → optional claim + onchain `refundWithSignature(config, amount, nonce, sig)`
|
|
1194
|
+
*
|
|
1195
|
+
* @param payload - The x402 payment payload envelope.
|
|
1196
|
+
* @param requirements - Server payment requirements.
|
|
1197
|
+
* @param context - Optional facilitator extension context.
|
|
1198
|
+
* @returns A {@link SettleResponse} with the transaction hash on success.
|
|
1199
|
+
*/
|
|
1200
|
+
async settle(payload, requirements, context) {
|
|
1201
|
+
const rawPayload = payload.payload;
|
|
1202
|
+
if (isBatchSettlementDepositPayload(rawPayload)) {
|
|
1203
|
+
return settleDeposit(this.signer, payload, rawPayload, requirements, context);
|
|
1204
|
+
}
|
|
1205
|
+
if (isBatchSettlementClaimPayload(rawPayload)) {
|
|
1206
|
+
return executeClaimWithSignature(
|
|
1207
|
+
this.signer,
|
|
1208
|
+
rawPayload,
|
|
1209
|
+
requirements,
|
|
1210
|
+
this.authorizerSigner
|
|
1211
|
+
);
|
|
1212
|
+
}
|
|
1213
|
+
if (isBatchSettlementEnrichedRefundPayload(rawPayload)) {
|
|
1214
|
+
return executeRefundWithSignature(
|
|
1215
|
+
this.signer,
|
|
1216
|
+
rawPayload,
|
|
1217
|
+
requirements,
|
|
1218
|
+
this.authorizerSigner
|
|
1219
|
+
);
|
|
1220
|
+
}
|
|
1221
|
+
if (isBatchSettlementSettlePayload(rawPayload)) {
|
|
1222
|
+
return executeSettle(this.signer, rawPayload, requirements);
|
|
1223
|
+
}
|
|
1224
|
+
return {
|
|
1225
|
+
success: false,
|
|
1226
|
+
errorReason: ErrInvalidPayloadType,
|
|
1227
|
+
transaction: "",
|
|
1228
|
+
network: requirements.network
|
|
1229
|
+
};
|
|
1230
|
+
}
|
|
1231
|
+
};
|
|
1232
|
+
export {
|
|
1233
|
+
BatchSettlementEvmScheme
|
|
1234
|
+
};
|
|
1235
|
+
//# sourceMappingURL=index.mjs.map
|