x402z-shared 0.0.2 → 0.0.3

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/dist/web.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var web_exports = {};
22
22
  __export(web_exports, {
23
23
  SepoliaConfig: () => import_bundle.SepoliaConfig,
24
+ confidentialErrorCodes: () => confidentialErrorCodes,
24
25
  confidentialPaymentTypes: () => confidentialPaymentTypes,
25
26
  confidentialTokenAbi: () => confidentialTokenAbi,
26
27
  createEncryptedAmountInput: () => createEncryptedAmountInput,
@@ -30,6 +31,8 @@ __export(web_exports, {
30
31
  initSDK: () => import_bundle.initSDK,
31
32
  normalizeAmount: () => normalizeAmount,
32
33
  publicDecrypt: () => publicDecrypt,
34
+ setObserver: () => setObserver,
35
+ setObserverWeb: () => setObserverWeb,
33
36
  userDecryptEuint64: () => userDecryptEuint64,
34
37
  viewConfidentialBalance: () => viewConfidentialBalance,
35
38
  viewConfidentialTransferAmounts: () => viewConfidentialTransferAmounts
@@ -86,6 +89,23 @@ var confidentialTokenAbi = [
86
89
  outputs: [{ internalType: "bool", name: "", type: "bool" }],
87
90
  stateMutability: "view",
88
91
  type: "function"
92
+ },
93
+ {
94
+ inputs: [{ internalType: "address", name: "account", type: "address" }],
95
+ name: "observer",
96
+ outputs: [{ internalType: "address", name: "", type: "address" }],
97
+ stateMutability: "view",
98
+ type: "function"
99
+ },
100
+ {
101
+ inputs: [
102
+ { internalType: "address", name: "account", type: "address" },
103
+ { internalType: "address", name: "newObserver", type: "address" }
104
+ ],
105
+ name: "setObserver",
106
+ outputs: [],
107
+ stateMutability: "nonpayable",
108
+ type: "function"
89
109
  }
90
110
  ];
91
111
 
@@ -102,6 +122,9 @@ var confidentialPaymentTypes = {
102
122
  { name: "encryptedAmountHash", type: "bytes32" }
103
123
  ]
104
124
  };
125
+ var confidentialErrorCodes = {
126
+ observerNotAuthorized: "observer_not_authorized"
127
+ };
105
128
 
106
129
  // src/utils.ts
107
130
  var import_viem = require("viem");
@@ -134,7 +157,11 @@ function normalizeAmount(amount) {
134
157
  var import_bundle = require("@zama-fhe/relayer-sdk/bundle");
135
158
  var import_viem2 = require("viem");
136
159
  async function createRelayerInstance(config) {
137
- return (0, import_bundle.createInstance)(config);
160
+ const instance = await (0, import_bundle.createInstance)(config);
161
+ if (typeof config.network === "string") {
162
+ instance.network = config.network;
163
+ }
164
+ return instance;
138
165
  }
139
166
  async function createEncryptedAmountInput(relayer, contractAddress, senderAddress, amount) {
140
167
  const normalizedContract = (0, import_viem2.getAddress)(contractAddress);
@@ -215,6 +242,18 @@ async function viewConfidentialBalance(options) {
215
242
  if (!options.signer) {
216
243
  throw new Error("Missing signer for decryption");
217
244
  }
245
+ const signerAddress = (0, import_viem3.getAddress)(options.signer.address);
246
+ if (!(0, import_viem3.isAddressEqual)(signerAddress, (0, import_viem3.getAddress)(account))) {
247
+ const observer = await publicClient.readContract({
248
+ address: options.tokenAddress,
249
+ abi: confidentialTokenAbi,
250
+ functionName: "observer",
251
+ args: [account]
252
+ });
253
+ if (!observer || !(0, import_viem3.isAddressEqual)(observer, signerAddress)) {
254
+ throw new Error(confidentialErrorCodes.observerNotAuthorized);
255
+ }
256
+ }
218
257
  if (handle === ZERO_HANDLE) {
219
258
  return { handle, balance: 0n };
220
259
  }
@@ -275,8 +314,27 @@ async function viewConfidentialTransferAmounts(options) {
275
314
  throw new Error("Missing signer for decryption");
276
315
  }
277
316
  const signerAddress = (0, import_viem4.getAddress)(options.signer.address);
278
- if (signerAddress !== (0, import_viem4.getAddress)(entry.holder) && signerAddress !== (0, import_viem4.getAddress)(entry.payee)) {
279
- throw new Error("Signer must be the holder or payee to decrypt transfer amount");
317
+ const holderAddress = (0, import_viem4.getAddress)(entry.holder);
318
+ const payeeAddress = (0, import_viem4.getAddress)(entry.payee);
319
+ if (!(0, import_viem4.isAddressEqual)(signerAddress, holderAddress) && !(0, import_viem4.isAddressEqual)(signerAddress, payeeAddress)) {
320
+ const [holderObserver, payeeObserver] = await Promise.all([
321
+ publicClient.readContract({
322
+ address: tokenAddress,
323
+ abi: confidentialTokenAbi,
324
+ functionName: "observer",
325
+ args: [holderAddress]
326
+ }),
327
+ publicClient.readContract({
328
+ address: tokenAddress,
329
+ abi: confidentialTokenAbi,
330
+ functionName: "observer",
331
+ args: [payeeAddress]
332
+ })
333
+ ]);
334
+ const allowed = holderObserver && (0, import_viem4.isAddressEqual)(holderObserver, signerAddress) || payeeObserver && (0, import_viem4.isAddressEqual)(payeeObserver, signerAddress);
335
+ if (!allowed) {
336
+ throw new Error(confidentialErrorCodes.observerNotAuthorized);
337
+ }
280
338
  }
281
339
  entry.amount = await userDecryptEuint64(
282
340
  options.relayer,
@@ -294,9 +352,65 @@ async function viewConfidentialTransferAmounts(options) {
294
352
  }
295
353
  return transfers;
296
354
  }
355
+
356
+ // src/observer.ts
357
+ var import_viem5 = require("viem");
358
+ async function setObserver(options) {
359
+ if (!(0, import_viem5.isAddress)(options.tokenAddress)) {
360
+ throw new Error(`Invalid token address: ${options.tokenAddress}`);
361
+ }
362
+ if (!(0, import_viem5.isAddress)(options.account)) {
363
+ throw new Error(`Invalid account address: ${options.account}`);
364
+ }
365
+ if (!(0, import_viem5.isAddress)(options.observer)) {
366
+ throw new Error(`Invalid observer address: ${options.observer}`);
367
+ }
368
+ const tokenAddress = (0, import_viem5.getAddress)(options.tokenAddress);
369
+ const account = (0, import_viem5.getAddress)(options.account);
370
+ const observer = (0, import_viem5.getAddress)(options.observer);
371
+ const publicClient = (0, import_viem5.createPublicClient)({ transport: (0, import_viem5.http)(options.rpcUrl) });
372
+ const existing = await publicClient.readContract({
373
+ address: tokenAddress,
374
+ abi: confidentialTokenAbi,
375
+ functionName: "observer",
376
+ args: [account]
377
+ });
378
+ if (existing && (0, import_viem5.isAddress)(existing) && (0, import_viem5.getAddress)(existing) === observer) {
379
+ return "0x" + "00".repeat(32);
380
+ }
381
+ return options.signer.writeContract({
382
+ address: tokenAddress,
383
+ abi: confidentialTokenAbi,
384
+ functionName: "setObserver",
385
+ args: [account, observer]
386
+ });
387
+ }
388
+
389
+ // src/observer-web.ts
390
+ var import_viem6 = require("viem");
391
+ async function setObserverWeb(options) {
392
+ if (!(0, import_viem6.isAddress)(options.tokenAddress)) {
393
+ throw new Error(`Invalid token address: ${options.tokenAddress}`);
394
+ }
395
+ if (!(0, import_viem6.isAddress)(options.account)) {
396
+ throw new Error(`Invalid account address: ${options.account}`);
397
+ }
398
+ if (!(0, import_viem6.isAddress)(options.observer)) {
399
+ throw new Error(`Invalid observer address: ${options.observer}`);
400
+ }
401
+ return options.walletClient.writeContract({
402
+ address: (0, import_viem6.getAddress)(options.tokenAddress),
403
+ abi: confidentialTokenAbi,
404
+ functionName: "setObserver",
405
+ args: [(0, import_viem6.getAddress)(options.account), (0, import_viem6.getAddress)(options.observer)],
406
+ chain: options.walletClient.chain ?? null,
407
+ account: options.signer ?? options.walletClient.account ?? null
408
+ });
409
+ }
297
410
  // Annotate the CommonJS export names for ESM import in node:
298
411
  0 && (module.exports = {
299
412
  SepoliaConfig,
413
+ confidentialErrorCodes,
300
414
  confidentialPaymentTypes,
301
415
  confidentialTokenAbi,
302
416
  createEncryptedAmountInput,
@@ -306,6 +420,8 @@ async function viewConfidentialTransferAmounts(options) {
306
420
  initSDK,
307
421
  normalizeAmount,
308
422
  publicDecrypt,
423
+ setObserver,
424
+ setObserverWeb,
309
425
  userDecryptEuint64,
310
426
  viewConfidentialBalance,
311
427
  viewConfidentialTransferAmounts
package/dist/web.mjs CHANGED
@@ -1,16 +1,22 @@
1
1
  import {
2
+ confidentialErrorCodes,
2
3
  confidentialPaymentTypes,
3
4
  confidentialTokenAbi,
4
5
  createNonce,
5
6
  hashEncryptedAmountInput,
6
- normalizeAmount
7
- } from "./chunk-V7Z3OAXS.mjs";
7
+ normalizeAmount,
8
+ setObserver
9
+ } from "./chunk-LTE4V3LT.mjs";
8
10
 
9
11
  // src/relayer-web.ts
10
12
  import { createInstance, initSDK, SepoliaConfig } from "@zama-fhe/relayer-sdk/bundle";
11
13
  import { getAddress, toHex } from "viem";
12
14
  async function createRelayerInstance(config) {
13
- return createInstance(config);
15
+ const instance = await createInstance(config);
16
+ if (typeof config.network === "string") {
17
+ instance.network = config.network;
18
+ }
19
+ return instance;
14
20
  }
15
21
  async function createEncryptedAmountInput(relayer, contractAddress, senderAddress, amount) {
16
22
  const normalizedContract = getAddress(contractAddress);
@@ -58,7 +64,7 @@ async function publicDecrypt(relayer, handles) {
58
64
  }
59
65
 
60
66
  // src/balance-web.ts
61
- import { createPublicClient, http, isAddress } from "viem";
67
+ import { createPublicClient, getAddress as getAddress2, http, isAddress, isAddressEqual } from "viem";
62
68
  var ZERO_HANDLE = "0x" + "00".repeat(32);
63
69
  async function viewConfidentialBalance(options) {
64
70
  if (!isAddress(options.tokenAddress)) {
@@ -91,6 +97,18 @@ async function viewConfidentialBalance(options) {
91
97
  if (!options.signer) {
92
98
  throw new Error("Missing signer for decryption");
93
99
  }
100
+ const signerAddress = getAddress2(options.signer.address);
101
+ if (!isAddressEqual(signerAddress, getAddress2(account))) {
102
+ const observer = await publicClient.readContract({
103
+ address: options.tokenAddress,
104
+ abi: confidentialTokenAbi,
105
+ functionName: "observer",
106
+ args: [account]
107
+ });
108
+ if (!observer || !isAddressEqual(observer, signerAddress)) {
109
+ throw new Error(confidentialErrorCodes.observerNotAuthorized);
110
+ }
111
+ }
94
112
  if (handle === ZERO_HANDLE) {
95
113
  return { handle, balance: 0n };
96
114
  }
@@ -99,7 +117,7 @@ async function viewConfidentialBalance(options) {
99
117
  }
100
118
 
101
119
  // src/transfer-web.ts
102
- import { createPublicClient as createPublicClient2, decodeEventLog, getAddress as getAddress2, http as http2, isAddress as isAddress2, isHex } from "viem";
120
+ import { createPublicClient as createPublicClient2, decodeEventLog, getAddress as getAddress3, http as http2, isAddress as isAddress2, isHex, isAddressEqual as isAddressEqual2 } from "viem";
103
121
  async function viewConfidentialTransferAmounts(options) {
104
122
  if (!isAddress2(options.tokenAddress)) {
105
123
  throw new Error(`Invalid token address: ${options.tokenAddress}`);
@@ -117,10 +135,10 @@ async function viewConfidentialTransferAmounts(options) {
117
135
  const receipt = await publicClient.getTransactionReceipt({
118
136
  hash: options.txHash
119
137
  });
120
- const tokenAddress = getAddress2(options.tokenAddress);
121
- const fromFilter = options.from ? getAddress2(options.from) : void 0;
122
- const toFilter = options.to ? getAddress2(options.to) : void 0;
123
- const logs = receipt.logs.filter((log) => getAddress2(log.address) === tokenAddress);
138
+ const tokenAddress = getAddress3(options.tokenAddress);
139
+ const fromFilter = options.from ? getAddress3(options.from) : void 0;
140
+ const toFilter = options.to ? getAddress3(options.to) : void 0;
141
+ const logs = receipt.logs.filter((log) => getAddress3(log.address) === tokenAddress);
124
142
  const transfers = [];
125
143
  for (const log of logs) {
126
144
  try {
@@ -132,8 +150,8 @@ async function viewConfidentialTransferAmounts(options) {
132
150
  });
133
151
  const args = decoded.args;
134
152
  const entry = {
135
- holder: getAddress2(args.holder),
136
- payee: getAddress2(args.payee),
153
+ holder: getAddress3(args.holder),
154
+ payee: getAddress3(args.payee),
137
155
  maxClearAmount: BigInt(args.maxClearAmount),
138
156
  resourceHash: args.resourceHash,
139
157
  nonce: args.nonce,
@@ -150,9 +168,28 @@ async function viewConfidentialTransferAmounts(options) {
150
168
  if (!options.signer) {
151
169
  throw new Error("Missing signer for decryption");
152
170
  }
153
- const signerAddress = getAddress2(options.signer.address);
154
- if (signerAddress !== getAddress2(entry.holder) && signerAddress !== getAddress2(entry.payee)) {
155
- throw new Error("Signer must be the holder or payee to decrypt transfer amount");
171
+ const signerAddress = getAddress3(options.signer.address);
172
+ const holderAddress = getAddress3(entry.holder);
173
+ const payeeAddress = getAddress3(entry.payee);
174
+ if (!isAddressEqual2(signerAddress, holderAddress) && !isAddressEqual2(signerAddress, payeeAddress)) {
175
+ const [holderObserver, payeeObserver] = await Promise.all([
176
+ publicClient.readContract({
177
+ address: tokenAddress,
178
+ abi: confidentialTokenAbi,
179
+ functionName: "observer",
180
+ args: [holderAddress]
181
+ }),
182
+ publicClient.readContract({
183
+ address: tokenAddress,
184
+ abi: confidentialTokenAbi,
185
+ functionName: "observer",
186
+ args: [payeeAddress]
187
+ })
188
+ ]);
189
+ const allowed = holderObserver && isAddressEqual2(holderObserver, signerAddress) || payeeObserver && isAddressEqual2(payeeObserver, signerAddress);
190
+ if (!allowed) {
191
+ throw new Error(confidentialErrorCodes.observerNotAuthorized);
192
+ }
156
193
  }
157
194
  entry.amount = await userDecryptEuint64(
158
195
  options.relayer,
@@ -170,8 +207,31 @@ async function viewConfidentialTransferAmounts(options) {
170
207
  }
171
208
  return transfers;
172
209
  }
210
+
211
+ // src/observer-web.ts
212
+ import { getAddress as getAddress4, isAddress as isAddress3 } from "viem";
213
+ async function setObserverWeb(options) {
214
+ if (!isAddress3(options.tokenAddress)) {
215
+ throw new Error(`Invalid token address: ${options.tokenAddress}`);
216
+ }
217
+ if (!isAddress3(options.account)) {
218
+ throw new Error(`Invalid account address: ${options.account}`);
219
+ }
220
+ if (!isAddress3(options.observer)) {
221
+ throw new Error(`Invalid observer address: ${options.observer}`);
222
+ }
223
+ return options.walletClient.writeContract({
224
+ address: getAddress4(options.tokenAddress),
225
+ abi: confidentialTokenAbi,
226
+ functionName: "setObserver",
227
+ args: [getAddress4(options.account), getAddress4(options.observer)],
228
+ chain: options.walletClient.chain ?? null,
229
+ account: options.signer ?? options.walletClient.account ?? null
230
+ });
231
+ }
173
232
  export {
174
233
  SepoliaConfig,
234
+ confidentialErrorCodes,
175
235
  confidentialPaymentTypes,
176
236
  confidentialTokenAbi,
177
237
  createEncryptedAmountInput,
@@ -181,6 +241,8 @@ export {
181
241
  initSDK,
182
242
  normalizeAmount,
183
243
  publicDecrypt,
244
+ setObserver,
245
+ setObserverWeb,
184
246
  userDecryptEuint64,
185
247
  viewConfidentialBalance,
186
248
  viewConfidentialTransferAmounts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x402z-shared",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",