@bankofai/x402-evm 1.0.0-beta.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 (176) hide show
  1. package/README.md +172 -0
  2. package/dist/cjs/auth-capture/client/index.d.ts +44 -0
  3. package/dist/cjs/auth-capture/client/index.js +298 -0
  4. package/dist/cjs/auth-capture/client/index.js.map +1 -0
  5. package/dist/cjs/batch-settlement/client/file-storage.d.ts +47 -0
  6. package/dist/cjs/batch-settlement/client/file-storage.js +116 -0
  7. package/dist/cjs/batch-settlement/client/file-storage.js.map +1 -0
  8. package/dist/cjs/batch-settlement/client/index.d.ts +111 -0
  9. package/dist/cjs/batch-settlement/client/index.js +1565 -0
  10. package/dist/cjs/batch-settlement/client/index.js.map +1 -0
  11. package/dist/cjs/batch-settlement/facilitator/index.d.ts +72 -0
  12. package/dist/cjs/batch-settlement/facilitator/index.js +2102 -0
  13. package/dist/cjs/batch-settlement/facilitator/index.js.map +1 -0
  14. package/dist/cjs/batch-settlement/server/file-storage.d.ts +53 -0
  15. package/dist/cjs/batch-settlement/server/file-storage.js +181 -0
  16. package/dist/cjs/batch-settlement/server/file-storage.js.map +1 -0
  17. package/dist/cjs/batch-settlement/server/index.d.ts +491 -0
  18. package/dist/cjs/batch-settlement/server/index.js +1978 -0
  19. package/dist/cjs/batch-settlement/server/index.js.map +1 -0
  20. package/dist/cjs/batch-settlement/server/redis-storage.d.ts +87 -0
  21. package/dist/cjs/batch-settlement/server/redis-storage.js +181 -0
  22. package/dist/cjs/batch-settlement/server/redis-storage.js.map +1 -0
  23. package/dist/cjs/client/agent-wallet.d.ts +69 -0
  24. package/dist/cjs/client/agent-wallet.js +84 -0
  25. package/dist/cjs/client/agent-wallet.js.map +1 -0
  26. package/dist/cjs/exact/client/index.d.ts +63 -0
  27. package/dist/cjs/exact/client/index.js +739 -0
  28. package/dist/cjs/exact/client/index.js.map +1 -0
  29. package/dist/cjs/exact/facilitator/index.d.ts +141 -0
  30. package/dist/cjs/exact/facilitator/index.js +1989 -0
  31. package/dist/cjs/exact/facilitator/index.js.map +1 -0
  32. package/dist/cjs/exact/server/index.d.ts +118 -0
  33. package/dist/cjs/exact/server/index.js +326 -0
  34. package/dist/cjs/exact/server/index.js.map +1 -0
  35. package/dist/cjs/exact/v1/client/index.d.ts +38 -0
  36. package/dist/cjs/exact/v1/client/index.js +193 -0
  37. package/dist/cjs/exact/v1/client/index.js.map +1 -0
  38. package/dist/cjs/exact/v1/facilitator/index.d.ts +84 -0
  39. package/dist/cjs/exact/v1/facilitator/index.js +739 -0
  40. package/dist/cjs/exact/v1/facilitator/index.js.map +1 -0
  41. package/dist/cjs/facilitator/agent-wallet.d.ts +109 -0
  42. package/dist/cjs/facilitator/agent-wallet.js +105 -0
  43. package/dist/cjs/facilitator/agent-wallet.js.map +1 -0
  44. package/dist/cjs/index.d.ts +338 -0
  45. package/dist/cjs/index.js +2860 -0
  46. package/dist/cjs/index.js.map +1 -0
  47. package/dist/cjs/permit2-DK5A8alk.d.ts +729 -0
  48. package/dist/cjs/permit2-DhJRUcgY.d.ts +729 -0
  49. package/dist/cjs/rpc-DULZzRne.d.ts +13 -0
  50. package/dist/cjs/scheme-7ehldYoO.d.ts +307 -0
  51. package/dist/cjs/scheme-BjBJzHF7.d.ts +307 -0
  52. package/dist/cjs/scheme-DWgpkDgz.d.ts +47 -0
  53. package/dist/cjs/signer-BFelv8DL.d.ts +170 -0
  54. package/dist/cjs/storage-6W5MO46W.d.ts +50 -0
  55. package/dist/cjs/storage-CHNote8s.d.ts +81 -0
  56. package/dist/cjs/storage-DjCv5IPh.d.ts +81 -0
  57. package/dist/cjs/types-CKd3Xoi1.d.ts +180 -0
  58. package/dist/cjs/types-DIt9uAUy.d.ts +180 -0
  59. package/dist/cjs/upto/client/index.d.ts +34 -0
  60. package/dist/cjs/upto/client/index.js +509 -0
  61. package/dist/cjs/upto/client/index.js.map +1 -0
  62. package/dist/cjs/upto/facilitator/index.d.ts +54 -0
  63. package/dist/cjs/upto/facilitator/index.js +1313 -0
  64. package/dist/cjs/upto/facilitator/index.js.map +1 -0
  65. package/dist/cjs/upto/server/index.d.ts +69 -0
  66. package/dist/cjs/upto/server/index.js +296 -0
  67. package/dist/cjs/upto/server/index.js.map +1 -0
  68. package/dist/cjs/v1/index.d.ts +40 -0
  69. package/dist/cjs/v1/index.js +199 -0
  70. package/dist/cjs/v1/index.js.map +1 -0
  71. package/dist/esm/auth-capture/client/index.d.mts +44 -0
  72. package/dist/esm/auth-capture/client/index.mjs +8 -0
  73. package/dist/esm/auth-capture/client/index.mjs.map +1 -0
  74. package/dist/esm/batch-settlement/client/file-storage.d.mts +47 -0
  75. package/dist/esm/batch-settlement/client/file-storage.mjs +63 -0
  76. package/dist/esm/batch-settlement/client/file-storage.mjs.map +1 -0
  77. package/dist/esm/batch-settlement/client/index.d.mts +111 -0
  78. package/dist/esm/batch-settlement/client/index.mjs +58 -0
  79. package/dist/esm/batch-settlement/client/index.mjs.map +1 -0
  80. package/dist/esm/batch-settlement/facilitator/index.d.mts +72 -0
  81. package/dist/esm/batch-settlement/facilitator/index.mjs +1252 -0
  82. package/dist/esm/batch-settlement/facilitator/index.mjs.map +1 -0
  83. package/dist/esm/batch-settlement/server/file-storage.d.mts +53 -0
  84. package/dist/esm/batch-settlement/server/file-storage.mjs +128 -0
  85. package/dist/esm/batch-settlement/server/file-storage.mjs.map +1 -0
  86. package/dist/esm/batch-settlement/server/index.d.mts +491 -0
  87. package/dist/esm/batch-settlement/server/index.mjs +1640 -0
  88. package/dist/esm/batch-settlement/server/index.mjs.map +1 -0
  89. package/dist/esm/batch-settlement/server/redis-storage.d.mts +87 -0
  90. package/dist/esm/batch-settlement/server/redis-storage.mjs +156 -0
  91. package/dist/esm/batch-settlement/server/redis-storage.mjs.map +1 -0
  92. package/dist/esm/chunk-2EUQTNJO.mjs +38 -0
  93. package/dist/esm/chunk-2EUQTNJO.mjs.map +1 -0
  94. package/dist/esm/chunk-3WZF6722.mjs +36 -0
  95. package/dist/esm/chunk-3WZF6722.mjs.map +1 -0
  96. package/dist/esm/chunk-E4Z7PNXC.mjs +275 -0
  97. package/dist/esm/chunk-E4Z7PNXC.mjs.map +1 -0
  98. package/dist/esm/chunk-GQVMVP4N.mjs +911 -0
  99. package/dist/esm/chunk-GQVMVP4N.mjs.map +1 -0
  100. package/dist/esm/chunk-H2EYJIZL.mjs +489 -0
  101. package/dist/esm/chunk-H2EYJIZL.mjs.map +1 -0
  102. package/dist/esm/chunk-H3KPLYGI.mjs +152 -0
  103. package/dist/esm/chunk-H3KPLYGI.mjs.map +1 -0
  104. package/dist/esm/chunk-HYABYUBD.mjs +432 -0
  105. package/dist/esm/chunk-HYABYUBD.mjs.map +1 -0
  106. package/dist/esm/chunk-I2DVUHM5.mjs +123 -0
  107. package/dist/esm/chunk-I2DVUHM5.mjs.map +1 -0
  108. package/dist/esm/chunk-JK7SLLF7.mjs +34 -0
  109. package/dist/esm/chunk-JK7SLLF7.mjs.map +1 -0
  110. package/dist/esm/chunk-JNT7C46S.mjs +352 -0
  111. package/dist/esm/chunk-JNT7C46S.mjs.map +1 -0
  112. package/dist/esm/chunk-MACPBXCT.mjs +415 -0
  113. package/dist/esm/chunk-MACPBXCT.mjs.map +1 -0
  114. package/dist/esm/chunk-P3QOX3QZ.mjs +113 -0
  115. package/dist/esm/chunk-P3QOX3QZ.mjs.map +1 -0
  116. package/dist/esm/chunk-QVATVA3J.mjs +47 -0
  117. package/dist/esm/chunk-QVATVA3J.mjs.map +1 -0
  118. package/dist/esm/chunk-SHJFA25H.mjs +159 -0
  119. package/dist/esm/chunk-SHJFA25H.mjs.map +1 -0
  120. package/dist/esm/chunk-TW7Z65AO.mjs +34 -0
  121. package/dist/esm/chunk-TW7Z65AO.mjs.map +1 -0
  122. package/dist/esm/chunk-U4HCGTLU.mjs +35 -0
  123. package/dist/esm/chunk-U4HCGTLU.mjs.map +1 -0
  124. package/dist/esm/chunk-VS3RYAYE.mjs +80 -0
  125. package/dist/esm/chunk-VS3RYAYE.mjs.map +1 -0
  126. package/dist/esm/chunk-W6ON4LG2.mjs +39 -0
  127. package/dist/esm/chunk-W6ON4LG2.mjs.map +1 -0
  128. package/dist/esm/chunk-XG2JLZVJ.mjs +627 -0
  129. package/dist/esm/chunk-XG2JLZVJ.mjs.map +1 -0
  130. package/dist/esm/chunk-ZCJRY5LQ.mjs +162 -0
  131. package/dist/esm/chunk-ZCJRY5LQ.mjs.map +1 -0
  132. package/dist/esm/client/agent-wallet.d.mts +69 -0
  133. package/dist/esm/client/agent-wallet.mjs +36 -0
  134. package/dist/esm/client/agent-wallet.mjs.map +1 -0
  135. package/dist/esm/exact/client/index.d.mts +63 -0
  136. package/dist/esm/exact/client/index.mjs +25 -0
  137. package/dist/esm/exact/client/index.mjs.map +1 -0
  138. package/dist/esm/exact/facilitator/index.d.mts +141 -0
  139. package/dist/esm/exact/facilitator/index.mjs +694 -0
  140. package/dist/esm/exact/facilitator/index.mjs.map +1 -0
  141. package/dist/esm/exact/server/index.d.mts +118 -0
  142. package/dist/esm/exact/server/index.mjs +153 -0
  143. package/dist/esm/exact/server/index.mjs.map +1 -0
  144. package/dist/esm/exact/v1/client/index.d.mts +38 -0
  145. package/dist/esm/exact/v1/client/index.mjs +12 -0
  146. package/dist/esm/exact/v1/client/index.mjs.map +1 -0
  147. package/dist/esm/exact/v1/facilitator/index.d.mts +84 -0
  148. package/dist/esm/exact/v1/facilitator/index.mjs +12 -0
  149. package/dist/esm/exact/v1/facilitator/index.mjs.map +1 -0
  150. package/dist/esm/facilitator/agent-wallet.d.mts +109 -0
  151. package/dist/esm/facilitator/agent-wallet.mjs +74 -0
  152. package/dist/esm/facilitator/agent-wallet.mjs.map +1 -0
  153. package/dist/esm/index.d.mts +338 -0
  154. package/dist/esm/index.mjs +144 -0
  155. package/dist/esm/index.mjs.map +1 -0
  156. package/dist/esm/permit2-DhJRUcgY.d.mts +729 -0
  157. package/dist/esm/rpc-DULZzRne.d.mts +13 -0
  158. package/dist/esm/scheme-CkNhpXrG.d.mts +307 -0
  159. package/dist/esm/scheme-D8ZbykGV.d.mts +47 -0
  160. package/dist/esm/signer-BFelv8DL.d.mts +170 -0
  161. package/dist/esm/storage-6W5MO46W.d.mts +50 -0
  162. package/dist/esm/storage-BEzTEiUr.d.mts +81 -0
  163. package/dist/esm/types-DIt9uAUy.d.mts +180 -0
  164. package/dist/esm/upto/client/index.d.mts +34 -0
  165. package/dist/esm/upto/client/index.mjs +22 -0
  166. package/dist/esm/upto/client/index.mjs.map +1 -0
  167. package/dist/esm/upto/facilitator/index.d.mts +54 -0
  168. package/dist/esm/upto/facilitator/index.mjs +507 -0
  169. package/dist/esm/upto/facilitator/index.mjs.map +1 -0
  170. package/dist/esm/upto/server/index.d.mts +69 -0
  171. package/dist/esm/upto/server/index.mjs +124 -0
  172. package/dist/esm/upto/server/index.mjs.map +1 -0
  173. package/dist/esm/v1/index.d.mts +40 -0
  174. package/dist/esm/v1/index.mjs +18 -0
  175. package/dist/esm/v1/index.mjs.map +1 -0
  176. package/package.json +250 -0
