x402z-shared 0.0.1

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 ADDED
@@ -0,0 +1,88 @@
1
+ # x402z-shared
2
+
3
+ Shared utilities for the erc7984-mind-v1 x402 scheme.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add x402z-shared
9
+ ```
10
+
11
+ ## Relayer helpers (Node)
12
+
13
+ ```ts
14
+ import {
15
+ createRelayerInstance,
16
+ createEncryptedAmountInput,
17
+ SepoliaConfig,
18
+ } from "x402z-shared";
19
+
20
+ ```
21
+
22
+ ## Relayer helpers (Browser)
23
+
24
+ ```ts
25
+ import {
26
+ createRelayerInstance,
27
+ createEncryptedAmountInput,
28
+ initSDK,
29
+ SepoliaConfig,
30
+ } from "x402z-shared/web";
31
+
32
+ await initSDK();
33
+ const relayer = await createRelayerInstance(SepoliaConfig);
34
+ const encrypted = await createEncryptedAmountInput(
35
+ relayer,
36
+ "0xToken",
37
+ "0xFacilitator",
38
+ 1000,
39
+ );
40
+ ```
41
+
42
+ ## Balance helper (Node)
43
+
44
+ ```ts
45
+ import { viewConfidentialBalance } from "x402z-shared";
46
+
47
+ const result = await viewConfidentialBalance({
48
+ rpcUrl: "https://sepolia.infura.io/v3/...",
49
+ tokenAddress: "0xToken",
50
+ relayer,
51
+ signer: { address, signTypedData },
52
+ });
53
+ console.log(result.balance?.toString());
54
+ ```
55
+
56
+ ## Transfer amount helper (Node)
57
+
58
+ ```ts
59
+ import { viewConfidentialTransferAmounts } from "x402z-shared";
60
+
61
+ const transfers = await viewConfidentialTransferAmounts({
62
+ rpcUrl: "https://sepolia.infura.io/v3/...",
63
+ tokenAddress: "0xToken",
64
+ txHash: "0xTransactionHash",
65
+ from: "0xSender",
66
+ to: "0xReceiver",
67
+ relayer,
68
+ signer: { address, signTypedData },
69
+ });
70
+ console.log(transfers[0]?.amount?.toString());
71
+ ```
72
+
73
+ ## Balance helper (Browser)
74
+
75
+ ```ts
76
+ import { viewConfidentialBalance } from "x402z-shared/web";
77
+ ```
78
+
79
+ ## Transfer amount helper (Browser)
80
+
81
+ ```ts
82
+ import { viewConfidentialTransferAmounts } from "x402z-shared/web";
83
+ ```
84
+
85
+ ## Notes
86
+
87
+ - Scheme name: `erc7984-mind-v1`
88
+ - `viewConfidentialBalance` can return handle-only if `decrypt: false`.
@@ -0,0 +1,101 @@
1
+ // src/abi.ts
2
+ var confidentialTokenAbi = [
3
+ {
4
+ inputs: [
5
+ {
6
+ components: [
7
+ { internalType: "address", name: "holder", type: "address" },
8
+ { internalType: "address", name: "payee", type: "address" },
9
+ { internalType: "uint256", name: "maxClearAmount", type: "uint256" },
10
+ { internalType: "bytes32", name: "resourceHash", type: "bytes32" },
11
+ { internalType: "uint48", name: "validAfter", type: "uint48" },
12
+ { internalType: "uint48", name: "validBefore", type: "uint48" },
13
+ { internalType: "bytes32", name: "nonce", type: "bytes32" },
14
+ { internalType: "bytes32", name: "encryptedAmountHash", type: "bytes32" }
15
+ ],
16
+ internalType: "struct FHEToken.ConfidentialPayment",
17
+ name: "p",
18
+ type: "tuple"
19
+ },
20
+ { internalType: "externalEuint64", name: "encryptedAmountInput", type: "bytes32" },
21
+ { internalType: "bytes", name: "inputProof", type: "bytes" },
22
+ { internalType: "bytes", name: "sig", type: "bytes" }
23
+ ],
24
+ name: "confidentialTransferWithAuthorization",
25
+ outputs: [{ internalType: "euint64", name: "transferred", type: "bytes32" }],
26
+ stateMutability: "nonpayable",
27
+ type: "function"
28
+ },
29
+ {
30
+ anonymous: false,
31
+ inputs: [
32
+ { indexed: true, internalType: "address", name: "holder", type: "address" },
33
+ { indexed: true, internalType: "address", name: "payee", type: "address" },
34
+ { indexed: false, internalType: "uint256", name: "maxClearAmount", type: "uint256" },
35
+ { indexed: true, internalType: "bytes32", name: "resourceHash", type: "bytes32" },
36
+ { indexed: false, internalType: "bytes32", name: "nonce", type: "bytes32" },
37
+ { indexed: false, internalType: "bytes32", name: "transferredAmount", type: "bytes32" }
38
+ ],
39
+ name: "ConfidentialPaymentExecuted",
40
+ type: "event"
41
+ },
42
+ {
43
+ inputs: [
44
+ { internalType: "address", name: "", type: "address" },
45
+ { internalType: "bytes32", name: "", type: "bytes32" }
46
+ ],
47
+ name: "usedNonces",
48
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
49
+ stateMutability: "view",
50
+ type: "function"
51
+ }
52
+ ];
53
+
54
+ // src/constants.ts
55
+ var confidentialPaymentTypes = {
56
+ ConfidentialPayment: [
57
+ { name: "holder", type: "address" },
58
+ { name: "payee", type: "address" },
59
+ { name: "maxClearAmount", type: "uint256" },
60
+ { name: "resourceHash", type: "bytes32" },
61
+ { name: "validAfter", type: "uint48" },
62
+ { name: "validBefore", type: "uint48" },
63
+ { name: "nonce", type: "bytes32" },
64
+ { name: "encryptedAmountHash", type: "bytes32" }
65
+ ]
66
+ };
67
+
68
+ // src/utils.ts
69
+ import { encodeAbiParameters, keccak256, toHex } from "viem";
70
+ function createNonce() {
71
+ const cryptoObj = typeof globalThis.crypto !== "undefined" ? globalThis.crypto : globalThis.crypto;
72
+ if (!cryptoObj) {
73
+ throw new Error("Crypto API not available");
74
+ }
75
+ return toHex(cryptoObj.getRandomValues(new Uint8Array(32)));
76
+ }
77
+ function hashEncryptedAmountInput(encryptedAmountInput) {
78
+ return keccak256(
79
+ encodeAbiParameters([{ type: "bytes32" }], [encryptedAmountInput])
80
+ );
81
+ }
82
+ function normalizeAmount(amount) {
83
+ if (typeof amount === "string") {
84
+ return amount;
85
+ }
86
+ if (typeof amount === "number") {
87
+ if (!Number.isFinite(amount)) {
88
+ throw new Error("Invalid amount");
89
+ }
90
+ return Math.trunc(amount).toString();
91
+ }
92
+ return amount.toString();
93
+ }
94
+
95
+ export {
96
+ confidentialTokenAbi,
97
+ confidentialPaymentTypes,
98
+ createNonce,
99
+ hashEncryptedAmountInput,
100
+ normalizeAmount
101
+ };
@@ -0,0 +1,71 @@
1
+ export { C as ConfidentialPaymentAuthorization, f as ConfidentialPaymentInput, d as ConfidentialPaymentPayload, e as ConfidentialRequirementsExtra, a as confidentialPaymentTypes, c as confidentialTokenAbi, b as createNonce, h as hashEncryptedAmountInput, n as normalizeAmount } from './types-RdcpJuhT.mjs';
2
+ import { createInstance } from '@zama-fhe/relayer-sdk/node';
3
+ export { SepoliaConfig } from '@zama-fhe/relayer-sdk/node';
4
+ export * from '@x402/core/types';
5
+
6
+ type RelayerInstance = Awaited<ReturnType<typeof createInstance>>;
7
+ type RelayerSigner = {
8
+ address: string;
9
+ signTypedData: ((domain: Record<string, unknown>, types: Record<string, Array<{
10
+ name: string;
11
+ type: string;
12
+ }>>, message: Record<string, unknown>) => Promise<string>) | ((args: {
13
+ domain: Record<string, unknown>;
14
+ types: Record<string, Array<{
15
+ name: string;
16
+ type: string;
17
+ }>>;
18
+ primaryType: string;
19
+ message: Record<string, unknown>;
20
+ }) => Promise<string>);
21
+ };
22
+ declare function createRelayerInstance(config: unknown): Promise<RelayerInstance>;
23
+
24
+ declare function createEncryptedAmountInput(relayer: RelayerInstance, contractAddress: string, senderAddress: string, amount: number): Promise<{
25
+ handle: `0x${string}`;
26
+ inputProof: `0x${string}`;
27
+ }>;
28
+ declare function userDecryptEuint64(relayer: RelayerInstance, handle: string, contractAddress: string, signer: RelayerSigner, options?: {
29
+ durationDays?: string;
30
+ startTimestamp?: string;
31
+ }): Promise<bigint>;
32
+ declare function publicDecrypt(relayer: RelayerInstance, handles: string[]): Promise<{
33
+ clearValues: Record<string, bigint>;
34
+ decryptionProof: string;
35
+ }>;
36
+
37
+ type ViewConfidentialBalanceOptions = {
38
+ rpcUrl: string;
39
+ tokenAddress: `0x${string}`;
40
+ account?: `0x${string}`;
41
+ relayer: RelayerInstance;
42
+ signer?: RelayerSigner;
43
+ decrypt?: boolean;
44
+ };
45
+ declare function viewConfidentialBalance(options: ViewConfidentialBalanceOptions): Promise<{
46
+ handle: `0x${string}`;
47
+ balance?: bigint;
48
+ }>;
49
+
50
+ type ConfidentialTransferAmount = {
51
+ holder: `0x${string}`;
52
+ payee: `0x${string}`;
53
+ maxClearAmount: bigint;
54
+ resourceHash: `0x${string}`;
55
+ nonce: `0x${string}`;
56
+ handle: `0x${string}`;
57
+ amount?: bigint;
58
+ };
59
+ type ViewConfidentialTransferOptions = {
60
+ rpcUrl: string;
61
+ tokenAddress: `0x${string}`;
62
+ txHash: `0x${string}`;
63
+ from?: `0x${string}`;
64
+ to?: `0x${string}`;
65
+ relayer: RelayerInstance;
66
+ signer?: RelayerSigner;
67
+ decrypt?: boolean;
68
+ };
69
+ declare function viewConfidentialTransferAmounts(options: ViewConfidentialTransferOptions): Promise<ConfidentialTransferAmount[]>;
70
+
71
+ export { type ConfidentialTransferAmount, type RelayerInstance, type RelayerSigner, type ViewConfidentialBalanceOptions, type ViewConfidentialTransferOptions, createEncryptedAmountInput, createRelayerInstance, publicDecrypt, userDecryptEuint64, viewConfidentialBalance, viewConfidentialTransferAmounts };
@@ -0,0 +1,71 @@
1
+ export { C as ConfidentialPaymentAuthorization, f as ConfidentialPaymentInput, d as ConfidentialPaymentPayload, e as ConfidentialRequirementsExtra, a as confidentialPaymentTypes, c as confidentialTokenAbi, b as createNonce, h as hashEncryptedAmountInput, n as normalizeAmount } from './types-RdcpJuhT.js';
2
+ import { createInstance } from '@zama-fhe/relayer-sdk/node';
3
+ export { SepoliaConfig } from '@zama-fhe/relayer-sdk/node';
4
+ export * from '@x402/core/types';
5
+
6
+ type RelayerInstance = Awaited<ReturnType<typeof createInstance>>;
7
+ type RelayerSigner = {
8
+ address: string;
9
+ signTypedData: ((domain: Record<string, unknown>, types: Record<string, Array<{
10
+ name: string;
11
+ type: string;
12
+ }>>, message: Record<string, unknown>) => Promise<string>) | ((args: {
13
+ domain: Record<string, unknown>;
14
+ types: Record<string, Array<{
15
+ name: string;
16
+ type: string;
17
+ }>>;
18
+ primaryType: string;
19
+ message: Record<string, unknown>;
20
+ }) => Promise<string>);
21
+ };
22
+ declare function createRelayerInstance(config: unknown): Promise<RelayerInstance>;
23
+
24
+ declare function createEncryptedAmountInput(relayer: RelayerInstance, contractAddress: string, senderAddress: string, amount: number): Promise<{
25
+ handle: `0x${string}`;
26
+ inputProof: `0x${string}`;
27
+ }>;
28
+ declare function userDecryptEuint64(relayer: RelayerInstance, handle: string, contractAddress: string, signer: RelayerSigner, options?: {
29
+ durationDays?: string;
30
+ startTimestamp?: string;
31
+ }): Promise<bigint>;
32
+ declare function publicDecrypt(relayer: RelayerInstance, handles: string[]): Promise<{
33
+ clearValues: Record<string, bigint>;
34
+ decryptionProof: string;
35
+ }>;
36
+
37
+ type ViewConfidentialBalanceOptions = {
38
+ rpcUrl: string;
39
+ tokenAddress: `0x${string}`;
40
+ account?: `0x${string}`;
41
+ relayer: RelayerInstance;
42
+ signer?: RelayerSigner;
43
+ decrypt?: boolean;
44
+ };
45
+ declare function viewConfidentialBalance(options: ViewConfidentialBalanceOptions): Promise<{
46
+ handle: `0x${string}`;
47
+ balance?: bigint;
48
+ }>;
49
+
50
+ type ConfidentialTransferAmount = {
51
+ holder: `0x${string}`;
52
+ payee: `0x${string}`;
53
+ maxClearAmount: bigint;
54
+ resourceHash: `0x${string}`;
55
+ nonce: `0x${string}`;
56
+ handle: `0x${string}`;
57
+ amount?: bigint;
58
+ };
59
+ type ViewConfidentialTransferOptions = {
60
+ rpcUrl: string;
61
+ tokenAddress: `0x${string}`;
62
+ txHash: `0x${string}`;
63
+ from?: `0x${string}`;
64
+ to?: `0x${string}`;
65
+ relayer: RelayerInstance;
66
+ signer?: RelayerSigner;
67
+ decrypt?: boolean;
68
+ };
69
+ declare function viewConfidentialTransferAmounts(options: ViewConfidentialTransferOptions): Promise<ConfidentialTransferAmount[]>;
70
+
71
+ export { type ConfidentialTransferAmount, type RelayerInstance, type RelayerSigner, type ViewConfidentialBalanceOptions, type ViewConfidentialTransferOptions, createEncryptedAmountInput, createRelayerInstance, publicDecrypt, userDecryptEuint64, viewConfidentialBalance, viewConfidentialTransferAmounts };
package/dist/index.js ADDED
@@ -0,0 +1,315 @@
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/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ SepoliaConfig: () => import_node.SepoliaConfig,
24
+ confidentialPaymentTypes: () => confidentialPaymentTypes,
25
+ confidentialTokenAbi: () => confidentialTokenAbi,
26
+ createEncryptedAmountInput: () => createEncryptedAmountInput,
27
+ createNonce: () => createNonce,
28
+ createRelayerInstance: () => createRelayerInstance,
29
+ hashEncryptedAmountInput: () => hashEncryptedAmountInput,
30
+ normalizeAmount: () => normalizeAmount,
31
+ publicDecrypt: () => publicDecrypt,
32
+ userDecryptEuint64: () => userDecryptEuint64,
33
+ viewConfidentialBalance: () => viewConfidentialBalance,
34
+ viewConfidentialTransferAmounts: () => viewConfidentialTransferAmounts
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // src/abi.ts
39
+ var confidentialTokenAbi = [
40
+ {
41
+ inputs: [
42
+ {
43
+ components: [
44
+ { internalType: "address", name: "holder", type: "address" },
45
+ { internalType: "address", name: "payee", type: "address" },
46
+ { internalType: "uint256", name: "maxClearAmount", type: "uint256" },
47
+ { internalType: "bytes32", name: "resourceHash", type: "bytes32" },
48
+ { internalType: "uint48", name: "validAfter", type: "uint48" },
49
+ { internalType: "uint48", name: "validBefore", type: "uint48" },
50
+ { internalType: "bytes32", name: "nonce", type: "bytes32" },
51
+ { internalType: "bytes32", name: "encryptedAmountHash", type: "bytes32" }
52
+ ],
53
+ internalType: "struct FHEToken.ConfidentialPayment",
54
+ name: "p",
55
+ type: "tuple"
56
+ },
57
+ { internalType: "externalEuint64", name: "encryptedAmountInput", type: "bytes32" },
58
+ { internalType: "bytes", name: "inputProof", type: "bytes" },
59
+ { internalType: "bytes", name: "sig", type: "bytes" }
60
+ ],
61
+ name: "confidentialTransferWithAuthorization",
62
+ outputs: [{ internalType: "euint64", name: "transferred", type: "bytes32" }],
63
+ stateMutability: "nonpayable",
64
+ type: "function"
65
+ },
66
+ {
67
+ anonymous: false,
68
+ inputs: [
69
+ { indexed: true, internalType: "address", name: "holder", type: "address" },
70
+ { indexed: true, internalType: "address", name: "payee", type: "address" },
71
+ { indexed: false, internalType: "uint256", name: "maxClearAmount", type: "uint256" },
72
+ { indexed: true, internalType: "bytes32", name: "resourceHash", type: "bytes32" },
73
+ { indexed: false, internalType: "bytes32", name: "nonce", type: "bytes32" },
74
+ { indexed: false, internalType: "bytes32", name: "transferredAmount", type: "bytes32" }
75
+ ],
76
+ name: "ConfidentialPaymentExecuted",
77
+ type: "event"
78
+ },
79
+ {
80
+ inputs: [
81
+ { internalType: "address", name: "", type: "address" },
82
+ { internalType: "bytes32", name: "", type: "bytes32" }
83
+ ],
84
+ name: "usedNonces",
85
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
86
+ stateMutability: "view",
87
+ type: "function"
88
+ }
89
+ ];
90
+
91
+ // src/constants.ts
92
+ var confidentialPaymentTypes = {
93
+ ConfidentialPayment: [
94
+ { name: "holder", type: "address" },
95
+ { name: "payee", type: "address" },
96
+ { name: "maxClearAmount", type: "uint256" },
97
+ { name: "resourceHash", type: "bytes32" },
98
+ { name: "validAfter", type: "uint48" },
99
+ { name: "validBefore", type: "uint48" },
100
+ { name: "nonce", type: "bytes32" },
101
+ { name: "encryptedAmountHash", type: "bytes32" }
102
+ ]
103
+ };
104
+
105
+ // src/utils.ts
106
+ var import_viem = require("viem");
107
+ function createNonce() {
108
+ const cryptoObj = typeof globalThis.crypto !== "undefined" ? globalThis.crypto : globalThis.crypto;
109
+ if (!cryptoObj) {
110
+ throw new Error("Crypto API not available");
111
+ }
112
+ return (0, import_viem.toHex)(cryptoObj.getRandomValues(new Uint8Array(32)));
113
+ }
114
+ function hashEncryptedAmountInput(encryptedAmountInput) {
115
+ return (0, import_viem.keccak256)(
116
+ (0, import_viem.encodeAbiParameters)([{ type: "bytes32" }], [encryptedAmountInput])
117
+ );
118
+ }
119
+ function normalizeAmount(amount) {
120
+ if (typeof amount === "string") {
121
+ return amount;
122
+ }
123
+ if (typeof amount === "number") {
124
+ if (!Number.isFinite(amount)) {
125
+ throw new Error("Invalid amount");
126
+ }
127
+ return Math.trunc(amount).toString();
128
+ }
129
+ return amount.toString();
130
+ }
131
+
132
+ // src/relayer.ts
133
+ var import_node = require("@zama-fhe/relayer-sdk/node");
134
+ var import_viem2 = require("viem");
135
+ async function createRelayerInstance(config) {
136
+ return (0, import_node.createInstance)(config);
137
+ }
138
+ async function createEncryptedAmountInput(relayer, contractAddress, senderAddress, amount) {
139
+ const normalizedContract = (0, import_viem2.getAddress)(contractAddress);
140
+ const normalizedSender = (0, import_viem2.getAddress)(senderAddress);
141
+ const encrypted = await relayer.createEncryptedInput(normalizedContract, normalizedSender).add64(amount).encrypt();
142
+ const handle = typeof encrypted.handles[0] === "string" ? encrypted.handles[0] : (0, import_viem2.toHex)(encrypted.handles[0]);
143
+ const inputProof = typeof encrypted.inputProof === "string" ? encrypted.inputProof : (0, import_viem2.toHex)(encrypted.inputProof);
144
+ return {
145
+ handle,
146
+ inputProof
147
+ };
148
+ }
149
+ async function userDecryptEuint64(relayer, handle, contractAddress, signer, options) {
150
+ const keypair = relayer.generateKeypair();
151
+ const startTimestamp = options?.startTimestamp ?? Math.floor(Date.now() / 1e3).toString();
152
+ const durationDays = options?.durationDays ?? "10";
153
+ const contractAddresses = [contractAddress];
154
+ const eip712 = relayer.createEIP712(keypair.publicKey, contractAddresses, startTimestamp, durationDays);
155
+ const types = { UserDecryptRequestVerification: eip712.types.UserDecryptRequestVerification };
156
+ const sign = signer.signTypedData;
157
+ const signature = sign.length >= 3 ? await sign(eip712.domain, types, eip712.message) : await sign({
158
+ domain: eip712.domain,
159
+ types,
160
+ primaryType: "UserDecryptRequestVerification",
161
+ message: eip712.message
162
+ });
163
+ const result = await relayer.userDecrypt(
164
+ [{ handle, contractAddress }],
165
+ keypair.privateKey,
166
+ keypair.publicKey,
167
+ signature.replace("0x", ""),
168
+ contractAddresses,
169
+ signer.address,
170
+ startTimestamp,
171
+ durationDays
172
+ );
173
+ return result[handle];
174
+ }
175
+ async function publicDecrypt(relayer, handles) {
176
+ const result = await relayer.publicDecrypt(handles);
177
+ return {
178
+ clearValues: result.clearValues,
179
+ decryptionProof: result.decryptionProof
180
+ };
181
+ }
182
+
183
+ // src/balance.ts
184
+ var import_viem3 = require("viem");
185
+ var ZERO_HANDLE = "0x" + "00".repeat(32);
186
+ async function viewConfidentialBalance(options) {
187
+ if (!(0, import_viem3.isAddress)(options.tokenAddress)) {
188
+ throw new Error(`Invalid token address: ${options.tokenAddress}`);
189
+ }
190
+ const account = options.account ?? options.signer?.address;
191
+ if (!account || !(0, import_viem3.isAddress)(account)) {
192
+ throw new Error(`Invalid account address: ${account ?? "undefined"}`);
193
+ }
194
+ const publicClient = (0, import_viem3.createPublicClient)({ transport: (0, import_viem3.http)(options.rpcUrl) });
195
+ const confidentialBalanceAbi = [
196
+ {
197
+ inputs: [{ internalType: "address", name: "account", type: "address" }],
198
+ name: "confidentialBalanceOf",
199
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
200
+ stateMutability: "view",
201
+ type: "function"
202
+ }
203
+ ];
204
+ const handle = await publicClient.readContract({
205
+ address: options.tokenAddress,
206
+ abi: confidentialBalanceAbi,
207
+ functionName: "confidentialBalanceOf",
208
+ args: [account]
209
+ });
210
+ const shouldDecrypt = options.decrypt ?? true;
211
+ if (!shouldDecrypt) {
212
+ return { handle };
213
+ }
214
+ if (!options.signer) {
215
+ throw new Error("Missing signer for decryption");
216
+ }
217
+ if (handle === ZERO_HANDLE) {
218
+ return { handle, balance: 0n };
219
+ }
220
+ const balance = await userDecryptEuint64(
221
+ options.relayer,
222
+ handle,
223
+ options.tokenAddress,
224
+ options.signer
225
+ );
226
+ return { handle, balance };
227
+ }
228
+
229
+ // src/transfer.ts
230
+ var import_viem4 = require("viem");
231
+ async function viewConfidentialTransferAmounts(options) {
232
+ if (!(0, import_viem4.isAddress)(options.tokenAddress)) {
233
+ throw new Error(`Invalid token address: ${options.tokenAddress}`);
234
+ }
235
+ if (!(0, import_viem4.isHex)(options.txHash, { strict: true })) {
236
+ throw new Error(`Invalid transaction hash: ${options.txHash}`);
237
+ }
238
+ if (options.from && !(0, import_viem4.isAddress)(options.from)) {
239
+ throw new Error(`Invalid from address: ${options.from}`);
240
+ }
241
+ if (options.to && !(0, import_viem4.isAddress)(options.to)) {
242
+ throw new Error(`Invalid to address: ${options.to}`);
243
+ }
244
+ const publicClient = (0, import_viem4.createPublicClient)({ transport: (0, import_viem4.http)(options.rpcUrl) });
245
+ const receipt = await publicClient.getTransactionReceipt({
246
+ hash: options.txHash
247
+ });
248
+ const tokenAddress = (0, import_viem4.getAddress)(options.tokenAddress);
249
+ const fromFilter = options.from ? (0, import_viem4.getAddress)(options.from) : void 0;
250
+ const toFilter = options.to ? (0, import_viem4.getAddress)(options.to) : void 0;
251
+ const logs = receipt.logs.filter((log) => (0, import_viem4.getAddress)(log.address) === tokenAddress);
252
+ const transfers = [];
253
+ for (const log of logs) {
254
+ try {
255
+ const decoded = (0, import_viem4.decodeEventLog)({
256
+ abi: confidentialTokenAbi,
257
+ eventName: "ConfidentialPaymentExecuted",
258
+ data: log.data,
259
+ topics: log.topics
260
+ });
261
+ const args = decoded.args;
262
+ const entry = {
263
+ holder: (0, import_viem4.getAddress)(args.holder),
264
+ payee: (0, import_viem4.getAddress)(args.payee),
265
+ maxClearAmount: BigInt(args.maxClearAmount),
266
+ resourceHash: args.resourceHash,
267
+ nonce: args.nonce,
268
+ handle: args.transferredAmount
269
+ };
270
+ if (fromFilter && entry.holder !== fromFilter) {
271
+ continue;
272
+ }
273
+ if (toFilter && entry.payee !== toFilter) {
274
+ continue;
275
+ }
276
+ const shouldDecrypt = options.decrypt ?? true;
277
+ if (shouldDecrypt) {
278
+ if (!options.signer) {
279
+ throw new Error("Missing signer for decryption");
280
+ }
281
+ const signerAddress = (0, import_viem4.getAddress)(options.signer.address);
282
+ if (signerAddress !== (0, import_viem4.getAddress)(entry.holder) && signerAddress !== (0, import_viem4.getAddress)(entry.payee)) {
283
+ throw new Error("Signer must be the holder or payee to decrypt transfer amount");
284
+ }
285
+ entry.amount = await userDecryptEuint64(
286
+ options.relayer,
287
+ entry.handle,
288
+ tokenAddress,
289
+ options.signer
290
+ );
291
+ }
292
+ transfers.push(entry);
293
+ } catch (error) {
294
+ if (error instanceof Error && error.message.includes("ConfidentialPaymentExecuted")) {
295
+ throw error;
296
+ }
297
+ }
298
+ }
299
+ return transfers;
300
+ }
301
+ // Annotate the CommonJS export names for ESM import in node:
302
+ 0 && (module.exports = {
303
+ SepoliaConfig,
304
+ confidentialPaymentTypes,
305
+ confidentialTokenAbi,
306
+ createEncryptedAmountInput,
307
+ createNonce,
308
+ createRelayerInstance,
309
+ hashEncryptedAmountInput,
310
+ normalizeAmount,
311
+ publicDecrypt,
312
+ userDecryptEuint64,
313
+ viewConfidentialBalance,
314
+ viewConfidentialTransferAmounts
315
+ });