uvd-x402-sdk 2.46.0 → 2.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -0
- package/dist/adapters/index.d.mts +3 -3
- package/dist/adapters/index.d.ts +3 -3
- package/dist/backend/index.d.mts +2 -2
- package/dist/backend/index.d.ts +2 -2
- package/dist/backend/index.js +7 -1
- package/dist/backend/index.js.map +1 -1
- package/dist/backend/index.mjs +7 -1
- package/dist/backend/index.mjs.map +1 -1
- package/dist/{index-DgChnK_c.d.mts → index-CVW8AhRX.d.mts} +1 -1
- package/dist/{index-DgChnK_c.d.ts → index-CVW8AhRX.d.ts} +1 -1
- package/dist/{index-BidnFGw_.d.mts → index-Cic-RnwJ.d.mts} +1 -1
- package/dist/{index-H64xdjKh.d.ts → index-DVJ9S8lP.d.ts} +1 -1
- package/dist/index.d.mts +387 -5
- package/dist/index.d.ts +387 -5
- package/dist/index.js +375 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +364 -2
- package/dist/index.mjs.map +1 -1
- package/dist/{ows-DTDixPzO.d.ts → ows-C-KmORG9.d.ts} +1 -1
- package/dist/{ows-CYIVd4xO.d.mts → ows-Z9v4GxOZ.d.mts} +1 -1
- package/dist/providers/algorand/index.d.mts +1 -1
- package/dist/providers/algorand/index.d.ts +1 -1
- package/dist/providers/evm/index.d.mts +1 -1
- package/dist/providers/evm/index.d.ts +1 -1
- package/dist/providers/near/index.d.mts +1 -1
- package/dist/providers/near/index.d.ts +1 -1
- package/dist/providers/solana/index.d.mts +1 -1
- package/dist/providers/solana/index.d.ts +1 -1
- package/dist/providers/stellar/index.d.mts +1 -1
- package/dist/providers/stellar/index.d.ts +1 -1
- package/dist/providers/sui/index.d.mts +1 -1
- package/dist/providers/sui/index.d.ts +1 -1
- package/dist/providers/xrpl/index.d.mts +1 -1
- package/dist/providers/xrpl/index.d.ts +1 -1
- package/dist/react/index.d.mts +3 -3
- package/dist/react/index.d.ts +3 -3
- package/dist/utils/index.d.mts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/{wallet-0cX9Pw2F.d.mts → wallet-w7BnImDG.d.mts} +1 -1
- package/dist/{wallet-0cX9Pw2F.d.ts → wallet-w7BnImDG.d.ts} +1 -1
- package/package.json +38 -13
- package/src/backend/index.ts +22 -1
- package/src/erc8128.ts +443 -0
- package/src/escrow-preauth.ts +416 -0
- package/src/index.ts +36 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* uvd-x402-sdk - Escrow Pre-Auth Builder (sign-on-assignment)
|
|
3
|
+
*
|
|
4
|
+
* Builds and signs the EIP-3009 `ReceiveWithAuthorization` that locks a
|
|
5
|
+
* bounty in the x402r AuthCaptureEscrow, and packs it into the raw-JSON
|
|
6
|
+
* `X-Payment-Auth` wrapper the Ultravioleta Facilitator's `/settle` expects.
|
|
7
|
+
* Used by marketplaces built on the escrow rail (e.g. Execution Market's
|
|
8
|
+
* universal escrow, ADR-002).
|
|
9
|
+
*
|
|
10
|
+
* >>> PROTOCOL CONSTRAINT <<<
|
|
11
|
+
* The EIP-3009 nonce is `AuthCaptureEscrow.getHash(paymentInfo)` which
|
|
12
|
+
* INCLUDES the receiver — the escrow signature can only be created AT
|
|
13
|
+
* ASSIGNMENT, when the worker/payee is known. Never design flows that sign
|
|
14
|
+
* an escrow auth before the receiver is chosen ("stored pre-auth with late
|
|
15
|
+
* receiver fill" is on-chain unsound).
|
|
16
|
+
*
|
|
17
|
+
* Fail-loud policy: an incomplete network config, an unknown tier, a bounty
|
|
18
|
+
* outside the on-chain deposit limit, or a `maxFeeBps` that cannot cover the
|
|
19
|
+
* operator's static fee all throw instead of falling back — a silent
|
|
20
|
+
* fallback would sign an EIP-3009 authorization with the WRONG EIP-712
|
|
21
|
+
* domain (chainId + verifyingContract): a mismatched, wallet-draining auth.
|
|
22
|
+
*
|
|
23
|
+
* Wire format is pinned by golden vectors (`src/escrow-preauth.vectors.json`):
|
|
24
|
+
* payer-zeroed raw-keccak nonce (`keccak(chainId, escrow,
|
|
25
|
+
* keccak(PAYMENT_INFO_TYPEHASH, paymentInfo tuple with payer=0))`),
|
|
26
|
+
* `ReceiveWithAuthorization` typed data, raw JSON (NOT base64) wrapper.
|
|
27
|
+
* Byte-parity with the production references is enforced in
|
|
28
|
+
* `src/escrow-preauth.test.ts`.
|
|
29
|
+
*
|
|
30
|
+
* @example Sign the escrow lock at assignment time
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { buildEscrowPreAuth, EnvKeyAdapter } from 'uvd-x402-sdk';
|
|
33
|
+
*
|
|
34
|
+
* // Server-published escrow config (e.g. GET /api/v1/h2a/payment-config
|
|
35
|
+
* // on Execution Market — escrow_networks[network]).
|
|
36
|
+
* const paymentAuth = await buildEscrowPreAuth(new EnvKeyAdapter(), {
|
|
37
|
+
* networkConfig: config.escrow.networks.base,
|
|
38
|
+
* payerWallet: '0xPublisher...',
|
|
39
|
+
* workerWallet: '0xWorker...', // committed by the nonce
|
|
40
|
+
* bountyAtomic: '100000', // $0.10 in 6-decimal USDC
|
|
41
|
+
* reviewDeadlineSec: taskDeadlineEpoch,
|
|
42
|
+
* });
|
|
43
|
+
* // Send as the X-Payment-Auth header (raw JSON, NOT base64).
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* Reference implementations (kept in byte-parity via the shared vectors):
|
|
47
|
+
* - Python: `em_plugin_sdk.escrow_signing.build_escrow_pre_auth`
|
|
48
|
+
* (production reference, derived from `uvd_x402_sdk.advanced_escrow`)
|
|
49
|
+
* - Browser (viem): Execution Market `dashboard/src/services/h2aSigning.ts`
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
import { ethers } from 'ethers';
|
|
53
|
+
import { X402Error } from './types';
|
|
54
|
+
|
|
55
|
+
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
56
|
+
|
|
57
|
+
const USDC_DECIMALS = 6;
|
|
58
|
+
|
|
59
|
+
/** AuthCaptureEscrow deposit condition: $100 max per deposit. */
|
|
60
|
+
export const ESCROW_DEPOSIT_LIMIT_USD = 100;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* StaticFeeCalculator: canonical 13% flat fee. The signed maxFeeBps MUST
|
|
64
|
+
* cover it or the on-chain release reverts.
|
|
65
|
+
*/
|
|
66
|
+
export const OPERATOR_FEE_BPS = 1300;
|
|
67
|
+
|
|
68
|
+
/** Fee-bound defaults for optional config keys only — never for the domain. */
|
|
69
|
+
const DEFAULT_MIN_FEE_BPS = 0;
|
|
70
|
+
const DEFAULT_MAX_FEE_BPS = 1800;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* EIP-712 type for the escrow flow. The token collector pulls the funds, so
|
|
74
|
+
* the scheme is ReceiveWithAuthorization (not TransferWithAuthorization).
|
|
75
|
+
*/
|
|
76
|
+
const RECEIVE_WITH_AUTHORIZATION_TYPES = {
|
|
77
|
+
ReceiveWithAuthorization: [
|
|
78
|
+
{ name: 'from', type: 'address' },
|
|
79
|
+
{ name: 'to', type: 'address' },
|
|
80
|
+
{ name: 'value', type: 'uint256' },
|
|
81
|
+
{ name: 'validAfter', type: 'uint256' },
|
|
82
|
+
{ name: 'validBefore', type: 'uint256' },
|
|
83
|
+
{ name: 'nonce', type: 'bytes32' },
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* ABI type of the paymentInfo tuple — mirrors AdvancedEscrowClient /
|
|
89
|
+
* em_plugin_sdk.escrow_signing (unnamed components, positional encoding).
|
|
90
|
+
*/
|
|
91
|
+
const PAYMENT_INFO_TUPLE =
|
|
92
|
+
'tuple(address,address,address,address,uint120,uint48,uint48,uint48,uint16,uint16,address,uint256)';
|
|
93
|
+
|
|
94
|
+
/** Per-tier expiry windows in seconds, relative to now() at signing. */
|
|
95
|
+
export interface EscrowTierWindows {
|
|
96
|
+
pre: number;
|
|
97
|
+
auth: number;
|
|
98
|
+
refund: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Canonical fallback — mirrors uvd_x402_sdk TIER_TIMINGS. */
|
|
102
|
+
export const ESCROW_TIER_WINDOWS: Record<string, EscrowTierWindows> = {
|
|
103
|
+
micro: { pre: 3600, auth: 7200, refund: 86400 },
|
|
104
|
+
standard: { pre: 7200, auth: 86400, refund: 604800 },
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Human review windows. Agents approve in seconds, so the short SDK tier
|
|
109
|
+
* windows (micro auth = 2h) work for them. A HUMAN publisher reviews on
|
|
110
|
+
* their own schedule — if authorizationExpiry passes before approval, the
|
|
111
|
+
* on-chain release REVERTS (AfterAuthorizationExpiry). So release/refund
|
|
112
|
+
* windows are extended to comfortably outlast the deadline + review buffer.
|
|
113
|
+
*/
|
|
114
|
+
const REVIEW_WINDOW_SEC = 7 * 24 * 3600; // >=7 days to approve after the deadline
|
|
115
|
+
const REFUND_WINDOW_SEC = 7 * 24 * 3600; // +7 days to refund after that
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* One network's escrow parameters, as published by the marketplace server
|
|
119
|
+
* (e.g. Execution Market's `GET /api/v1/h2a/payment-config` →
|
|
120
|
+
* `escrow_networks[network]`). Snake_case on purpose: pass the server's
|
|
121
|
+
* JSON straight through so the SDK builds the paymentInfo EXACTLY like the
|
|
122
|
+
* production reference.
|
|
123
|
+
*/
|
|
124
|
+
export interface EscrowNetworkConfig {
|
|
125
|
+
chain_id: number;
|
|
126
|
+
operator: string;
|
|
127
|
+
escrow: string;
|
|
128
|
+
token_collector: string;
|
|
129
|
+
usdc: string;
|
|
130
|
+
usdc_domain_name: string;
|
|
131
|
+
usdc_domain_version: string;
|
|
132
|
+
/** 32-byte hex PAYMENT_INFO_TYPEHASH (with or without 0x prefix). */
|
|
133
|
+
payment_info_typehash: string;
|
|
134
|
+
/** Server-published tier windows; falls back to ESCROW_TIER_WINDOWS. */
|
|
135
|
+
tiers?: Record<string, EscrowTierWindows>;
|
|
136
|
+
min_fee_bps?: number;
|
|
137
|
+
max_fee_bps?: number;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** paymentInfo exactly as serialized in the X-Payment-Auth wrapper. */
|
|
141
|
+
export interface EscrowPaymentInfo {
|
|
142
|
+
operator: string;
|
|
143
|
+
receiver: string;
|
|
144
|
+
token: string;
|
|
145
|
+
/** Atomic units, as string (uint120 on-chain). */
|
|
146
|
+
maxAmount: string;
|
|
147
|
+
preApprovalExpiry: number;
|
|
148
|
+
authorizationExpiry: number;
|
|
149
|
+
refundExpiry: number;
|
|
150
|
+
minFeeBps: number;
|
|
151
|
+
maxFeeBps: number;
|
|
152
|
+
feeReceiver: string;
|
|
153
|
+
/** 32-byte hex. */
|
|
154
|
+
salt: string;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const REQUIRED_NETWORK_KEYS: Array<keyof EscrowNetworkConfig> = [
|
|
158
|
+
'chain_id',
|
|
159
|
+
'operator',
|
|
160
|
+
'escrow',
|
|
161
|
+
'token_collector',
|
|
162
|
+
'usdc',
|
|
163
|
+
'usdc_domain_name',
|
|
164
|
+
'usdc_domain_version',
|
|
165
|
+
'payment_info_typehash',
|
|
166
|
+
];
|
|
167
|
+
|
|
168
|
+
function hex32(value: string): string {
|
|
169
|
+
return value.startsWith('0x') ? value : `0x${value}`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 32 random bytes as 0x-hex (WebCrypto when available, ethers otherwise). */
|
|
173
|
+
function randomSalt(): string {
|
|
174
|
+
const webCrypto = (globalThis as { crypto?: Crypto }).crypto;
|
|
175
|
+
if (webCrypto?.getRandomValues) {
|
|
176
|
+
const bytes = new Uint8Array(32);
|
|
177
|
+
webCrypto.getRandomValues(bytes);
|
|
178
|
+
return ethers.hexlify(bytes);
|
|
179
|
+
}
|
|
180
|
+
return ethers.hexlify(ethers.randomBytes(32));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Compute the escrow EIP-3009 nonce = `AuthCaptureEscrow.getHash(paymentInfo)`.
|
|
185
|
+
*
|
|
186
|
+
* `keccak(chainId, escrow, keccak(PAYMENT_INFO_TYPEHASH, paymentInfo tuple
|
|
187
|
+
* with payer=0))` — the payer slot is zeroed for the payer-agnostic hash;
|
|
188
|
+
* the RECEIVER is part of the hash, which is why the signature commits to
|
|
189
|
+
* the chosen worker. Port of `AdvancedEscrowClient._compute_nonce` /
|
|
190
|
+
* `em_plugin_sdk.escrow_signing.compute_escrow_nonce`.
|
|
191
|
+
*
|
|
192
|
+
* @param chainId - EVM chain id.
|
|
193
|
+
* @param escrowAddress - AuthCaptureEscrow contract on that chain.
|
|
194
|
+
* @param paymentInfoTypehash - 32-byte hex typehash from the server config.
|
|
195
|
+
* @param pi - paymentInfo exactly as serialized on the wire.
|
|
196
|
+
* @returns 32-byte hex nonce (0x-prefixed, lowercase).
|
|
197
|
+
*/
|
|
198
|
+
export function computeEscrowNonce(
|
|
199
|
+
chainId: number,
|
|
200
|
+
escrowAddress: string,
|
|
201
|
+
paymentInfoTypehash: string,
|
|
202
|
+
pi: EscrowPaymentInfo
|
|
203
|
+
): string {
|
|
204
|
+
const coder = ethers.AbiCoder.defaultAbiCoder();
|
|
205
|
+
|
|
206
|
+
const piTuple = [
|
|
207
|
+
ethers.getAddress(pi.operator),
|
|
208
|
+
ZERO_ADDRESS, // payer = 0 for the payer-agnostic hash
|
|
209
|
+
ethers.getAddress(pi.receiver),
|
|
210
|
+
ethers.getAddress(pi.token),
|
|
211
|
+
BigInt(pi.maxAmount), // uint120
|
|
212
|
+
pi.preApprovalExpiry, // uint48
|
|
213
|
+
pi.authorizationExpiry, // uint48
|
|
214
|
+
pi.refundExpiry, // uint48
|
|
215
|
+
pi.minFeeBps, // uint16
|
|
216
|
+
pi.maxFeeBps, // uint16
|
|
217
|
+
ethers.getAddress(pi.feeReceiver),
|
|
218
|
+
BigInt(hex32(pi.salt)), // uint256
|
|
219
|
+
];
|
|
220
|
+
|
|
221
|
+
const piHash = ethers.keccak256(
|
|
222
|
+
coder.encode(['bytes32', PAYMENT_INFO_TUPLE], [hex32(paymentInfoTypehash), piTuple])
|
|
223
|
+
);
|
|
224
|
+
return ethers.keccak256(
|
|
225
|
+
coder.encode(
|
|
226
|
+
['uint256', 'address', 'bytes32'],
|
|
227
|
+
[chainId, ethers.getAddress(escrowAddress), piHash]
|
|
228
|
+
)
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Minimal signer for {@link buildEscrowPreAuth}: EIP-712 typed-data signing
|
|
234
|
+
* over the full typed-data JSON string (`domain`, `types`, `primaryType`,
|
|
235
|
+
* `message`) — the same contract as `SigningWalletAdapter.signTypedData`,
|
|
236
|
+
* so every adapter (EnvKeyAdapter, OWSWalletAdapter, custom) satisfies it
|
|
237
|
+
* structurally. Browser wallets can wrap their own signTypedData in this
|
|
238
|
+
* shape; the key never leaves the signer.
|
|
239
|
+
*/
|
|
240
|
+
export interface EscrowPreAuthSigner {
|
|
241
|
+
signTypedData(typedData: string): Promise<{ signature: string }>;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export interface EscrowPreAuthParams {
|
|
245
|
+
/** Network escrow config as published by the marketplace server. */
|
|
246
|
+
networkConfig: EscrowNetworkConfig;
|
|
247
|
+
/**
|
|
248
|
+
* Publisher wallet (payer). Not checked against the signer's address —
|
|
249
|
+
* payer binding is enforced on-chain and by the backend, not here.
|
|
250
|
+
*/
|
|
251
|
+
payerWallet: string;
|
|
252
|
+
/** Chosen worker's wallet (escrow receiver — committed by the nonce). */
|
|
253
|
+
workerWallet: string;
|
|
254
|
+
/** Bounty in atomic units (6-decimal USDC). */
|
|
255
|
+
bountyAtomic: string | bigint;
|
|
256
|
+
/** Expiry tier; windows come from config when present. Default 'micro'. */
|
|
257
|
+
tier?: string;
|
|
258
|
+
/**
|
|
259
|
+
* Task deadline (epoch seconds). The release/refund windows are extended
|
|
260
|
+
* to outlast it + a review buffer so a human publisher can still approve
|
|
261
|
+
* after the worker delivers. When omitted, falls back to now()-based
|
|
262
|
+
* windows.
|
|
263
|
+
*/
|
|
264
|
+
reviewDeadlineSec?: number;
|
|
265
|
+
/**
|
|
266
|
+
* On-chain deposit limit in USD (AuthCaptureEscrow deposit condition).
|
|
267
|
+
* Default {@link ESCROW_DEPOSIT_LIMIT_USD}; override only when the server
|
|
268
|
+
* publishes a different limit.
|
|
269
|
+
*/
|
|
270
|
+
depositLimitUsd?: number;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Build + sign the escrow lock authorization AT ASSIGNMENT time and return
|
|
275
|
+
* the raw JSON string for the `X-Payment-Auth` header (NOT base64 — the
|
|
276
|
+
* backend relays it verbatim to the Facilitator `/settle`). Mirror of
|
|
277
|
+
* `AdvancedEscrowClient.authorize()`'s /settle payload and of
|
|
278
|
+
* `em_plugin_sdk.escrow_signing.build_escrow_pre_auth`.
|
|
279
|
+
*
|
|
280
|
+
* @throws {X402Error} `INVALID_CONFIG` on an incomplete network config,
|
|
281
|
+
* unknown tier, or a `maxFeeBps` that cannot cover the operator's static
|
|
282
|
+
* fee; `INVALID_AMOUNT` on a bounty outside (0, deposit limit].
|
|
283
|
+
*/
|
|
284
|
+
export async function buildEscrowPreAuth(
|
|
285
|
+
wallet: EscrowPreAuthSigner,
|
|
286
|
+
params: EscrowPreAuthParams
|
|
287
|
+
): Promise<string> {
|
|
288
|
+
const cfg = params.networkConfig;
|
|
289
|
+
|
|
290
|
+
const missing = REQUIRED_NETWORK_KEYS.filter((key) => !cfg[key]);
|
|
291
|
+
if (missing.length > 0) {
|
|
292
|
+
throw new X402Error(
|
|
293
|
+
`Incomplete escrow network config (missing ${missing.join(', ')}) — ` +
|
|
294
|
+
'refusing to sign an EIP-3009 authorization with a mismatched domain.',
|
|
295
|
+
'INVALID_CONFIG'
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const maxAmount = BigInt(params.bountyAtomic);
|
|
300
|
+
if (maxAmount <= 0n) {
|
|
301
|
+
throw new X402Error(
|
|
302
|
+
`Bounty must be positive, got ${maxAmount} atomic units`,
|
|
303
|
+
'INVALID_AMOUNT'
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const depositLimitUsd = params.depositLimitUsd ?? ESCROW_DEPOSIT_LIMIT_USD;
|
|
307
|
+
const depositLimitAtomic = BigInt(Math.round(depositLimitUsd * 10 ** USDC_DECIMALS));
|
|
308
|
+
if (maxAmount > depositLimitAtomic) {
|
|
309
|
+
throw new X402Error(
|
|
310
|
+
`Bounty ${maxAmount} atomic units exceeds the on-chain escrow deposit ` +
|
|
311
|
+
`limit ($${depositLimitUsd}).`,
|
|
312
|
+
'INVALID_AMOUNT'
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const minFeeBps = cfg.min_fee_bps ?? DEFAULT_MIN_FEE_BPS;
|
|
317
|
+
const maxFeeBps = cfg.max_fee_bps ?? DEFAULT_MAX_FEE_BPS;
|
|
318
|
+
if (maxFeeBps < OPERATOR_FEE_BPS) {
|
|
319
|
+
throw new X402Error(
|
|
320
|
+
`maxFeeBps=${maxFeeBps} cannot cover the operator's ${OPERATOR_FEE_BPS} ` +
|
|
321
|
+
'bps static fee — the on-chain release would revert.',
|
|
322
|
+
'INVALID_CONFIG'
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const tierKey = params.tier ?? 'micro';
|
|
327
|
+
const windows = cfg.tiers?.[tierKey] ?? ESCROW_TIER_WINDOWS[tierKey];
|
|
328
|
+
if (!windows) {
|
|
329
|
+
const known = [...new Set([...Object.keys(cfg.tiers ?? {}), ...Object.keys(ESCROW_TIER_WINDOWS)])].sort();
|
|
330
|
+
throw new X402Error(
|
|
331
|
+
`Unknown escrow tier '${tierKey}' — known tiers: ${known.join(', ')}`,
|
|
332
|
+
'INVALID_CONFIG'
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const now = Math.floor(Date.now() / 1000);
|
|
337
|
+
|
|
338
|
+
// The release window must outlast the human review. Base it on the task
|
|
339
|
+
// deadline (the worker delivers near it) plus a generous buffer, and never
|
|
340
|
+
// shorter than the SDK tier window. preApprovalExpiry stays short — the
|
|
341
|
+
// lock executes immediately at assignment.
|
|
342
|
+
const reviewBase = Math.max(now, params.reviewDeadlineSec ?? now);
|
|
343
|
+
const authExpiry = Math.max(now + windows.auth, reviewBase + REVIEW_WINDOW_SEC);
|
|
344
|
+
const refundExpiry = Math.max(now + windows.refund, authExpiry + REFUND_WINDOW_SEC);
|
|
345
|
+
|
|
346
|
+
const paymentInfo: EscrowPaymentInfo = {
|
|
347
|
+
operator: ethers.getAddress(cfg.operator),
|
|
348
|
+
receiver: ethers.getAddress(params.workerWallet),
|
|
349
|
+
token: ethers.getAddress(cfg.usdc),
|
|
350
|
+
maxAmount: maxAmount.toString(),
|
|
351
|
+
preApprovalExpiry: now + windows.pre,
|
|
352
|
+
authorizationExpiry: authExpiry,
|
|
353
|
+
refundExpiry: refundExpiry,
|
|
354
|
+
minFeeBps: minFeeBps,
|
|
355
|
+
maxFeeBps: maxFeeBps,
|
|
356
|
+
feeReceiver: ethers.getAddress(cfg.operator),
|
|
357
|
+
salt: randomSalt(),
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const nonce = computeEscrowNonce(
|
|
361
|
+
cfg.chain_id,
|
|
362
|
+
cfg.escrow,
|
|
363
|
+
cfg.payment_info_typehash,
|
|
364
|
+
paymentInfo
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const payer = ethers.getAddress(params.payerWallet);
|
|
368
|
+
const tokenCollector = ethers.getAddress(cfg.token_collector);
|
|
369
|
+
|
|
370
|
+
// String-valued message fields: the typed data travels as JSON to the
|
|
371
|
+
// adapter (SigningWalletAdapter.signTypedData contract), and ethers
|
|
372
|
+
// accepts decimal strings for uint256 / 0x-hex for bytes32.
|
|
373
|
+
const { signature } = await wallet.signTypedData(
|
|
374
|
+
JSON.stringify({
|
|
375
|
+
domain: {
|
|
376
|
+
name: cfg.usdc_domain_name,
|
|
377
|
+
version: cfg.usdc_domain_version,
|
|
378
|
+
chainId: cfg.chain_id,
|
|
379
|
+
verifyingContract: ethers.getAddress(cfg.usdc),
|
|
380
|
+
},
|
|
381
|
+
types: RECEIVE_WITH_AUTHORIZATION_TYPES,
|
|
382
|
+
primaryType: 'ReceiveWithAuthorization',
|
|
383
|
+
message: {
|
|
384
|
+
from: payer,
|
|
385
|
+
to: tokenCollector,
|
|
386
|
+
value: maxAmount.toString(),
|
|
387
|
+
validAfter: '0',
|
|
388
|
+
validBefore: String(paymentInfo.preApprovalExpiry),
|
|
389
|
+
nonce: nonce,
|
|
390
|
+
},
|
|
391
|
+
})
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
// Raw JSON (NOT base64): the backend relays this verbatim to the
|
|
395
|
+
// Facilitator /settle after validating payer/amount/receiver.
|
|
396
|
+
return JSON.stringify({
|
|
397
|
+
x402Version: 2,
|
|
398
|
+
scheme: 'escrow',
|
|
399
|
+
payload: {
|
|
400
|
+
authorization: {
|
|
401
|
+
from: payer,
|
|
402
|
+
to: tokenCollector,
|
|
403
|
+
value: maxAmount.toString(),
|
|
404
|
+
validAfter: '0',
|
|
405
|
+
validBefore: String(paymentInfo.preApprovalExpiry),
|
|
406
|
+
nonce: nonce,
|
|
407
|
+
},
|
|
408
|
+
signature: signature,
|
|
409
|
+
paymentInfo: paymentInfo,
|
|
410
|
+
},
|
|
411
|
+
paymentRequirements: {
|
|
412
|
+
scheme: 'escrow',
|
|
413
|
+
network: `eip155:${cfg.chain_id}`,
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -268,6 +268,42 @@ export type {
|
|
|
268
268
|
SSEFrame,
|
|
269
269
|
} from './events';
|
|
270
270
|
|
|
271
|
+
// ERC-8128 signed HTTP requests (RFC 9421 signature base + EIP-191)
|
|
272
|
+
export {
|
|
273
|
+
signRequest,
|
|
274
|
+
signRequestWithWallet,
|
|
275
|
+
signRequestWithSigner,
|
|
276
|
+
fetchNonce,
|
|
277
|
+
createSignedFetch,
|
|
278
|
+
buildSignatureBase,
|
|
279
|
+
buildSignatureParams,
|
|
280
|
+
} from './erc8128';
|
|
281
|
+
export type {
|
|
282
|
+
ERC8128RequestOptions,
|
|
283
|
+
SignRequestOptions,
|
|
284
|
+
SignRequestWithSignerOptions,
|
|
285
|
+
SignatureHeaders,
|
|
286
|
+
CreateSignedFetchConfig,
|
|
287
|
+
SignatureBaseParams,
|
|
288
|
+
SignatureParamsInput,
|
|
289
|
+
} from './erc8128';
|
|
290
|
+
|
|
291
|
+
// Escrow pre-auth (sign-on-assignment X-Payment-Auth for the x402r escrow)
|
|
292
|
+
export {
|
|
293
|
+
buildEscrowPreAuth,
|
|
294
|
+
computeEscrowNonce,
|
|
295
|
+
ESCROW_TIER_WINDOWS,
|
|
296
|
+
ESCROW_DEPOSIT_LIMIT_USD,
|
|
297
|
+
OPERATOR_FEE_BPS,
|
|
298
|
+
} from './escrow-preauth';
|
|
299
|
+
export type {
|
|
300
|
+
EscrowNetworkConfig,
|
|
301
|
+
EscrowPaymentInfo,
|
|
302
|
+
EscrowPreAuthParams,
|
|
303
|
+
EscrowPreAuthSigner,
|
|
304
|
+
EscrowTierWindows,
|
|
305
|
+
} from './escrow-preauth';
|
|
306
|
+
|
|
271
307
|
// Server-side middleware and facilitator client
|
|
272
308
|
export {
|
|
273
309
|
FacilitatorClient,
|