x402z-client 0.0.6 → 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 +1 -0
- package/dist/index.d.mts +29 -6
- package/dist/index.d.ts +29 -6
- package/dist/index.mjs +98 -3
- package/dist/web.d.mts +18 -5
- package/dist/web.d.ts +18 -5
- package/dist/web.js +14 -14
- package/dist/web.mjs +102 -4
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { b as ConfidentialClientConfig, a as ConfidentialEvmScheme, r as registerConfidentialEvmScheme } from './register-DmPDc2uL.mjs';
|
|
3
|
-
import { PaymentRequirements } from '@x402/core/types';
|
|
1
|
+
import { SchemeNetworkClient, PaymentRequirements, PaymentPayload, Network } from '@x402/core/types';
|
|
4
2
|
export * from '@x402/core/types';
|
|
5
|
-
import {
|
|
3
|
+
import { ClientEvmSigner } from '@x402/evm';
|
|
4
|
+
import { ConfidentialPaymentInput, RelayerInstance } from 'x402z-shared';
|
|
5
|
+
import { x402Client } from '@x402/core/client';
|
|
6
6
|
export { AfterPaymentCreationHook, BeforePaymentCreationHook, OnPaymentCreationFailureHook, PaymentCreatedContext, PaymentCreationContext, PaymentCreationFailureContext, PaymentPolicy, SchemeRegistration, SelectPaymentRequirements, x402Client, x402ClientConfig, x402HTTPClient } from '@x402/core/client';
|
|
7
|
-
|
|
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;
|
|
8
31
|
|
|
9
32
|
declare function buildPaymentInputFromRelayer(relayer: RelayerInstance, requirements: PaymentRequirements, amount: number): Promise<ConfidentialPaymentInput>;
|
|
10
33
|
|
|
@@ -20,4 +43,4 @@ declare function createX402zClient(config: X402zClientConfig): {
|
|
|
20
43
|
pay(url: string, options?: PayOptions): Promise<Response>;
|
|
21
44
|
};
|
|
22
45
|
|
|
23
|
-
export { ConfidentialClientRegisterConfig, type PayOptions, type X402zClientConfig, buildPaymentInputFromRelayer, createX402zClient };
|
|
46
|
+
export { type ConfidentialClientConfig, type ConfidentialClientRegisterConfig, ConfidentialEvmScheme, type PayOptions, type X402zClientConfig, buildPaymentInputFromRelayer, createX402zClient, registerConfidentialEvmScheme };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { b as ConfidentialClientConfig, a as ConfidentialEvmScheme, r as registerConfidentialEvmScheme } from './register-DmPDc2uL.js';
|
|
3
|
-
import { PaymentRequirements } from '@x402/core/types';
|
|
1
|
+
import { SchemeNetworkClient, PaymentRequirements, PaymentPayload, Network } from '@x402/core/types';
|
|
4
2
|
export * from '@x402/core/types';
|
|
5
|
-
import {
|
|
3
|
+
import { ClientEvmSigner } from '@x402/evm';
|
|
4
|
+
import { ConfidentialPaymentInput, RelayerInstance } from 'x402z-shared';
|
|
5
|
+
import { x402Client } from '@x402/core/client';
|
|
6
6
|
export { AfterPaymentCreationHook, BeforePaymentCreationHook, OnPaymentCreationFailureHook, PaymentCreatedContext, PaymentCreationContext, PaymentCreationFailureContext, PaymentPolicy, SchemeRegistration, SelectPaymentRequirements, x402Client, x402ClientConfig, x402HTTPClient } from '@x402/core/client';
|
|
7
|
-
|
|
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;
|
|
8
31
|
|
|
9
32
|
declare function buildPaymentInputFromRelayer(relayer: RelayerInstance, requirements: PaymentRequirements, amount: number): Promise<ConfidentialPaymentInput>;
|
|
10
33
|
|
|
@@ -20,4 +43,4 @@ declare function createX402zClient(config: X402zClientConfig): {
|
|
|
20
43
|
pay(url: string, options?: PayOptions): Promise<Response>;
|
|
21
44
|
};
|
|
22
45
|
|
|
23
|
-
export { ConfidentialClientRegisterConfig, type PayOptions, type X402zClientConfig, buildPaymentInputFromRelayer, createX402zClient };
|
|
46
|
+
export { type ConfidentialClientConfig, type ConfidentialClientRegisterConfig, ConfidentialEvmScheme, type PayOptions, type X402zClientConfig, buildPaymentInputFromRelayer, createX402zClient, registerConfidentialEvmScheme };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,102 @@
|
|
|
1
|
+
// src/scheme.ts
|
|
2
|
+
import { getAddress } from "viem";
|
|
1
3
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
}
|
|
5
100
|
|
|
6
101
|
// src/relayer.ts
|
|
7
102
|
import { createEncryptedAmountInput } from "x402z-shared";
|
package/dist/web.d.mts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import * as _zama_fhe_relayer_sdk_web from '@zama-fhe/relayer-sdk/web';
|
|
2
|
-
import {
|
|
3
|
-
import '@x402/
|
|
4
|
-
import '
|
|
5
|
-
|
|
6
|
-
|
|
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
|
+
};
|
|
7
20
|
|
|
8
21
|
type X402zWebClientConfig = Omit<ConfidentialClientRegisterConfig, "buildPayment"> & {
|
|
9
22
|
relayerConfig: unknown;
|
package/dist/web.d.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import * as _zama_fhe_relayer_sdk_web from '@zama-fhe/relayer-sdk/web';
|
|
2
|
-
import {
|
|
3
|
-
import '@x402/
|
|
4
|
-
import '
|
|
5
|
-
|
|
6
|
-
|
|
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
|
+
};
|
|
7
20
|
|
|
8
21
|
type X402zWebClientConfig = Omit<ConfidentialClientRegisterConfig, "buildPayment"> & {
|
|
9
22
|
relayerConfig: unknown;
|
package/dist/web.js
CHANGED
|
@@ -26,19 +26,19 @@ module.exports = __toCommonJS(web_exports);
|
|
|
26
26
|
var import_client = require("@x402/core/client");
|
|
27
27
|
var import_http = require("@x402/core/http");
|
|
28
28
|
var import_viem2 = require("viem");
|
|
29
|
-
var
|
|
29
|
+
var import_web2 = require("x402z-shared/web");
|
|
30
30
|
|
|
31
|
-
// src/scheme.ts
|
|
31
|
+
// src/scheme-web.ts
|
|
32
32
|
var import_viem = require("viem");
|
|
33
|
-
var
|
|
33
|
+
var import_web = require("x402z-shared/web");
|
|
34
34
|
var ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
35
35
|
var DECIMAL_POINT = ".";
|
|
36
36
|
function normalizeIntegerAmount(value, fallback) {
|
|
37
|
-
const normalized = (0,
|
|
37
|
+
const normalized = (0, import_web.normalizeAmount)(value);
|
|
38
38
|
if (!normalized.includes(DECIMAL_POINT)) {
|
|
39
39
|
return normalized;
|
|
40
40
|
}
|
|
41
|
-
const fallbackNormalized = (0,
|
|
41
|
+
const fallbackNormalized = (0, import_web.normalizeAmount)(fallback);
|
|
42
42
|
if (fallbackNormalized.includes(DECIMAL_POINT)) {
|
|
43
43
|
throw new Error(`Invalid amount: ${normalized}`);
|
|
44
44
|
}
|
|
@@ -48,7 +48,7 @@ var ConfidentialEvmScheme = class {
|
|
|
48
48
|
constructor(config) {
|
|
49
49
|
this.config = config;
|
|
50
50
|
this.scheme = "erc7984-mind-v1";
|
|
51
|
-
this.hashFn = config.hashEncryptedAmountInput ??
|
|
51
|
+
this.hashFn = config.hashEncryptedAmountInput ?? import_web.hashEncryptedAmountInput;
|
|
52
52
|
this.clock = config.clock ?? (() => Math.floor(Date.now() / 1e3));
|
|
53
53
|
}
|
|
54
54
|
async createPaymentPayload(x402Version, paymentRequirements) {
|
|
@@ -61,7 +61,7 @@ var ConfidentialEvmScheme = class {
|
|
|
61
61
|
const now = this.clock();
|
|
62
62
|
const validAfter = input.validAfter ?? Math.max(0, now - 60);
|
|
63
63
|
const validBefore = input.validBefore ?? now + paymentRequirements.maxTimeoutSeconds;
|
|
64
|
-
const nonce = input.nonce ?? (0,
|
|
64
|
+
const nonce = input.nonce ?? (0, import_web.createNonce)();
|
|
65
65
|
const maxClearAmount = normalizeIntegerAmount(
|
|
66
66
|
input.maxClearAmount ?? extra?.confidential?.maxClearAmount ?? paymentRequirements.amount,
|
|
67
67
|
paymentRequirements.amount
|
|
@@ -72,8 +72,8 @@ var ConfidentialEvmScheme = class {
|
|
|
72
72
|
payee: (0, import_viem.getAddress)(paymentRequirements.payTo),
|
|
73
73
|
maxClearAmount,
|
|
74
74
|
resourceHash,
|
|
75
|
-
validAfter: (0,
|
|
76
|
-
validBefore: (0,
|
|
75
|
+
validAfter: (0, import_web.normalizeAmount)(validAfter),
|
|
76
|
+
validBefore: (0, import_web.normalizeAmount)(validBefore),
|
|
77
77
|
nonce,
|
|
78
78
|
encryptedAmountHash: this.hashFn(input.encryptedAmountInput)
|
|
79
79
|
};
|
|
@@ -85,7 +85,7 @@ var ConfidentialEvmScheme = class {
|
|
|
85
85
|
chainId,
|
|
86
86
|
verifyingContract: (0, import_viem.getAddress)(paymentRequirements.asset)
|
|
87
87
|
},
|
|
88
|
-
types:
|
|
88
|
+
types: import_web.confidentialPaymentTypes,
|
|
89
89
|
primaryType: "ConfidentialPayment",
|
|
90
90
|
message: {
|
|
91
91
|
holder: (0, import_viem.getAddress)(authorization.holder),
|
|
@@ -111,7 +111,7 @@ var ConfidentialEvmScheme = class {
|
|
|
111
111
|
}
|
|
112
112
|
};
|
|
113
113
|
|
|
114
|
-
// src/register.ts
|
|
114
|
+
// src/register-web.ts
|
|
115
115
|
function registerConfidentialEvmScheme(client, config) {
|
|
116
116
|
if (config.networks && config.networks.length > 0) {
|
|
117
117
|
for (const network of config.networks) {
|
|
@@ -131,8 +131,8 @@ async function createX402zWebClient(config) {
|
|
|
131
131
|
if (!fetchFn) {
|
|
132
132
|
throw new Error("fetch is not available; provide a fetch implementation");
|
|
133
133
|
}
|
|
134
|
-
await (0,
|
|
135
|
-
const relayer = await (0,
|
|
134
|
+
await (0, import_web2.initSDK)();
|
|
135
|
+
const relayer = await (0, import_web2.createRelayerInstance)(config.relayerConfig);
|
|
136
136
|
const buildPayment = async (requirements) => {
|
|
137
137
|
if (!(0, import_viem2.isAddress)(requirements.asset)) {
|
|
138
138
|
throw new Error(`Invalid TOKEN_ADDRESS from requirements: ${requirements.asset}`);
|
|
@@ -142,7 +142,7 @@ async function createX402zWebClient(config) {
|
|
|
142
142
|
if (!batcherAddress) {
|
|
143
143
|
throw new Error("Missing confidential.batcherAddress in payment requirements");
|
|
144
144
|
}
|
|
145
|
-
const encrypted = await (0,
|
|
145
|
+
const encrypted = await (0, import_web2.createEncryptedAmountInput)(
|
|
146
146
|
relayer,
|
|
147
147
|
requirements.asset,
|
|
148
148
|
batcherAddress,
|
package/dist/web.mjs
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
registerConfidentialEvmScheme
|
|
3
|
-
} from "./chunk-WKGSJ5YZ.mjs";
|
|
4
|
-
|
|
5
1
|
// src/web.ts
|
|
6
2
|
import { x402Client } from "@x402/core/client";
|
|
7
3
|
import { x402HTTPClient } from "@x402/core/http";
|
|
@@ -11,6 +7,108 @@ import {
|
|
|
11
7
|
createRelayerInstance,
|
|
12
8
|
initSDK
|
|
13
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
|
|
14
112
|
async function createX402zWebClient(config) {
|
|
15
113
|
const { fetch: fetchOverride, ...registerConfig } = config;
|
|
16
114
|
const fetchFn = fetchOverride ?? globalThis.fetch;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@x402/core": "^2.0.0",
|
|
24
24
|
"@x402/evm": "^2.0.0",
|
|
25
25
|
"viem": "^2.39.3",
|
|
26
|
-
"x402z-shared": "0.0.
|
|
26
|
+
"x402z-shared": "0.0.7"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"jest": "^29.7.0",
|