@integraledger/profile-facilitator 0.5.0 → 0.6.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +556 -0
  2. package/README.md +754 -111
  3. package/dist/core.d.ts +1 -1
  4. package/dist/core.js +683 -66
  5. package/dist/http.d.ts +19 -4
  6. package/dist/http.js +16 -6
  7. package/dist/module.d.ts +111 -24
  8. package/dist/module.js +39 -0
  9. package/dist/modules/algorand/index.js +1 -1
  10. package/dist/modules/aptos/index.js +1 -1
  11. package/dist/modules/cardano/index.js +1 -1
  12. package/dist/modules/casper/index.js +15 -3
  13. package/dist/modules/concordium/index.js +1 -1
  14. package/dist/modules/erc7710/index.d.ts +8 -5
  15. package/dist/modules/erc7710/index.js +1111 -34
  16. package/dist/modules/evm/escrow.d.ts +125 -0
  17. package/dist/modules/evm/escrow.js +619 -0
  18. package/dist/modules/evm/forms.d.ts +39 -11
  19. package/dist/modules/evm/forms.js +191 -25
  20. package/dist/modules/evm/index.d.ts +41 -6
  21. package/dist/modules/evm/index.js +1135 -20
  22. package/dist/modules/evm/node.d.ts +16 -1
  23. package/dist/modules/evm/node.js +181 -26
  24. package/dist/modules/evm/nonce-proof.d.ts +186 -0
  25. package/dist/modules/evm/nonce-proof.js +779 -0
  26. package/dist/modules/hedera/executor.js +1 -1
  27. package/dist/modules/hedera/index.js +1 -1
  28. package/dist/modules/lnbtc/index.js +3 -2
  29. package/dist/modules/near/index.js +1 -1
  30. package/dist/modules/polkadot/index.js +1 -1
  31. package/dist/modules/solana/index.d.ts +3 -0
  32. package/dist/modules/solana/index.js +119 -10
  33. package/dist/modules/solana/reader.d.ts +2 -1
  34. package/dist/modules/solana/reader.js +42 -4
  35. package/dist/modules/solana/upto-claim.d.ts +58 -0
  36. package/dist/modules/solana/upto-claim.js +269 -0
  37. package/dist/modules/starknet/index.js +9 -2
  38. package/dist/modules/stellar/index.d.ts +5 -0
  39. package/dist/modules/stellar/index.js +18 -8
  40. package/dist/modules/stellar/node.d.ts +7 -1
  41. package/dist/modules/stellar/node.js +51 -7
  42. package/dist/modules/sui/index.js +66 -20
  43. package/dist/modules/sui/node.d.ts +13 -0
  44. package/dist/modules/sui/node.js +60 -0
  45. package/dist/modules/ton/index.js +6 -2
  46. package/dist/modules/tron/index.js +1 -1
  47. package/dist/modules/xrpl/index.js +61 -12
  48. package/dist/modules/xrpl/node.d.ts +31 -2
  49. package/dist/modules/xrpl/node.js +88 -17
  50. package/dist/modules/xrpl/upto.js +106 -20
  51. package/dist/record.d.ts +52 -8
  52. package/dist/record.js +39 -5
  53. package/dist/wrapper.d.ts +7 -5
  54. package/dist/wrapper.js +7 -5
  55. package/package.json +4 -3
