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