x402z-client 0.0.4 → 0.0.7
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 +23 -0
- package/dist/chunk-WKGSJ5YZ.mjs +104 -0
- package/dist/register-DmPDc2uL.d.mts +30 -0
- package/dist/register-DmPDc2uL.d.ts +30 -0
- package/dist/web.d.mts +34 -0
- package/dist/web.d.ts +34 -0
- package/dist/web.js +204 -0
- package/dist/web.mjs +188 -0
- package/package.json +15 -3
package/README.md
CHANGED
|
@@ -30,6 +30,24 @@ 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
52
|
`confidential.batcherAddress` provided by the server’s payment requirements.
|
|
35
53
|
|
|
@@ -39,9 +57,14 @@ 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
|
|
45
67
|
|
|
46
68
|
- Scheme name: `erc7984-mind-v1`
|
|
47
69
|
- The client does not expose balance helpers; use `x402z-shared` for that.
|
|
70
|
+
- The browser entry (`x402z-client/web`) uses a web-only scheme implementation wired to `x402z-shared/web`.
|
|
@@ -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
|
+
};
|
|
@@ -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,34 @@
|
|
|
1
|
+
import * as _zama_fhe_relayer_sdk_web from '@zama-fhe/relayer-sdk/web';
|
|
2
|
+
import { PaymentRequirements, Network } from '@x402/core/types';
|
|
3
|
+
import { ClientEvmSigner } from '@x402/evm';
|
|
4
|
+
import { ConfidentialPaymentInput } from 'x402z-shared/web';
|
|
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
|
+
|
|
17
|
+
type ConfidentialClientRegisterConfig = ConfidentialClientConfig & {
|
|
18
|
+
networks?: Network[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type X402zWebClientConfig = Omit<ConfidentialClientRegisterConfig, "buildPayment"> & {
|
|
22
|
+
relayerConfig: unknown;
|
|
23
|
+
fetch?: typeof fetch;
|
|
24
|
+
debug?: boolean;
|
|
25
|
+
};
|
|
26
|
+
type PayOptions = {
|
|
27
|
+
headers?: Record<string, string>;
|
|
28
|
+
};
|
|
29
|
+
declare function createX402zWebClient(config: X402zWebClientConfig): Promise<{
|
|
30
|
+
relayer: _zama_fhe_relayer_sdk_web.FhevmInstance;
|
|
31
|
+
pay(url: string, options?: PayOptions): Promise<Response>;
|
|
32
|
+
}>;
|
|
33
|
+
|
|
34
|
+
export { type PayOptions, type X402zWebClientConfig, createX402zWebClient };
|
package/dist/web.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as _zama_fhe_relayer_sdk_web from '@zama-fhe/relayer-sdk/web';
|
|
2
|
+
import { PaymentRequirements, Network } from '@x402/core/types';
|
|
3
|
+
import { ClientEvmSigner } from '@x402/evm';
|
|
4
|
+
import { ConfidentialPaymentInput } from 'x402z-shared/web';
|
|
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
|
+
|
|
17
|
+
type ConfidentialClientRegisterConfig = ConfidentialClientConfig & {
|
|
18
|
+
networks?: Network[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type X402zWebClientConfig = Omit<ConfidentialClientRegisterConfig, "buildPayment"> & {
|
|
22
|
+
relayerConfig: unknown;
|
|
23
|
+
fetch?: typeof fetch;
|
|
24
|
+
debug?: boolean;
|
|
25
|
+
};
|
|
26
|
+
type PayOptions = {
|
|
27
|
+
headers?: Record<string, string>;
|
|
28
|
+
};
|
|
29
|
+
declare function createX402zWebClient(config: X402zWebClientConfig): Promise<{
|
|
30
|
+
relayer: _zama_fhe_relayer_sdk_web.FhevmInstance;
|
|
31
|
+
pay(url: string, options?: PayOptions): Promise<Response>;
|
|
32
|
+
}>;
|
|
33
|
+
|
|
34
|
+
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_web2 = require("x402z-shared/web");
|
|
30
|
+
|
|
31
|
+
// src/scheme-web.ts
|
|
32
|
+
var import_viem = require("viem");
|
|
33
|
+
var import_web = require("x402z-shared/web");
|
|
34
|
+
var ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
35
|
+
var DECIMAL_POINT = ".";
|
|
36
|
+
function normalizeIntegerAmount(value, fallback) {
|
|
37
|
+
const normalized = (0, import_web.normalizeAmount)(value);
|
|
38
|
+
if (!normalized.includes(DECIMAL_POINT)) {
|
|
39
|
+
return normalized;
|
|
40
|
+
}
|
|
41
|
+
const fallbackNormalized = (0, import_web.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_web.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_web.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_web.normalizeAmount)(validAfter),
|
|
76
|
+
validBefore: (0, import_web.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_web.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-web.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_web2.initSDK)();
|
|
135
|
+
const relayer = await (0, import_web2.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_web2.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,188 @@
|
|
|
1
|
+
// src/web.ts
|
|
2
|
+
import { x402Client } from "@x402/core/client";
|
|
3
|
+
import { x402HTTPClient } from "@x402/core/http";
|
|
4
|
+
import { isAddress } from "viem";
|
|
5
|
+
import {
|
|
6
|
+
createEncryptedAmountInput,
|
|
7
|
+
createRelayerInstance,
|
|
8
|
+
initSDK
|
|
9
|
+
} from "x402z-shared/web";
|
|
10
|
+
|
|
11
|
+
// src/scheme-web.ts
|
|
12
|
+
import { getAddress } from "viem";
|
|
13
|
+
import {
|
|
14
|
+
confidentialPaymentTypes,
|
|
15
|
+
createNonce,
|
|
16
|
+
hashEncryptedAmountInput,
|
|
17
|
+
normalizeAmount
|
|
18
|
+
} from "x402z-shared/web";
|
|
19
|
+
var ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
20
|
+
var DECIMAL_POINT = ".";
|
|
21
|
+
function normalizeIntegerAmount(value, fallback) {
|
|
22
|
+
const normalized = normalizeAmount(value);
|
|
23
|
+
if (!normalized.includes(DECIMAL_POINT)) {
|
|
24
|
+
return normalized;
|
|
25
|
+
}
|
|
26
|
+
const fallbackNormalized = normalizeAmount(fallback);
|
|
27
|
+
if (fallbackNormalized.includes(DECIMAL_POINT)) {
|
|
28
|
+
throw new Error(`Invalid amount: ${normalized}`);
|
|
29
|
+
}
|
|
30
|
+
return fallbackNormalized;
|
|
31
|
+
}
|
|
32
|
+
var ConfidentialEvmScheme = class {
|
|
33
|
+
constructor(config) {
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.scheme = "erc7984-mind-v1";
|
|
36
|
+
this.hashFn = config.hashEncryptedAmountInput ?? hashEncryptedAmountInput;
|
|
37
|
+
this.clock = config.clock ?? (() => Math.floor(Date.now() / 1e3));
|
|
38
|
+
}
|
|
39
|
+
async createPaymentPayload(x402Version, paymentRequirements) {
|
|
40
|
+
const input = await this.config.buildPayment(paymentRequirements);
|
|
41
|
+
const extra = paymentRequirements.extra;
|
|
42
|
+
const eip712 = extra?.eip712 ?? this.config.eip712;
|
|
43
|
+
if (!eip712?.name || !eip712?.version) {
|
|
44
|
+
throw new Error("Missing EIP-712 domain parameters (name, version) in requirements or config");
|
|
45
|
+
}
|
|
46
|
+
const now = this.clock();
|
|
47
|
+
const validAfter = input.validAfter ?? Math.max(0, now - 60);
|
|
48
|
+
const validBefore = input.validBefore ?? now + paymentRequirements.maxTimeoutSeconds;
|
|
49
|
+
const nonce = input.nonce ?? createNonce();
|
|
50
|
+
const maxClearAmount = normalizeIntegerAmount(
|
|
51
|
+
input.maxClearAmount ?? extra?.confidential?.maxClearAmount ?? paymentRequirements.amount,
|
|
52
|
+
paymentRequirements.amount
|
|
53
|
+
);
|
|
54
|
+
const resourceHash = input.resourceHash ?? extra?.confidential?.resourceHash ?? ZERO_BYTES32;
|
|
55
|
+
const authorization = {
|
|
56
|
+
holder: this.config.signer.address,
|
|
57
|
+
payee: getAddress(paymentRequirements.payTo),
|
|
58
|
+
maxClearAmount,
|
|
59
|
+
resourceHash,
|
|
60
|
+
validAfter: normalizeAmount(validAfter),
|
|
61
|
+
validBefore: normalizeAmount(validBefore),
|
|
62
|
+
nonce,
|
|
63
|
+
encryptedAmountHash: this.hashFn(input.encryptedAmountInput)
|
|
64
|
+
};
|
|
65
|
+
const chainId = parseInt(paymentRequirements.network.split(":")[1]);
|
|
66
|
+
const signature = await this.config.signer.signTypedData({
|
|
67
|
+
domain: {
|
|
68
|
+
name: eip712.name,
|
|
69
|
+
version: eip712.version,
|
|
70
|
+
chainId,
|
|
71
|
+
verifyingContract: getAddress(paymentRequirements.asset)
|
|
72
|
+
},
|
|
73
|
+
types: confidentialPaymentTypes,
|
|
74
|
+
primaryType: "ConfidentialPayment",
|
|
75
|
+
message: {
|
|
76
|
+
holder: getAddress(authorization.holder),
|
|
77
|
+
payee: getAddress(authorization.payee),
|
|
78
|
+
maxClearAmount: BigInt(authorization.maxClearAmount),
|
|
79
|
+
resourceHash: authorization.resourceHash,
|
|
80
|
+
validAfter: BigInt(authorization.validAfter),
|
|
81
|
+
validBefore: BigInt(authorization.validBefore),
|
|
82
|
+
nonce: authorization.nonce,
|
|
83
|
+
encryptedAmountHash: authorization.encryptedAmountHash
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
const payload = {
|
|
87
|
+
authorization,
|
|
88
|
+
signature,
|
|
89
|
+
encryptedAmountInput: input.encryptedAmountInput,
|
|
90
|
+
inputProof: input.inputProof
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
x402Version,
|
|
94
|
+
payload
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/register-web.ts
|
|
100
|
+
function registerConfidentialEvmScheme(client, config) {
|
|
101
|
+
if (config.networks && config.networks.length > 0) {
|
|
102
|
+
for (const network of config.networks) {
|
|
103
|
+
client.register(network, new ConfidentialEvmScheme(config));
|
|
104
|
+
}
|
|
105
|
+
return client;
|
|
106
|
+
}
|
|
107
|
+
client.register("eip155:*", new ConfidentialEvmScheme(config));
|
|
108
|
+
return client;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/web.ts
|
|
112
|
+
async function createX402zWebClient(config) {
|
|
113
|
+
const { fetch: fetchOverride, ...registerConfig } = config;
|
|
114
|
+
const fetchFn = fetchOverride ?? globalThis.fetch;
|
|
115
|
+
const debugEnabled = config.debug ?? process.env.X402Z_DEBUG === "1";
|
|
116
|
+
if (!fetchFn) {
|
|
117
|
+
throw new Error("fetch is not available; provide a fetch implementation");
|
|
118
|
+
}
|
|
119
|
+
await initSDK();
|
|
120
|
+
const relayer = await createRelayerInstance(config.relayerConfig);
|
|
121
|
+
const buildPayment = async (requirements) => {
|
|
122
|
+
if (!isAddress(requirements.asset)) {
|
|
123
|
+
throw new Error(`Invalid TOKEN_ADDRESS from requirements: ${requirements.asset}`);
|
|
124
|
+
}
|
|
125
|
+
const extra = requirements.extra;
|
|
126
|
+
const batcherAddress = extra?.confidential?.batcherAddress;
|
|
127
|
+
if (!batcherAddress) {
|
|
128
|
+
throw new Error("Missing confidential.batcherAddress in payment requirements");
|
|
129
|
+
}
|
|
130
|
+
const encrypted = await createEncryptedAmountInput(
|
|
131
|
+
relayer,
|
|
132
|
+
requirements.asset,
|
|
133
|
+
batcherAddress,
|
|
134
|
+
Number(requirements.amount)
|
|
135
|
+
);
|
|
136
|
+
return {
|
|
137
|
+
encryptedAmountInput: encrypted.handle,
|
|
138
|
+
inputProof: encrypted.inputProof
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
const client = new x402Client();
|
|
142
|
+
registerConfidentialEvmScheme(client, {
|
|
143
|
+
...registerConfig,
|
|
144
|
+
buildPayment
|
|
145
|
+
});
|
|
146
|
+
const httpClient = new x402HTTPClient(client);
|
|
147
|
+
return {
|
|
148
|
+
relayer,
|
|
149
|
+
async pay(url, options) {
|
|
150
|
+
const initial = await fetchFn(url, { headers: options?.headers });
|
|
151
|
+
if (initial.status !== 402) {
|
|
152
|
+
return initial;
|
|
153
|
+
}
|
|
154
|
+
const paymentRequired = httpClient.getPaymentRequiredResponse(
|
|
155
|
+
(name) => initial.headers.get(name),
|
|
156
|
+
await initial.json().catch(() => ({}))
|
|
157
|
+
);
|
|
158
|
+
const payload = await httpClient.createPaymentPayload(paymentRequired);
|
|
159
|
+
const payHeaders = httpClient.encodePaymentSignatureHeader(payload);
|
|
160
|
+
if (debugEnabled) {
|
|
161
|
+
console.debug("[x402z-client] payment payload", payload);
|
|
162
|
+
console.debug("[x402z-client] payment headers", payHeaders);
|
|
163
|
+
}
|
|
164
|
+
const mergedHeaders = { ...options?.headers ?? {}, ...payHeaders };
|
|
165
|
+
const paidResponse = await fetchFn(url, { headers: mergedHeaders });
|
|
166
|
+
if (debugEnabled) {
|
|
167
|
+
try {
|
|
168
|
+
const body = await paidResponse.clone().text();
|
|
169
|
+
console.debug("[x402z-client] response", {
|
|
170
|
+
status: paidResponse.status,
|
|
171
|
+
headers: Object.fromEntries(paidResponse.headers.entries()),
|
|
172
|
+
body
|
|
173
|
+
});
|
|
174
|
+
} catch (error) {
|
|
175
|
+
console.debug("[x402z-client] response", {
|
|
176
|
+
status: paidResponse.status,
|
|
177
|
+
headers: Object.fromEntries(paidResponse.headers.entries()),
|
|
178
|
+
body: "<unavailable>"
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return paidResponse;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
export {
|
|
187
|
+
createX402zWebClient
|
|
188
|
+
};
|
package/package.json
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "x402z-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
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.
|
|
26
|
+
"x402z-shared": "0.0.7"
|
|
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
|
}
|