@@ -0,0 +1,170 @@
1
+ import { Log } from 'viem';
2
+
3
+ /**
4
+ * ClientEvmSigner - Used by x402 clients to sign payment authorizations.
5
+ *
6
+ * Typically a viem WalletClient extended with publicActions:
7
+ * ```typescript
8
+ * const client = createWalletClient({
9
+ * account: privateKeyToAccount('0x...'),
10
+ * chain: baseSepolia,
11
+ * transport: http(),
12
+ * }).extend(publicActions);
13
+ * ```
14
+ *
15
+ * Or composed via `toClientEvmSigner(account, publicClient)`.
16
+ */
17
+ type ClientEvmSigner = {
18
+ readonly address: `0x${string}`;
19
+ signTypedData(message: {
20
+ domain: Record<string, unknown>;
21
+ types: Record<string, unknown>;
22
+ primaryType: string;
23
+ message: Record<string, unknown>;
24
+ }): Promise<`0x${string}`>;
25
+ /**
26
+ * Optional on-chain reads.
27
+ * Required only for extension enrichment (EIP-2612 / ERC-20 approval).
28
+ */
29
+ readContract?(args: {
30
+ address: `0x${string}`;
31
+ abi: readonly unknown[];
32
+ functionName: string;
33
+ args?: readonly unknown[];
34
+ }): Promise<unknown>;
35
+ /**
36
+ * Optional: Signs a raw EIP-1559 transaction without broadcasting.
37
+ * Required for ERC-20 approval gas sponsoring when the token lacks EIP-2612.
38
+ */
39
+ signTransaction?(args: {
40
+ to: `0x${string}`;
41
+ data: `0x${string}`;
42
+ nonce: number;
43
+ gas: bigint;
44
+ maxFeePerGas: bigint;
45
+ maxPriorityFeePerGas: bigint;
46
+ chainId: number;
47
+ }): Promise<`0x${string}`>;
48
+ /**
49
+ * Optional: Gets the current transaction count (nonce) for an address.
50
+ * Required for ERC-20 approval gas sponsoring.
51
+ */
52
+ getTransactionCount?(args: {
53
+ address: `0x${string}`;
54
+ }): Promise<number>;
55
+ /**
56
+ * Optional: Estimates current gas fees per gas.
57
+ * Required for ERC-20 approval gas sponsoring.
58
+ */
59
+ estimateFeesPerGas?(): Promise<{
60
+ maxFeePerGas: bigint;
61
+ maxPriorityFeePerGas: bigint;
62
+ }>;
63
+ };
64
+ /**
65
+ * FacilitatorEvmSigner - Used by x402 facilitators to verify and settle payments
66
+ * This is typically a viem PublicClient + WalletClient combination that can
67
+ * read contract state, verify signatures, write transactions, and wait for receipts
68
+ *
69
+ * Supports multiple addresses for load balancing, key rotation, and high availability
70
+ */
71
+ type FacilitatorEvmSigner = {
72
+ /**
73
+ * Get all addresses this facilitator can use for signing
74
+ * Enables dynamic address selection for load balancing and key rotation
75
+ */
76
+ getAddresses(): readonly `0x${string}`[];
77
+ readContract(args: {
78
+ address: `0x${string}`;
79
+ abi: readonly unknown[];
80
+ functionName: string;
81
+ args?: readonly unknown[];
82
+ }): Promise<unknown>;
83
+ verifyTypedData(args: {
84
+ address: `0x${string}`;
85
+ domain: Record<string, unknown>;
86
+ types: Record<string, unknown>;
87
+ primaryType: string;
88
+ message: Record<string, unknown>;
89
+ signature: `0x${string}`;
90
+ }): Promise<boolean>;
91
+ writeContract(args: {
92
+ address: `0x${string}`;
93
+ abi: readonly unknown[];
94
+ functionName: string;
95
+ args: readonly unknown[];
96
+ gas?: bigint;
97
+ dataSuffix?: `0x${string}`;
98
+ }): Promise<`0x${string}`>;
99
+ sendTransaction(args: {
100
+ to: `0x${string}`;
101
+ data: `0x${string}`;
102
+ }): Promise<`0x${string}`>;
103
+ waitForTransactionReceipt(args: {
104
+ hash: `0x${string}`;
105
+ }): Promise<{
106
+ status: string;
107
+ logs?: readonly Log[];
108
+ }>;
109
+ getCode(args: {
110
+ address: `0x${string}`;
111
+ }): Promise<`0x${string}` | undefined>;
112
+ };
113
+ /**
114
+ * Composes a ClientEvmSigner from a local account and a public client.
115
+ *
116
+ * Use this when your signer (e.g., `privateKeyToAccount`) doesn't have
117
+ * `readContract`. The `publicClient` provides the on-chain read capability.
118
+ *
119
+ * Alternatively, use a WalletClient extended with publicActions directly:
120
+ * ```typescript
121
+ * const signer = createWalletClient({
122
+ * account: privateKeyToAccount('0x...'),
123
+ * chain: baseSepolia,
124
+ * transport: http(),
125
+ * }).extend(publicActions);
126
+ * ```
127
+ *
128
+ * @param signer - A signer with `address` and `signTypedData` (and optionally `readContract`)
129
+ * @param publicClient - A client with optional read/nonce/fee helpers
130
+ * @param publicClient.readContract - The readContract method from the public client
131
+ * @param publicClient.getTransactionCount - Optional getTransactionCount for ERC-20 approval
132
+ * @param publicClient.estimateFeesPerGas - Optional estimateFeesPerGas for ERC-20 approval
133
+ * @returns A ClientEvmSigner with any available optional capabilities
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const account = privateKeyToAccount("0x...");
138
+ * const publicClient = createPublicClient({ chain: baseSepolia, transport: http() });
139
+ * const signer = toClientEvmSigner(account, publicClient);
140
+ * ```
141
+ */
142
+ declare function toClientEvmSigner(signer: Omit<ClientEvmSigner, "readContract"> & {
143
+ readContract?: ClientEvmSigner["readContract"];
144
+ }, publicClient?: {
145
+ readContract(args: {
146
+ address: `0x${string}`;
147
+ abi: readonly unknown[];
148
+ functionName: string;
149
+ args?: readonly unknown[];
150
+ }): Promise<unknown>;
151
+ getTransactionCount?(args: {
152
+ address: `0x${string}`;
153
+ }): Promise<number>;
154
+ estimateFeesPerGas?(): Promise<{
155
+ maxFeePerGas: bigint;
156
+ maxPriorityFeePerGas: bigint;
157
+ }>;
158
+ }): ClientEvmSigner;
159
+ /**
160
+ * Converts a viem client with single address to a FacilitatorEvmSigner
161
+ * Wraps the single address in a getAddresses() function for compatibility
162
+ *
163
+ * @param client - The client to convert (must have 'address' property)
164
+ * @returns FacilitatorEvmSigner with getAddresses() support
165
+ */
166
+ declare function toFacilitatorEvmSigner(client: Omit<FacilitatorEvmSigner, "getAddresses"> & {
167
+ address: `0x${string}`;
168
+ }): FacilitatorEvmSigner;
169
+
170
+ export { type ClientEvmSigner as C, type FacilitatorEvmSigner as F, toFacilitatorEvmSigner as a, toClientEvmSigner as t };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Client-side channel fields mirrored from PAYMENT-RESPONSE / recovery flows.
3
+ */
4
+ interface BatchSettlementClientContext {
5
+ /** Current cumulative amount charged by the server for this channel */
6
+ chargedCumulativeAmount?: string;
7
+ /** Current onchain channel balance */
8
+ balance?: string;
9
+ /** Total claimed onchain */
10
+ totalClaimed?: string;
11
+ /** Latest client-signed maxClaimableAmount cap (after corrective recovery, optional) */
12
+ signedMaxClaimable?: string;
13
+ /** Client voucher signature for {@link signedMaxClaimable} (optional) */
14
+ signature?: `0x${string}`;
15
+ }
16
+ interface ClientChannelStorage {
17
+ get(key: string): Promise<BatchSettlementClientContext | undefined>;
18
+ set(key: string, context: BatchSettlementClientContext): Promise<void>;
19
+ delete(key: string): Promise<void>;
20
+ }
21
+ /**
22
+ * Default in-memory {@link ClientChannelStorage} (channel records do not survive process restart).
23
+ */
24
+ declare class InMemoryClientChannelStorage implements ClientChannelStorage {
25
+ private readonly channels;
26
+ /**
27
+ * Returns the channel record for `key` if present.
28
+ *
29
+ * @param key - Channel storage key (channelId).
30
+ * @returns Persisted context or undefined.
31
+ */
32
+ get(key: string): Promise<BatchSettlementClientContext | undefined>;
33
+ /**
34
+ * Stores or replaces the channel record for `key`.
35
+ *
36
+ * @param key - Channel storage key.
37
+ * @param context - Channel fields to persist.
38
+ * @returns Resolves when stored.
39
+ */
40
+ set(key: string, context: BatchSettlementClientContext): Promise<void>;
41
+ /**
42
+ * Removes the channel record for `key` if it exists.
43
+ *
44
+ * @param key - Channel storage key.
45
+ * @returns Resolves when removed.
46
+ */
47
+ delete(key: string): Promise<void>;
48
+ }
49
+
50
+ export { type BatchSettlementClientContext as B, type ClientChannelStorage as C, InMemoryClientChannelStorage as I };
@@ -0,0 +1,81 @@
1
+ import { C as ChannelConfig } from './types-CKd3Xoi1.js';
2
+
3
+ interface Channel {
4
+ channelId: string;
5
+ channelConfig: ChannelConfig;
6
+ chargedCumulativeAmount: string;
7
+ signedMaxClaimable: string;
8
+ signature: string;
9
+ balance: string;
10
+ totalClaimed: string;
11
+ withdrawRequestedAt: number;
12
+ refundNonce: number;
13
+ onchainSyncedAt?: number;
14
+ lastRequestTimestamp: number;
15
+ pendingRequest?: PendingRequest;
16
+ }
17
+ interface PendingRequest {
18
+ pendingId: string;
19
+ signedMaxClaimable: string;
20
+ expiresAt: number;
21
+ }
22
+ interface ChannelUpdateResult {
23
+ channel: Channel | undefined;
24
+ status: "updated" | "unchanged" | "deleted";
25
+ }
26
+ interface ChannelStorage {
27
+ get(channelId: string): Promise<Channel | undefined>;
28
+ list(): Promise<Channel[]>;
29
+ /**
30
+ * Atomically inspects and mutates a channel record.
31
+ *
32
+ * Implementations must guarantee that no concurrent mutation can interleave between
33
+ * reading `current` and writing the callback result for all application instances that
34
+ * share the backend. The in-memory backend only provides this guarantee inside one JS
35
+ * runtime; production multi-instance deployments need storage with backend-level atomic
36
+ * conditional mutation, such as Redis/Valkey Lua scripts, SQL transactions, or Durable Objects.
37
+ *
38
+ * @param channelId - The channel identifier.
39
+ * @param update - Mutation callback. Return `undefined` to delete, or `current` to leave unchanged.
40
+ * @returns The final stored channel and whether storage updated, stayed unchanged, or deleted.
41
+ */
42
+ updateChannel(channelId: string, update: (current: Channel | undefined) => Channel | undefined): Promise<ChannelUpdateResult>;
43
+ }
44
+ /**
45
+ * In-memory {@link ChannelStorage} backed by a Map keyed by `channelId`.
46
+ */
47
+ declare class InMemoryChannelStorage implements ChannelStorage {
48
+ private readonly channels;
49
+ private readonly channelLocks;
50
+ /**
51
+ * Returns the channel record for a channel, if present.
52
+ *
53
+ * @param channelId - The channel identifier.
54
+ * @returns The channel record or undefined when not found.
55
+ */
56
+ get(channelId: string): Promise<Channel | undefined>;
57
+ /**
58
+ * Lists all stored channel records.
59
+ *
60
+ * @returns All channel records in storage.
61
+ */
62
+ list(): Promise<Channel[]>;
63
+ /**
64
+ * Atomically inspects and mutates a channel record while holding a per-channel lock.
65
+ *
66
+ * @param channelId - The channel identifier.
67
+ * @param update - Mutation callback. Return `undefined` to delete, or `current` to leave unchanged.
68
+ * @returns The final stored channel and whether storage updated, stayed unchanged, or deleted.
69
+ */
70
+ updateChannel(channelId: string, update: (current: Channel | undefined) => Channel | undefined): Promise<ChannelUpdateResult>;
71
+ /**
72
+ * Runs `fn` after any prior locked work for the same channel key has finished.
73
+ *
74
+ * @param key - Lowercased channel id used as the lock key.
75
+ * @param fn - Async work to run while holding the logical per-channel lock.
76
+ * @returns The resolved result of `fn`.
77
+ */
78
+ private withChannelLock;
79
+ }
80
+
81
+ export { type ChannelStorage as C, InMemoryChannelStorage as I, type PendingRequest as P, type Channel as a, type ChannelUpdateResult as b };
@@ -0,0 +1,81 @@
1
+ import { C as ChannelConfig } from './types-DIt9uAUy.js';
2
+
3
+ interface Channel {
4
+ channelId: string;
5
+ channelConfig: ChannelConfig;
6
+ chargedCumulativeAmount: string;
7
+ signedMaxClaimable: string;
8
+ signature: string;
9
+ balance: string;
10
+ totalClaimed: string;
11
+ withdrawRequestedAt: number;
12
+ refundNonce: number;
13
+ onchainSyncedAt?: number;
14
+ lastRequestTimestamp: number;
15
+ pendingRequest?: PendingRequest;
16
+ }
17
+ interface PendingRequest {
18
+ pendingId: string;
19
+ signedMaxClaimable: string;
20
+ expiresAt: number;
21
+ }
22
+ interface ChannelUpdateResult {
23
+ channel: Channel | undefined;
24
+ status: "updated" | "unchanged" | "deleted";
25
+ }
26
+ interface ChannelStorage {
27
+ get(channelId: string): Promise<Channel | undefined>;
28
+ list(): Promise<Channel[]>;
29
+ /**
30
+ * Atomically inspects and mutates a channel record.
31
+ *
32
+ * Implementations must guarantee that no concurrent mutation can interleave between
33
+ * reading `current` and writing the callback result for all application instances that
34
+ * share the backend. The in-memory backend only provides this guarantee inside one JS
35
+ * runtime; production multi-instance deployments need storage with backend-level atomic
36
+ * conditional mutation, such as Redis/Valkey Lua scripts, SQL transactions, or Durable Objects.
37
+ *
38
+ * @param channelId - The channel identifier.
39
+ * @param update - Mutation callback. Return `undefined` to delete, or `current` to leave unchanged.
40
+ * @returns The final stored channel and whether storage updated, stayed unchanged, or deleted.
41
+ */
42
+ updateChannel(channelId: string, update: (current: Channel | undefined) => Channel | undefined): Promise<ChannelUpdateResult>;
43
+ }
44
+ /**
45
+ * In-memory {@link ChannelStorage} backed by a Map keyed by `channelId`.
46
+ */
47
+ declare class InMemoryChannelStorage implements ChannelStorage {
48
+ private readonly channels;
49
+ private readonly channelLocks;
50
+ /**
51
+ * Returns the channel record for a channel, if present.
52
+ *
53
+ * @param channelId - The channel identifier.
54
+ * @returns The channel record or undefined when not found.
55
+ */
56
+ get(channelId: string): Promise<Channel | undefined>;
57
+ /**
58
+ * Lists all stored channel records.
59
+ *
60
+ * @returns All channel records in storage.
61
+ */
62
+ list(): Promise<Channel[]>;
63
+ /**
64
+ * Atomically inspects and mutates a channel record while holding a per-channel lock.
65
+ *
66
+ * @param channelId - The channel identifier.
67
+ * @param update - Mutation callback. Return `undefined` to delete, or `current` to leave unchanged.
68
+ * @returns The final stored channel and whether storage updated, stayed unchanged, or deleted.
69
+ */
70
+ updateChannel(channelId: string, update: (current: Channel | undefined) => Channel | undefined): Promise<ChannelUpdateResult>;
71
+ /**
72
+ * Runs `fn` after any prior locked work for the same channel key has finished.
73
+ *
74
+ * @param key - Lowercased channel id used as the lock key.
75
+ * @param fn - Async work to run while holding the logical per-channel lock.
76
+ * @returns The resolved result of `fn`.
77
+ */
78
+ private withChannelLock;
79
+ }
80
+
81
+ export { type ChannelStorage as C, InMemoryChannelStorage as I, type PendingRequest as P, type Channel as a, type ChannelUpdateResult as b };
@@ -0,0 +1,180 @@
1
+ import { TypedData } from 'viem';
2
+
3
+ interface AuthorizerSigner {
4
+ address: `0x${string}`;
5
+ signTypedData(params: {
6
+ domain: Record<string, unknown>;
7
+ types: TypedData;
8
+ primaryType: string;
9
+ message: Record<string, unknown>;
10
+ }): Promise<`0x${string}`>;
11
+ }
12
+ type ChannelState = {
13
+ balance: bigint;
14
+ totalClaimed: bigint;
15
+ withdrawRequestedAt: number;
16
+ refundNonce: bigint;
17
+ };
18
+ type ChannelConfig = {
19
+ payer: `0x${string}`;
20
+ payerAuthorizer: `0x${string}`;
21
+ receiver: `0x${string}`;
22
+ receiverAuthorizer: `0x${string}`;
23
+ token: `0x${string}`;
24
+ withdrawDelay: number;
25
+ salt: `0x${string}`;
26
+ };
27
+ type BatchSettlementErc3009Authorization = {
28
+ validAfter: string;
29
+ validBefore: string;
30
+ salt: `0x${string}`;
31
+ signature: `0x${string}`;
32
+ };
33
+ type BatchSettlementPermit2Authorization = {
34
+ from: `0x${string}`;
35
+ permitted: {
36
+ token: `0x${string}`;
37
+ amount: string;
38
+ };
39
+ spender: `0x${string}`;
40
+ nonce: string;
41
+ deadline: string;
42
+ witness: {
43
+ channelId: `0x${string}`;
44
+ };
45
+ signature: `0x${string}`;
46
+ };
47
+ type BatchSettlementAssetTransferMethod = "eip3009" | "permit2";
48
+ type BatchSettlementDepositAuthorization = {
49
+ erc3009Authorization: BatchSettlementErc3009Authorization;
50
+ permit2Authorization?: never;
51
+ } | {
52
+ erc3009Authorization?: never;
53
+ permit2Authorization: BatchSettlementPermit2Authorization;
54
+ };
55
+ type BatchSettlementDepositPayload = {
56
+ type: "deposit";
57
+ channelConfig: ChannelConfig;
58
+ voucher: BatchSettlementVoucherFields;
59
+ deposit: {
60
+ amount: string;
61
+ authorization: BatchSettlementDepositAuthorization;
62
+ };
63
+ };
64
+ type BatchSettlementVoucherPayload = {
65
+ type: "voucher";
66
+ channelConfig: ChannelConfig;
67
+ voucher: BatchSettlementVoucherFields;
68
+ };
69
+ type BatchSettlementRefundPayload = {
70
+ type: "refund";
71
+ channelConfig: ChannelConfig;
72
+ voucher: BatchSettlementVoucherFields;
73
+ amount?: string;
74
+ };
75
+ type BatchSettlementVoucherFields = {
76
+ channelId: `0x${string}`;
77
+ maxClaimableAmount: string;
78
+ signature: `0x${string}`;
79
+ };
80
+ type BatchSettlementVoucherClaim = {
81
+ voucher: {
82
+ channel: ChannelConfig;
83
+ maxClaimableAmount: string;
84
+ };
85
+ signature: `0x${string}`;
86
+ totalClaimed: string;
87
+ };
88
+ type BatchSettlementChannelStateExtra = {
89
+ channelId: `0x${string}`;
90
+ balance: string;
91
+ totalClaimed: string;
92
+ withdrawRequestedAt: number;
93
+ refundNonce: string;
94
+ chargedCumulativeAmount?: string;
95
+ };
96
+ type BatchSettlementVoucherStateExtra = {
97
+ signedMaxClaimable?: string;
98
+ signature?: `0x${string}`;
99
+ };
100
+ type BatchSettlementPaymentRequirementsExtra = {
101
+ receiverAuthorizer: `0x${string}`;
102
+ withdrawDelay: number;
103
+ name: string;
104
+ version: string;
105
+ assetTransferMethod?: BatchSettlementAssetTransferMethod;
106
+ channelState?: BatchSettlementChannelStateExtra;
107
+ voucherState?: BatchSettlementVoucherStateExtra;
108
+ };
109
+ type FileChannelStorageOptions = {
110
+ /** Root directory; channels are stored under `{directory}/{client|server}/{channelId}.json`. */
111
+ directory: string;
112
+ };
113
+ type BatchSettlementPaymentResponseExtra = {
114
+ chargedAmount?: string;
115
+ channelState?: BatchSettlementChannelStateExtra;
116
+ voucherState?: BatchSettlementVoucherStateExtra;
117
+ };
118
+ type BatchSettlementClaimPayload = {
119
+ type: "claim";
120
+ claims: BatchSettlementVoucherClaim[];
121
+ claimAuthorizerSignature?: `0x${string}`;
122
+ };
123
+ type BatchSettlementSettlePayload = {
124
+ type: "settle";
125
+ receiver: `0x${string}`;
126
+ token: `0x${string}`;
127
+ };
128
+ type BatchSettlementEnrichedRefundPayload = BatchSettlementRefundPayload & {
129
+ amount: string;
130
+ refundNonce: string;
131
+ claims: BatchSettlementVoucherClaim[];
132
+ refundAuthorizerSignature?: `0x${string}`;
133
+ claimAuthorizerSignature?: `0x${string}`;
134
+ };
135
+ type BatchSettlementPayload = BatchSettlementDepositPayload | BatchSettlementVoucherPayload | BatchSettlementRefundPayload;
136
+ type BatchSettlementFacilitatorSettlePayload = BatchSettlementDepositPayload | BatchSettlementClaimPayload | BatchSettlementSettlePayload | BatchSettlementEnrichedRefundPayload;
137
+ /**
138
+ * Type guard for {@link BatchSettlementDepositPayload}.
139
+ *
140
+ * @param payload - Unknown payload to check.
141
+ * @returns True if `payload` is a deposit payload (carries `deposit` and `voucher`).
142
+ */
143
+ declare function isBatchSettlementDepositPayload(payload: unknown): payload is BatchSettlementDepositPayload;
144
+ /**
145
+ * Type guard for {@link BatchSettlementVoucherPayload}.
146
+ *
147
+ * @param payload - Unknown payload to check.
148
+ * @returns True if `payload` is a voucher payload with channel and signature fields.
149
+ */
150
+ declare function isBatchSettlementVoucherPayload(payload: unknown): payload is BatchSettlementVoucherPayload;
151
+ /**
152
+ * Type guard for {@link BatchSettlementRefundPayload}.
153
+ *
154
+ * @param payload - Unknown payload to check.
155
+ * @returns True if `payload` is a refund payload with channel config and voucher fields.
156
+ */
157
+ declare function isBatchSettlementRefundPayload(payload: unknown): payload is BatchSettlementRefundPayload;
158
+ /**
159
+ * Type guard for {@link BatchSettlementClaimPayload}.
160
+ *
161
+ * @param payload - Unknown payload to check.
162
+ * @returns True if `payload` is a settle-action `claimWithSignature` payload.
163
+ */
164
+ declare function isBatchSettlementClaimPayload(payload: unknown): payload is BatchSettlementClaimPayload;
165
+ /**
166
+ * Type guard for {@link BatchSettlementSettlePayload}.
167
+ *
168
+ * @param payload - Unknown payload to check.
169
+ * @returns True if `payload` is a settle-action `settle` payload.
170
+ */
171
+ declare function isBatchSettlementSettlePayload(payload: unknown): payload is BatchSettlementSettlePayload;
172
+ /**
173
+ * Type guard for {@link BatchSettlementEnrichedRefundPayload}.
174
+ *
175
+ * @param payload - Unknown payload to check.
176
+ * @returns True if `payload` is a settle-action `refundWithSignature` payload.
177
+ */
178
+ declare function isBatchSettlementEnrichedRefundPayload(payload: unknown): payload is BatchSettlementEnrichedRefundPayload;
179
+
180
+ export { type AuthorizerSigner as A, type BatchSettlementVoucherClaim as B, type ChannelConfig as C, type FileChannelStorageOptions as F, type BatchSettlementVoucherFields as a, type BatchSettlementChannelStateExtra as b, type BatchSettlementVoucherStateExtra as c, type BatchSettlementClaimPayload as d, type BatchSettlementDepositPayload as e, type BatchSettlementEnrichedRefundPayload as f, type BatchSettlementErc3009Authorization as g, type BatchSettlementFacilitatorSettlePayload as h, type BatchSettlementPayload as i, type BatchSettlementPaymentRequirementsExtra as j, type BatchSettlementPaymentResponseExtra as k, type BatchSettlementRefundPayload as l, type BatchSettlementSettlePayload as m, type BatchSettlementVoucherPayload as n, type ChannelState as o, isBatchSettlementClaimPayload as p, isBatchSettlementDepositPayload as q, isBatchSettlementEnrichedRefundPayload as r, isBatchSettlementRefundPayload as s, isBatchSettlementSettlePayload as t, isBatchSettlementVoucherPayload as u };