mppx 0.12.0 → 0.13.2
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/CHANGELOG.md +64 -0
- package/dist/Method.d.ts +7 -1
- package/dist/Method.js +7 -2
- package/dist/Receipt.d.ts +2 -0
- package/dist/Receipt.js +2 -0
- package/dist/cli/cli.d.ts +8 -0
- package/dist/cli/cli.js +134 -136
- package/dist/cli/plugins/index.js +12 -14
- package/dist/cli/plugins/tempo.js +9 -2
- package/dist/cli/sessions/request.d.ts +1 -1
- package/dist/cli/sessions/request.js +6 -8
- package/dist/cli/utils.d.ts +11 -1
- package/dist/cli/utils.js +15 -2
- package/dist/cli/validate/challenge.d.ts +2 -0
- package/dist/cli/validate/challenge.js +19 -4
- package/dist/cli/validate/discovery.d.ts +1 -0
- package/dist/cli/validate/discovery.js +10 -1
- package/dist/cli/validate/payment.js +34 -12
- package/dist/client/Methods.d.ts +0 -2
- package/dist/client/Methods.js +0 -2
- package/dist/client/Mppx.js +1 -0
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client/internal/Fetch.d.ts +2 -0
- package/dist/client/internal/Fetch.js +22 -7
- package/dist/client/internal/MethodChallenge.d.ts +1 -0
- package/dist/evm/Types.js +1 -1
- package/dist/evm/server/Charge.js +5 -0
- package/dist/integrations/x402/Evm.js +4 -3
- package/dist/internal/HeaderCodec.d.ts +2 -2
- package/dist/internal/version.d.ts +2 -2
- package/dist/internal/version.js +1 -1
- package/dist/mcp/client/McpClient.js +4 -7
- package/dist/middlewares/elysia.d.ts +11 -8
- package/dist/middlewares/elysia.js +67 -14
- package/dist/middlewares/express.js +13 -6
- package/dist/middlewares/hono.js +4 -4
- package/dist/middlewares/nextjs.js +9 -5
- package/dist/proxy/internal/Headers.js +65 -0
- package/dist/proxy/services/stripe.js +1 -0
- package/dist/server/Mppx.d.ts +14 -2
- package/dist/server/Mppx.js +85 -44
- package/dist/server/NodeListener.js +71 -6
- package/dist/server/Request.d.ts +11 -1
- package/dist/server/Request.js +132 -13
- package/dist/server/Transport.d.ts +14 -1
- package/dist/server/Transport.js +1 -0
- package/dist/stripe/server/Methods.d.ts +10 -3
- package/dist/stripe/server/Methods.js +17 -2
- package/dist/stripe/server/internal/html.gen.js +1 -1
- package/dist/tempo/Methods.d.ts +1 -0
- package/dist/tempo/Methods.js +1 -0
- package/dist/tempo/client/Charge.js +17 -13
- package/dist/tempo/client/Methods.d.ts +1 -5
- package/dist/tempo/client/Methods.js +10 -5
- package/dist/tempo/client/ResolveAccount.d.ts +3 -0
- package/dist/tempo/client/index.d.ts +0 -3
- package/dist/tempo/client/index.js +0 -2
- package/dist/tempo/internal/fee-payer.d.ts +6 -3
- package/dist/tempo/internal/fee-payer.js +23 -6
- package/dist/tempo/internal/machine-token-charge.d.ts +23 -0
- package/dist/tempo/internal/machine-token-charge.js +2 -1
- package/dist/tempo/legacy/index.d.ts +1 -3
- package/dist/tempo/legacy/index.js +1 -3
- package/dist/tempo/server/Charge.js +87 -53
- package/dist/tempo/server/Methods.d.ts +55 -10
- package/dist/tempo/server/Subscription.d.ts +1 -1
- package/dist/tempo/server/Subscription.js +31 -4
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/dist/tempo/server/internal/transport.js +28 -11
- package/dist/tempo/session/client/ChannelOps.js +5 -0
- package/dist/tempo/session/client/Session.d.ts +1 -0
- package/dist/tempo/session/client/Session.js +7 -4
- package/dist/tempo/session/client/SessionManager.js +3 -0
- package/dist/tempo/session/client/Transports.d.ts +8 -1
- package/dist/tempo/session/client/Transports.js +12 -3
- package/dist/tempo/session/precompile/Chain.js +16 -1
- package/dist/tempo/session/precompile/Protocol.d.ts +2 -0
- package/dist/tempo/session/precompile/Voucher.d.ts +16 -0
- package/dist/tempo/session/precompile/Voucher.js +48 -0
- package/dist/tempo/session/server/ChannelStore.d.ts +20 -2
- package/dist/tempo/session/server/ChannelStore.js +25 -4
- package/dist/tempo/session/server/CredentialVerification.d.ts +4 -1
- package/dist/tempo/session/server/CredentialVerification.js +34 -6
- package/dist/tempo/session/server/MeteredStream.d.ts +3 -2
- package/dist/tempo/session/server/MeteredStream.js +85 -29
- package/dist/tempo/session/server/Session.d.ts +2 -1
- package/dist/tempo/session/server/Session.js +2 -1
- package/dist/tempo/session/server/Settlement.d.ts +21 -0
- package/dist/tempo/session/server/Settlement.js +92 -17
- package/dist/tempo/session/server/Sse.d.ts +3 -1
- package/dist/tempo/session/server/Sse.js +105 -32
- package/dist/tempo/session/server/Transports.d.ts +24 -19
- package/dist/tempo/session/server/Transports.js +169 -58
- package/dist/tempo/session/server/Ws.d.ts +5 -1
- package/dist/tempo/session/server/Ws.js +75 -20
- package/dist/tempo/subscription/Store.d.ts +2 -0
- package/dist/tempo/subscription/Store.js +3 -0
- package/dist/validation/core.js +2 -1
- package/dist/x402/server/EvmCharge.d.ts +3 -1
- package/dist/x402/server/EvmCharge.js +14 -1
- package/package.json +6 -6
- package/dist/tempo/legacy/client/ChannelOps.d.ts +0 -69
- package/dist/tempo/legacy/client/ChannelOps.js +0 -168
- package/dist/tempo/legacy/client/Session.d.ts +0 -185
- package/dist/tempo/legacy/client/Session.js +0 -279
- package/dist/tempo/legacy/client/SessionManager.d.ts +0 -100
- package/dist/tempo/legacy/client/SessionManager.js +0 -706
- package/dist/tempo/legacy/client/index.d.ts +0 -7
- package/dist/tempo/legacy/client/index.js +0 -5
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
import { parseUnits } from 'viem';
|
|
2
|
-
import { tempo as tempo_chain } from 'viem/tempo/chains';
|
|
3
|
-
import * as Constants from '../../../Constants.js';
|
|
4
|
-
import * as Method from '../../../Method.js';
|
|
5
|
-
import * as Account from '../../../viem/Account.js';
|
|
6
|
-
import * as Client from '../../../viem/Client.js';
|
|
7
|
-
import * as z from '../../../zod.js';
|
|
8
|
-
import { getAccountSignerAddress } from '../../internal/account.js';
|
|
9
|
-
import * as defaults from '../../internal/defaults.js';
|
|
10
|
-
import * as Methods from '../../Methods.js';
|
|
11
|
-
import { signVoucher } from '../session/Voucher.js';
|
|
12
|
-
import { createOpenPayload, createVoucherPayload, resolveEscrow, serializeCredential, tryRecoverChannel, } from './ChannelOps.js';
|
|
13
|
-
/** Runtime schema for legacy low-level session credential context. */
|
|
14
|
-
export const sessionContextSchema = z.object({
|
|
15
|
-
account: z.optional(z.custom()),
|
|
16
|
-
action: z.optional(z.enum(['open', 'topUp', 'voucher', 'close'])),
|
|
17
|
-
channelId: z.optional(z.string()),
|
|
18
|
-
cumulativeAmount: z.optional(z.amount()),
|
|
19
|
-
cumulativeAmountRaw: z.optional(z.string()),
|
|
20
|
-
transaction: z.optional(z.string()),
|
|
21
|
-
additionalDeposit: z.optional(z.amount()),
|
|
22
|
-
additionalDepositRaw: z.optional(z.string()),
|
|
23
|
-
depositRaw: z.optional(z.string()),
|
|
24
|
-
});
|
|
25
|
-
/**
|
|
26
|
-
* Creates a session payment method for use with `Mppx.create()`.
|
|
27
|
-
*
|
|
28
|
-
* Supports both auto mode (set `deposit` to manage channels automatically)
|
|
29
|
-
* and manual mode (pass `context.action` to control each step).
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```ts
|
|
33
|
-
* // Auto mode
|
|
34
|
-
* import { Mppx, tempo } from 'mppx/client'
|
|
35
|
-
*
|
|
36
|
-
* const mppx = Mppx.create({
|
|
37
|
-
* methods: [tempo({
|
|
38
|
-
* account: privateKeyToAccount('0x...'),
|
|
39
|
-
* deposit: '10',
|
|
40
|
-
* })],
|
|
41
|
-
* })
|
|
42
|
-
*
|
|
43
|
-
* const res = await mppx.fetch('/api/chat?prompt=hello')
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* // Manual mode
|
|
49
|
-
* const mppx = Mppx.create({
|
|
50
|
-
* methods: [tempo({ account })],
|
|
51
|
-
* })
|
|
52
|
-
*
|
|
53
|
-
* const credential = await mppx.createCredential(response, {
|
|
54
|
-
* action: 'voucher',
|
|
55
|
-
* channelId: '0x...',
|
|
56
|
-
* cumulativeAmount: '1',
|
|
57
|
-
* })
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
export function session(parameters = {}) {
|
|
61
|
-
const { allowCustomEscrow = false, decimals = defaults.decimals } = parameters;
|
|
62
|
-
const getClient = Client.getResolver({
|
|
63
|
-
chain: tempo_chain,
|
|
64
|
-
getClient: parameters.getClient,
|
|
65
|
-
rpcUrl: defaults.rpcUrl,
|
|
66
|
-
});
|
|
67
|
-
const getAccount = Account.getResolver({ account: parameters.account });
|
|
68
|
-
const maxDeposit = parameters.maxDeposit !== undefined ? parseUnits(parameters.maxDeposit, decimals) : undefined;
|
|
69
|
-
const escrowContractMap = new Map();
|
|
70
|
-
const channels = new Map();
|
|
71
|
-
const channelIdToKey = new Map();
|
|
72
|
-
function notifyUpdate(entry) {
|
|
73
|
-
parameters.onChannelUpdate?.(entry);
|
|
74
|
-
}
|
|
75
|
-
function channelKey(payee, currency, escrow) {
|
|
76
|
-
return `${payee.toLowerCase()}:${currency.toLowerCase()}:${escrow.toLowerCase()}`;
|
|
77
|
-
}
|
|
78
|
-
function resolveEscrowCached(challenge, chainId, channelId) {
|
|
79
|
-
if (channelId) {
|
|
80
|
-
const cached = escrowContractMap.get(channelId);
|
|
81
|
-
if (cached)
|
|
82
|
-
return resolveEscrow(challenge, chainId, cached);
|
|
83
|
-
}
|
|
84
|
-
return resolveEscrow(challenge, chainId, parameters.escrowContract, allowCustomEscrow);
|
|
85
|
-
}
|
|
86
|
-
async function autoManageCredential(challenge, account, context) {
|
|
87
|
-
const md = challenge.request.methodDetails;
|
|
88
|
-
const chainId = md?.chainId ?? 0;
|
|
89
|
-
const client = await getClient({ chainId });
|
|
90
|
-
const escrowContract = resolveEscrowCached(challenge, chainId);
|
|
91
|
-
const payee = challenge.request.recipient;
|
|
92
|
-
const currency = challenge.request.currency;
|
|
93
|
-
const amount = BigInt(challenge.request.amount);
|
|
94
|
-
const suggestedDepositRaw = challenge.request
|
|
95
|
-
.suggestedDeposit;
|
|
96
|
-
const suggestedDeposit = suggestedDepositRaw ? BigInt(suggestedDepositRaw) : undefined;
|
|
97
|
-
const deposit = (() => {
|
|
98
|
-
if (context?.depositRaw)
|
|
99
|
-
return BigInt(context.depositRaw);
|
|
100
|
-
if (parameters.deposit !== undefined)
|
|
101
|
-
return parseUnits(parameters.deposit, decimals);
|
|
102
|
-
if (suggestedDeposit !== undefined && maxDeposit !== undefined)
|
|
103
|
-
return suggestedDeposit < maxDeposit ? suggestedDeposit : maxDeposit;
|
|
104
|
-
if (maxDeposit !== undefined)
|
|
105
|
-
return maxDeposit;
|
|
106
|
-
if (suggestedDeposit !== undefined)
|
|
107
|
-
return suggestedDeposit;
|
|
108
|
-
throw new Error('No deposit amount available. Set `deposit`, `maxDeposit`, or ensure the server challenge includes `suggestedDeposit`.');
|
|
109
|
-
})();
|
|
110
|
-
const voucherSigner = parameters.voucherSigner ?? account;
|
|
111
|
-
const key = channelKey(payee, currency, escrowContract);
|
|
112
|
-
let entry = channels.get(key);
|
|
113
|
-
if (!entry) {
|
|
114
|
-
const suggestedChannelId = (context?.channelId ?? md?.channelId);
|
|
115
|
-
if (suggestedChannelId) {
|
|
116
|
-
const recovered = await tryRecoverChannel(client, escrowContract, suggestedChannelId, chainId);
|
|
117
|
-
if (recovered) {
|
|
118
|
-
const contextCumulative = context?.cumulativeAmountRaw
|
|
119
|
-
? BigInt(context.cumulativeAmountRaw)
|
|
120
|
-
: context?.cumulativeAmount
|
|
121
|
-
? parseUnits(context.cumulativeAmount, decimals)
|
|
122
|
-
: undefined;
|
|
123
|
-
if (contextCumulative !== undefined)
|
|
124
|
-
recovered.cumulativeAmount = contextCumulative;
|
|
125
|
-
channels.set(key, recovered);
|
|
126
|
-
channelIdToKey.set(recovered.channelId, key);
|
|
127
|
-
escrowContractMap.set(recovered.channelId, escrowContract);
|
|
128
|
-
entry = recovered;
|
|
129
|
-
notifyUpdate(entry);
|
|
130
|
-
}
|
|
131
|
-
else if (context?.channelId) {
|
|
132
|
-
throw new Error(`Channel ${context.channelId} cannot be reused (closed or not found on-chain).`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
// A channel only admits vouchers from the authorizedSigner it was opened
|
|
137
|
-
// with, so a cached or recovered entry stops being reusable once the
|
|
138
|
-
// resolved signer changes. Fall through to open a fresh channel instead
|
|
139
|
-
// of emitting a voucher the escrow would reject.
|
|
140
|
-
if (entry?.opened &&
|
|
141
|
-
entry.authorizedSigner.toLowerCase() !== getAccountSignerAddress(voucherSigner).toLowerCase())
|
|
142
|
-
entry = undefined;
|
|
143
|
-
let payload;
|
|
144
|
-
if (entry?.opened) {
|
|
145
|
-
entry.cumulativeAmount += amount;
|
|
146
|
-
payload = await createVoucherPayload(client, account, entry.channelId, entry.cumulativeAmount, escrowContract, chainId, voucherSigner);
|
|
147
|
-
notifyUpdate(entry);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
const result = await createOpenPayload(client, account, {
|
|
151
|
-
voucherSigner,
|
|
152
|
-
escrowContract,
|
|
153
|
-
payee,
|
|
154
|
-
currency,
|
|
155
|
-
deposit,
|
|
156
|
-
initialAmount: amount,
|
|
157
|
-
chainId,
|
|
158
|
-
feePayer: md?.feePayer,
|
|
159
|
-
});
|
|
160
|
-
channels.set(key, result.entry);
|
|
161
|
-
channelIdToKey.set(result.entry.channelId, key);
|
|
162
|
-
escrowContractMap.set(result.entry.channelId, escrowContract);
|
|
163
|
-
payload = result.payload;
|
|
164
|
-
notifyUpdate(result.entry);
|
|
165
|
-
}
|
|
166
|
-
return serializeCredential(challenge, payload, chainId, account);
|
|
167
|
-
}
|
|
168
|
-
async function manualCredential(challenge, account, context) {
|
|
169
|
-
const md = challenge.request.methodDetails;
|
|
170
|
-
const chainId = md?.chainId ?? 0;
|
|
171
|
-
const client = await getClient({ chainId });
|
|
172
|
-
const action = context.action;
|
|
173
|
-
const { channelId: channelIdRaw, transaction } = context;
|
|
174
|
-
const voucherSigner = parameters.voucherSigner ?? account;
|
|
175
|
-
const channelId = channelIdRaw;
|
|
176
|
-
const cumulativeAmount = context.cumulativeAmountRaw
|
|
177
|
-
? BigInt(context.cumulativeAmountRaw)
|
|
178
|
-
: context.cumulativeAmount
|
|
179
|
-
? parseUnits(context.cumulativeAmount, decimals)
|
|
180
|
-
: undefined;
|
|
181
|
-
const resolvedAdditionalDeposit = context.additionalDepositRaw
|
|
182
|
-
? BigInt(context.additionalDepositRaw)
|
|
183
|
-
: context.additionalDeposit
|
|
184
|
-
? parseUnits(context.additionalDeposit, decimals)
|
|
185
|
-
: undefined;
|
|
186
|
-
const escrowContract = resolveEscrowCached(challenge, chainId, channelId);
|
|
187
|
-
escrowContractMap.set(channelId, escrowContract);
|
|
188
|
-
let payload;
|
|
189
|
-
switch (action) {
|
|
190
|
-
case 'open': {
|
|
191
|
-
if (!transaction)
|
|
192
|
-
throw new Error('transaction required for open action');
|
|
193
|
-
if (cumulativeAmount === undefined)
|
|
194
|
-
throw new Error('cumulativeAmount required for open action');
|
|
195
|
-
const signature = await signVoucher(client, account, { channelId, cumulativeAmount }, escrowContract, chainId, voucherSigner);
|
|
196
|
-
payload = {
|
|
197
|
-
action: 'open',
|
|
198
|
-
type: 'transaction',
|
|
199
|
-
channelId,
|
|
200
|
-
transaction: transaction,
|
|
201
|
-
authorizedSigner: getAccountSignerAddress(voucherSigner),
|
|
202
|
-
cumulativeAmount: cumulativeAmount.toString(),
|
|
203
|
-
signature,
|
|
204
|
-
};
|
|
205
|
-
break;
|
|
206
|
-
}
|
|
207
|
-
case 'topUp':
|
|
208
|
-
if (!transaction)
|
|
209
|
-
throw new Error('transaction required for topUp action');
|
|
210
|
-
if (resolvedAdditionalDeposit === undefined)
|
|
211
|
-
throw new Error('additionalDeposit required for topUp action');
|
|
212
|
-
payload = {
|
|
213
|
-
action: 'topUp',
|
|
214
|
-
type: 'transaction',
|
|
215
|
-
channelId,
|
|
216
|
-
transaction: transaction,
|
|
217
|
-
additionalDeposit: resolvedAdditionalDeposit.toString(),
|
|
218
|
-
};
|
|
219
|
-
break;
|
|
220
|
-
case 'voucher': {
|
|
221
|
-
if (cumulativeAmount === undefined)
|
|
222
|
-
throw new Error('cumulativeAmount required for voucher action');
|
|
223
|
-
payload = await createVoucherPayload(client, account, channelId, cumulativeAmount, escrowContract, chainId, voucherSigner);
|
|
224
|
-
const key = channelIdToKey.get(channelId);
|
|
225
|
-
if (key) {
|
|
226
|
-
const entry = channels.get(key);
|
|
227
|
-
if (entry) {
|
|
228
|
-
entry.cumulativeAmount =
|
|
229
|
-
entry.cumulativeAmount > cumulativeAmount ? entry.cumulativeAmount : cumulativeAmount;
|
|
230
|
-
notifyUpdate(entry);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
case 'close': {
|
|
236
|
-
if (cumulativeAmount === undefined)
|
|
237
|
-
throw new Error('cumulativeAmount required for close action');
|
|
238
|
-
const signature = await signVoucher(client, account, { channelId, cumulativeAmount }, escrowContract, chainId, voucherSigner);
|
|
239
|
-
payload = {
|
|
240
|
-
action: 'close',
|
|
241
|
-
channelId,
|
|
242
|
-
cumulativeAmount: cumulativeAmount.toString(),
|
|
243
|
-
signature,
|
|
244
|
-
};
|
|
245
|
-
const closeKey = channelIdToKey.get(channelId);
|
|
246
|
-
if (closeKey) {
|
|
247
|
-
const entry = channels.get(closeKey);
|
|
248
|
-
if (entry) {
|
|
249
|
-
entry.opened = false;
|
|
250
|
-
entry.cumulativeAmount =
|
|
251
|
-
entry.cumulativeAmount > cumulativeAmount ? entry.cumulativeAmount : cumulativeAmount;
|
|
252
|
-
notifyUpdate(entry);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
break;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return serializeCredential(challenge, payload, chainId, account);
|
|
259
|
-
}
|
|
260
|
-
return Method.toClient(Methods.session, {
|
|
261
|
-
canHandleChallenge({ challenge }) {
|
|
262
|
-
const sessionProtocolMarker = Constants.getMethodDetail(challenge.request.methodDetails, Constants.MethodDetailKeys.sessionProtocol);
|
|
263
|
-
return (sessionProtocolMarker === undefined ||
|
|
264
|
-
sessionProtocolMarker === Constants.SessionProtocols.v1);
|
|
265
|
-
},
|
|
266
|
-
context: sessionContextSchema,
|
|
267
|
-
async createCredential({ challenge, context }) {
|
|
268
|
-
const chainId = challenge.request.methodDetails?.chainId ?? 0;
|
|
269
|
-
const client = await getClient({ chainId });
|
|
270
|
-
const account = getAccount(client, context);
|
|
271
|
-
if (!context?.action && (parameters.deposit !== undefined || maxDeposit !== undefined))
|
|
272
|
-
return autoManageCredential(challenge, account, context);
|
|
273
|
-
if (context?.action)
|
|
274
|
-
return manualCredential(challenge, account, context);
|
|
275
|
-
throw new Error('No `action` in context and no `deposit` or `maxDeposit` configured. Either provide context with action/channelId/cumulativeAmount, or configure `deposit`/`maxDeposit` for auto-management.');
|
|
276
|
-
},
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
//# sourceMappingURL=Session.js.map
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import type { Hex } from 'ox';
|
|
2
|
-
import { type Account as viem_Account, type Address } from 'viem';
|
|
3
|
-
import * as Challenge from '../../../Challenge.js';
|
|
4
|
-
import * as AcceptPayment from '../../../internal/AcceptPayment.js';
|
|
5
|
-
import type * as Account from '../../../viem/Account.js';
|
|
6
|
-
import type * as Client from '../../../viem/Client.js';
|
|
7
|
-
import type { SessionReceipt } from '../../session/precompile/Protocol.js';
|
|
8
|
-
import { session as sessionPlugin } from './Session.js';
|
|
9
|
-
type WebSocketConstructor = {
|
|
10
|
-
new (url: string | URL, protocols?: string | string[]): WebSocket;
|
|
11
|
-
};
|
|
12
|
-
type SessionMethod = ReturnType<typeof sessionPlugin>;
|
|
13
|
-
type SessionOrderChallenges = AcceptPayment.OrderChallenges<readonly [SessionMethod]>;
|
|
14
|
-
type SessionRequestInit = RequestInit & {
|
|
15
|
-
orderChallenges?: SessionOrderChallenges | undefined;
|
|
16
|
-
};
|
|
17
|
-
/** Auto-driving legacy client manager for HTTP, SSE, and WebSocket sessions. */
|
|
18
|
-
export type SessionManager = {
|
|
19
|
-
/** Active legacy channel ID, when opened or recovered. */
|
|
20
|
-
readonly channelId: Hex.Hex | undefined;
|
|
21
|
-
/** Local cumulative voucher authorization in raw token units. */
|
|
22
|
-
readonly cumulative: bigint;
|
|
23
|
-
/** Whether the manager currently has an open local channel. */
|
|
24
|
-
readonly opened: boolean;
|
|
25
|
-
/** Opens a legacy contract-backed channel before the first paid request. */
|
|
26
|
-
open(options?: {
|
|
27
|
-
deposit?: bigint;
|
|
28
|
-
}): Promise<void>;
|
|
29
|
-
/** Performs the HTTP 402 challenge/retry flow and returns receipt metadata. */
|
|
30
|
-
fetch(input: RequestInfo | URL, init?: SessionRequestInit): Promise<PaymentResponse>;
|
|
31
|
-
/** Opens a paid SSE stream and responds to voucher requests. */
|
|
32
|
-
sse(input: RequestInfo | URL, init?: SessionRequestInit & {
|
|
33
|
-
onReceipt?: ((receipt: SessionReceipt) => void) | undefined;
|
|
34
|
-
signal?: AbortSignal | undefined;
|
|
35
|
-
}): Promise<AsyncIterable<string>>;
|
|
36
|
-
/** Opens a paid WebSocket session and manages in-band payment frames. */
|
|
37
|
-
ws(input: string | URL, init?: {
|
|
38
|
-
onReceipt?: ((receipt: SessionReceipt) => void) | undefined;
|
|
39
|
-
orderChallenges?: SessionOrderChallenges | undefined;
|
|
40
|
-
protocols?: string | string[] | undefined;
|
|
41
|
-
signal?: AbortSignal | undefined;
|
|
42
|
-
}): Promise<WebSocket>;
|
|
43
|
-
/** Cooperatively closes the active legacy channel. */
|
|
44
|
-
close(): Promise<SessionReceipt | undefined>;
|
|
45
|
-
};
|
|
46
|
-
/** HTTP response enriched with latest legacy session metadata. */
|
|
47
|
-
export type PaymentResponse = Response & {
|
|
48
|
-
/** Parsed payment receipt, when the response included one. */
|
|
49
|
-
receipt: SessionReceipt | null;
|
|
50
|
-
/** Last session challenge observed by the manager. */
|
|
51
|
-
challenge: Challenge.Challenge | null;
|
|
52
|
-
/** Active channel ID, when available. */
|
|
53
|
-
channelId: Hex.Hex | null;
|
|
54
|
-
/** Local cumulative voucher authorization in raw token units. */
|
|
55
|
-
cumulative: bigint;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Creates a session manager that handles the full client payment lifecycle:
|
|
59
|
-
* channel open, incremental vouchers, SSE streaming, and channel close.
|
|
60
|
-
*
|
|
61
|
-
* Internally delegates to the `session()` method for all
|
|
62
|
-
* channel state management and credential creation, and to `Fetch.from`
|
|
63
|
-
* for the 402 challenge/retry flow.
|
|
64
|
-
*
|
|
65
|
-
* ## Session resumption
|
|
66
|
-
*
|
|
67
|
-
* All channel state is held **in memory**. If the client process restarts,
|
|
68
|
-
* the session is lost and a new on-chain channel will be opened on the next
|
|
69
|
-
* request — the previous channel's deposit is orphaned until manually closed.
|
|
70
|
-
*
|
|
71
|
-
* When the server includes a `channelId` in the 402 challenge `methodDetails`,
|
|
72
|
-
* the client will attempt to recover the channel by reading its on-chain state
|
|
73
|
-
* via `getOnChainChannel()`. If the channel has a positive deposit and is not
|
|
74
|
-
* finalized, it resumes from the on-chain settled amount.
|
|
75
|
-
*/
|
|
76
|
-
export declare function sessionManager(parameters: sessionManager.Parameters): SessionManager;
|
|
77
|
-
/** Type helpers for the legacy auto-driving session manager. */
|
|
78
|
-
export declare namespace sessionManager {
|
|
79
|
-
type Parameters = Account.getResolver.Parameters & Client.getResolver.Parameters & {
|
|
80
|
-
/** Accept a noncanonical escrow contract advertised by the server. @default false */
|
|
81
|
-
allowCustomEscrow?: boolean | undefined;
|
|
82
|
-
/** Account that signs voucher digests. Defaults to `account`; access-key accounts sign raw vouchers as their access-key address. */
|
|
83
|
-
voucherSigner?: viem_Account | undefined;
|
|
84
|
-
/** Viem client instance. Shorthand for `getClient: () => client`. */
|
|
85
|
-
client?: import('viem').Client | undefined;
|
|
86
|
-
/** Token decimals used to convert `maxDeposit` to raw units. Defaults to `6`. */
|
|
87
|
-
decimals?: number | undefined;
|
|
88
|
-
/** Exact escrow contract address pin. Takes precedence over `allowCustomEscrow`. */
|
|
89
|
-
escrowContract?: Address | undefined;
|
|
90
|
-
fetch?: typeof globalThis.fetch | undefined;
|
|
91
|
-
/** Maximum deposit in human-readable units (e.g. `'10'` for 10 tokens). Converted to raw units via `decimals`. */
|
|
92
|
-
maxDeposit?: string | undefined;
|
|
93
|
-
/** Filters and sorts supported session challenges before credential creation. */
|
|
94
|
-
orderChallenges?: SessionOrderChallenges | undefined;
|
|
95
|
-
/** Optional websocket constructor for runtimes without a global WebSocket. */
|
|
96
|
-
webSocket?: WebSocketConstructor | undefined;
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
export {};
|
|
100
|
-
//# sourceMappingURL=SessionManager.d.ts.map
|