x402z-shared 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/web.js CHANGED
@@ -20,27 +20,37 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/web.ts
21
21
  var web_exports = {};
22
22
  __export(web_exports, {
23
- SepoliaConfig: () => import_bundle.SepoliaConfig,
23
+ ConfidentialTokenWeb: () => ConfidentialTokenWeb,
24
+ SepoliaConfig: () => import_web.SepoliaConfig,
24
25
  confidentialErrorCodes: () => confidentialErrorCodes,
25
26
  confidentialPaymentTypes: () => confidentialPaymentTypes,
27
+ confidentialToken: () => ConfidentialTokenWeb,
26
28
  confidentialTokenAbi: () => confidentialTokenAbi,
27
29
  createEncryptedAmountInput: () => createEncryptedAmountInput,
28
30
  createNonce: () => createNonce,
29
31
  createRelayerInstance: () => createRelayerInstance,
30
32
  hashEncryptedAmountInput: () => hashEncryptedAmountInput,
31
- initSDK: () => import_bundle.initSDK,
33
+ initSDK: () => import_web.initSDK,
32
34
  normalizeAmount: () => normalizeAmount,
33
35
  publicDecrypt: () => publicDecrypt,
34
- setObserver: () => setObserver,
35
- setObserverWeb: () => setObserverWeb,
36
36
  userDecryptEuint64: () => userDecryptEuint64,
37
- viewConfidentialBalance: () => viewConfidentialBalance,
38
37
  viewConfidentialTransferAmounts: () => viewConfidentialTransferAmounts
39
38
  });
40
39
  module.exports = __toCommonJS(web_exports);
41
40
 
42
41
  // src/abi.ts
