Skip to content

Commit a681ab6

Browse files
committed
rename session -> channel
1 parent ac47a39 commit a681ab6

28 files changed

Lines changed: 403 additions & 418 deletions

File tree

examples/typescript/clients/batch-settlement-streaming/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { isBatchSettlementDepositPayload, toClientEvmSigner } from "@x402/evm";
22
import {
33
BatchSettlementEvmScheme,
4-
FileClientSessionStorage,
5-
InMemoryClientSessionStorage,
4+
FileClientChannelStorage,
5+
InMemoryClientChannelStorage,
66
computeChannelId,
77
} from "@x402/evm/batch-settlement/client";
8-
import type { ClientSessionStorage } from "@x402/evm/batch-settlement/client";
8+
import type { ClientChannelStorage } from "@x402/evm/batch-settlement/client";
99
import { x402Client, x402HTTPClient } from "@x402/fetch";
1010
import {
1111
decodePaymentRequiredHeader,
@@ -68,14 +68,14 @@ const voucherSigner = evmVoucherSignerPrivateKey
6868

6969
const effectiveVoucherSigner = voucherSigner ?? signer;
7070

71-
const sessionStorage: ClientSessionStorage = storageDir
72-
? new FileClientSessionStorage({ directory: storageDir })
73-
: new InMemoryClientSessionStorage();
71+
const channelStorage: ClientChannelStorage = storageDir
72+
? new FileClientChannelStorage({ directory: storageDir })
73+
: new InMemoryClientChannelStorage();
7474

7575
const batchedScheme = new BatchSettlementEvmScheme(signer, {
7676
depositPolicy,
7777
salt: channelSalt,
78-
storage: sessionStorage,
78+
storage: channelStorage,
7979
...(voucherSigner ? { voucherSigner } : {}),
8080
});
8181

examples/typescript/clients/batch-settlement/concurrent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { toClientEvmSigner } from "@x402/evm";
22
import {
33
BatchSettlementEvmScheme,
4-
FileClientSessionStorage,
4+
FileClientChannelStorage,
55
} from "@x402/evm/batch-settlement/client";
66
import { x402Client, wrapFetchWithPayment, x402HTTPClient } from "@x402/fetch";
77
import { config } from "dotenv";
@@ -77,7 +77,7 @@ async function main(): Promise<void> {
7777
depositPolicy: { maxDeposit: "1000000", depositMultiplier: 5 },
7878
salt,
7979
...(voucherSigner ? { voucherSigner } : {}),
80-
...(storageDir ? { storage: new FileClientSessionStorage({ directory: storageDir }) } : {}),
80+
...(storageDir ? { storage: new FileClientChannelStorage({ directory: storageDir }) } : {}),
8181
});
8282

8383
const client = new x402Client();

examples/typescript/clients/batch-settlement/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { toClientEvmSigner } from "@x402/evm";
22
import {
33
BatchSettlementEvmScheme,
4-
FileClientSessionStorage,
4+
FileClientChannelStorage,
55
} from "@x402/evm/batch-settlement/client";
66
import { x402Client, wrapFetchWithPayment, x402HTTPClient } from "@x402/fetch";
77
import { config } from "dotenv";
@@ -60,7 +60,7 @@ async function main(): Promise<void> {
6060
},
6161
salt: channelSalt,
6262
...(voucherSigner ? { voucherSigner } : {}),
63-
...(storageDir ? { storage: new FileClientSessionStorage({ directory: storageDir }) } : {}),
63+
...(storageDir ? { storage: new FileClientChannelStorage({ directory: storageDir }) } : {}),
6464
});
6565

6666
const client = new x402Client();

