mppx 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/dist/Challenge.d.ts +59 -3
- package/dist/Challenge.js +162 -21
- package/dist/Errors.d.ts +35 -1
- package/dist/Errors.js +41 -3
- package/dist/Method.js +2 -2
- package/dist/Receipt.d.ts +2 -0
- package/dist/Receipt.js +5 -0
- package/dist/Store.d.ts +39 -10
- package/dist/Store.js +136 -29
- package/dist/cli/cli.d.ts +5 -0
- package/dist/cli/cli.js +135 -135
- package/dist/cli/plugins/index.js +12 -12
- package/dist/cli/sessions/commands.js +9 -0
- package/dist/cli/sessions/store.js +3 -2
- package/dist/cli/validate/challenge.js +15 -1
- package/dist/client/Mppx.d.ts +1 -1
- package/dist/client/Mppx.js +2 -1
- package/dist/client/internal/Fetch.d.ts +2 -1
- package/dist/client/internal/Fetch.js +43 -5
- package/dist/client/internal/PendingSettlement.d.ts +11 -0
- package/dist/client/internal/PendingSettlement.js +22 -0
- package/dist/client/internal/protocols/Mcp.js +8 -0
- package/dist/evm/client/Charge.js +7 -1
- package/dist/evm/server/Charge.d.ts +5 -0
- package/dist/evm/server/Charge.js +148 -0
- package/dist/evm/server/Methods.d.ts +8 -0
- package/dist/evm/server/Methods.js +8 -0
- package/dist/extensions/identity/attestation/Issuer.d.ts +91 -0
- package/dist/extensions/identity/attestation/Issuer.js +159 -0
- package/dist/extensions/identity/attestation/Jwks.js +3 -3
- package/dist/extensions/identity/attestation/Token.js +7 -0
- package/dist/extensions/identity/attestation/Types.d.ts +31 -3
- package/dist/extensions/identity/attestation/index.d.ts +3 -1
- package/dist/extensions/identity/attestation/index.js +3 -1
- package/dist/extensions/identity/link/server/index.js +4 -4
- package/dist/extensions/identity/persona/server/index.js +3 -3
- package/dist/extensions/identity/tempo/Constants.d.ts +4 -0
- package/dist/extensions/identity/tempo/Constants.js +4 -0
- package/dist/extensions/identity/tempo/client/index.d.ts +63 -0
- package/dist/extensions/identity/tempo/client/index.js +151 -0
- package/dist/extensions/identity/tempo/index.d.ts +1 -0
- package/dist/extensions/identity/tempo/index.js +1 -0
- package/dist/extensions/identity/tempo/issuer/index.d.ts +83 -0
- package/dist/extensions/identity/tempo/issuer/index.js +183 -0
- package/dist/internal/CredentialExpiry.d.ts +30 -0
- package/dist/internal/CredentialExpiry.js +51 -0
- package/dist/internal/version.d.ts +2 -2
- package/dist/internal/version.js +1 -1
- package/dist/mcp/client/McpClient.js +29 -10
- package/dist/middlewares/express.d.ts +7 -2
- package/dist/middlewares/express.js +20 -23
- package/dist/middlewares/internal/express.d.ts +9 -1
- package/dist/middlewares/internal/express.js +348 -2
- package/dist/proxy/Proxy.js +15 -3
- package/dist/proxy/internal/Headers.js +1 -0
- package/dist/proxy/internal/Replay.d.ts +10 -0
- package/dist/proxy/internal/Replay.js +5 -0
- package/dist/server/Mppx.d.ts +1 -1
- package/dist/server/Mppx.js +47 -15
- package/dist/server/NodeListener.js +7 -0
- package/dist/server/Request.d.ts +6 -0
- package/dist/server/Request.js +43 -22
- package/dist/server/internal/challengeMeta.d.ts +10 -0
- package/dist/server/internal/challengeMeta.js +31 -1
- package/dist/server/internal/host.d.ts +4 -0
- package/dist/server/internal/host.js +41 -0
- package/dist/tempo/client/Charge.d.ts +5 -4
- package/dist/tempo/client/Charge.js +210 -38
- package/dist/tempo/client/Subscription.js +3 -0
- package/dist/tempo/internal/zone.d.ts +8 -0
- package/dist/tempo/internal/zone.js +15 -0
- package/dist/tempo/server/Charge.d.ts +19 -0
- package/dist/tempo/server/Charge.js +60 -15
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/dist/tempo/server/internal/transport.js +10 -54
- package/dist/tempo/session/client/ChannelOps.js +1 -1
- package/dist/tempo/session/client/SessionManager.d.ts +4 -1
- package/dist/tempo/session/client/SessionManager.js +8 -2
- package/dist/tempo/session/server/CredentialVerification.js +2 -3
- package/dist/viem/Client.d.ts +2 -0
- package/dist/viem/Client.js +4 -1
- package/dist/x402/Assets.d.ts +6 -0
- package/dist/x402/Assets.js +10 -0
- package/dist/x402/Siwx.d.ts +270 -0
- package/dist/x402/Siwx.js +458 -0
- package/dist/x402/Types.d.ts +72 -12
- package/dist/x402/Types.js +18 -3
- package/dist/x402/client/Exact.js +4 -1
- package/dist/x402/index.d.ts +1 -0
- package/dist/x402/index.js +1 -0
- package/dist/x402/middlewares/express.js +10 -1
- package/dist/x402/server/EvmCharge.d.ts +27 -3
- package/dist/x402/server/EvmCharge.js +27 -2
- package/dist/x402/server/Facilitator.d.ts +42 -3
- package/dist/x402/server/Facilitator.js +126 -16
- package/package.json +1 -1
|
@@ -145,12 +145,19 @@ export function sse(options) {
|
|
|
145
145
|
const available = BigInt(currentReceipt.acceptedCumulative) - BigInt(currentReceipt.spent);
|
|
146
146
|
if (available < tickCost)
|
|
147
147
|
return paymentRequired(available);
|
|
148
|
+
// Plain content authorizes its headers as well as its body. Commit before
|
|
149
|
+
// returning the response so adapters can safely honor an explicit flush.
|
|
150
|
+
const result = await ChannelStore.deductFromChannel(store, channelId, tickCost);
|
|
151
|
+
if (!result.ok)
|
|
152
|
+
return paymentRequired(result.channel.highestVoucherAmount - result.channel.spent);
|
|
153
|
+
await options.settleCharged?.(result.channel);
|
|
148
154
|
const chargedReceipt = {
|
|
149
155
|
...currentReceipt,
|
|
150
|
-
|
|
151
|
-
|
|
156
|
+
acceptedCumulative: result.channel.highestVoucherAmount.toString(),
|
|
157
|
+
spent: result.channel.spent.toString(),
|
|
158
|
+
units: result.channel.units,
|
|
152
159
|
};
|
|
153
|
-
|
|
160
|
+
return base.respondReceipt({
|
|
154
161
|
credential: verifiedCredential,
|
|
155
162
|
envelope,
|
|
156
163
|
input,
|
|
@@ -158,54 +165,6 @@ export function sse(options) {
|
|
|
158
165
|
response: response,
|
|
159
166
|
challengeId: verifiedChallengeId,
|
|
160
167
|
});
|
|
161
|
-
const chargePlainResponse = async () => {
|
|
162
|
-
const result = await ChannelStore.deductFromChannel(store, channelId, tickCost);
|
|
163
|
-
if (result.ok)
|
|
164
|
-
await options.settleCharged?.(result.channel);
|
|
165
|
-
return result;
|
|
166
|
-
};
|
|
167
|
-
// Non-SSE response (e.g. upstream returned JSON instead of event-stream).
|
|
168
|
-
// Need to deduct tickCost so request isn't free.
|
|
169
|
-
// For null-body statuses, the request shape determines whether the
|
|
170
|
-
// response is management (no charge) or plain content (charge one tick).
|
|
171
|
-
if (isNullBodyStatus(chargedResponse.status)) {
|
|
172
|
-
const result = await chargePlainResponse();
|
|
173
|
-
if (!result.ok)
|
|
174
|
-
return paymentRequired(result.channel.highestVoucherAmount - result.channel.spent);
|
|
175
|
-
return chargedResponse;
|
|
176
|
-
}
|
|
177
|
-
const stream = new ReadableStream({
|
|
178
|
-
async start(controller) {
|
|
179
|
-
// deduction completes before consumer reads
|
|
180
|
-
const result = await chargePlainResponse();
|
|
181
|
-
if (!result.ok) {
|
|
182
|
-
controller.error(insufficientBalance(result.channel.highestVoucherAmount - result.channel.spent));
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
if (!chargedResponse.body) {
|
|
186
|
-
controller.close();
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
const reader = chargedResponse.body.getReader();
|
|
190
|
-
try {
|
|
191
|
-
while (true) {
|
|
192
|
-
const { done, value } = await reader.read();
|
|
193
|
-
if (done)
|
|
194
|
-
break;
|
|
195
|
-
controller.enqueue(value);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
finally {
|
|
199
|
-
reader.releaseLock();
|
|
200
|
-
controller.close();
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
});
|
|
204
|
-
return new Response(stream, {
|
|
205
|
-
status: chargedResponse.status,
|
|
206
|
-
statusText: chargedResponse.statusText,
|
|
207
|
-
headers: chargedResponse.headers,
|
|
208
|
-
});
|
|
209
168
|
},
|
|
210
169
|
});
|
|
211
170
|
}
|
|
@@ -256,7 +215,4 @@ function isAsyncGeneratorFunction(value) {
|
|
|
256
215
|
function isAsyncIterable(value) {
|
|
257
216
|
return value !== null && typeof value === 'object' && Symbol.asyncIterator in value;
|
|
258
217
|
}
|
|
259
|
-
function isNullBodyStatus(status) {
|
|
260
|
-
return [101, 204, 205, 304].includes(status);
|
|
261
|
-
}
|
|
262
218
|
//# sourceMappingURL=transport.js.map
|
|
@@ -82,7 +82,7 @@ export function serializeCredential(challenge, payload, chainId, account) {
|
|
|
82
82
|
return Credential.serialize({
|
|
83
83
|
challenge,
|
|
84
84
|
payload,
|
|
85
|
-
source: `did:pkh:eip155:${chainId}:${account.address}`,
|
|
85
|
+
source: `did:pkh:eip155:${chainId}:${payload.descriptor?.payer ?? account.address}`,
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
/** Case-insensitive EVM address equality. */
|
|
@@ -26,7 +26,10 @@ export type SessionManager = {
|
|
|
26
26
|
readonly state: SessionState;
|
|
27
27
|
/** Runs the HTTP 402 probe, signs/open/top-ups as needed, retries, and returns receipt metadata. */
|
|
28
28
|
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<PaymentResponse>;
|
|
29
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Opens a paid SSE stream and auto-posts vouchers/top-ups when the server requests more headroom.
|
|
31
|
+
* Rejects `Fetch.withoutPayment` requests; use `fetch` to read an unpaid stream without session management.
|
|
32
|
+
*/
|
|
30
33
|
sse(input: RequestInfo | URL, init?: SessionManagerSseOptions): Promise<AsyncIterable<string>>;
|
|
31
34
|
/** Opens a paid WebSocket session after an HTTP challenge probe and manages in-band voucher frames. */
|
|
32
35
|
ws(input: string | URL, init?: SessionManagerWebSocketOptions): Promise<SessionManagedWebSocket>;
|
|
@@ -672,10 +672,11 @@ export function sessionManager(parameters) {
|
|
|
672
672
|
if (runtime.channel?.opened)
|
|
673
673
|
await persistChannel(runtime.channel);
|
|
674
674
|
}
|
|
675
|
-
function toPaymentResponse(response) {
|
|
675
|
+
function toPaymentResponse(response, acceptReceipt = true) {
|
|
676
676
|
const receiptHeader = response.headers.get(Constants.Headers.paymentReceipt);
|
|
677
677
|
const receipt = receiptHeader ? deserializeSessionReceipt(receiptHeader) : null;
|
|
678
|
-
|
|
678
|
+
if (acceptReceipt)
|
|
679
|
+
updateSpentFromReceipt(receipt);
|
|
679
680
|
return Object.assign(response, {
|
|
680
681
|
receipt,
|
|
681
682
|
challenge: runtime.lastChallenge,
|
|
@@ -684,6 +685,9 @@ export function sessionManager(parameters) {
|
|
|
684
685
|
});
|
|
685
686
|
}
|
|
686
687
|
async function doFetch(input, init) {
|
|
688
|
+
// Opted-out requests must not bootstrap, restore, top up, or retry a session.
|
|
689
|
+
if (init?.[Fetch.skipPayment])
|
|
690
|
+
return toPaymentResponse(await config.fetch(input, Fetch.withoutPayment(init)), false);
|
|
687
691
|
// The manager drives one shared `runtime` state machine, so requests are
|
|
688
692
|
// single-flight. Reject overlap loudly instead of letting concurrent calls
|
|
689
693
|
// corrupt each other's runtime and channel tracking.
|
|
@@ -855,6 +859,8 @@ export function sessionManager(parameters) {
|
|
|
855
859
|
});
|
|
856
860
|
},
|
|
857
861
|
async sse(input, init) {
|
|
862
|
+
if (init?.[Fetch.skipPayment])
|
|
863
|
+
throw new Error('SessionManager.sse requires payment; use fetch for unpaid streams.');
|
|
858
864
|
return openSseSession(input, init, sseDriver);
|
|
859
865
|
},
|
|
860
866
|
async ws(input, init) {
|
|
@@ -92,11 +92,10 @@ export function assertOpenCredentialCoversRequest(parameters) {
|
|
|
92
92
|
/** Verifies that the credential source is authorized to spend from the channel. */
|
|
93
93
|
export function assertCredentialSourceCanSpend(parameters) {
|
|
94
94
|
const sourceAddress = readCredentialSourceAddress(parameters.source, parameters.chainId);
|
|
95
|
-
if (isAddressEqual(sourceAddress, parameters.channel.payer)
|
|
96
|
-
isAddressEqual(sourceAddress, parameters.channel.authorizedSigner))
|
|
95
|
+
if (isAddressEqual(sourceAddress, parameters.channel.payer))
|
|
97
96
|
return;
|
|
98
97
|
throw new VerificationFailedError({
|
|
99
|
-
reason: 'credential source does not match channel payer
|
|
98
|
+
reason: 'credential source does not match channel payer',
|
|
100
99
|
});
|
|
101
100
|
}
|
|
102
101
|
/** Binds an opening payment to the payer authenticated by its signed transaction. */
|
package/dist/viem/Client.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export declare function remoteFeePayerTransport(config: RemoteFeePayer.Config, o
|
|
|
6
6
|
retryCount?: number | undefined;
|
|
7
7
|
}): import("viem").HttpTransport<undefined, false>;
|
|
8
8
|
export declare function getResolver(parameters: getResolver.Parameters & {
|
|
9
|
+
/** Require custom clients to declare the requested chain before applying defaults. */
|
|
10
|
+
requireChain?: boolean | undefined;
|
|
9
11
|
/** Default chain to use if not provided. */
|
|
10
12
|
chain?: Chain | undefined;
|
|
11
13
|
/** Remote fee-payer configuration. When set, the transport is wrapped with `withFeePayer`. */
|
package/dist/viem/Client.js
CHANGED
|
@@ -21,10 +21,13 @@ export function getResolver(parameters) {
|
|
|
21
21
|
// ensure user-provided clients inherit those serializers. Without this, clients
|
|
22
22
|
// created without the Tempo chain config will use the default viem serializer,
|
|
23
23
|
// causing errors like "maxFeePerGas is not a valid Legacy Transaction attribute".
|
|
24
|
-
if (!chain?.serializers && !remoteFeePayer)
|
|
24
|
+
if (!chain?.serializers && !remoteFeePayer && !parameters.requireChain)
|
|
25
25
|
return getClient;
|
|
26
26
|
return async (params) => {
|
|
27
27
|
const client = await getClient(params);
|
|
28
|
+
if (parameters.requireChain &&
|
|
29
|
+
(client.chain?.id === undefined || client.chain.id !== params.chainId))
|
|
30
|
+
throw new Error(`Subscription chain mismatch: expected ${params.chainId}, got ${client.chain?.id ?? 'unknown'}.`);
|
|
28
31
|
let resolvedClient = client;
|
|
29
32
|
// Wrap the client's transport with `withFeePayer` when a fee payer URL is provided.
|
|
30
33
|
if (remoteFeePayer && client.transport.key !== 'feePayer') {
|
package/dist/x402/Assets.d.ts
CHANGED
|
@@ -61,6 +61,12 @@ export declare const celo: {
|
|
|
61
61
|
export declare const celoSepolia: {
|
|
62
62
|
readonly USDC: KnownAsset;
|
|
63
63
|
};
|
|
64
|
+
/**
|
|
65
|
+
* Returns the known asset mppx ships for `address` on `network` (such as `base.USDC`), compared
|
|
66
|
+
* case-insensitively. Clients use it for trusted decimals when no currency allowlist names the
|
|
67
|
+
* asset.
|
|
68
|
+
*/
|
|
69
|
+
export declare function lookup(address: string, network: EvmNetwork): KnownAsset | undefined;
|
|
64
70
|
/** Returns true when a value is known x402 asset metadata. */
|
|
65
71
|
export declare function isAsset(value: unknown): value is KnownAsset;
|
|
66
72
|
/** Returns true when a value is a `viem/tokens` token definition. */
|
package/dist/x402/Assets.js
CHANGED
|
@@ -88,6 +88,16 @@ export const celoSepolia = {
|
|
|
88
88
|
},
|
|
89
89
|
}),
|
|
90
90
|
};
|
|
91
|
+
const knownAssets = [base, baseSepolia, celo, celoSepolia].flatMap((assets) => Object.values(assets));
|
|
92
|
+
/**
|
|
93
|
+
* Returns the known asset mppx ships for `address` on `network` (such as `base.USDC`), compared
|
|
94
|
+
* case-insensitively. Clients use it for trusted decimals when no currency allowlist names the
|
|
95
|
+
* asset.
|
|
96
|
+
*/
|
|
97
|
+
export function lookup(address, network) {
|
|
98
|
+
const normalized = address.toLowerCase();
|
|
99
|
+
return knownAssets.find((asset) => asset.network === network && asset.address.toLowerCase() === normalized);
|
|
100
|
+
}
|
|
91
101
|
/** Returns true when a value is known x402 asset metadata. */
|
|
92
102
|
export function isAsset(value) {
|
|
93
103
|
if (typeof value !== 'object' || value === null)
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { type Account, type Address, type Client } from 'viem';
|
|
2
|
+
import * as Store from '../Store.js';
|
|
3
|
+
import * as z from '../zod.js';
|
|
4
|
+
/** Key of the x402 `sign-in-with-x` (SIWX) extension in a 402's `extensions`. */
|
|
5
|
+
export declare const extensionKey = "sign-in-with-x";
|
|
6
|
+
/** Request header that carries a signed SIWX proof as base64-encoded JSON. */
|
|
7
|
+
export declare const header = "SIGN-IN-WITH-X";
|
|
8
|
+
/** How long a SIWX challenge stays valid, in milliseconds. */
|
|
9
|
+
export declare const ttl: number;
|
|
10
|
+
/**
|
|
11
|
+
* Machine-readable codes for a rejected proof, as the x402 SIWX spec names them.
|
|
12
|
+
*
|
|
13
|
+
* @see https://github.com/coinbase/x402/blob/main/specs/extensions/sign-in-with-x.md
|
|
14
|
+
*/
|
|
15
|
+
export type ErrorCode = 'invalid_siwx_payload' | 'invalid_siwx_domain_mismatch' | 'invalid_siwx_uri_mismatch' | 'invalid_siwx_issued_at' | 'invalid_siwx_issued_at_too_old' | 'invalid_siwx_issued_at_in_future' | 'invalid_siwx_expiration_time' | 'invalid_siwx_expired' | 'invalid_siwx_not_before' | 'invalid_siwx_not_yet_valid' | 'invalid_siwx_nonce' | 'invalid_siwx_signature' | 'invalid_siwx_chain_id' | 'invalid_siwx_unsupported_chain' | 'invalid_siwx_verifier_error';
|
|
16
|
+
/** A signing method a server accepts. */
|
|
17
|
+
export declare const ChainSchema: z.ZodMiniObject<{
|
|
18
|
+
chainId: z.ZodMiniString<string>;
|
|
19
|
+
signatureScheme: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
20
|
+
type: z.ZodMiniString<string>;
|
|
21
|
+
}, z.core.$strip>;
|
|
22
|
+
/** A signing method a server accepts. */
|
|
23
|
+
export type Chain = z.infer<typeof ChainSchema>;
|
|
24
|
+
/** Message fields a server declares in its 402. */
|
|
25
|
+
export declare const InfoSchema: z.ZodMiniObject<{
|
|
26
|
+
domain: z.ZodMiniString<string>;
|
|
27
|
+
expirationTime: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
28
|
+
issuedAt: z.ZodMiniString<string>;
|
|
29
|
+
nonce: z.ZodMiniString<string>;
|
|
30
|
+
notBefore: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
31
|
+
requestId: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
32
|
+
resources: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniString<string>>>;
|
|
33
|
+
statement: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
34
|
+
uri: z.ZodMiniString<string>;
|
|
35
|
+
version: z.ZodMiniString<string>;
|
|
36
|
+
}, z.core.$strip>;
|
|
37
|
+
/** Message fields a server declares in its 402. */
|
|
38
|
+
export type Info = z.infer<typeof InfoSchema>;
|
|
39
|
+
/** The extension's value in a 402's `extensions`. */
|
|
40
|
+
export declare const ExtensionSchema: z.ZodMiniObject<{
|
|
41
|
+
info: z.ZodMiniObject<{
|
|
42
|
+
domain: z.ZodMiniString<string>;
|
|
43
|
+
expirationTime: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
44
|
+
issuedAt: z.ZodMiniString<string>;
|
|
45
|
+
nonce: z.ZodMiniString<string>;
|
|
46
|
+
notBefore: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
47
|
+
requestId: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
48
|
+
resources: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniString<string>>>;
|
|
49
|
+
statement: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
50
|
+
uri: z.ZodMiniString<string>;
|
|
51
|
+
version: z.ZodMiniString<string>;
|
|
52
|
+
}, z.core.$strip>;
|
|
53
|
+
schema: z.ZodMiniRecord<z.ZodMiniString<string>, z.ZodMiniUnknown>;
|
|
54
|
+
supportedChains: z.ZodMiniArray<z.ZodMiniObject<{
|
|
55
|
+
chainId: z.ZodMiniString<string>;
|
|
56
|
+
signatureScheme: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
57
|
+
type: z.ZodMiniString<string>;
|
|
58
|
+
}, z.core.$strip>>;
|
|
59
|
+
}, z.core.$strip>;
|
|
60
|
+
/** The extension's value in a 402's `extensions`. */
|
|
61
|
+
export type Extension = z.infer<typeof ExtensionSchema>;
|
|
62
|
+
/** The signed proof a client sends in the {@link header}. */
|
|
63
|
+
export declare const PayloadSchema: z.ZodMiniObject<{
|
|
64
|
+
domain: z.ZodMiniString<string>;
|
|
65
|
+
expirationTime: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
66
|
+
issuedAt: z.ZodMiniString<string>;
|
|
67
|
+
nonce: z.ZodMiniString<string>;
|
|
68
|
+
notBefore: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
69
|
+
requestId: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
70
|
+
resources: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniString<string>>>;
|
|
71
|
+
statement: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
72
|
+
uri: z.ZodMiniString<string>;
|
|
73
|
+
version: z.ZodMiniString<string>;
|
|
74
|
+
address: z.ZodMiniString<string>;
|
|
75
|
+
chainId: z.ZodMiniString<string>;
|
|
76
|
+
signature: z.ZodMiniString<string>;
|
|
77
|
+
signatureScheme: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
78
|
+
type: z.ZodMiniString<string>;
|
|
79
|
+
}, z.core.$strip>;
|
|
80
|
+
/** The signed proof a client sends in the {@link header}. */
|
|
81
|
+
export type Payload = z.infer<typeof PayloadSchema>;
|
|
82
|
+
/** JSON Schema of a {@link Payload}, advertised in the 402 as the spec requires. */
|
|
83
|
+
export declare const payloadJsonSchema: {
|
|
84
|
+
readonly $schema: 'https://json-schema.org/draft/2020-12/schema';
|
|
85
|
+
readonly properties: {
|
|
86
|
+
readonly address: {
|
|
87
|
+
readonly type: 'string';
|
|
88
|
+
};
|
|
89
|
+
readonly chainId: {
|
|
90
|
+
readonly type: 'string';
|
|
91
|
+
};
|
|
92
|
+
readonly domain: {
|
|
93
|
+
readonly type: 'string';
|
|
94
|
+
};
|
|
95
|
+
readonly expirationTime: {
|
|
96
|
+
readonly format: 'date-time';
|
|
97
|
+
readonly type: 'string';
|
|
98
|
+
};
|
|
99
|
+
readonly issuedAt: {
|
|
100
|
+
readonly format: 'date-time';
|
|
101
|
+
readonly type: 'string';
|
|
102
|
+
};
|
|
103
|
+
readonly nonce: {
|
|
104
|
+
readonly type: 'string';
|
|
105
|
+
};
|
|
106
|
+
readonly notBefore: {
|
|
107
|
+
readonly format: 'date-time';
|
|
108
|
+
readonly type: 'string';
|
|
109
|
+
};
|
|
110
|
+
readonly requestId: {
|
|
111
|
+
readonly type: 'string';
|
|
112
|
+
};
|
|
113
|
+
readonly resources: {
|
|
114
|
+
readonly items: {
|
|
115
|
+
readonly format: 'uri';
|
|
116
|
+
readonly type: 'string';
|
|
117
|
+
};
|
|
118
|
+
readonly type: 'array';
|
|
119
|
+
};
|
|
120
|
+
readonly signature: {
|
|
121
|
+
readonly type: 'string';
|
|
122
|
+
};
|
|
123
|
+
readonly statement: {
|
|
124
|
+
readonly type: 'string';
|
|
125
|
+
};
|
|
126
|
+
readonly type: {
|
|
127
|
+
readonly type: 'string';
|
|
128
|
+
};
|
|
129
|
+
readonly uri: {
|
|
130
|
+
readonly format: 'uri';
|
|
131
|
+
readonly type: 'string';
|
|
132
|
+
};
|
|
133
|
+
readonly version: {
|
|
134
|
+
readonly type: 'string';
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
readonly required: readonly ['domain', 'address', 'uri', 'version', 'chainId', 'type', 'nonce', 'issuedAt', 'signature'];
|
|
138
|
+
readonly type: 'object';
|
|
139
|
+
};
|
|
140
|
+
/** Error for a proof the server rejects. Its message is the spec's {@link ErrorCode}. */
|
|
141
|
+
export declare class InvalidProofError extends Error {
|
|
142
|
+
readonly name = "Siwx.InvalidProofError";
|
|
143
|
+
/** The spec's machine-readable code for the failed check. */
|
|
144
|
+
readonly code: ErrorCode;
|
|
145
|
+
constructor(code: ErrorCode);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Builds the EIP-4361 (SIWE) message a payload signs.
|
|
149
|
+
*
|
|
150
|
+
* The layout is viem's `createSiweMessage`, which also validates the fields, but timestamps
|
|
151
|
+
* are signed exactly as the payload carries them, as the x402 reference does, so a proof
|
|
152
|
+
* with any RFC 3339 timestamp verifies across implementations.
|
|
153
|
+
*/
|
|
154
|
+
export declare function toMessage(payload: Omit<Payload, 'signature'>): string;
|
|
155
|
+
/** Encodes a payload as a {@link header} value. */
|
|
156
|
+
export declare function encode(payload: Payload): string;
|
|
157
|
+
/** Decodes a {@link header} value. Throws {@link InvalidProofError} when it is malformed. */
|
|
158
|
+
export declare function decode(value: string): Payload;
|
|
159
|
+
/** Reads the SIWX extension from a decoded x402 402 body, if it offers one. */
|
|
160
|
+
export declare function fromPaymentRequired(paymentRequired: {
|
|
161
|
+
extensions?: Record<string, unknown> | undefined;
|
|
162
|
+
}): Extension | undefined;
|
|
163
|
+
/** Builds the extension a server adds to a 402, with a fresh nonce. */
|
|
164
|
+
export declare function createExtension(parameters: {
|
|
165
|
+
/** EVM chain ID the server accepts signatures for. */
|
|
166
|
+
chainId: number;
|
|
167
|
+
/** The server's public origin. */
|
|
168
|
+
origin: string;
|
|
169
|
+
/** URL of the requested resource. */
|
|
170
|
+
url: string;
|
|
171
|
+
}): Extension;
|
|
172
|
+
/** Server options for accepting SIWX proofs on an x402 route. */
|
|
173
|
+
export type ServerOptions = {
|
|
174
|
+
/**
|
|
175
|
+
* Client for verifying smart-wallet (EIP-1271 and EIP-6492) signatures with an RPC call.
|
|
176
|
+
* It must serve the offer's chain: proofs for another chain are rejected.
|
|
177
|
+
* Without it, only EOA signatures verify.
|
|
178
|
+
*/
|
|
179
|
+
client?: Client | undefined;
|
|
180
|
+
/**
|
|
181
|
+
* Decides whether a wallet that proved its address gets the resource without paying.
|
|
182
|
+
* Return `false` to answer 402 so it pays.
|
|
183
|
+
*/
|
|
184
|
+
grant: (parameters: {
|
|
185
|
+
/** Address the proof recovered. */
|
|
186
|
+
address: Address;
|
|
187
|
+
/** EVM chain ID the proof was signed for. */
|
|
188
|
+
chainId: number;
|
|
189
|
+
/** The request being authorized. */
|
|
190
|
+
request: Request;
|
|
191
|
+
}) => boolean | Promise<boolean>;
|
|
192
|
+
/**
|
|
193
|
+
* Public origin of the server as clients see it, such as `https://api.example.com`.
|
|
194
|
+
* Proofs must name it. It is never taken from request headers such as `Host`,
|
|
195
|
+
* which a caller controls.
|
|
196
|
+
*/
|
|
197
|
+
origin: string;
|
|
198
|
+
/**
|
|
199
|
+
* Atomic store that records used nonces, so each proof is accepted once. In a composed
|
|
200
|
+
* route, give offers that share a backend the same store object: each store object
|
|
201
|
+
* records the nonce on its own, so two objects over one backend refuse a later offer.
|
|
202
|
+
*/
|
|
203
|
+
store: Store.AtomicStore;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Verifies a SIWX proof against the server's origin and chain. It does not record the
|
|
207
|
+
* nonce: call {@link claim} once the proof is used, so a check that declines the wallet
|
|
208
|
+
* leaves the proof for another.
|
|
209
|
+
* Throws {@link InvalidProofError} with the spec's code when the proof is rejected.
|
|
210
|
+
*/
|
|
211
|
+
export declare function verify(parameters: {
|
|
212
|
+
/** EVM chain ID the server accepts signatures for. */
|
|
213
|
+
chainId: number;
|
|
214
|
+
/** The server's SIWX options. */
|
|
215
|
+
options: Pick<ServerOptions, 'client' | 'origin'>;
|
|
216
|
+
/** The decoded proof. */
|
|
217
|
+
payload: Payload;
|
|
218
|
+
}): Promise<{
|
|
219
|
+
address: Address;
|
|
220
|
+
chainId: number;
|
|
221
|
+
}>;
|
|
222
|
+
/**
|
|
223
|
+
* Records a verified proof's nonce so it is accepted once.
|
|
224
|
+
* Throws {@link InvalidProofError} with `invalid_siwx_nonce` when it was already used.
|
|
225
|
+
*/
|
|
226
|
+
export declare function claim(parameters: {
|
|
227
|
+
/** The verified proof. */
|
|
228
|
+
payload: Pick<Payload, 'issuedAt' | 'nonce'>;
|
|
229
|
+
/** Atomic store that records used nonces. */
|
|
230
|
+
store: Store.AtomicStore;
|
|
231
|
+
}): Promise<void>;
|
|
232
|
+
/**
|
|
233
|
+
* Releases a nonce {@link claim} recorded, so the proof can be accepted again. Call it
|
|
234
|
+
* only for a claim this request made, such as when `grant` declines the wallet.
|
|
235
|
+
* Does nothing once that claim has expired, since another request may hold the nonce by then.
|
|
236
|
+
*/
|
|
237
|
+
export declare function release(parameters: {
|
|
238
|
+
/** The claimed proof. */
|
|
239
|
+
payload: Pick<Payload, 'issuedAt' | 'nonce'>;
|
|
240
|
+
/** The store `claim` recorded the nonce in. */
|
|
241
|
+
store: Store.AtomicStore;
|
|
242
|
+
}): Promise<void>;
|
|
243
|
+
/**
|
|
244
|
+
* Wraps `fetch` so a 402 that offers SIWX is answered once with a proof signed by
|
|
245
|
+
* `account`, before any payment. Pass the result as the `fetch` of an mppx client:
|
|
246
|
+
* when the server still answers 402 because it doesn't grant this wallet, mppx pays
|
|
247
|
+
* from that response as usual.
|
|
248
|
+
*
|
|
249
|
+
* It signs only when the challenge's `domain` and `uri` origin match the URL that
|
|
250
|
+
* returned the 402, and only for `eip155` chains.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* import { Fetch, evm } from 'mppx/client'
|
|
255
|
+
* import { Siwx } from 'mppx/x402'
|
|
256
|
+
*
|
|
257
|
+
* const fetch = Fetch.from({
|
|
258
|
+
* fetch: Siwx.wrapFetch(globalThis.fetch, { account }),
|
|
259
|
+
* methods: [evm({ account })],
|
|
260
|
+
* })
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
export declare function wrapFetch(fetch: typeof globalThis.fetch, parameters: wrapFetch.Parameters): typeof globalThis.fetch;
|
|
264
|
+
export declare namespace wrapFetch {
|
|
265
|
+
type Parameters = {
|
|
266
|
+
/** Account that signs SIWX challenges. Must be able to sign messages. */
|
|
267
|
+
account: Account;
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
//# sourceMappingURL=Siwx.d.ts.map
|