mppx 0.20.1 → 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 +24 -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 +44 -0
- package/dist/evm/server/Methods.d.ts +8 -0
- package/dist/evm/server/Methods.js +8 -0
- package/dist/extensions/identity/attestation/Jwks.js +3 -3
- package/dist/extensions/identity/link/server/index.js +4 -4
- package/dist/extensions/identity/persona/server/index.js +3 -3
- 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/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 +19 -14
- 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/Types.d.ts +68 -8
- package/dist/x402/Types.js +17 -2
- package/dist/x402/client/Exact.js +4 -1
- package/dist/x402/middlewares/express.js +10 -1
- package/dist/x402/server/EvmCharge.d.ts +20 -3
- package/dist/x402/server/EvmCharge.js +13 -1
- package/dist/x402/server/Facilitator.d.ts +42 -3
- package/dist/x402/server/Facilitator.js +126 -16
- package/package.json +1 -1
|
@@ -1,36 +1,124 @@
|
|
|
1
|
+
import { getAddress } from 'viem';
|
|
2
|
+
import { FacilitatorResponseError } from '../../Errors.js';
|
|
1
3
|
import * as Types from '../Types.js';
|
|
2
4
|
const mppxFetchWrapper = Symbol.for('mppx.fetch.wrapper');
|
|
3
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Resolves an x402 facilitator URL, `{ url, headers }` config or client into a
|
|
7
|
+
* facilitator client. A config's `headers` take precedence over `options`.
|
|
8
|
+
*/
|
|
4
9
|
export function resolve(facilitator, errorMessage = 'x402 exact requires `facilitator`.', options) {
|
|
5
|
-
if (typeof facilitator === 'object' && facilitator !== null)
|
|
6
|
-
return facilitator;
|
|
7
10
|
if (typeof facilitator === 'string')
|
|
8
11
|
return http(facilitator, options);
|
|
12
|
+
if (typeof facilitator === 'object' && facilitator !== null) {
|
|
13
|
+
if (isConfig(facilitator))
|
|
14
|
+
return http(facilitator.url, {
|
|
15
|
+
...options,
|
|
16
|
+
...(facilitator.headers ? { headers: facilitator.headers } : {}),
|
|
17
|
+
});
|
|
18
|
+
return facilitator;
|
|
19
|
+
}
|
|
9
20
|
throw new Error(errorMessage);
|
|
10
21
|
}
|
|
11
|
-
|
|
22
|
+
const paths = ['settle', 'supported', 'verify'];
|
|
23
|
+
/**
|
|
24
|
+
* Picks the headers for one facilitator path. `headers` is per-path only when it has at least one
|
|
25
|
+
* key, every key is `verify`, `settle` or `supported` and every value is an object; otherwise it
|
|
26
|
+
* is a flat record sent on every path.
|
|
27
|
+
*/
|
|
28
|
+
function pathHeaders(headers, path) {
|
|
29
|
+
if (!headers)
|
|
30
|
+
return undefined;
|
|
31
|
+
const entries = Object.entries(headers);
|
|
32
|
+
const perPath = entries.length > 0 &&
|
|
33
|
+
entries.every(([key, value]) => paths.includes(key) && typeof value === 'object' && value !== null);
|
|
34
|
+
return perPath
|
|
35
|
+
? headers[path]
|
|
36
|
+
: headers;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates an x402 facilitator client from an HTTP base URL.
|
|
40
|
+
*
|
|
41
|
+
* A non-2xx response throws `Errors.FacilitatorResponseError`, except a `/verify` or `/settle`
|
|
42
|
+
* error whose body is a failed result (`isValid: false` or `success: false`), which is returned
|
|
43
|
+
* as that result, the way `@x402/core` reads it.
|
|
44
|
+
*/
|
|
12
45
|
export function http(url, options) {
|
|
13
46
|
const base = url.replace(/\/$/, '');
|
|
14
47
|
const fetch = unwrapFetch(options?.fetch ?? globalThis.fetch);
|
|
48
|
+
const authHeaders = async (path) => {
|
|
49
|
+
const headers = options?.headers;
|
|
50
|
+
return pathHeaders(typeof headers === 'function' ? await headers() : headers, path);
|
|
51
|
+
};
|
|
52
|
+
async function post(path, body) {
|
|
53
|
+
return fetch(`${base}/${path}`, {
|
|
54
|
+
body: JSON.stringify(body),
|
|
55
|
+
headers: { ...(await authHeaders(path)), 'Content-Type': 'application/json' },
|
|
56
|
+
method: 'POST',
|
|
57
|
+
});
|
|
58
|
+
}
|
|
15
59
|
return {
|
|
16
60
|
async verify(paymentPayload, paymentRequirements) {
|
|
17
|
-
const response = await
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
61
|
+
const response = await post('verify', { paymentPayload, paymentRequirements, x402Version: 2 });
|
|
62
|
+
const body = await readBody(response);
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
const failed = Types.VerifyResponseSchema.safeParse(body.json);
|
|
65
|
+
if (failed.success && failed.data.isValid === false)
|
|
66
|
+
return failed.data;
|
|
67
|
+
throw responseError('verify', response, body.text);
|
|
68
|
+
}
|
|
69
|
+
return parseResult('verify', Types.VerifyResponseSchema, response, body);
|
|
23
70
|
},
|
|
24
71
|
async settle(paymentPayload, paymentRequirements) {
|
|
25
|
-
const response = await
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
72
|
+
const response = await post('settle', { paymentPayload, paymentRequirements, x402Version: 2 });
|
|
73
|
+
const body = await readBody(response);
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
const failed = Types.SettleResponseSchema.safeParse(body.json);
|
|
76
|
+
if (failed.success && failed.data.success === false)
|
|
77
|
+
return failed.data;
|
|
78
|
+
throw responseError('settle', response, body.text);
|
|
79
|
+
}
|
|
80
|
+
return parseResult('settle', Types.SettleResponseSchema, response, body);
|
|
81
|
+
},
|
|
82
|
+
async supported() {
|
|
83
|
+
const headers = await authHeaders('supported');
|
|
84
|
+
const response = await fetch(`${base}/supported`, headers ? { headers, method: 'GET' } : { method: 'GET' });
|
|
85
|
+
const body = await readBody(response);
|
|
86
|
+
if (!response.ok)
|
|
87
|
+
throw responseError('supported', response, body.text);
|
|
88
|
+
return parseResult('supported', Types.SupportedResponseSchema, response, body);
|
|
31
89
|
},
|
|
32
90
|
};
|
|
33
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Reads the `payTo` a facilitator publishes for a scheme and network in `GET /supported`
|
|
94
|
+
* (`kinds[].extra.payTo`), for facilitators that settle into an address of their own, the way
|
|
95
|
+
* Solana facilitators publish `extra.feePayer`. Call it once at startup and pass the result as
|
|
96
|
+
* `recipient`. Accepts the same facilitator forms as `x402.facilitator`.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* evm({
|
|
101
|
+
* currency: evm.assets.base.USDC,
|
|
102
|
+
* recipient: await x402.Facilitator.payTo(FACILITATOR_URL, { network: 'eip155:8453' }),
|
|
103
|
+
* x402: { facilitator: FACILITATOR_URL, extra: { tempoSettlement: '0x…' } },
|
|
104
|
+
* })
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export async function payTo(facilitator, parameters) {
|
|
108
|
+
const { headers, fetch, network, scheme = 'exact' } = parameters;
|
|
109
|
+
const client = resolve(facilitator, 'x402 payTo requires `facilitator`.', {
|
|
110
|
+
headers,
|
|
111
|
+
fetch,
|
|
112
|
+
});
|
|
113
|
+
if (!client.supported)
|
|
114
|
+
throw new Error('x402 facilitator does not implement `supported()`, so it cannot provide payTo.');
|
|
115
|
+
const { kinds } = await client.supported();
|
|
116
|
+
const kind = kinds.find((kind) => kind.x402Version === 2 && kind.scheme === scheme && kind.network === network);
|
|
117
|
+
const value = kind?.extra?.payTo;
|
|
118
|
+
if (typeof value !== 'string' || !/^0x[0-9a-fA-F]{40}$/.test(value))
|
|
119
|
+
throw new Error(`x402 facilitator publishes no payTo for ${scheme} on ${network}.`);
|
|
120
|
+
return getAddress(value);
|
|
121
|
+
}
|
|
34
122
|
/** Returns the underlying raw fetch implementation when given an mppx wrapper. */
|
|
35
123
|
export function unwrapFetch(fetch) {
|
|
36
124
|
let current = fetch;
|
|
@@ -39,4 +127,26 @@ export function unwrapFetch(fetch) {
|
|
|
39
127
|
}
|
|
40
128
|
return current;
|
|
41
129
|
}
|
|
130
|
+
function isConfig(facilitator) {
|
|
131
|
+
return (typeof facilitator.url === 'string' &&
|
|
132
|
+
typeof facilitator.verify !== 'function');
|
|
133
|
+
}
|
|
134
|
+
async function readBody(response) {
|
|
135
|
+
const text = await response.text();
|
|
136
|
+
let json;
|
|
137
|
+
try {
|
|
138
|
+
json = JSON.parse(text);
|
|
139
|
+
}
|
|
140
|
+
catch { }
|
|
141
|
+
return { json, text };
|
|
142
|
+
}
|
|
143
|
+
function parseResult(path, schema, response, body) {
|
|
144
|
+
const parsed = schema.safeParse(body.json);
|
|
145
|
+
if (!parsed.success)
|
|
146
|
+
throw responseError(path, response, body.text, parsed.error);
|
|
147
|
+
return parsed.data;
|
|
148
|
+
}
|
|
149
|
+
function responseError(path, response, body, cause) {
|
|
150
|
+
return new FacilitatorResponseError({ body, cause, path, status: response.status });
|
|
151
|
+
}
|
|
42
152
|
//# sourceMappingURL=Facilitator.js.map
|