examples/typescript/servers/batch-settlement-streaming/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
encodePaymentResponseHeader,
66
} from "@x402/core/http";
77
import type { PaymentPayload, SettleResponse } from "@x402/core/types";
8-
import { BatchSettlementEvmScheme, FileSessionStorage } from "@x402/evm/batch-settlement/server";
8+
import { BatchSettlementEvmScheme, FileChannelStorage } from "@x402/evm/batch-settlement/server";
99
import {
1010
isBatchSettlementDepositPayload,
1111
isBatchSettlementVoucherPayload,
@@ -85,7 +85,7 @@ const facilitatorClient = new HTTPFacilitatorClient({ url: facilitatorUrl });
8585
const batchedScheme = new BatchSettlementEvmScheme(evmAddress, {
8686
...(receiverAuthorizerSigner ? { receiverAuthorizerSigner } : {}),
8787
withdrawDelay,
88-
...(storageDir ? { storage: new FileSessionStorage({ directory: storageDir }) } : {}),
88+
...(storageDir ? { storage: new FileChannelStorage({ directory: storageDir }) } : {}),
8989
});
9090

9191
const resourceServer = new x402ResourceServer(facilitatorClient).register(NETWORK, batchedScheme);

examples/typescript/servers/batch-settlement/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HTTPFacilitatorClient } from "@x402/core/server";
2-
import { BatchSettlementEvmScheme, FileSessionStorage } from "@x402/evm/batch-settlement/server";
2+
import { BatchSettlementEvmScheme, FileChannelStorage } from "@x402/evm/batch-settlement/server";
33
import { paymentMiddleware, setSettlementOverrides, x402ResourceServer } from "@x402/express";
44
import { config } from "dotenv";
55
import express from "express";
@@ -36,7 +36,7 @@ const facilitatorClient = new HTTPFacilitatorClient({ url: facilitatorUrl });
3636
const batchedScheme = new BatchSettlementEvmScheme(evmAddress, {
3737
...(receiverAuthorizerSigner ? { receiverAuthorizerSigner } : {}),
3838
withdrawDelay,
39-
...(storageDir ? { storage: new FileSessionStorage({ directory: storageDir }) } : {}),
39+
...(storageDir ? { storage: new FileChannelStorage({ directory: storageDir }) } : {}),
4040
});
4141

4242
const resourceServer = new x402ResourceServer(facilitatorClient).register(NETWORK, batchedScheme);

typescript/packages/mechanisms/evm/src/batch-settlement/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ The server claims any outstanding vouchers and then executes `refundWithSignatur
7373

7474
### Persistence
7575

76-
By default, channel state is stored in memory. For long-lived clients, use `FileClientSessionStorage`:
76+
By default, channel state is stored in memory. For long-lived clients, use `FileClientChannelStorage`:
7777

7878
```typescript
79-
import { FileClientSessionStorage } from "@x402/evm/batch-settlement/client";
79+
import { FileClientChannelStorage } from "@x402/evm/batch-settlement/client";
8080

8181
const scheme = new BatchSettlementEvmScheme(signer, {
82-
storage: new FileClientSessionStorage({ directory: "./channels" }),
82+
storage: new FileClientChannelStorage({ directory: "./channels" }),
8383
});
8484
```
8585

@@ -93,13 +93,13 @@ Register the scheme with an `x402ResourceServer` and pair it with a `ChannelMana
9393
import { x402ResourceServer } from "@x402/core/server";
9494
import {
9595
BatchSettlementEvmScheme,
96-
FileSessionStorage,
96+
FileChannelStorage,
9797
} from "@x402/evm/batch-settlement/server";
9898

9999
const scheme = new BatchSettlementEvmScheme(receiverAddress, {
100100
receiverAuthorizerSigner, // optional: self-managed authorizer (recommended)
101101
withdrawDelay: 900, // 15 min – 30 days
102-
storage: new FileSessionStorage({ directory: "./sessions" }),
102+
storage: new FileChannelStorage({ directory: "./channels" }),
103103
});
104104

105105
const server = new x402ResourceServer(facilitatorClient).register("eip155:84532", scheme);

typescript/packages/mechanisms/evm/src/batch-settlement/client/session.ts renamed to typescript/packages/mechanisms/evm/src/batch-settlement/client/channel.ts

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ import { batchSettlementABI } from "../abi";
66
import { BATCH_SETTLEMENT_ADDRESS, MIN_WITHDRAW_DELAY } from "../constants";
77
import type { BatchSettlementPaymentRequirementsExtra, ChannelConfig } from "../types";
88
import { computeChannelId } from "../utils";
9-
import type { BatchSettlementClientContext, ClientSessionStorage } from "./storage";
9+
import type { BatchSettlementClientContext, ClientChannelStorage } from "./storage";
1010