@@ -21,11 +21,17 @@ export declare const ERC7710_PAIRINGS: readonly ["x402/exact/eip155/erc7710", "x
21
21
  type EvmPairing = (typeof PAIRINGS)[EvmScheme][number] | (typeof ERC7710_PAIRINGS)[number];
22
22
  /** A presented payment's read keys, as the pairing's `reference` gives them: an object lcp checks again at `status`. */
23
23
  type Ref = Record<string, unknown>;
24
- /** What the pure checks found: the payment's pairing and the read keys lcp gives it, where its form has them. */
24
+ /**
25
+ * What the pure checks found: the payment's pairing and the read keys lcp gives it, where its form has them; `later`
26
+ * where it is a later message of its scheme (`laterMessage`).
27
+ */
25
28
  export interface Admitted {
26
29
  readonly pairing: EvmPairing;
27
30
  readonly ref?: Ref;
31
+ readonly later?: true;
28
32
  }
33
+ /** Whether a payment under `scheme` is a later message of the scheme, by its payload's type alone. */
34
+ export declare function laterMessage(scheme: EvmScheme | "erc7710", payload: unknown): boolean;
29
35
  /**
30
36
  * The options of `exact` on EVM that no published EVM class may settle: one that pays several payees (a non-empty
31
37
  * `extra.splits`, which `exact` does not carry), and Circle's batched option, which signs under the Gateway Wallet's
@@ -39,12 +45,21 @@ export declare function exactOptionRefused(requirements: {
39
45
  /**
40
46
  * The profile's pure checks of a payment answering an option of `scheme`: the option is one of the pairings `pairings`
41
47
  * names, as lcp's option filter reads it alone (`invalid_payment_requirements` otherwise), and the payment is that
42
- * pairing's as lcp's `reference` reads it against the option (`invalid_payload` otherwise). A batch settlement's
43
- * payment that is no opening has no reference: lcp's channel read holds its configuration to its voucher's channel,
44
- * and the claim, settle and refund calls a resource server makes of the channel, which lcp does not decode, are x402's
45
- * class's to check.
48
+ * pairing's as lcp's `reference` reads it against the option (`invalid_payload` otherwise), an `upto` option being
49
+ * the one `uptoOption` holds it to. A batch settlement's payment that is no opening has no reference: lcp's channel
50
+ * read holds a voucher's or top-up's configuration to its voucher's channel. A later message of the scheme
51
+ * (`laterMessage`: an escrow's capture, void or refund; a channel's claim, settle or refund) has no reference either,
52
+ * since lcp does not decode it: it is x402's class's to check, and the escrow or the channel contract bounds what it
53
+ * moves by what the opening funded. A batch settlement's is admitted before its requirements are read, since the
54
+ * channel manager's name no pairing; an auth-capture one's requirements are the option the hold answered.
46
55
  */
47
56
  export declare function admit(scheme: EvmScheme | "erc7710", p: X402Payment, pairings: readonly PairingId[]): Promise<Admitted | Refused>;
57
+ /**
58
+ * The delegation an admitted ERC-7710 payment redeems, as signed, whatever its spelling: the manager and the delegator
59
+ * in lowercase, and the leaf delegation's EIP-712 hash where the context decodes as the reference manager's encoding,
60
+ * else the context's bytes in lowercase hex. A redemption's record key is its digest with the sale's H.
61
+ */
62
+ export declare function delegationOf(p: X402Payment, a: Admitted): Record<string, string>;
48
63
  /**
49
64
  * The record's key and keep for an admitted payment. EIP-3009: the network, the token, the authorization's `from` and
50
65
  * its nonce, kept to `validBefore`; the auth-capture escrow's ERC-3009 collector signs its nonce in the token's one
@@ -52,20 +67,33 @@ export declare function admit(scheme: EvmScheme | "erc7710", p: X402Payment, pai
52
67
  * owner, kept to the deadline. Neither is the nonce alone, and for an agreement the nonce signed is the agreement's own
53
68
  * value, so one payer's agreement and payment under one H are two rows. A batch settlement's call and an ERC-7710
54
69
  * redemption hold no single-use value: each is keyed by a digest, for the settler's life. A batch call's is the digest
55
- * of its signed payload. A redemption's is the digest of the delegation it redeems as signed, whatever its spelling: the
56
- * manager and the delegator in lowercase, and the leaf delegation's EIP-712 hash where the context decodes as the
57
- * reference manager's encoding (the hash commits to its caveats, its salt and, through its authority, the chain above
58
- * it; not to the signatures or the encoding's padding), else the context's bytes in lowercase hex. So one delegation is
59
- * one row, presented in any letter case or re-encoded.
70
+ * of its signed payload. A redemption's is the digest of the delegation it redeems as signed, whatever its spelling, and
71
+ * of the sale: the manager and the delegator in lowercase, the leaf delegation's EIP-712 hash where the context decodes
72
+ * as the reference manager's encoding (the hash commits to its caveats, its salt and, through its authority, the chain
73
+ * above it; not to the signatures or the encoding's padding), else the context's bytes in lowercase hex, and the H the
74
+ * payment echoes. So one sale through one delegation is one row, presented in any letter case or re-encoded, and each
75
+ * other sale through a delegation that may be redeemed again (x402's ERC-7710 is one-time or multi-use, its caveats
76
+ * the limit) is its own row, which the redemption's simulation refuses once the caveats are spent.
77
+ * An escrow's capture, void or refund (`Admitted.later`) is its own row, never the row of the hold whose signed
78
+ * authorization a payload the resource server stamps with a type still carries: the digest of the message its receiver
79
+ * authorizer signs (`signedMessage`), every member it signs and nothing else, its signature excluded; the core also
80
+ * takes that message as the row's body (`Instrumented.body`). The escrow's amounts only fall as a payment's lifecycle
81
+ * goes on, so each such message occurs once in it, and one message spelt two ways, or signed again, is one row: never
82
+ * sent twice at once. A batch settlement's later message is keyed by the digest of its signed payload; its claim and settle are not
83
+ * recorded (`Instrumented.unrecorded`).
60
84
  */
61
85
  export declare function instrumentOf(network: Network, p: X402Payment, a: Admitted): Promise<Instrumented | Refused>;
86
+ /** Whether an admitted payment is an auth-capture escrow's later message: its capture, void or refund. */
87
+ export declare function escrowMessage(a: Admitted): boolean;
62
88
  /**
63
89
  * lcp's read of an admitted payment, by the pairing's search, at the final mark `finalized`, as the act `payment`: the
64
90
  * value an agreement signs stands in H's place in its reference, so the search for it is the search for that value.
65
91
  * `at.search` resumes an earlier search. `at.transaction` is the transaction the settler's record holds for the
66
92
  * payment, which the reference names as the transaction a settler names: read beside the search, and on a level that
67
93
  * signs no H (`x402/exact/eip155/erc7710`) the only way the read finds the payment. A batch settlement's payment that
68
- * is no opening has no reference and no read of its own: `pending`, `search-incomplete`, live.
94
+ * is no opening has no reference and no read of its own: `pending`, `search-incomplete`, live (the module reads a
95
+ * channel refund's own receipt before it, `revertedAtFinalized` and `refundedAtFinalized`). An `exact` EIP-3009
96
+ * payment is read by the authorization it presents (`byAuthorization`).
69
97
  */
70
98
  export declare function readOf(reader: EvmReader, a: Admitted, at: {
71
99
  readonly search?: unknown;
@@ -3,10 +3,11 @@
3
3
  * of its profile, the instrument the record keys it by, how long a final row is kept, and lcp's read of it. Nothing here
4
4
  * reads the chain or the clock but the read itself.
5
5
  */
6
- import { digestJson, isRefusal, pairingOf } from "@integraledger/lcp";
6
+ import { canonicalJson, digestJson, isRefusal, pairingOf } from "@integraledger/lcp";
7
7
  import { PERMIT2 } from "@integraledger/lcp/evm";
8
8
  import { authCaptureEip3009, authCapturePermit2, exactEip3009, exactErc7710, exactErc7710Salt, exactPermit2, pairingOfPayment, uptoPermit2, } from "@integraledger/lcp/x402";
9
9
  import { batchEvm } from "@integraledger/lcp/x402-batch-settlement";
10
+ import { resolveAuthCaptureDeployment } from "@x402/evm";
10
11
  import { outcomeOf } from "../../module.js";
11
12
  /** The pairings each scheme's class submits on an `eip155` network. */
12
13
  export const PAIRINGS = {
@@ -34,6 +35,30 @@ function codec(b) {
34
35
  function isObject(v) {
35
36
  return typeof v === "object" && v !== null && !Array.isArray(v);
36
37
  }
38
+ /**
39
+ * The payload types x402's classes (`@x402/evm` 2.31.0) take as a later message on an instrument an earlier payment
40
+ * opened, which a resource server sends and the payer did not sign as a payment: `AuthCaptureEvmScheme` takes every
41
+ * payload of type `capture`, `void` or `refund` as the escrow's lifecycle call of that name (`isLifecyclePayload`),
42
+ * the hold's own payload the resource server stamps with a type (its capture after the handler, its void on cancel)
43
+ * among them; `BatchSettlementEvmScheme` takes `claim`, `settle` and `refund` as the channel contract's call of that
44
+ * name, `claim` and `settle` as x402's channel manager builds them, under requirements of its own (amount `"0"`, no
45
+ * `extra`) that name no lcp pairing.
46
+ */
47
+ const LATER = {
48
+ "auth-capture": ["capture", "void", "refund"],
49
+ "batch-settlement": ["claim", "settle", "refund"],
50
+ };
51
+ /** A payment's payload's `type`, where it states one. */
52
+ function laterType(payload) {
53
+ const body = isObject(payload) ? payload["payload"] : undefined;
54
+ const kind = isObject(body) ? body["type"] : undefined;
55
+ return typeof kind === "string" ? kind : undefined;
56
+ }
57
+ /** Whether a payment under `scheme` is a later message of the scheme, by its payload's type alone. */
58
+ export function laterMessage(scheme, payload) {
59
+ const kind = laterType(payload);
60
+ return scheme !== "erc7710" && kind !== undefined && (LATER[scheme] ?? []).includes(kind);
61
+ }
37
62
  /**
38
63
  * The options of `exact` on EVM that no published EVM class may settle: one that pays several payees (a non-empty
39
64
  * `extra.splits`, which `exact` does not carry), and Circle's batched option, which signs under the Gateway Wallet's
@@ -53,13 +78,37 @@ export function exactOptionRefused(requirements) {
53
78
  return (verifying !== undefined &&
54
79
  !(typeof verifying === "string" && typeof requirements.asset === "string" && verifying.toLowerCase() === requirements.asset.toLowerCase()));
55
80
  }
81
+ const DECIMAL = /^[0-9]+$/;
82
+ /**
83
+ * The option an `upto` payment is held to. x402's `upto` settles the amount its requirements state, at most the maximum
84
+ * the payment accepted: requirements that are the option the payment accepted in every member but an `amount` at or
85
+ * below the accepted one, as RFC 8785 forms compare them, are held as that option. Any other requirements are the
86
+ * option as given.
87
+ */
88
+ function uptoOption(p) {
89
+ const { requirements } = p;
90
+ const accepted = p.payload.accepted;
91
+ if (!isObject(accepted))
92
+ return requirements;
93
+ const charge = requirements.amount;
94
+ const max = accepted.amount;
95
+ if (typeof charge !== "string" || typeof max !== "string" || !DECIMAL.test(charge) || !DECIMAL.test(max))
96
+ return requirements;
97
+ if (BigInt(charge) > BigInt(max))
98
+ return requirements;
99
+ const asAccepted = canonicalJson({ ...requirements, amount: max });
100
+ return typeof asAccepted === "string" && asAccepted === canonicalJson(accepted) ? accepted : requirements;
101
+ }
56
102
  /**
57
103
  * The profile's pure checks of a payment answering an option of `scheme`: the option is one of the pairings `pairings`
58
104
  * names, as lcp's option filter reads it alone (`invalid_payment_requirements` otherwise), and the payment is that
59
- * pairing's as lcp's `reference` reads it against the option (`invalid_payload` otherwise). A batch settlement's
60
- * payment that is no opening has no reference: lcp's channel read holds its configuration to its voucher's channel,
61
- * and the claim, settle and refund calls a resource server makes of the channel, which lcp does not decode, are x402's
62
- * class's to check.
105
+ * pairing's as lcp's `reference` reads it against the option (`invalid_payload` otherwise), an `upto` option being
106
+ * the one `uptoOption` holds it to. A batch settlement's payment that is no opening has no reference: lcp's channel
107
+ * read holds a voucher's or top-up's configuration to its voucher's channel. A later message of the scheme
108
+ * (`laterMessage`: an escrow's capture, void or refund; a channel's claim, settle or refund) has no reference either,
109
+ * since lcp does not decode it: it is x402's class's to check, and the escrow or the channel contract bounds what it
110
+ * moves by what the opening funded. A batch settlement's is admitted before its requirements are read, since the
111
+ * channel manager's name no pairing; an auth-capture one's requirements are the option the hold answered.
63
112
  */
64
113
  export async function admit(scheme, p, pairings) {
65
114
  const { payload, requirements } = p;
@@ -67,6 +116,9 @@ export async function admit(scheme, p, pairings) {
67
116
  return { refused: "invalid_payment_requirements" };
68
117
  if (requirements.scheme === "exact" && exactOptionRefused(requirements))
69
118
  return { refused: "invalid_payment_requirements" };
119
+ const later = laterMessage(scheme, payload);
120
+ if (later && scheme === "batch-settlement")
121
+ return { pairing: batchEvm.id, later: true };
70
122
  const option = pairingOf(requirements);
71
123
  if (option === undefined)
72
124
  return { refused: "invalid_payment_requirements" };
@@ -75,10 +127,9 @@ export async function admit(scheme, p, pairings) {
75
127
  return { refused: "invalid_payment_requirements" };
76
128
  }
77
129
  const pairing = level;
130
+ if (later)
131
+ return { pairing, later: true };
78
132
  if (pairing === batchEvm.id) {
79
- const kind = isObject(payload.payload) ? payload.payload["type"] : undefined;
80
- if (kind === "claim" || kind === "settle")
81
- return { pairing };
82
133
  const k = batchEvm.channel.kind(payload);
83
134
  if (isRefusal(k))
84
135
  return { refused: "invalid_payload" };
@@ -87,7 +138,8 @@ export async function admit(scheme, p, pairings) {
87
138
  return isRefusal(channel) ? { refused: "invalid_payload" } : { pairing };
88
139
  }
89
140
  }
90
- const ref = await CODEC[pairing].reference(payload, requirements);
141
+ const held = requirements.scheme === "upto" ? uptoOption(p) : requirements;
142
+ const ref = await CODEC[pairing].reference(payload, held);
91
143
  if (isRefusal(ref))
92
144
  return { refused: "invalid_payload" };
93
145
  return { pairing, ref: ref };
@@ -103,6 +155,20 @@ function permit2Nonce(v) {
103
155
  function lapse(seconds) {
104
156
  return { until: "lapse", at: Number(BigInt(String(seconds))) * 1000 };
105
157
  }
158
+ /**
159
+ * The delegation an admitted ERC-7710 payment redeems, as signed, whatever its spelling: the manager and the delegator
160
+ * in lowercase, and the leaf delegation's EIP-712 hash where the context decodes as the reference manager's encoding,
161
+ * else the context's bytes in lowercase hex. A redemption's record key is its digest with the sale's H.
162
+ */
163
+ export function delegationOf(p, a) {
164
+ const r = isObject(p.payload.payload) ? p.payload.payload : {};
165
+ const leaf = isObject(a.ref?.["presented"]) ? a.ref["presented"]["delegation"] : undefined;
166
+ return {
167
+ delegationManager: lower(r["delegationManager"]),
168
+ delegator: lower(r["delegator"]),
169
+ ...(typeof leaf === "string" ? { delegation: lower(leaf) } : { permissionContext: lower(r["permissionContext"]) }),
170
+ };
171
+ }
106
172
  /**
107
173
  * The record's key and keep for an admitted payment. EIP-3009: the network, the token, the authorization's `from` and
108
174
  * its nonce, kept to `validBefore`; the auth-capture escrow's ERC-3009 collector signs its nonce in the token's one
@@ -110,11 +176,20 @@ function lapse(seconds) {
110
176
  * owner, kept to the deadline. Neither is the nonce alone, and for an agreement the nonce signed is the agreement's own
111
177
  * value, so one payer's agreement and payment under one H are two rows. A batch settlement's call and an ERC-7710
112
178
  * redemption hold no single-use value: each is keyed by a digest, for the settler's life. A batch call's is the digest
113
- * of its signed payload. A redemption's is the digest of the delegation it redeems as signed, whatever its spelling: the
114
- * manager and the delegator in lowercase, and the leaf delegation's EIP-712 hash where the context decodes as the
115
- * reference manager's encoding (the hash commits to its caveats, its salt and, through its authority, the chain above
116
- * it; not to the signatures or the encoding's padding), else the context's bytes in lowercase hex. So one delegation is
117
- * one row, presented in any letter case or re-encoded.
179
+ * of its signed payload. A redemption's is the digest of the delegation it redeems as signed, whatever its spelling, and
180
+ * of the sale: the manager and the delegator in lowercase, the leaf delegation's EIP-712 hash where the context decodes
181
+ * as the reference manager's encoding (the hash commits to its caveats, its salt and, through its authority, the chain
182
+ * above it; not to the signatures or the encoding's padding), else the context's bytes in lowercase hex, and the H the
183
+ * payment echoes. So one sale through one delegation is one row, presented in any letter case or re-encoded, and each
184
+ * other sale through a delegation that may be redeemed again (x402's ERC-7710 is one-time or multi-use, its caveats
185
+ * the limit) is its own row, which the redemption's simulation refuses once the caveats are spent.
186
+ * An escrow's capture, void or refund (`Admitted.later`) is its own row, never the row of the hold whose signed
187
+ * authorization a payload the resource server stamps with a type still carries: the digest of the message its receiver
188
+ * authorizer signs (`signedMessage`), every member it signs and nothing else, its signature excluded; the core also
189
+ * takes that message as the row's body (`Instrumented.body`). The escrow's amounts only fall as a payment's lifecycle
190
+ * goes on, so each such message occurs once in it, and one message spelt two ways, or signed again, is one row: never
191
+ * sent twice at once. A batch settlement's later message is keyed by the digest of its signed payload; its claim and settle are not
192
+ * recorded (`Instrumented.unrecorded`).
118
193
  */
119
194
  export async function instrumentOf(network, p, a) {
120
195
  const nonce = CODEC[a.pairing].nonce;
@@ -123,18 +198,26 @@ export async function instrumentOf(network, p, a) {
123
198
  const d = await digestJson(signed);
124
199
  if (isRefusal(d))
125
200
  return { refused: "invalid_payload" };
126
- return { pairing: a.pairing, instrument: { by: "digest", network, digest: d }, nonce, keep: { until: "settler" } };
201
+ const unrecorded = a.later === true && a.pairing === batchEvm.id && laterType(p.payload) !== "refund";
202
+ return {
203
+ pairing: a.pairing,
204
+ instrument: { by: "digest", network, digest: d },
205
+ nonce,
206
+ keep: { until: "settler" },
207
+ ...(a.later ? { later: true } : {}),
208
+ ...(unrecorded ? { unrecorded: true } : {}),
209
+ };
127
210
  };
128
211
  const digest = () => digestOf(p.payload.payload);
129
- if (a.pairing === exactErc7710.id || a.pairing === exactErc7710Salt.id) {
130
- const r = isObject(p.payload.payload) ? p.payload.payload : {};
131
- const leaf = isObject(ref?.["presented"]) ? ref["presented"]["delegation"] : undefined;
132
- return digestOf({
133
- delegationManager: lower(r["delegationManager"]),
134
- delegator: lower(r["delegator"]),
135
- ...(typeof leaf === "string" ? { delegation: lower(leaf) } : { permissionContext: lower(r["permissionContext"]) }),
136
- });
212
+ if (escrowMessage(a)) {
213
+ // The row's key and its body are the message the receiver authorizer signs, so that one message signed again (a
214
+ // signer that does not sign deterministically) is one row and one body, and its signature is the class's to judge.
215
+ const message = signedMessage(p);
216
+ const keyed = await digestOf(message);
217
+ return "refused" in keyed ? keyed : { ...keyed, resendable: true, body: message };
137
218
  }
219
+ if (a.pairing === exactErc7710.id || a.pairing === exactErc7710Salt.id)
220
+ return digestOf({ ...delegationOf(p, a), h: lower(a.ref?.["h"]) });
138
221
  if (ref === undefined)
139
222
  return digest();
140
223
  const presented = isObject(ref["presented"]) ? ref["presented"] : {};
@@ -163,18 +246,101 @@ export async function instrumentOf(network, p, a) {
163
246
  }
164
247
  return { pairing: a.pairing, instrument, nonce, keep };
165
248
  }
249
+ /**
250
+ * A value as a signature binds it: text `BigInt` reads as an integer, as its decimal digits, since x402's class and
251
+ * viem's encoder read a message's numbers with `BigInt` (so `" 400000"`, `"+400000"` and `"0x61a80"` are one amount);
252
+ * other text in lowercase.
253
+ */
254
+ function bound(v) {
255
+ if (typeof v !== "string")
256
+ return v;
257
+ try {
258
+ return BigInt(v).toString(10);
259
+ }
260
+ catch {
261
+ return v.toLowerCase();
262
+ }
263
+ }
264
+ /** Whether an admitted payment is an auth-capture escrow's later message: its capture, void or refund. */
265
+ export function escrowMessage(a) {
266
+ return a.later === true && (a.pairing === authCaptureEip3009.id || a.pairing === authCapturePermit2.id);
267
+ }
268
+ /** The members of the commerce-payments escrow's `PaymentInfo`, in its order. */
269
+ const PAYMENT_INFO = [
270
+ "operator",
271
+ "payer",
272
+ "receiver",
273
+ "token",
274
+ "maxAmount",
275
+ "preApprovalExpiry",
276
+ "authorizationExpiry",
277
+ "refundExpiry",
278
+ "minFeeBps",
279
+ "maxFeeBps",
280
+ "feeReceiver",
281
+ "salt",
282
+ ];
283
+ /**
284
+ * The EIP-712 domain's name and version under which x402's `AuthCaptureEvmScheme` of `@x402/evm` 2.31.0 has the receiver
285
+ * authorizer sign a capture, void or refund (its `getOperatorEip712Domain`, whose `verifyingContract` is the
286
+ * requirements' `captureAuthorizer` and whose `chainId` is the network's).
287
+ */
288
+ const OPERATOR_DOMAIN = { name: "x402 Auth Capture Operator", version: "1" };
289
+ /**
290
+ * The message the receiver authorizer of an auth-capture lifecycle message signs, as x402's `AuthCaptureEvmScheme` of
291
+ * `@x402/evm` 2.31.0 has it signed and verifies it, every member it signs, as `instrumentOf` keys the row and states
292
+ * its body; the signatures are not in it. The typed-data domain: its name and version, the network's chain id, and the
293
+ * requirements' `captureAuthorizer` as its verifying contract. The payment's hash (`paymentInfoHash`) as the members it
294
+ * commits to: the chain id, the escrow of the requirements' deployment, and the twelve members of the escrow's
295
+ * `PaymentInfo`. Its type; for a capture, its amount, its fee receiver and fee (`feeBps` or `feeAmount`, as the
296
+ * deployment's version signs) and the capturable and refundable amounts it expects; for a refund, its amount, its token
297
+ * collector (the deployment's `operatorRefundCollector`, which the class has signed and verifies) and the amounts it
298
+ * expects. Each integer or address is the number it is, so one message spelt two ways is one. The void of the
299
+ * payment's remainder a capture may carry is another message the receiver authorizer signs (`Void`, over the payment's
300
+ * hash alone), which the escrow's event of the capture does not show: a capture with it and the same capture without
301
+ * it are one message, one row and one body, and the void goes with whichever carries it.
302
+ */
303
+ function signedMessage(p) {
304
+ const payload = p.payload.payload;
305
+ const body = isObject(payload) ? payload : {};
306
+ const extra = isObject(p.requirements.extra) ? p.requirements.extra : {};
307
+ const info = isObject(body["paymentInfo"]) ? body["paymentInfo"] : {};
308
+ const deployment = resolveAuthCaptureDeployment(typeof extra["authCaptureEscrow"] === "string" ? extra["authCaptureEscrow"] : undefined);
309
+ const chainId = String(p.requirements.network).startsWith("eip155:") ? bound(String(p.requirements.network).slice("eip155:".length)) : null;
310
+ const domain = { ...OPERATOR_DOMAIN, chainId, verifyingContract: bound(extra["captureAuthorizer"]) ?? null };
311
+ // The members the escrow's `PaymentInfo` holds, and so its hash: a member the escrow does not take changes nothing.
312
+ const paymentInfo = Object.fromEntries(PAYMENT_INFO.map((k) => [k, bound(info[k]) ?? null]));
313
+ const payment = { chainId, escrow: deployment === undefined ? null : bound(deployment.escrow), paymentInfo };
314
+ const type = body["type"];
315
+ const member = (k) => bound(body[k]) ?? null;
316
+ const expected = [member("expectedCapturableAmount"), member("expectedRefundableAmount")];
317
+ if (type === "capture") {
318
+ const fee = body["feeAmount"] !== undefined ? { feeAmount: member("feeAmount") } : { feeBps: member("feeBps") };
319
+ return { domain, type, payment, amount: member("amount"), feeReceiver: member("feeReceiver"), ...fee, expected };
320
+ }
321
+ if (type === "refund") {
322
+ const tokenCollector = deployment === undefined ? null : bound(deployment.operatorRefundCollector);
323
+ return { domain, type, payment, amount: member("amount"), tokenCollector, expected };
324
+ }
325
+ return { domain, type, payment };
326
+ }
166
327
  /**
167
328
  * lcp's read of an admitted payment, by the pairing's search, at the final mark `finalized`, as the act `payment`: the
168
329
  * value an agreement signs stands in H's place in its reference, so the search for it is the search for that value.
169
330
  * `at.search` resumes an earlier search. `at.transaction` is the transaction the settler's record holds for the
170
331
  * payment, which the reference names as the transaction a settler names: read beside the search, and on a level that
171
332
  * signs no H (`x402/exact/eip155/erc7710`) the only way the read finds the payment. A batch settlement's payment that
172
- * is no opening has no reference and no read of its own: `pending`, `search-incomplete`, live.
333
+ * is no opening has no reference and no read of its own: `pending`, `search-incomplete`, live (the module reads a
334
+ * channel refund's own receipt before it, `revertedAtFinalized` and `refundedAtFinalized`). An `exact` EIP-3009
335
+ * payment is read by the authorization it presents (`byAuthorization`).
173
336
  */
174
337
  export async function readOf(reader, a, at) {
175
338
  if (a.ref === undefined)
176
339
  return outcomeOf({ state: "pending", reason: "search-incomplete", foreign: [] });
177
- const ref = at.transaction === undefined ? a.ref : { ...a.ref, transaction: at.transaction.toLowerCase() };
340
+ // An EIP-3009 payment is read by the authorization it presents, its use on the asset (`byAuthorization`): the payer's
341
+ // use of the same H on another token is another payment, never this one's answer.
342
+ const own = a.pairing === exactEip3009.id ? { ...a.ref, byAuthorization: true } : a.ref;
343
+ const ref = at.transaction === undefined ? own : { ...own, transaction: at.transaction.toLowerCase() };
178
344
  const query = at.search === undefined
179
345
  ? { act: "payment", finality: "finalized" }
180
346
  : { act: "payment", finality: "finalized", search: at.search };
@@ -1,7 +1,8 @@
1
+ import type { Hex } from "@integraledger/lcp/evm";
1
2
  import type { PendingSettlementStore } from "@x402/core/facilitator";
2
3
  import { type AuthCaptureFacilitatorConfig } from "@x402/evm/auth-capture/facilitator";
3
- import type { FacilitatorEvmSigner } from "@x402/evm";
4
- import type { FeeMeter, FeeNeeds, Module, ModuleStarter } from "../../module.js";
4
+ import { type FacilitatorEvmSigner } from "@x402/evm";
5
+ import { type Core, type FeeMeter, type FeeNeeds, type Module, type ModuleStarter, type ModuleState } from "../../module.js";
5
6
  import { type EvmScheme } from "./forms.js";
6
7
  /** One `eip155` network the `evm` module serves. */
7
8
  export interface EvmConfig {
@@ -11,9 +12,11 @@ export interface EvmConfig {
11
12
  readonly rpc: string;
12
13
  /**
13
14
  * The seller's gas key, as x402's EVM classes take it (`toFacilitatorEvmSigner` of a client over the same node): it
14
- * sends every transaction the module submits and pays its gas.
15
+ * sends every transaction the module submits and pays its gas. For `auth-capture` it must also sign a transaction
16
+ * without sending it, and send one signed (`EvmSender`): the escrow's calls are signed first, named in the record,
17
+ * then sent, and the module does not start where `auth-capture` is registered with a key without these members.
15
18
  */
16
- readonly signer: FacilitatorEvmSigner;
19
+ readonly signer: FacilitatorEvmSigner & Partial<EvmSender>;
17
20
  /** The schemes registered on the network; every one of the four where absent. */
18
21
  readonly schemes?: readonly EvmScheme[];
19
22
  /** ERC-6492 factories `exact` and `batch-settlement` may call to deploy a payer's wallet; none where absent. */
@@ -21,17 +24,49 @@ export interface EvmConfig {
21
24
  /** `auth-capture`'s own configuration (fee terms, operators, its authorizers), passed to its class unchanged. */
22
25
  readonly authCapture?: AuthCaptureFacilitatorConfig;
23
26
  }
27
+ /**
28
+ * A gas key that signs a transaction without sending it, and sends one signed: with viem's wallet client over a local
29
+ * account, `prepareTransactionRequest` then `signTransaction`, and `sendRawTransaction`.
30
+ */
31
+ export interface EvmSender {
32
+ /**
33
+ * Signs, and does not send, a transaction from the key calling `to` with `data`: its gas and fees filled as viem's
34
+ * `prepareTransactionRequest` fills them, unless the request names them (`gas`, and `maxFeePerGas` with
35
+ * `maxPriorityFeePerGas`, which a replacement at the same nonce names), and its nonce, unless `nonce`
36
+ * names one, the key's `pending` count. The signed transaction, serialized.
37
+ */
38
+ signTransaction(request: {
39
+ readonly to: Hex;
40
+ readonly data: Hex;
41
+ readonly gas?: bigint;
42
+ readonly nonce?: number;
43
+ readonly maxFeePerGas?: bigint;
44
+ readonly maxPriorityFeePerGas?: bigint;
45
+ }): Promise<Hex>;
46
+ /** Sends a signed, serialized transaction (`eth_sendRawTransaction`), once: its hash. */
47
+ sendRawTransaction(request: {
48
+ readonly serializedTransaction: Hex;
49
+ }): Promise<Hex>;
50
+ }
24
51
  /** A module whose key pays the fee of what it sends, with the read of each transaction's fee. */
25
52
  export type EvmModule = Module & {
26
53
  readonly fees: Required<FeeNeeds>;
27
54
  };
55
+ /**
56
+ * `receipt` where it is the receipt of `transaction`; otherwise it throws, so that the escrow's class or the `erc7710`
57
+ * module answers the transaction pending and the record's read decides (neither keeps a pending settlement of its own). viem's `waitForTransactionReceipt` answers, by default, the receipt of another
58
+ * transaction of the same key at the same nonce where it finds one: a replacement of the same call, which the read
59
+ * then shows, or another payment's transaction, which paid nothing of this one.
60
+ */
61
+ export declare function ownReceipt<R>(transaction: Hex, receipt: R): R;
28
62
  /**
29
63
  * The `evm` module's schemes on one network, each x402's class with what the core needs beside it, the gas key counted
30
64
  * by `meter`, the network's fee meter. `pending` is where `exact`, `upto` and `batch-settlement` keep a settlement they
31
65
  * sent and the chain does not yet show, so that a retried settle reconciles against it; each class keeps its own in
32
- * memory where absent.
66
+ * memory where absent. `core` names an escrow call's replacement in the row before it is sent (`Core.naming`) and
67
+ * gives the settle wait a tip-only replacement waits for; where absent, no replacement is made.
33
68
  */
34
- export declare function evmModules(c: EvmConfig, meter: FeeMeter, pending?: PendingSettlementStore): EvmModule[];
69
+ export declare function evmModules(c: EvmConfig, meter: FeeMeter, state: ModuleState, pending?: PendingSettlementStore, core?: Pick<Core, "naming" | "settleWaitMs">): EvmModule[];
35
70
  /**
36
71
  * How the core starts the module: the configuration's `evm` member lists the networks, one entry each, and each
37
72
  * configured scheme on a network is one registration of x402's class, whose pending settlements are kept in the