x402z-client 0.1.0 → 0.1.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/README.md +31 -3
- package/dist/index.d.mts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +34 -14
- package/dist/index.mjs +34 -14
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ const account = privateKeyToAccount("0x...");
|
|
|
26
26
|
const relayer = await createRelayer(SepoliaConfig);
|
|
27
27
|
|
|
28
28
|
const client = createX402zClient({
|
|
29
|
-
|
|
29
|
+
preferredMethods: ["erc7984-mind-v1.CONFIDENTIAL_USDC.sepolia"] as const,
|
|
30
30
|
signer: {
|
|
31
31
|
address: account.address,
|
|
32
32
|
signTypedData: account.signTypedData,
|
|
@@ -41,10 +41,37 @@ console.log(paid.response.status);
|
|
|
41
41
|
`createX402zClient` builds the confidential payment input automatically using the
|
|
42
42
|
`confidential.batcherAddress` provided by the server’s payment requirements.
|
|
43
43
|
|
|
44
|
+
## Choosing Payment Methods (Client)
|
|
45
|
+
|
|
46
|
+
The client picks the **first** preferred method that matches the server’s `accepts` list
|
|
47
|
+
by `scheme + network + asset`.
|
|
48
|
+
|
|
49
|
+
You can specify preferred methods in two ways:
|
|
50
|
+
|
|
51
|
+
1) **Name string** (scheme + token + network alias):
|
|
52
|
+
```ts
|
|
53
|
+
preferredMethods: ["exact.USDC.base", "erc7984-mind-v1.CONFIDENTIAL_USDC.sepolia"] as const
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
2) **Config entry** (from `SCHEME_CONFIG`):
|
|
57
|
+
```ts
|
|
58
|
+
import { SCHEME_CONFIG } from "x402z-scheme-config";
|
|
59
|
+
|
|
60
|
+
preferredMethods: [
|
|
61
|
+
SCHEME_CONFIG.exact.USDC.base,
|
|
62
|
+
SCHEME_CONFIG["erc7984-mind-v1"].CONFIDENTIAL_USDC.sepolia,
|
|
63
|
+
]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
If `preferredMethods` is omitted, the client defaults to `SCHEME_CONFIG_NAMES`
|
|
67
|
+
(all known methods, in order).
|
|
68
|
+
|
|
44
69
|
## API
|
|
45
70
|
|
|
46
71
|
- `createX402zClient(config)`
|
|
47
|
-
- `
|
|
72
|
+
- `preferredMethods` (optional): ordered list of preferred methods.
|
|
73
|
+
- Name form: `"exact.USDC.base"` (matches scheme/token/network alias from `x402z-scheme-config`).
|
|
74
|
+
- Config form: `SCHEME_CONFIG.exact.USDC.base`.
|
|
48
75
|
- `signer` (required): signer used for both exact and confidential schemes
|
|
49
76
|
- `relayer` (required when using `erc7984-mind-v1`): Zama relayer instance used to build encrypted inputs
|
|
50
77
|
- `fetch` (optional): custom fetch implementation
|
|
@@ -63,7 +90,7 @@ import { privateKeyToAccount } from "viem/accounts";
|
|
|
63
90
|
|
|
64
91
|
const account = privateKeyToAccount("0x...");
|
|
65
92
|
const client = createX402zClient({
|
|
66
|
-
|
|
93
|
+
preferredMethods: ["exact.USDC.sepolia"] as const,
|
|
67
94
|
signer: account,
|
|
68
95
|
});
|
|
69
96
|
|
|
@@ -91,6 +118,7 @@ Exports:
|
|
|
91
118
|
Types:
|
|
92
119
|
- `X402zClientSchemeOptions`: scheme config for client registration.
|
|
93
120
|
- `X402zClientRegistrationOptions`: registration options for the scheme.
|
|
121
|
+
- `PreferredPaymentMethod`: preferred payment method selector (object form or scheme-config name string).
|
|
94
122
|
- `X402zClientOptions`: options for `createX402zClient`.
|
|
95
123
|
- `X402zConfidentialClientOptions`: options for confidential payments.
|
|
96
124
|
- `X402zExactClientOptions`: options for exact payments.
|
package/dist/index.d.mts
CHANGED
|
@@ -4,6 +4,7 @@ import { ClientEvmSigner } from '@x402/evm';
|
|
|
4
4
|
import { ConfidentialPaymentInput, RelayerInstance } from 'x402z-shared';
|
|
5
5
|
import { x402Client, SelectPaymentRequirements, PaymentPolicy } from '@x402/core/client';
|
|
6
6
|
export { AfterPaymentCreationHook, BeforePaymentCreationHook, OnPaymentCreationFailureHook, PaymentCreatedContext, PaymentCreationContext, PaymentCreationFailureContext, PaymentPolicy, SchemeRegistration, SelectPaymentRequirements, x402Client, x402ClientConfig, x402HTTPClient } from '@x402/core/client';
|
|
7
|
+
import { SchemeConfigName, ExactTokenConfig, ConfidentialTokenConfig } from 'x402z-scheme-config';
|
|
7
8
|
|
|
8
9
|
type X402zClientSchemeOptions = {
|
|
9
10
|
signer: ClientEvmSigner;
|
|
@@ -31,13 +32,13 @@ declare function registerX402zEvmClientScheme(client: x402Client, config: X402zC
|
|
|
31
32
|
|
|
32
33
|
declare function buildPaymentInput(relayer: RelayerInstance, requirements: PaymentRequirements, amount: number): Promise<ConfidentialPaymentInput>;
|
|
33
34
|
|
|
34
|
-
type
|
|
35
|
+
type PreferredPaymentMethod = SchemeConfigName | ExactTokenConfig | ConfidentialTokenConfig;
|
|
35
36
|
type BaseClientOptions = {
|
|
36
37
|
fetch?: typeof fetch;
|
|
37
38
|
debug?: boolean;
|
|
38
39
|
};
|
|
39
40
|
type X402zClientOptions = {
|
|
40
|
-
|
|
41
|
+
preferredMethods?: readonly PreferredPaymentMethod[];
|
|
41
42
|
signer: ClientEvmSigner;
|
|
42
43
|
relayer?: RelayerInstance;
|
|
43
44
|
networks?: Network[];
|
|
@@ -47,7 +48,7 @@ type X402zClientOptions = {
|
|
|
47
48
|
hashEncryptedAmountInput?: X402zClientSchemeOptions["hashEncryptedAmountInput"];
|
|
48
49
|
clock?: X402zClientSchemeOptions["clock"];
|
|
49
50
|
onPaymentSelected?: (info: {
|
|
50
|
-
|
|
51
|
+
method: PreferredPaymentMethod;
|
|
51
52
|
requirements: PaymentRequirements;
|
|
52
53
|
}) => void;
|
|
53
54
|
} & BaseClientOptions;
|
|
@@ -65,4 +66,4 @@ type X402zClient = {
|
|
|
65
66
|
};
|
|
66
67
|
declare function createX402zClient(config: X402zClientOptions): X402zClient;
|
|
67
68
|
|
|
68
|
-
export { type PayOptions, type PayResult, type X402zClientOptions, type X402zClientRegistrationOptions, type X402zClientSchemeOptions, X402zEvmClientScheme, buildPaymentInput, createX402zClient, registerX402zEvmClientScheme };
|
|
69
|
+
export { type PayOptions, type PayResult, type PreferredPaymentMethod, type X402zClient, type X402zClientOptions, type X402zClientRegistrationOptions, type X402zClientSchemeOptions, X402zEvmClientScheme, buildPaymentInput, createX402zClient, registerX402zEvmClientScheme };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { ClientEvmSigner } from '@x402/evm';
|
|
|
4
4
|
import { ConfidentialPaymentInput, RelayerInstance } from 'x402z-shared';
|
|
5
5
|
import { x402Client, SelectPaymentRequirements, PaymentPolicy } from '@x402/core/client';
|
|
6
6
|
export { AfterPaymentCreationHook, BeforePaymentCreationHook, OnPaymentCreationFailureHook, PaymentCreatedContext, PaymentCreationContext, PaymentCreationFailureContext, PaymentPolicy, SchemeRegistration, SelectPaymentRequirements, x402Client, x402ClientConfig, x402HTTPClient } from '@x402/core/client';
|
|
7
|
+
import { SchemeConfigName, ExactTokenConfig, ConfidentialTokenConfig } from 'x402z-scheme-config';
|
|
7
8
|
|
|
8
9
|
type X402zClientSchemeOptions = {
|
|
9
10
|
signer: ClientEvmSigner;
|
|
@@ -31,13 +32,13 @@ declare function registerX402zEvmClientScheme(client: x402Client, config: X402zC
|
|
|
31
32
|
|
|
32
33
|
declare function buildPaymentInput(relayer: RelayerInstance, requirements: PaymentRequirements, amount: number): Promise<ConfidentialPaymentInput>;
|
|
33
34
|
|
|
34
|
-
type
|
|
35
|
+
type PreferredPaymentMethod = SchemeConfigName | ExactTokenConfig | ConfidentialTokenConfig;
|
|
35
36
|
type BaseClientOptions = {
|
|
36
37
|
fetch?: typeof fetch;
|
|
37
38
|
debug?: boolean;
|
|
38
39
|
};
|
|
39
40
|
type X402zClientOptions = {
|
|
40
|
-
|
|
41
|
+
preferredMethods?: readonly PreferredPaymentMethod[];
|
|
41
42
|
signer: ClientEvmSigner;
|
|
42
43
|
relayer?: RelayerInstance;
|
|
43
44
|
networks?: Network[];
|
|
@@ -47,7 +48,7 @@ type X402zClientOptions = {
|
|
|
47
48
|
hashEncryptedAmountInput?: X402zClientSchemeOptions["hashEncryptedAmountInput"];
|
|
48
49
|
clock?: X402zClientSchemeOptions["clock"];
|
|
49
50
|
onPaymentSelected?: (info: {
|
|
50
|
-
|
|
51
|
+
method: PreferredPaymentMethod;
|
|
51
52
|
requirements: PaymentRequirements;
|
|
52
53
|
}) => void;
|
|
53
54
|
} & BaseClientOptions;
|
|
@@ -65,4 +66,4 @@ type X402zClient = {
|
|
|
65
66
|
};
|
|
66
67
|
declare function createX402zClient(config: X402zClientOptions): X402zClient;
|
|
67
68
|
|
|
68
|
-
export { type PayOptions, type PayResult, type X402zClientOptions, type X402zClientRegistrationOptions, type X402zClientSchemeOptions, X402zEvmClientScheme, buildPaymentInput, createX402zClient, registerX402zEvmClientScheme };
|
|
69
|
+
export { type PayOptions, type PayResult, type PreferredPaymentMethod, type X402zClient, type X402zClientOptions, type X402zClientRegistrationOptions, type X402zClientSchemeOptions, X402zEvmClientScheme, buildPaymentInput, createX402zClient, registerX402zEvmClientScheme };
|
package/dist/index.js
CHANGED
|
@@ -150,8 +150,29 @@ var import_http = require("@x402/core/http");
|
|
|
150
150
|
var import_client2 = require("@x402/evm/exact/client");
|
|
151
151
|
var import_viem2 = require("viem");
|
|
152
152
|
var import_x402z_shared3 = require("x402z-shared");
|
|
153
|
-
|
|
154
|
-
|
|
153
|
+
var import_x402z_scheme_config = require("x402z-scheme-config");
|
|
154
|
+
function resolvePreferredMethod(method) {
|
|
155
|
+
if (typeof method !== "string") {
|
|
156
|
+
return method;
|
|
157
|
+
}
|
|
158
|
+
const config = (0, import_x402z_scheme_config.getSchemeConfigByName)(method);
|
|
159
|
+
if (!config) {
|
|
160
|
+
throw new Error(`Unknown preferred method: ${method}`);
|
|
161
|
+
}
|
|
162
|
+
return config;
|
|
163
|
+
}
|
|
164
|
+
function matchesMethod(requirements, method) {
|
|
165
|
+
const resolved = resolvePreferredMethod(method);
|
|
166
|
+
const scheme = (0, import_x402z_scheme_config.isConfidentialTokenConfig)(resolved) ? "erc7984-mind-v1" : "exact";
|
|
167
|
+
if (requirements.scheme !== scheme) return false;
|
|
168
|
+
if (requirements.network !== resolved.network) return false;
|
|
169
|
+
if (requirements.asset && requirements.asset?.toLowerCase() !== resolved.asset.toLowerCase()) return false;
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
function filterPaymentRequired(paymentRequired, method) {
|
|
173
|
+
const accepts = paymentRequired.accepts.filter(
|
|
174
|
+
(requirement) => matchesMethod(requirement, method)
|
|
175
|
+
);
|
|
155
176
|
return { ...paymentRequired, accepts };
|
|
156
177
|
}
|
|
157
178
|
function createExactPayer(config, debugEnabled) {
|
|
@@ -242,7 +263,7 @@ function createX402zClient(config) {
|
|
|
242
263
|
if (!fetchFn) {
|
|
243
264
|
throw new Error("fetch is not available; provide a fetch implementation");
|
|
244
265
|
}
|
|
245
|
-
const
|
|
266
|
+
const preferredMethods = config.preferredMethods?.length ? [...config.preferredMethods] : import_x402z_scheme_config.SCHEME_CONFIG_NAMES;
|
|
246
267
|
const exactPayer = createExactPayer(
|
|
247
268
|
{
|
|
248
269
|
signer: config.signer,
|
|
@@ -274,27 +295,26 @@ function createX402zClient(config) {
|
|
|
274
295
|
(name) => initial.headers.get(name),
|
|
275
296
|
await initial.json().catch(() => ({}))
|
|
276
297
|
);
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
(scheme) => supportedSchemes.has(scheme)
|
|
298
|
+
const selectedMethod = preferredMethods.find(
|
|
299
|
+
(method) => paymentRequired.accepts.some(
|
|
300
|
+
(requirement) => matchesMethod(requirement, method)
|
|
301
|
+
)
|
|
282
302
|
);
|
|
283
|
-
if (!
|
|
284
|
-
throw new Error("No preferred
|
|
303
|
+
if (!selectedMethod) {
|
|
304
|
+
throw new Error("No preferred payment methods are supported by payment requirements");
|
|
285
305
|
}
|
|
286
|
-
const filteredRequirements = filterPaymentRequired(paymentRequired,
|
|
306
|
+
const filteredRequirements = filterPaymentRequired(paymentRequired, selectedMethod);
|
|
287
307
|
if (filteredRequirements.accepts.length === 0) {
|
|
288
|
-
throw new Error(
|
|
308
|
+
throw new Error("Missing payment requirements for selected method");
|
|
289
309
|
}
|
|
290
310
|
let headersResult;
|
|
291
311
|
let confidentialRequirements;
|
|
292
312
|
const selectedRequirements = filteredRequirements.accepts[0];
|
|
293
313
|
config.onPaymentSelected?.({
|
|
294
|
-
|
|
314
|
+
method: selectedMethod,
|
|
295
315
|
requirements: selectedRequirements
|
|
296
316
|
});
|
|
297
|
-
if (
|
|
317
|
+
if (selectedRequirements.scheme === "exact") {
|
|
298
318
|
headersResult = await exactPayer.buildHeaders(filteredRequirements);
|
|
299
319
|
} else {
|
|
300
320
|
if (!confidentialPayer) {
|
package/dist/index.mjs
CHANGED
|
@@ -126,8 +126,29 @@ import { isAddress } from "viem";
|
|
|
126
126
|
import {
|
|
127
127
|
createEncryptedAmountInput as createEncryptedAmountInput2
|
|
128
128
|
} from "x402z-shared";
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
import { getSchemeConfigByName, SCHEME_CONFIG_NAMES, isConfidentialTokenConfig } from "x402z-scheme-config";
|
|
130
|
+
function resolvePreferredMethod(method) {
|
|
131
|
+
if (typeof method !== "string") {
|
|
132
|
+
return method;
|
|
133
|
+
}
|
|
134
|
+
const config = getSchemeConfigByName(method);
|
|
135
|
+
if (!config) {
|
|
136
|
+
throw new Error(`Unknown preferred method: ${method}`);
|
|
137
|
+
}
|
|
138
|
+
return config;
|
|
139
|
+
}
|
|
140
|
+
function matchesMethod(requirements, method) {
|
|
141
|
+
const resolved = resolvePreferredMethod(method);
|
|
142
|
+
const scheme = isConfidentialTokenConfig(resolved) ? "erc7984-mind-v1" : "exact";
|
|
143
|
+
if (requirements.scheme !== scheme) return false;
|
|
144
|
+
if (requirements.network !== resolved.network) return false;
|
|
145
|
+
if (requirements.asset && requirements.asset?.toLowerCase() !== resolved.asset.toLowerCase()) return false;
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
function filterPaymentRequired(paymentRequired, method) {
|
|
149
|
+
const accepts = paymentRequired.accepts.filter(
|
|
150
|
+
(requirement) => matchesMethod(requirement, method)
|
|
151
|
+
);
|
|
131
152
|
return { ...paymentRequired, accepts };
|
|
132
153
|
}
|
|
133
154
|
function createExactPayer(config, debugEnabled) {
|
|
@@ -218,7 +239,7 @@ function createX402zClient(config) {
|
|
|
218
239
|
if (!fetchFn) {
|
|
219
240
|
throw new Error("fetch is not available; provide a fetch implementation");
|
|
220
241
|
}
|
|
221
|
-
const
|
|
242
|
+
const preferredMethods = config.preferredMethods?.length ? [...config.preferredMethods] : SCHEME_CONFIG_NAMES;
|
|
222
243
|
const exactPayer = createExactPayer(
|
|
223
244
|
{
|
|
224
245
|
signer: config.signer,
|
|
@@ -250,27 +271,26 @@ function createX402zClient(config) {
|
|
|
250
271
|
(name) => initial.headers.get(name),
|
|
251
272
|
await initial.json().catch(() => ({}))
|
|
252
273
|
);
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
(scheme) => supportedSchemes.has(scheme)
|
|
274
|
+
const selectedMethod = preferredMethods.find(
|
|
275
|
+
(method) => paymentRequired.accepts.some(
|
|
276
|
+
(requirement) => matchesMethod(requirement, method)
|
|
277
|
+
)
|
|
258
278
|
);
|
|
259
|
-
if (!
|
|
260
|
-
throw new Error("No preferred
|
|
279
|
+
if (!selectedMethod) {
|
|
280
|
+
throw new Error("No preferred payment methods are supported by payment requirements");
|
|
261
281
|
}
|
|
262
|
-
const filteredRequirements = filterPaymentRequired(paymentRequired,
|
|
282
|
+
const filteredRequirements = filterPaymentRequired(paymentRequired, selectedMethod);
|
|
263
283
|
if (filteredRequirements.accepts.length === 0) {
|
|
264
|
-
throw new Error(
|
|
284
|
+
throw new Error("Missing payment requirements for selected method");
|
|
265
285
|
}
|
|
266
286
|
let headersResult;
|
|
267
287
|
let confidentialRequirements;
|
|
268
288
|
const selectedRequirements = filteredRequirements.accepts[0];
|
|
269
289
|
config.onPaymentSelected?.({
|
|
270
|
-
|
|
290
|
+
method: selectedMethod,
|
|
271
291
|
requirements: selectedRequirements
|
|
272
292
|
});
|
|
273
|
-
if (
|
|
293
|
+
if (selectedRequirements.scheme === "exact") {
|
|
274
294
|
headersResult = await exactPayer.buildHeaders(filteredRequirements);
|
|
275
295
|
} else {
|
|
276
296
|
if (!confidentialPayer) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "x402z-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"module": "./dist/index.mjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -15,10 +15,11 @@
|
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@x402/core": "^2.
|
|
19
|
-
"@x402/evm": "^2.
|
|
18
|
+
"@x402/core": "^2.2.0",
|
|
19
|
+
"@x402/evm": "^2.2.0",
|
|
20
20
|
"viem": "^2.39.3",
|
|
21
|
-
"x402z-shared": "0.1.
|
|
21
|
+
"x402z-shared": "0.1.2",
|
|
22
|
+
"x402z-scheme-config": "0.1.2"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
25
|
"jest": "^29.7.0",
|