1111
/**
12-
* Runtime dependency bag shared by every storage-bound client helper (session,
13-
* recovery, refund) and the {@link BatchSettlementEvmScheme} class. Carries the
14-
* live signer + storage plus the channel-id inputs (`salt`, `payerAuthorizer`,
15-
* optional separate `voucherSigner`).
12+
* Runtime dependency bag shared by every storage-bound client helper (channel,
13+
* recovery, refund) and the {@link BatchSettlementEvmScheme} class.
1614
*/
1715
export interface BatchSettlementClientDeps {
1816
signer: ClientEvmSigner;
19-
storage: ClientSessionStorage;
17+
storage: ClientChannelStorage;
2018
salt: `0x${string}`;
2119
payerAuthorizer?: `0x${string}`;
2220
voucherSigner?: ClientEvmSigner;
@@ -53,13 +51,13 @@ export function buildChannelConfig(
5351
}
5452

5553
/**
56-
* Updates local session state from a parsed `SettleResponse`.
54+
* Updates local channel state from a parsed `SettleResponse`.
5755
*
58-
* @param storage - Client session storage.
56+
* @param storage - Client channel storage.
5957
* @param settle - The parsed settle response.
6058
*/
6159
export async function processSettleResponse(
62-
storage: ClientSessionStorage,
60+
storage: ClientChannelStorage,
6361
settle: SettleResponse,
6462
): Promise<void> {
6563
const extra = settle.extra ?? {};
@@ -70,7 +68,7 @@ export async function processSettleResponse(
7068
const key = channelId.toLowerCase();
7169

7270
if (extra.refund === true) {
73-
await updateSessionAfterRefund(storage, key, extra);
71+
await updateChannelAfterRefund(storage, key, extra);
7472
return;
7573
}
7674

@@ -91,18 +89,18 @@ export async function processSettleResponse(
9189
}
9290

9391
/**
94-
* Reconciles local session state with the outcome of a cooperative refund.
92+
* Reconciles local channel state with the outcome of a cooperative refund.
9593
*
96-
* Deletes the session when the post-refund balance is zero (full refund),
94+
* Deletes the channel record when the post-refund balance is zero (full refund),
9795
* otherwise updates `balance`, `chargedCumulativeAmount`, and `totalClaimed`
9896
* from the server snapshot (partial refund — channel stays open).
9997
*
100-
* @param storage - Client session storage.
98+
* @param storage - Client channel storage.
10199
* @param channelKey - Lowercased channel id used as the storage key.
102100
* @param settleExtra - The `extra` block from the refund settle response.
103101
*/
104-
export async function updateSessionAfterRefund(
105-
storage: ClientSessionStorage,
102+
export async function updateChannelAfterRefund(
103+
storage: ClientChannelStorage,
106104
channelKey: string,
107105
settleExtra: Record<string, unknown>,
108106
): Promise<void> {
@@ -132,11 +130,11 @@ export async function updateSessionAfterRefund(
132130
* Decodes the header into a `SettleResponse` and delegates to
133131
* {@link processSettleResponse}.
134132
*
135-
* @param storage - Client session storage.
133+
* @param storage - Client channel storage.
136134
* @param getHeader - Function to retrieve a response header by name.
137135
*/
138136
export async function processPaymentResponse(
139-
storage: ClientSessionStorage,
137+
storage: ClientChannelStorage,
140138
getHeader: (name: string) => string | null | undefined,
141139
): Promise<void> {
142140
const raw = getHeader("PAYMENT-RESPONSE");
@@ -147,19 +145,19 @@ export async function processPaymentResponse(
147145
}
148146

149147
/**
150-
* Recovers a channel session from on-chain state (useful after a cold start or
151-
* session loss).
148+
* Recovers a channel record from on-chain state (useful after a cold start or
149+
* channel record loss).
152150
*
153151
* @param deps - Signer + storage + identity inputs.
154152
* @param paymentRequirements - Server payment requirements used to derive the ChannelConfig.
155153
* @returns The recovered client context.
156154
*/
157-
export async function recoverSession(
155+
export async function recoverChannel(
158156
deps: BatchSettlementClientDeps,
159157
paymentRequirements: PaymentRequirements,
160158
): Promise<BatchSettlementClientContext> {
161159
if (!deps.signer.readContract) {
162-
throw new Error("recoverSession requires ClientEvmSigner.readContract");
160+
throw new Error("recoverChannel requires ClientEvmSigner.readContract");
163161
}
164162

165163
const config = buildChannelConfig(deps, paymentRequirements);
@@ -203,29 +201,29 @@ export async function readChannelBalanceAndTotalClaimed(
203201
}
204202

205203
/**
206-
* Returns whether a local session exists for the given channel.
204+
* Returns whether a local channel record exists for the given channel.
207205
*
208-
* @param storage - Client session storage.
206+
* @param storage - Client channel storage.
209207
* @param channelId - The channel identifier to check.
210-
* @returns `true` when a session is stored for the channel.
208+
* @returns `true` when a channel record is stored.
211209
*/
212-
export async function hasSession(
213-
storage: ClientSessionStorage,
210+
export async function hasChannel(
211+
storage: ClientChannelStorage,
214212
channelId: string,
215213
): Promise<boolean> {
216-
const session = await storage.get(channelId.toLowerCase());
217-
return session !== undefined;
214+
const channel = await storage.get(channelId.toLowerCase());
215+
return channel !== undefined;
218216
}
219217

220218
/**
221-
* Returns the local session context for a channel, if present.
219+
* Returns the local channel context for a channel, if present.
222220
*
223-
* @param storage - Client session storage.
221+
* @param storage - Client channel storage.
224222
* @param channelId - The channel identifier.
225223
* @returns Stored context or `undefined`.
226224
*/
227-
export async function getSession(
228-
storage: ClientSessionStorage,
225+
export async function getChannel(
226+
storage: ClientChannelStorage,
229227
channelId: string,
230228
): Promise<BatchSettlementClientContext | undefined> {
231229
return storage.get(channelId.toLowerCase());

typescript/packages/mechanisms/evm/src/batch-settlement/client/config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ClientEvmSigner } from "../../signer";
2-
import { type ClientSessionStorage, InMemoryClientSessionStorage } from "./storage";
2+
import { type ClientChannelStorage, InMemoryClientChannelStorage } from "./storage";
33

44
const DEFAULT_SALT =
55
"0x0000000000000000000000000000000000000000000000000000000000000000" as `0x${string}`;
@@ -21,7 +21,7 @@ export interface BatchSettlementDepositPolicy {
2121
*/
2222
export interface BatchSettlementEvmSchemeOptions {
2323
depositPolicy?: BatchSettlementDepositPolicy;
24-
storage?: ClientSessionStorage;
24+
storage?: ClientChannelStorage;
2525
salt?: `0x${string}`;
2626
payerAuthorizer?: `0x${string}`;
2727
/** When set, EIP-712 vouchers are signed with this key; deposits still use the main `signer`. */
@@ -34,7 +34,7 @@ export interface BatchSettlementEvmSchemeOptions {
3434
*/
3535
export interface ResolvedClientOptions {
3636
depositPolicy?: BatchSettlementDepositPolicy;
37-
storage: ClientSessionStorage;
37+
storage: ClientChannelStorage;
3838
salt: `0x${string}`;
3939
payerAuthorizer?: `0x${string}`;
4040
voucherSigner?: ClientEvmSigner;
@@ -70,19 +70,19 @@ export function resolveClientOptions(
7070
second?: BatchSettlementEvmSchemeOptions | BatchSettlementDepositPolicy,
7171
): ResolvedClientOptions {
7272
if (second === undefined) {
73-
return { storage: new InMemoryClientSessionStorage(), salt: DEFAULT_SALT };
73+
return { storage: new InMemoryClientChannelStorage(), salt: DEFAULT_SALT };
7474
}
7575
if (isBatchSettlementEvmSchemeOptions(second)) {
7676
return {
77-
storage: second.storage ?? new InMemoryClientSessionStorage(),
77+
storage: second.storage ?? new InMemoryClientChannelStorage(),
7878
depositPolicy: second.depositPolicy,
7979
salt: second.salt ?? DEFAULT_SALT,
8080
payerAuthorizer: second.payerAuthorizer,
8181
voucherSigner: second.voucherSigner,
8282
};
8383
}
8484
return {
85-
storage: new InMemoryClientSessionStorage(),
85+
storage: new InMemoryClientChannelStorage(),
8686
depositPolicy: second,
8787
salt: DEFAULT_SALT,
8888
};

typescript/packages/mechanisms/evm/src/batch-settlement/client/fileStorage.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import { unlink } from "node:fs/promises";
22
import { join } from "node:path";
33

44
import { isNodeEnoent, readJsonFile, writeJsonAtomic } from "../storage-utils";
5-
import type { FileSessionStorageOptions } from "../types";
6-
import type { ClientSessionStorage, BatchSettlementClientContext } from "./storage";
5+
import type { FileChannelStorageOptions } from "../types";
6+
import type { ClientChannelStorage, BatchSettlementClientContext } from "./storage";
77

88
/**
9-
* Node.js file-backed {@link ClientSessionStorage} for the batched client scheme.
10-
* Each channel's context is persisted as `{root}/client/{channelId}.json` so that sessions
11-
* survive process restarts.
9+
* Node.js file-backed {@link ClientChannelStorage} for the batched client scheme.
10+
* Each channel's context is persisted as `{root}/client/{channelId}.json` so that channel
11+
* records survive process restarts.
1212
*/
13-
export class FileClientSessionStorage implements ClientSessionStorage {
13+
export class FileClientChannelStorage implements ClientChannelStorage {
1414
private readonly root: string;
1515

1616
/**
17-
* Creates file-backed client session storage under the given root directory.
17+
* Creates file-backed client channel storage under the given root directory.
1818
*
1919
* @param options - Configuration including the storage root directory.
2020
*/
21-
constructor(options: FileSessionStorageOptions) {
21+
constructor(options: FileChannelStorageOptions) {
2222
this.root = options.directory;
2323
}
2424

0 commit comments

Comments
 (0)