43
42
  var confidentialTokenAbi = [
43
+ {
44
+ inputs: [
45
+ { internalType: "address", name: "to", type: "address" },
46
+ { internalType: "bytes32", name: "encryptedAmountInput", type: "bytes32" },
47
+ { internalType: "bytes", name: "inputProof", type: "bytes" }
48
+ ],
49
+ name: "confidentialTransfer",
50
+ outputs: [{ internalType: "euint64", name: "transferred", type: "bytes32" }],
51
+ stateMutability: "nonpayable",
52
+ type: "function"
53
+ },
44
54
  {
45
55
  inputs: [
46
56
  {
@@ -67,6 +77,28 @@ var confidentialTokenAbi = [
67
77
  stateMutability: "nonpayable",
68
78
  type: "function"
69
79
  },
80
+ {
81
+ inputs: [
82
+ { internalType: "address", name: "to", type: "address" },
83
+ { internalType: "uint256", name: "amount", type: "uint256" }
84
+ ],
85
+ name: "wrap",
86
+ outputs: [],
87
+ stateMutability: "nonpayable",
88
+ type: "function"
89
+ },
90
+ {
91
+ inputs: [
92
+ { internalType: "address", name: "from", type: "address" },
93
+ { internalType: "address", name: "to", type: "address" },
94
+ { internalType: "bytes32", name: "encryptedAmountInput", type: "bytes32" },
95
+ { internalType: "bytes", name: "inputProof", type: "bytes" }
96
+ ],
97
+ name: "unwrap",
98
+ outputs: [],
99
+ stateMutability: "nonpayable",
100
+ type: "function"
101
+ },
70
102
  {
71
103
  anonymous: false,
72
104
  inputs: [
@@ -154,10 +186,10 @@ function normalizeAmount(amount) {
154
186
  }
155
187
 
156
188
  // src/relayer-web.ts
157
- var import_bundle = require("@zama-fhe/relayer-sdk/bundle");
189
+ var import_web = require("@zama-fhe/relayer-sdk/web");
158
190
  var import_viem2 = require("viem");
159
191
  async function createRelayerInstance(config) {
160
- const instance = await (0, import_bundle.createInstance)(config);
192
+ const instance = await (0, import_web.createInstance)(config);
161
193
  if (typeof config.network === "string") {
162
194
  instance.network = config.network;
163
195
  }
@@ -208,86 +240,33 @@ async function publicDecrypt(relayer, handles) {
208
240
  };
209
241
  }
210
242
 
211
- // src/balance-web.ts
212
- var import_viem3 = require("viem");
213
- var ZERO_HANDLE = "0x" + "00".repeat(32);
214
- async function viewConfidentialBalance(options) {
215
- if (!(0, import_viem3.isAddress)(options.tokenAddress)) {
216
- throw new Error(`Invalid token address: ${options.tokenAddress}`);
217
- }
218
- const account = options.account ?? options.signer?.address;
219
- if (!account || !(0, import_viem3.isAddress)(account)) {
220
- throw new Error(`Invalid account address: ${account ?? "undefined"}`);
221
- }
222
- const publicClient = (0, import_viem3.createPublicClient)({ transport: (0, import_viem3.http)(options.rpcUrl) });
223
- const confidentialBalanceAbi = [
224
- {
225
- inputs: [{ internalType: "address", name: "account", type: "address" }],
226
- name: "confidentialBalanceOf",
227
- outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
228
- stateMutability: "view",
229
- type: "function"
230
- }
231
- ];
232
- const handle = await publicClient.readContract({
233
- address: options.tokenAddress,
234
- abi: confidentialBalanceAbi,
235
- functionName: "confidentialBalanceOf",
236
- args: [account]
237
- });
238
- const shouldDecrypt = options.decrypt ?? true;
239
- if (!shouldDecrypt) {
240
- return { handle };
241
- }
242
- if (!options.signer) {
243
- throw new Error("Missing signer for decryption");
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
- }
257
- if (handle === ZERO_HANDLE) {
258
- return { handle, balance: 0n };
259
- }
260
- const balance = await userDecryptEuint64(options.relayer, handle, options.tokenAddress, options.signer);
261
- return { handle, balance };
262
- }
263
-
264
243
  // src/transfer-web.ts
265
- var import_viem4 = require("viem");
244
+ var import_viem3 = require("viem");
266
245
  async function viewConfidentialTransferAmounts(options) {
267
- if (!(0, import_viem4.isAddress)(options.tokenAddress)) {
246
+ if (!(0, import_viem3.isAddress)(options.tokenAddress)) {
268
247
  throw new Error(`Invalid token address: ${options.tokenAddress}`);
269
248
  }
270
- if (!(0, import_viem4.isHex)(options.txHash, { strict: true })) {
249
+ if (!(0, import_viem3.isHex)(options.txHash, { strict: true })) {
271
250
  throw new Error(`Invalid transaction hash: ${options.txHash}`);
272
251
  }
273
- if (options.from && !(0, import_viem4.isAddress)(options.from)) {
252
+ if (options.from && !(0, import_viem3.isAddress)(options.from)) {
274
253
  throw new Error(`Invalid from address: ${options.from}`);
275
254
  }
276
- if (options.to && !(0, import_viem4.isAddress)(options.to)) {
255
+ if (options.to && !(0, import_viem3.isAddress)(options.to)) {
277
256
  throw new Error(`Invalid to address: ${options.to}`);
278
257
  }
279
- const publicClient = (0, import_viem4.createPublicClient)({ transport: (0, import_viem4.http)(options.rpcUrl) });
258
+ const publicClient = (0, import_viem3.createPublicClient)({ transport: (0, import_viem3.http)(options.rpcUrl) });
280
259
  const receipt = await publicClient.getTransactionReceipt({
281
260
  hash: options.txHash
282
261
  });
283
- const tokenAddress = (0, import_viem4.getAddress)(options.tokenAddress);
284
- const fromFilter = options.from ? (0, import_viem4.getAddress)(options.from) : void 0;
285
- const toFilter = options.to ? (0, import_viem4.getAddress)(options.to) : void 0;
286
- const logs = receipt.logs.filter((log) => (0, import_viem4.getAddress)(log.address) === tokenAddress);
262
+ const tokenAddress = (0, import_viem3.getAddress)(options.tokenAddress);
263
+ const fromFilter = options.from ? (0, import_viem3.getAddress)(options.from) : void 0;
264
+ const toFilter = options.to ? (0, import_viem3.getAddress)(options.to) : void 0;
265
+ const logs = receipt.logs.filter((log) => (0, import_viem3.getAddress)(log.address) === tokenAddress);
287
266
  const transfers = [];
288
267
  for (const log of logs) {
289
268
  try {
290
- const decoded = (0, import_viem4.decodeEventLog)({
269
+ const decoded = (0, import_viem3.decodeEventLog)({
291
270
  abi: confidentialTokenAbi,
292
271
  eventName: "ConfidentialPaymentExecuted",
293
272
  data: log.data,
@@ -295,8 +274,8 @@ async function viewConfidentialTransferAmounts(options) {
295
274
  });
296
275
  const args = decoded.args;
297
276
  const entry = {
298
- holder: (0, import_viem4.getAddress)(args.holder),
299
- payee: (0, import_viem4.getAddress)(args.payee),
277
+ holder: (0, import_viem3.getAddress)(args.holder),
278
+ payee: (0, import_viem3.getAddress)(args.payee),
300
279
  maxClearAmount: BigInt(args.maxClearAmount),
301
280
  resourceHash: args.resourceHash,
302
281
  nonce: args.nonce,
@@ -313,10 +292,10 @@ async function viewConfidentialTransferAmounts(options) {
313
292
  if (!options.signer) {
314
293
  throw new Error("Missing signer for decryption");
315
294
  }
316
- const signerAddress = (0, import_viem4.getAddress)(options.signer.address);
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)) {
295
+ const signerAddress = (0, import_viem3.getAddress)(options.signer.address);
296
+ const holderAddress = (0, import_viem3.getAddress)(entry.holder);
297
+ const payeeAddress = (0, import_viem3.getAddress)(entry.payee);
298
+ if (!(0, import_viem3.isAddressEqual)(signerAddress, holderAddress) && !(0, import_viem3.isAddressEqual)(signerAddress, payeeAddress)) {
320
299
  const [holderObserver, payeeObserver] = await Promise.all([
321
300
  publicClient.readContract({
322
301
  address: tokenAddress,
@@ -331,7 +310,7 @@ async function viewConfidentialTransferAmounts(options) {
331
310
  args: [payeeAddress]
332
311
  })
333
312
  ]);
334
- const allowed = holderObserver && (0, import_viem4.isAddressEqual)(holderObserver, signerAddress) || payeeObserver && (0, import_viem4.isAddressEqual)(payeeObserver, signerAddress);
313
+ const allowed = holderObserver && (0, import_viem3.isAddressEqual)(holderObserver, signerAddress) || payeeObserver && (0, import_viem3.isAddressEqual)(payeeObserver, signerAddress);
335
314
  if (!allowed) {
336
315
  throw new Error(confidentialErrorCodes.observerNotAuthorized);
337
316
  }
@@ -353,65 +332,208 @@ async function viewConfidentialTransferAmounts(options) {
353
332
  return transfers;
354
333
  }
355
334
 
356
- // src/observer.ts
357
- var import_viem5 = require("viem");
358
- async function setObserver(options) {
359
- if (!(0, import_viem5.isAddress)(options.tokenAddress)) {
335
+ // src/token-web.ts
336
+ var import_viem6 = require("viem");
337
+
338
+ // src/balance-web.ts
339
+ var import_viem4 = require("viem");
340
+ var ZERO_HANDLE = "0x" + "00".repeat(32);
341
+ async function viewConfidentialBalance(options) {
342
+ if (!(0, import_viem4.isAddress)(options.tokenAddress)) {
360
343
  throw new Error(`Invalid token address: ${options.tokenAddress}`);
361
344
  }
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}`);
345
+ const account = options.account ?? options.signer?.address;
346
+ if (!account || !(0, import_viem4.isAddress)(account)) {
347
+ throw new Error(`Invalid account address: ${account ?? "undefined"}`);
367
348
  }
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",
349
+ const publicClient = (0, import_viem4.createPublicClient)({ transport: (0, import_viem4.http)(options.rpcUrl) });
350
+ const confidentialBalanceAbi = [
351
+ {
352
+ inputs: [{ internalType: "address", name: "account", type: "address" }],
353
+ name: "confidentialBalanceOf",
354
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
355
+ stateMutability: "view",
356
+ type: "function"
357
+ }
358
+ ];
359
+ const handle = await publicClient.readContract({
360
+ address: options.tokenAddress,
361
+ abi: confidentialBalanceAbi,
362
+ functionName: "confidentialBalanceOf",
376
363
  args: [account]
377
364
  });
378
- if (existing && (0, import_viem5.isAddress)(existing) && (0, import_viem5.getAddress)(existing) === observer) {
379
- return "0x" + "00".repeat(32);
365
+ const shouldDecrypt = options.decrypt ?? true;
366
+ if (!shouldDecrypt) {
367
+ return { handle };
380
368
  }
381
- return options.signer.writeContract({
382
- address: tokenAddress,
383
- abi: confidentialTokenAbi,
384
- functionName: "setObserver",
385
- args: [account, observer]
386
- });
369
+ if (!options.signer) {
370
+ throw new Error("Missing signer for decryption");
371
+ }
372
+ const signerAddress = (0, import_viem4.getAddress)(options.signer.address);
373
+ if (!(0, import_viem4.isAddressEqual)(signerAddress, (0, import_viem4.getAddress)(account))) {
374
+ const observer = await publicClient.readContract({
375
+ address: options.tokenAddress,
376
+ abi: confidentialTokenAbi,
377
+ functionName: "observer",
378
+ args: [account]
379
+ });
380
+ if (!observer || !(0, import_viem4.isAddressEqual)(observer, signerAddress)) {
381
+ throw new Error(confidentialErrorCodes.observerNotAuthorized);
382
+ }
383
+ }
384
+ if (handle === ZERO_HANDLE) {
385
+ return { handle, balance: 0n };
386
+ }
387
+ const balance = await userDecryptEuint64(options.relayer, handle, options.tokenAddress, options.signer);
388
+ return { handle, balance };
387
389
  }
388
390
 
389
391
  // src/observer-web.ts
390
- var import_viem6 = require("viem");
392
+ var import_viem5 = require("viem");
391
393
  async function setObserverWeb(options) {
392
- if (!(0, import_viem6.isAddress)(options.tokenAddress)) {
394
+ if (!(0, import_viem5.isAddress)(options.tokenAddress)) {
393
395
  throw new Error(`Invalid token address: ${options.tokenAddress}`);
394
396
  }
395
- if (!(0, import_viem6.isAddress)(options.account)) {
397
+ if (!(0, import_viem5.isAddress)(options.account)) {
396
398
  throw new Error(`Invalid account address: ${options.account}`);
397
399
  }
398
- if (!(0, import_viem6.isAddress)(options.observer)) {
400
+ if (!(0, import_viem5.isAddress)(options.observer)) {
399
401
  throw new Error(`Invalid observer address: ${options.observer}`);
400
402
  }
401
403
  return options.walletClient.writeContract({
402
- address: (0, import_viem6.getAddress)(options.tokenAddress),
404
+ address: (0, import_viem5.getAddress)(options.tokenAddress),
403
405
  abi: confidentialTokenAbi,
404
406
  functionName: "setObserver",
405
- args: [(0, import_viem6.getAddress)(options.account), (0, import_viem6.getAddress)(options.observer)],
407
+ args: [(0, import_viem5.getAddress)(options.account), (0, import_viem5.getAddress)(options.observer)],
406
408
  chain: options.walletClient.chain ?? null,
407
409
  account: options.signer ?? options.walletClient.account ?? null
408
410
  });
409
411
  }
412
+
413
+ // src/token-web.ts
414
+ function assertAddress(value, label) {
415
+ if (!(0, import_viem6.isAddress)(value)) {
416
+ throw new Error(`Invalid ${label}: ${value}`);
417
+ }
418
+ return (0, import_viem6.getAddress)(value);
419
+ }
420
+ function resolveSignerAddress(signer, walletClient) {
421
+ if (signer?.address) {
422
+ return assertAddress(signer.address, "signer address");
423
+ }
424
+ const account = walletClient.account;
425
+ if (typeof account === "string") {
426
+ return assertAddress(account, "wallet address");
427
+ }
428
+ if (account && typeof account === "object" && "address" in account && account.address) {
429
+ return assertAddress(account.address, "wallet address");
430
+ }
431
+ throw new Error("Missing signer address");
432
+ }
433
+ function resolveWriteAccount(signer, walletClient) {
434
+ if (signer?.address) {
435
+ return assertAddress(signer.address, "signer address");
436
+ }
437
+ const account = walletClient.account;
438
+ if (typeof account === "string") {
439
+ return assertAddress(account, "wallet address");
440
+ }
441
+ return account ?? null;
442
+ }
443
+ var ConfidentialTokenWeb = class {
444
+ constructor(config) {
445
+ this.rpcUrl = config.rpcUrl;
446
+ this.tokenAddress = config.tokenAddress;
447
+ this.relayer = config.relayer;
448
+ this.walletClient = config.walletClient;
449
+ this.signer = config.signer;
450
+ }
451
+ async balanceOf(account) {
452
+ const result = await viewConfidentialBalance({
453
+ rpcUrl: this.rpcUrl,
454
+ tokenAddress: this.tokenAddress,
455
+ account,
456
+ relayer: this.relayer,
457
+ signer: this.signer,
458
+ decrypt: true
459
+ });
460
+ return result.balance ?? 0n;
461
+ }
462
+ async setObserver(account, observer) {
463
+ return setObserverWeb({
464
+ tokenAddress: this.tokenAddress,
465
+ account,
466
+ observer,
467
+ signer: this.signer?.address ? assertAddress(this.signer.address, "signer address") : void 0,
468
+ walletClient: this.walletClient
469
+ });
470
+ }
471
+ async transfer(to, amount) {
472
+ const tokenAddress = assertAddress(this.tokenAddress, "token address");
473
+ const toAddress = assertAddress(to, "recipient address");
474
+ const fromAddress = resolveSignerAddress(this.signer, this.walletClient);
475
+ if (!Number.isSafeInteger(amount)) {
476
+ throw new Error("Transfer amount must be a safe integer");
477
+ }
478
+ const { handle, inputProof } = await createEncryptedAmountInput(
479
+ this.relayer,
480
+ tokenAddress,
481
+ fromAddress,
482
+ amount
483
+ );
484
+ return this.walletClient.writeContract({
485
+ address: tokenAddress,
486
+ abi: confidentialTokenAbi,
487
+ functionName: "confidentialTransfer",
488
+ args: [toAddress, handle, inputProof],
489
+ chain: this.walletClient.chain ?? null,
490
+ account: resolveWriteAccount(this.signer, this.walletClient)
491
+ });
492
+ }
493
+ async wrap(to, amount) {
494
+ const tokenAddress = assertAddress(this.tokenAddress, "token address");
495
+ const toAddress = assertAddress(to, "recipient address");
496
+ const normalizedAmount = BigInt(normalizeAmount(amount));
497
+ return this.walletClient.writeContract({
498
+ address: tokenAddress,
499
+ abi: confidentialTokenAbi,
500
+ functionName: "wrap",
501
+ args: [toAddress, normalizedAmount],
502
+ chain: this.walletClient.chain ?? null,
503
+ account: resolveWriteAccount(this.signer, this.walletClient)
504
+ });
505
+ }
506
+ async unwrap(from, to, amount) {
507
+ const tokenAddress = assertAddress(this.tokenAddress, "token address");
508
+ const fromAddress = assertAddress(from, "from address");
509
+ const toAddress = assertAddress(to, "to address");
510
+ const signerAddress = resolveSignerAddress(this.signer, this.walletClient);
511
+ if (!Number.isSafeInteger(amount)) {
512
+ throw new Error("Unwrap amount must be a safe integer");
513
+ }
514
+ const { handle, inputProof } = await createEncryptedAmountInput(
515
+ this.relayer,
516
+ tokenAddress,
517
+ signerAddress,
518
+ amount
519
+ );
520
+ return this.walletClient.writeContract({
521
+ address: tokenAddress,
522
+ abi: confidentialTokenAbi,
523
+ functionName: "unwrap",
524
+ args: [fromAddress, toAddress, handle, inputProof],
525
+ chain: this.walletClient.chain ?? null,
526
+ account: resolveWriteAccount(this.signer, this.walletClient)
527
+ });
528
+ }
529
+ };
410
530
  // Annotate the CommonJS export names for ESM import in node:
411
531
  0 && (module.exports = {
532
+ ConfidentialTokenWeb,
412
533
  SepoliaConfig,
413
534
  confidentialErrorCodes,
414
535
  confidentialPaymentTypes,
536
+ confidentialToken,
415
537
  confidentialTokenAbi,
416
538
  createEncryptedAmountInput,
417
539
  createNonce,
@@ -420,9 +542,6 @@ async function setObserverWeb(options) {
420
542
  initSDK,
421
543
  normalizeAmount,
422
544
  publicDecrypt,
423
- setObserver,
424
- setObserverWeb,
425
545
  userDecryptEuint64,
426
- viewConfidentialBalance,
427
546
  viewConfidentialTransferAmounts
428
547
  });