x402z-client 0.0.3 → 0.0.6

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 CHANGED
@@ -30,8 +30,26 @@ const response = await client.pay("https://example.com/demo");
30
30
  console.log(response.status);
31
31
  ```
32
32
 
33
+ ## Usage (Browser)
34
+
35
+ ```ts
36
+ import { createX402zWebClient } from "x402z-client/web";
37
+ import { SepoliaConfig } from "x402z-shared/web";
38
+
39
+ const client = await createX402zWebClient({
40
+ signer: {
41
+ address: "0x...",
42
+ signTypedData: async args => window.ethereum.request({ method: "eth_signTypedData_v4", params: [args] }),
43
+ },
44
+ relayerConfig: { ...SepoliaConfig, network: window.ethereum },
45
+ });
46
+
47
+ const response = await client.pay("https://example.com/demo");
48
+ console.log(response.status);
49
+ ```
50
+
33
51
  `createX402zClient` builds the confidential payment input automatically using the
34
- `confidential.facilitatorAddress` provided by the server’s payment requirements.
52
+ `confidential.batcherAddress` provided by the server’s payment requirements.
35
53
 
36
54
  ## API
37
55
 
@@ -39,6 +57,10 @@ console.log(response.status);
39
57
  - `signer` (required): EIP-712 signer for x402 payloads
40
58
  - `relayer` (required): Zama relayer instance used to build encrypted inputs
41
59
  - `fetch` (optional): custom fetch implementation
60
+ - `createX402zWebClient(config)`
61
+ - `signer` (required): EIP-712 signer for x402 payloads
62
+ - `relayerConfig` (required): relayer instance config (browser)
63
+ - `fetch` (optional): custom fetch implementation
42
64
  - `client.pay(url, options?)`: performs the 402 handshake and retries with payment headers
43
65
 
44
66
  ## Notes
@@ -0,0 +1,104 @@
1
+ // src/scheme.ts
2
+ import { getAddress } from "viem";
3
+ import {
4
+ confidentialPaymentTypes,
5
+ createNonce,
6
+ hashEncryptedAmountInput,
7
+ normalizeAmount
8
+ } from "x402z-shared";
9
+ var ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
10
+ var DECIMAL_POINT = ".";
11
+ function normalizeIntegerAmount(value, fallback) {
12
+ const normalized = normalizeAmount(value);
13
+ if (!normalized.includes(DECIMAL_POINT)) {
14
+ return normalized;
15
+ }
16
+ const fallbackNormalized = normalizeAmount(fallback);
17
+ if (fallbackNormalized.includes(DECIMAL_POINT)) {
18
+ throw new Error(`Invalid amount: ${normalized}`);
19
+ }
20
+ return fallbackNormalized;
21
+ }
22
+ var ConfidentialEvmScheme = class {
23
+ constructor(config) {
24
+ this.config = config;
25
+ this.scheme = "erc7984-mind-v1";
26
+ this.hashFn = config.hashEncryptedAmountInput ?? hashEncryptedAmountInput;
27
+ this.clock = config.clock ?? (() => Math.floor(Date.now() / 1e3));
28
+ }
29
+ async createPaymentPayload(x402Version, paymentRequirements) {
30
+ const input = await this.config.buildPayment(paymentRequirements);
31
+ const extra = paymentRequirements.extra;
32
+ const eip712 = extra?.eip712 ?? this.config.eip712;
33
+ if (!eip712?.name || !eip712?.version) {
34
+ throw new Error("Missing EIP-712 domain parameters (name, version) in requirements or config");
35
+ }
36
+ const now = this.clock();
37
+ const validAfter = input.validAfter ?? Math.max(0, now - 60);
38
+ const validBefore = input.validBefore ?? now + paymentRequirements.maxTimeoutSeconds;
39
+ const nonce = input.nonce ?? createNonce();
40
+ const maxClearAmount = normalizeIntegerAmount(
41
+ input.maxClearAmount ?? extra?.confidential?.maxClearAmount ?? paymentRequirements.amount,
42
+ paymentRequirements.amount
43
+ );
44
+ const resourceHash = input.resourceHash ?? extra?.confidential?.resourceHash ?? ZERO_BYTES32;
45
+ const authorization = {
46
+ holder: this.config.signer.address,
47
+ payee: getAddress(paymentRequirements.payTo),
48
+ maxClearAmount,
49
+ resourceHash,
50
+ validAfter: normalizeAmount(validAfter),
51
+ validBefore: normalizeAmount(validBefore),
52
+ nonce,
53
+ encryptedAmountHash: this.hashFn(input.encryptedAmountInput)
54
+ };
55
+ const chainId = parseInt(paymentRequirements.network.split(":")[1]);
56
+ const signature = await this.config.signer.signTypedData({
57
+ domain: {
58
+ name: eip712.name,
59
+ version: eip712.version,
60
+ chainId,
61
+ verifyingContract: getAddress(paymentRequirements.asset)
62
+ },
63
+ types: confidentialPaymentTypes,
64
+ primaryType: "ConfidentialPayment",
65
+ message: {
66
+ holder: getAddress(authorization.holder),
67
+ payee: getAddress(authorization.payee),
68
+ maxClearAmount: BigInt(authorization.maxClearAmount),
69
+ resourceHash: authorization.resourceHash,
70
+ validAfter: BigInt(authorization.validAfter),
71
+ validBefore: BigInt(authorization.validBefore),
72
+ nonce: authorization.nonce,
73
+ encryptedAmountHash: authorization.encryptedAmountHash
74
+ }
75
+ });
76
+ const payload = {
77
+ authorization,
78
+ signature,
79
+ encryptedAmountInput: input.encryptedAmountInput,
80
+ inputProof: input.inputProof
81
+ };
82
+ return {
83
+ x402Version,
84
+ payload
85
+ };
86
+ }
87
+ };
88
+
89
+ // src/register.ts
90
+ function registerConfidentialEvmScheme(client, config) {
91
+ if (config.networks && config.networks.length > 0) {
92
+ for (const network of config.networks) {
93
+ client.register(network, new ConfidentialEvmScheme(config));
94
+ }
95
+ return client;
96
+ }
97
+ client.register("eip155:*", new ConfidentialEvmScheme(config));
98
+ return client;
99
+ }
100
+
101
+ export {
102
+ ConfidentialEvmScheme,
103
+ registerConfidentialEvmScheme
104
+ };
package/dist/index.d.mts CHANGED
@@ -1,33 +1,10 @@
1
- import { SchemeNetworkClient, PaymentRequirements, PaymentPayload, Network } from '@x402/core/types';
1
+ import { C as ConfidentialClientRegisterConfig } from './register-DmPDc2uL.mjs';
2
+ export { b as ConfidentialClientConfig, a as ConfidentialEvmScheme, r as registerConfidentialEvmScheme } from './register-DmPDc2uL.mjs';
3
+ import { PaymentRequirements } from '@x402/core/types';
2
4
  export * from '@x402/core/types';
3
- import { ClientEvmSigner } from '@x402/evm';
4
- import { ConfidentialPaymentInput, RelayerInstance } from 'x402z-shared';
5
- import { x402Client } from '@x402/core/client';
5
+ import { RelayerInstance, ConfidentialPaymentInput } from 'x402z-shared';
6
6
  export { AfterPaymentCreationHook, BeforePaymentCreationHook, OnPaymentCreationFailureHook, PaymentCreatedContext, PaymentCreationContext, PaymentCreationFailureContext, PaymentPolicy, SchemeRegistration, SelectPaymentRequirements, x402Client, x402ClientConfig, x402HTTPClient } from '@x402/core/client';
7
-
8
- type ConfidentialClientConfig = {
9
- signer: ClientEvmSigner;
10
- buildPayment: (requirements: PaymentRequirements) => ConfidentialPaymentInput | Promise<ConfidentialPaymentInput>;
11
- eip712?: {
12
- name: string;
13
- version: string;
14
- };
15
- hashEncryptedAmountInput?: (encryptedAmountInput: `0x${string}`) => `0x${string}`;
16
- clock?: () => number;
17
- };
18
- declare class ConfidentialEvmScheme implements SchemeNetworkClient {
19
- private readonly config;
20
- readonly scheme = "erc7984-mind-v1";
21
- private readonly hashFn;
22
- private readonly clock;
23
- constructor(config: ConfidentialClientConfig);
24
- createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, "x402Version" | "payload">>;
25
- }
26
-
27
- type ConfidentialClientRegisterConfig = ConfidentialClientConfig & {
28
- networks?: Network[];
29
- };
30
- declare function registerConfidentialEvmScheme(client: x402Client, config: ConfidentialClientRegisterConfig): x402Client;
7
+ import '@x402/evm';
31
8
 
32
9
  declare function buildPaymentInputFromRelayer(relayer: RelayerInstance, requirements: PaymentRequirements, amount: number): Promise<ConfidentialPaymentInput>;
33
10
 
@@ -43,4 +20,4 @@ declare function createX402zClient(config: X402zClientConfig): {
43
20
  pay(url: string, options?: PayOptions): Promise<Response>;
44
21
  };
45
22
 
46
- export { type ConfidentialClientConfig, type ConfidentialClientRegisterConfig, ConfidentialEvmScheme, type PayOptions, type X402zClientConfig, buildPaymentInputFromRelayer, createX402zClient, registerConfidentialEvmScheme };
23
+ export { ConfidentialClientRegisterConfig, type PayOptions, type X402zClientConfig, buildPaymentInputFromRelayer, createX402zClient };
package/dist/index.d.ts CHANGED
@@ -1,33 +1,10 @@
1
- import { SchemeNetworkClient, PaymentRequirements, PaymentPayload, Network } from '@x402/core/types';
1
+ import { C as ConfidentialClientRegisterConfig } from './register-DmPDc2uL.js';
2
+ export { b as ConfidentialClientConfig, a as ConfidentialEvmScheme, r as registerConfidentialEvmScheme } from './register-DmPDc2uL.js';
3
+ import { PaymentRequirements } from '@x402/core/types';
2
4
  export * from '@x402/core/types';
3
- import { ClientEvmSigner } from '@x402/evm';
4
- import { ConfidentialPaymentInput, RelayerInstance } from 'x402z-shared';
5
- import { x402Client } from '@x402/core/client';
5
+ import { RelayerInstance, ConfidentialPaymentInput } from 'x402z-shared';
6
6
  export { AfterPaymentCreationHook, BeforePaymentCreationHook, OnPaymentCreationFailureHook, PaymentCreatedContext, PaymentCreationContext, PaymentCreationFailureContext, PaymentPolicy, SchemeRegistration, SelectPaymentRequirements, x402Client, x402ClientConfig, x402HTTPClient } from '@x402/core/client';
7
-
8
- type ConfidentialClientConfig = {
9
- signer: ClientEvmSigner;
10
- buildPayment: (requirements: PaymentRequirements) => ConfidentialPaymentInput | Promise<ConfidentialPaymentInput>;
11
- eip712?: {
12
- name: string;
13
- version: string;
14
- };
15
- hashEncryptedAmountInput?: (encryptedAmountInput: `0x${string}`) => `0x${string}`;
16
- clock?: () => number;
17
- };
18
- declare class ConfidentialEvmScheme implements SchemeNetworkClient {
19
- private readonly config;
20
- readonly scheme = "erc7984-mind-v1";
21
- private readonly hashFn;
22
- private readonly clock;
23
- constructor(config: ConfidentialClientConfig);
24
- createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, "x402Version" | "payload">>;
25
- }
26
-
27
- type ConfidentialClientRegisterConfig = ConfidentialClientConfig & {
28
- networks?: Network[];
29
- };
30
- declare function registerConfidentialEvmScheme(client: x402Client, config: ConfidentialClientRegisterConfig): x402Client;
7
+ import '@x402/evm';
31
8
 
32
9
  declare function buildPaymentInputFromRelayer(relayer: RelayerInstance, requirements: PaymentRequirements, amount: number): Promise<ConfidentialPaymentInput>;
33
10
 
@@ -43,4 +20,4 @@ declare function createX402zClient(config: X402zClientConfig): {
43
20
  pay(url: string, options?: PayOptions): Promise<Response>;
44
21
  };
45
22
 
46
- export { type ConfidentialClientConfig, type ConfidentialClientRegisterConfig, ConfidentialEvmScheme, type PayOptions, type X402zClientConfig, buildPaymentInputFromRelayer, createX402zClient, registerConfidentialEvmScheme };
23
+ export { ConfidentialClientRegisterConfig, type PayOptions, type X402zClientConfig, buildPaymentInputFromRelayer, createX402zClient };
package/dist/index.js CHANGED
@@ -33,6 +33,18 @@ module.exports = __toCommonJS(index_exports);
33
33
  var import_viem = require("viem");
34
34
  var import_x402z_shared = require("x402z-shared");
35
35
  var ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
36
+ var DECIMAL_POINT = ".";
37
+ function normalizeIntegerAmount(value, fallback) {
38
+ const normalized = (0, import_x402z_shared.normalizeAmount)(value);
39
+ if (!normalized.includes(DECIMAL_POINT)) {
40
+ return normalized;
41
+ }
42
+ const fallbackNormalized = (0, import_x402z_shared.normalizeAmount)(fallback);
43
+ if (fallbackNormalized.includes(DECIMAL_POINT)) {
44
+ throw new Error(`Invalid amount: ${normalized}`);
45
+ }
46
+ return fallbackNormalized;
47
+ }
36
48
  var ConfidentialEvmScheme = class {
37
49
  constructor(config) {
38
50
  this.config = config;
@@ -51,7 +63,10 @@ var ConfidentialEvmScheme = class {
51
63
  const validAfter = input.validAfter ?? Math.max(0, now - 60);
52
64
  const validBefore = input.validBefore ?? now + paymentRequirements.maxTimeoutSeconds;
53
65
  const nonce = input.nonce ?? (0, import_x402z_shared.createNonce)();
54
- const maxClearAmount = input.maxClearAmount ?? extra?.confidential?.maxClearAmount ?? (0, import_x402z_shared.normalizeAmount)(paymentRequirements.amount);
66
+ const maxClearAmount = normalizeIntegerAmount(
67
+ input.maxClearAmount ?? extra?.confidential?.maxClearAmount ?? paymentRequirements.amount,
68
+ paymentRequirements.amount
69
+ );
55
70
  const resourceHash = input.resourceHash ?? extra?.confidential?.resourceHash ?? ZERO_BYTES32;
56
71
  const authorization = {
57
72
  holder: this.config.signer.address,
@@ -113,14 +128,14 @@ function registerConfidentialEvmScheme(client, config) {
113
128
  var import_x402z_shared2 = require("x402z-shared");
114
129
  async function buildPaymentInputFromRelayer(relayer, requirements, amount) {
115
130
  const extra = requirements.extra;
116
- const facilitatorAddress = extra?.confidential?.facilitatorAddress;
117
- if (!facilitatorAddress) {
118
- throw new Error("Missing confidential.facilitatorAddress in payment requirements");
131
+ const batcherAddress = extra?.confidential?.batcherAddress;
132
+ if (!batcherAddress) {
133
+ throw new Error("Missing confidential.batcherAddress in payment requirements");
119
134
  }
120
135
  const encrypted = await (0, import_x402z_shared2.createEncryptedAmountInput)(
121
136
  relayer,
122
137
  requirements.asset,
123
- facilitatorAddress,
138
+ batcherAddress,
124
139
  amount
125
140
  );
126
141
  return {
@@ -146,14 +161,14 @@ function createX402zClient(config) {
146
161
  throw new Error(`Invalid TOKEN_ADDRESS from requirements: ${requirements.asset}`);
147
162
  }
148
163
  const extra = requirements.extra;
149
- const facilitatorAddress = extra?.confidential?.facilitatorAddress;
150
- if (!facilitatorAddress) {
151
- throw new Error("Missing confidential.facilitatorAddress in payment requirements");
164
+ const batcherAddress = extra?.confidential?.batcherAddress;
165
+ if (!batcherAddress) {
166
+ throw new Error("Missing confidential.batcherAddress in payment requirements");
152
167
  }
153
168
  const encrypted = await (0, import_x402z_shared3.createEncryptedAmountInput)(
154
169
  config.relayer,
155
170
  requirements.asset,
156
- facilitatorAddress,
171
+ batcherAddress,
157
172
  Number(requirements.amount)
158
173
  );
159
174
  return {
package/dist/index.mjs CHANGED
@@ -1,100 +1,20 @@
1
- // src/scheme.ts
2
- import { getAddress } from "viem";
3
1
  import {
4
- confidentialPaymentTypes,
5
- createNonce,
6
- hashEncryptedAmountInput,
7
- normalizeAmount
8
- } from "x402z-shared";
9
- var ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
10
- var ConfidentialEvmScheme = class {
11
- constructor(config) {
12
- this.config = config;
13
- this.scheme = "erc7984-mind-v1";
14
- this.hashFn = config.hashEncryptedAmountInput ?? hashEncryptedAmountInput;
15
- this.clock = config.clock ?? (() => Math.floor(Date.now() / 1e3));
16
- }
17
- async createPaymentPayload(x402Version, paymentRequirements) {
18
- const input = await this.config.buildPayment(paymentRequirements);
19
- const extra = paymentRequirements.extra;
20
- const eip712 = extra?.eip712 ?? this.config.eip712;
21
- if (!eip712?.name || !eip712?.version) {
22
- throw new Error("Missing EIP-712 domain parameters (name, version) in requirements or config");
23
- }
24
- const now = this.clock();
25
- const validAfter = input.validAfter ?? Math.max(0, now - 60);
26
- const validBefore = input.validBefore ?? now + paymentRequirements.maxTimeoutSeconds;
27
- const nonce = input.nonce ?? createNonce();
28
- const maxClearAmount = input.maxClearAmount ?? extra?.confidential?.maxClearAmount ?? normalizeAmount(paymentRequirements.amount);
29
- const resourceHash = input.resourceHash ?? extra?.confidential?.resourceHash ?? ZERO_BYTES32;
30
- const authorization = {
31
- holder: this.config.signer.address,
32
- payee: getAddress(paymentRequirements.payTo),
33
- maxClearAmount,
34
- resourceHash,
35
- validAfter: normalizeAmount(validAfter),
36
- validBefore: normalizeAmount(validBefore),
37
- nonce,
38
- encryptedAmountHash: this.hashFn(input.encryptedAmountInput)
39
- };
40
- const chainId = parseInt(paymentRequirements.network.split(":")[1]);
41
- const signature = await this.config.signer.signTypedData({
42
- domain: {
43
- name: eip712.name,
44
- version: eip712.version,
45
- chainId,
46
- verifyingContract: getAddress(paymentRequirements.asset)
47
- },
48
- types: confidentialPaymentTypes,
49
- primaryType: "ConfidentialPayment",
50
- message: {
51
- holder: getAddress(authorization.holder),
52
- payee: getAddress(authorization.payee),
53
- maxClearAmount: BigInt(authorization.maxClearAmount),
54
- resourceHash: authorization.resourceHash,
55
- validAfter: BigInt(authorization.validAfter),
56
- validBefore: BigInt(authorization.validBefore),
57
- nonce: authorization.nonce,
58
- encryptedAmountHash: authorization.encryptedAmountHash
59
- }
60
- });
61
- const payload = {
62
- authorization,
63
- signature,
64
- encryptedAmountInput: input.encryptedAmountInput,
65
- inputProof: input.inputProof
66
- };
67
- return {
68
- x402Version,
69
- payload
70
- };
71
- }
72
- };
73
-
74
- // src/register.ts
75
- function registerConfidentialEvmScheme(client, config) {
76
- if (config.networks && config.networks.length > 0) {
77
- for (const network of config.networks) {
78
- client.register(network, new ConfidentialEvmScheme(config));
79
- }
80
- return client;
81
- }
82
- client.register("eip155:*", new ConfidentialEvmScheme(config));
83
- return client;
84
- }
2
+ ConfidentialEvmScheme,
3
+ registerConfidentialEvmScheme
4
+ } from "./chunk-WKGSJ5YZ.mjs";
85
5
 
86
6
  // src/relayer.ts
87
7
  import { createEncryptedAmountInput } from "x402z-shared";
88
8
  async function buildPaymentInputFromRelayer(relayer, requirements, amount) {
89
9
  const extra = requirements.extra;
90
- const facilitatorAddress = extra?.confidential?.facilitatorAddress;
91
- if (!facilitatorAddress) {
92
- throw new Error("Missing confidential.facilitatorAddress in payment requirements");
10
+ const batcherAddress = extra?.confidential?.batcherAddress;
11
+ if (!batcherAddress) {
12
+ throw new Error("Missing confidential.batcherAddress in payment requirements");
93
13
  }
94
14
  const encrypted = await createEncryptedAmountInput(
95
15
  relayer,
96
16
  requirements.asset,
97
- facilitatorAddress,
17
+ batcherAddress,
98
18
  amount
99
19
  );
100
20
  return {
@@ -122,14 +42,14 @@ function createX402zClient(config) {
122
42
  throw new Error(`Invalid TOKEN_ADDRESS from requirements: ${requirements.asset}`);
123
43
  }
124
44
  const extra = requirements.extra;
125
- const facilitatorAddress = extra?.confidential?.facilitatorAddress;
126
- if (!facilitatorAddress) {
127
- throw new Error("Missing confidential.facilitatorAddress in payment requirements");
45
+ const batcherAddress = extra?.confidential?.batcherAddress;
46
+ if (!batcherAddress) {
47
+ throw new Error("Missing confidential.batcherAddress in payment requirements");
128
48
  }
129
49
  const encrypted = await createEncryptedAmountInput2(
130
50
  config.relayer,
131
51
  requirements.asset,
132
- facilitatorAddress,
52
+ batcherAddress,
133
53
  Number(requirements.amount)
134
54
  );
135
55
  return {
@@ -0,0 +1,30 @@
1
+ import { x402Client } from '@x402/core/client';
2
+ import { SchemeNetworkClient, PaymentRequirements, PaymentPayload, Network } from '@x402/core/types';
3
+ import { ClientEvmSigner } from '@x402/evm';
4
+ import { ConfidentialPaymentInput } from 'x402z-shared';
5
+
6
+ type ConfidentialClientConfig = {
7
+ signer: ClientEvmSigner;
8
+ buildPayment: (requirements: PaymentRequirements) => ConfidentialPaymentInput | Promise<ConfidentialPaymentInput>;
9
+ eip712?: {
10
+ name: string;
11
+ version: string;
12
+ };
13
+ hashEncryptedAmountInput?: (encryptedAmountInput: `0x${string}`) => `0x${string}`;
14
+ clock?: () => number;
15
+ };
16
+ declare class ConfidentialEvmScheme implements SchemeNetworkClient {
17
+ private readonly config;
18
+ readonly scheme = "erc7984-mind-v1";
19
+ private readonly hashFn;
20
+ private readonly clock;
21
+ constructor(config: ConfidentialClientConfig);
22
+ createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, "x402Version" | "payload">>;
23
+ }
24
+
25
+ type ConfidentialClientRegisterConfig = ConfidentialClientConfig & {
26
+ networks?: Network[];
27
+ };
28
+ declare function registerConfidentialEvmScheme(client: x402Client, config: ConfidentialClientRegisterConfig): x402Client;
29
+
30
+ export { type ConfidentialClientRegisterConfig as C, ConfidentialEvmScheme as a, type ConfidentialClientConfig as b, registerConfidentialEvmScheme as r };
@@ -0,0 +1,30 @@
1
+ import { x402Client } from '@x402/core/client';
2
+ import { SchemeNetworkClient, PaymentRequirements, PaymentPayload, Network } from '@x402/core/types';
3
+ import { ClientEvmSigner } from '@x402/evm';
4
+ import { ConfidentialPaymentInput } from 'x402z-shared';
5
+
6
+ type ConfidentialClientConfig = {
7
+ signer: ClientEvmSigner;
8
+ buildPayment: (requirements: PaymentRequirements) => ConfidentialPaymentInput | Promise<ConfidentialPaymentInput>;
9
+ eip712?: {
10
+ name: string;
11
+ version: string;
12
+ };
13
+ hashEncryptedAmountInput?: (encryptedAmountInput: `0x${string}`) => `0x${string}`;
14
+ clock?: () => number;
15
+ };
16
+ declare class ConfidentialEvmScheme implements SchemeNetworkClient {
17
+ private readonly config;
18
+ readonly scheme = "erc7984-mind-v1";
19
+ private readonly hashFn;
20
+ private readonly clock;
21
+ constructor(config: ConfidentialClientConfig);
22
+ createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, "x402Version" | "payload">>;
23
+ }
24
+
25
+ type ConfidentialClientRegisterConfig = ConfidentialClientConfig & {
26
+ networks?: Network[];
27
+ };
28
+ declare function registerConfidentialEvmScheme(client: x402Client, config: ConfidentialClientRegisterConfig): x402Client;
29
+
30
+ export { type ConfidentialClientRegisterConfig as C, ConfidentialEvmScheme as a, type ConfidentialClientConfig as b, registerConfidentialEvmScheme as r };
package/dist/web.d.mts ADDED
@@ -0,0 +1,21 @@
1
+ import * as _zama_fhe_relayer_sdk_web from '@zama-fhe/relayer-sdk/web';
2
+ import { C as ConfidentialClientRegisterConfig } from './register-DmPDc2uL.mjs';
3
+ import '@x402/core/client';
4
+ import '@x402/core/types';
5
+ import '@x402/evm';
6
+ import 'x402z-shared';
7
+
8
+ type X402zWebClientConfig = Omit<ConfidentialClientRegisterConfig, "buildPayment"> & {
9
+ relayerConfig: unknown;
10
+ fetch?: typeof fetch;
11
+ debug?: boolean;
12
+ };
13
+ type PayOptions = {
14
+ headers?: Record<string, string>;
15
+ };
16
+ declare function createX402zWebClient(config: X402zWebClientConfig): Promise<{
17
+ relayer: _zama_fhe_relayer_sdk_web.FhevmInstance;
18
+ pay(url: string, options?: PayOptions): Promise<Response>;
19
+ }>;
20
+
21
+ export { type PayOptions, type X402zWebClientConfig, createX402zWebClient };
package/dist/web.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import * as _zama_fhe_relayer_sdk_web from '@zama-fhe/relayer-sdk/web';
2
+ import { C as ConfidentialClientRegisterConfig } from './register-DmPDc2uL.js';
3
+ import '@x402/core/client';
4
+ import '@x402/core/types';
5
+ import '@x402/evm';
6
+ import 'x402z-shared';
7
+
8
+ type X402zWebClientConfig = Omit<ConfidentialClientRegisterConfig, "buildPayment"> & {
9
+ relayerConfig: unknown;
10
+ fetch?: typeof fetch;
11
+ debug?: boolean;
12
+ };
13
+ type PayOptions = {
14
+ headers?: Record<string, string>;
15
+ };
16
+ declare function createX402zWebClient(config: X402zWebClientConfig): Promise<{
17
+ relayer: _zama_fhe_relayer_sdk_web.FhevmInstance;
18
+ pay(url: string, options?: PayOptions): Promise<Response>;
19
+ }>;
20
+
21
+ export { type PayOptions, type X402zWebClientConfig, createX402zWebClient };
package/dist/web.js ADDED
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/web.ts
21
+ var web_exports = {};
22
+ __export(web_exports, {
23
+ createX402zWebClient: () => createX402zWebClient
24
+ });
25
+ module.exports = __toCommonJS(web_exports);
26
+ var import_client = require("@x402/core/client");
27
+ var import_http = require("@x402/core/http");
28
+ var import_viem2 = require("viem");
29
+ var import_web = require("x402z-shared/web");
30
+
31
+ // src/scheme.ts
32
+ var import_viem = require("viem");
33
+ var import_x402z_shared = require("x402z-shared");
34
+ var ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
35
+ var DECIMAL_POINT = ".";
36
+ function normalizeIntegerAmount(value, fallback) {
37
+ const normalized = (0, import_x402z_shared.normalizeAmount)(value);
38
+ if (!normalized.includes(DECIMAL_POINT)) {
39
+ return normalized;
40
+ }
41
+ const fallbackNormalized = (0, import_x402z_shared.normalizeAmount)(fallback);
42
+ if (fallbackNormalized.includes(DECIMAL_POINT)) {
43
+ throw new Error(`Invalid amount: ${normalized}`);
44
+ }
45
+ return fallbackNormalized;
46
+ }
47
+ var ConfidentialEvmScheme = class {
48
+ constructor(config) {
49
+ this.config = config;
50
+ this.scheme = "erc7984-mind-v1";
51
+ this.hashFn = config.hashEncryptedAmountInput ?? import_x402z_shared.hashEncryptedAmountInput;
52
+ this.clock = config.clock ?? (() => Math.floor(Date.now() / 1e3));
53
+ }
54
+ async createPaymentPayload(x402Version, paymentRequirements) {
55
+ const input = await this.config.buildPayment(paymentRequirements);
56
+ const extra = paymentRequirements.extra;
57
+ const eip712 = extra?.eip712 ?? this.config.eip712;
58
+ if (!eip712?.name || !eip712?.version) {
59
+ throw new Error("Missing EIP-712 domain parameters (name, version) in requirements or config");
60
+ }
61
+ const now = this.clock();
62
+ const validAfter = input.validAfter ?? Math.max(0, now - 60);
63
+ const validBefore = input.validBefore ?? now + paymentRequirements.maxTimeoutSeconds;
64
+ const nonce = input.nonce ?? (0, import_x402z_shared.createNonce)();
65
+ const maxClearAmount = normalizeIntegerAmount(
66
+ input.maxClearAmount ?? extra?.confidential?.maxClearAmount ?? paymentRequirements.amount,
67
+ paymentRequirements.amount
68
+ );
69
+ const resourceHash = input.resourceHash ?? extra?.confidential?.resourceHash ?? ZERO_BYTES32;
70
+ const authorization = {
71
+ holder: this.config.signer.address,
72
+ payee: (0, import_viem.getAddress)(paymentRequirements.payTo),
73
+ maxClearAmount,
74
+ resourceHash,
75
+ validAfter: (0, import_x402z_shared.normalizeAmount)(validAfter),
76
+ validBefore: (0, import_x402z_shared.normalizeAmount)(validBefore),
77
+ nonce,
78
+ encryptedAmountHash: this.hashFn(input.encryptedAmountInput)
79
+ };
80
+ const chainId = parseInt(paymentRequirements.network.split(":")[1]);
81
+ const signature = await this.config.signer.signTypedData({
82
+ domain: {
83
+ name: eip712.name,
84
+ version: eip712.version,
85
+ chainId,
86
+ verifyingContract: (0, import_viem.getAddress)(paymentRequirements.asset)
87
+ },
88
+ types: import_x402z_shared.confidentialPaymentTypes,
89
+ primaryType: "ConfidentialPayment",
90
+ message: {
91
+ holder: (0, import_viem.getAddress)(authorization.holder),
92
+ payee: (0, import_viem.getAddress)(authorization.payee),
93
+ maxClearAmount: BigInt(authorization.maxClearAmount),
94
+ resourceHash: authorization.resourceHash,
95
+ validAfter: BigInt(authorization.validAfter),
96
+ validBefore: BigInt(authorization.validBefore),
97
+ nonce: authorization.nonce,
98
+ encryptedAmountHash: authorization.encryptedAmountHash
99
+ }
100
+ });
101
+ const payload = {
102
+ authorization,
103
+ signature,
104
+ encryptedAmountInput: input.encryptedAmountInput,
105
+ inputProof: input.inputProof
106
+ };
107
+ return {
108
+ x402Version,
109
+ payload
110
+ };
111
+ }
112
+ };
113
+
114
+ // src/register.ts
115
+ function registerConfidentialEvmScheme(client, config) {
116
+ if (config.networks && config.networks.length > 0) {
117
+ for (const network of config.networks) {
118
+ client.register(network, new ConfidentialEvmScheme(config));
119
+ }
120
+ return client;
121
+ }
122
+ client.register("eip155:*", new ConfidentialEvmScheme(config));
123
+ return client;
124
+ }
125
+
126
+ // src/web.ts
127
+ async function createX402zWebClient(config) {
128
+ const { fetch: fetchOverride, ...registerConfig } = config;
129
+ const fetchFn = fetchOverride ?? globalThis.fetch;
130
+ const debugEnabled = config.debug ?? process.env.X402Z_DEBUG === "1";
131
+ if (!fetchFn) {
132
+ throw new Error("fetch is not available; provide a fetch implementation");
133
+ }
134
+ await (0, import_web.initSDK)();
135
+ const relayer = await (0, import_web.createRelayerInstance)(config.relayerConfig);
136
+ const buildPayment = async (requirements) => {
137
+ if (!(0, import_viem2.isAddress)(requirements.asset)) {
138
+ throw new Error(`Invalid TOKEN_ADDRESS from requirements: ${requirements.asset}`);
139
+ }
140
+ const extra = requirements.extra;
141
+ const batcherAddress = extra?.confidential?.batcherAddress;
142
+ if (!batcherAddress) {
143
+ throw new Error("Missing confidential.batcherAddress in payment requirements");
144
+ }
145
+ const encrypted = await (0, import_web.createEncryptedAmountInput)(
146
+ relayer,
147
+ requirements.asset,
148
+ batcherAddress,
149
+ Number(requirements.amount)
150
+ );
151
+ return {
152
+ encryptedAmountInput: encrypted.handle,
153
+ inputProof: encrypted.inputProof
154
+ };
155
+ };
156
+ const client = new import_client.x402Client();
157
+ registerConfidentialEvmScheme(client, {
158
+ ...registerConfig,
159
+ buildPayment
160
+ });
161
+ const httpClient = new import_http.x402HTTPClient(client);
162
+ return {
163
+ relayer,
164
+ async pay(url, options) {
165
+ const initial = await fetchFn(url, { headers: options?.headers });
166
+ if (initial.status !== 402) {
167
+ return initial;
168
+ }
169
+ const paymentRequired = httpClient.getPaymentRequiredResponse(
170
+ (name) => initial.headers.get(name),
171
+ await initial.json().catch(() => ({}))
172
+ );
173
+ const payload = await httpClient.createPaymentPayload(paymentRequired);
174
+ const payHeaders = httpClient.encodePaymentSignatureHeader(payload);
175
+ if (debugEnabled) {
176
+ console.debug("[x402z-client] payment payload", payload);
177
+ console.debug("[x402z-client] payment headers", payHeaders);
178
+ }
179
+ const mergedHeaders = { ...options?.headers ?? {}, ...payHeaders };
180
+ const paidResponse = await fetchFn(url, { headers: mergedHeaders });
181
+ if (debugEnabled) {
182
+ try {
183
+ const body = await paidResponse.clone().text();
184
+ console.debug("[x402z-client] response", {
185
+ status: paidResponse.status,
186
+ headers: Object.fromEntries(paidResponse.headers.entries()),
187
+ body
188
+ });
189
+ } catch (error) {
190
+ console.debug("[x402z-client] response", {
191
+ status: paidResponse.status,
192
+ headers: Object.fromEntries(paidResponse.headers.entries()),
193
+ body: "<unavailable>"
194
+ });
195
+ }
196
+ }
197
+ return paidResponse;
198
+ }
199
+ };
200
+ }
201
+ // Annotate the CommonJS export names for ESM import in node:
202
+ 0 && (module.exports = {
203
+ createX402zWebClient
204
+ });
package/dist/web.mjs ADDED
@@ -0,0 +1,90 @@
1
+ import {
2
+ registerConfidentialEvmScheme
3
+ } from "./chunk-WKGSJ5YZ.mjs";
4
+
5
+ // src/web.ts
6
+ import { x402Client } from "@x402/core/client";
7
+ import { x402HTTPClient } from "@x402/core/http";
8
+ import { isAddress } from "viem";
9
+ import {
10
+ createEncryptedAmountInput,
11
+ createRelayerInstance,
12
+ initSDK
13
+ } from "x402z-shared/web";
14
+ async function createX402zWebClient(config) {
15
+ const { fetch: fetchOverride, ...registerConfig } = config;
16
+ const fetchFn = fetchOverride ?? globalThis.fetch;
17
+ const debugEnabled = config.debug ?? process.env.X402Z_DEBUG === "1";
18
+ if (!fetchFn) {
19
+ throw new Error("fetch is not available; provide a fetch implementation");
20
+ }
21
+ await initSDK();
22
+ const relayer = await createRelayerInstance(config.relayerConfig);
23
+ const buildPayment = async (requirements) => {
24
+ if (!isAddress(requirements.asset)) {
25
+ throw new Error(`Invalid TOKEN_ADDRESS from requirements: ${requirements.asset}`);
26
+ }
27
+ const extra = requirements.extra;
28
+ const batcherAddress = extra?.confidential?.batcherAddress;
29
+ if (!batcherAddress) {
30
+ throw new Error("Missing confidential.batcherAddress in payment requirements");
31
+ }
32
+ const encrypted = await createEncryptedAmountInput(
33
+ relayer,
34
+ requirements.asset,
35
+ batcherAddress,
36
+ Number(requirements.amount)
37
+ );
38
+ return {
39
+ encryptedAmountInput: encrypted.handle,
40
+ inputProof: encrypted.inputProof
41
+ };
42
+ };
43
+ const client = new x402Client();
44
+ registerConfidentialEvmScheme(client, {
45
+ ...registerConfig,
46
+ buildPayment
47
+ });
48
+ const httpClient = new x402HTTPClient(client);
49
+ return {
50
+ relayer,
51
+ async pay(url, options) {
52
+ const initial = await fetchFn(url, { headers: options?.headers });
53
+ if (initial.status !== 402) {
54
+ return initial;
55
+ }
56
+ const paymentRequired = httpClient.getPaymentRequiredResponse(
57
+ (name) => initial.headers.get(name),
58
+ await initial.json().catch(() => ({}))
59
+ );
60
+ const payload = await httpClient.createPaymentPayload(paymentRequired);
61
+ const payHeaders = httpClient.encodePaymentSignatureHeader(payload);
62
+ if (debugEnabled) {
63
+ console.debug("[x402z-client] payment payload", payload);
64
+ console.debug("[x402z-client] payment headers", payHeaders);
65
+ }
66
+ const mergedHeaders = { ...options?.headers ?? {}, ...payHeaders };
67
+ const paidResponse = await fetchFn(url, { headers: mergedHeaders });
68
+ if (debugEnabled) {
69
+ try {
70
+ const body = await paidResponse.clone().text();
71
+ console.debug("[x402z-client] response", {
72
+ status: paidResponse.status,
73
+ headers: Object.fromEntries(paidResponse.headers.entries()),
74
+ body
75
+ });
76
+ } catch (error) {
77
+ console.debug("[x402z-client] response", {
78
+ status: paidResponse.status,
79
+ headers: Object.fromEntries(paidResponse.headers.entries()),
80
+ body: "<unavailable>"
81
+ });
82
+ }
83
+ }
84
+ return paidResponse;
85
+ }
86
+ };
87
+ }
88
+ export {
89
+ createX402zWebClient
90
+ };
package/package.json CHANGED
@@ -1,17 +1,29 @@
1
1
  {
2
2
  "name": "x402z-client",
3
- "version": "0.0.3",
3
+ "version": "0.0.6",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js"
15
+ },
16
+ "./web": {
17
+ "types": "./dist/web.d.ts",
18
+ "import": "./dist/web.mjs",
19
+ "require": "./dist/web.js"
20
+ }
21
+ },
10
22
  "dependencies": {
11
23
  "@x402/core": "^2.0.0",
12
24
  "@x402/evm": "^2.0.0",
13
25
  "viem": "^2.39.3",
14
- "x402z-shared": "0.0.3"
26
+ "x402z-shared": "0.0.6"
15
27
  },
16
28
  "devDependencies": {
17
29
  "jest": "^29.7.0",
@@ -19,7 +31,7 @@
19
31
  "@types/jest": "^29.5.12"
20
32
  },
21
33
  "scripts": {
22
- "build": "tsup src/index.ts --format cjs,esm --dts",
34
+ "build": "tsup src/index.ts src/web.ts --format cjs,esm --dts",
23
35
  "test": "jest"
24
36
  }
25
37
  }