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/README.md +17 -23
- package/dist/chunk-7OLOM2AQ.mjs +155 -0
- package/dist/index.d.mts +60 -13
- package/dist/index.d.ts +60 -13
- package/dist/index.js +214 -80
- package/dist/index.mjs +212 -79
- package/dist/types-I3K7qzdc.d.mts +274 -0
- package/dist/types-I3K7qzdc.d.ts +274 -0
- package/dist/web.d.mts +21 -23
- package/dist/web.d.ts +21 -23
- package/dist/web.js +231 -112
- package/dist/web.mjs +195 -76
- package/package.json +1 -1
package/dist/web.mjs
CHANGED
|
@@ -4,12 +4,11 @@ import {
|
|
|
4
4
|
confidentialTokenAbi,
|
|
5
5
|
createNonce,
|
|
6
6
|
hashEncryptedAmountInput,
|
|
7
|
-
normalizeAmount
|
|
8
|
-
|
|
9
|
-
} from "./chunk-LTE4V3LT.mjs";
|
|
7
|
+
normalizeAmount
|
|
8
|
+
} from "./chunk-7OLOM2AQ.mjs";
|
|
10
9
|
|
|
11
10
|
// src/relayer-web.ts
|
|
12
|
-
import { createInstance, initSDK, SepoliaConfig } from "@zama-fhe/relayer-sdk/
|
|
11
|
+
import { createInstance, initSDK, SepoliaConfig } from "@zama-fhe/relayer-sdk/web";
|
|
13
12
|
import { getAddress, toHex } from "viem";
|
|
14
13
|
async function createRelayerInstance(config) {
|
|
15
14
|
const instance = await createInstance(config);
|
|
@@ -63,82 +62,29 @@ async function publicDecrypt(relayer, handles) {
|
|
|
63
62
|
};
|
|
64
63
|
}
|
|
65
64
|
|
|
66
|
-
// src/balance-web.ts
|
|
67
|
-
import { createPublicClient, getAddress as getAddress2, http, isAddress, isAddressEqual } from "viem";
|
|
68
|
-
var ZERO_HANDLE = "0x" + "00".repeat(32);
|
|
69
|
-
async function viewConfidentialBalance(options) {
|
|
70
|
-
if (!isAddress(options.tokenAddress)) {
|
|
71
|
-
throw new Error(`Invalid token address: ${options.tokenAddress}`);
|
|
72
|
-
}
|
|
73
|
-
const account = options.account ?? options.signer?.address;
|
|
74
|
-
if (!account || !isAddress(account)) {
|
|
75
|
-
throw new Error(`Invalid account address: ${account ?? "undefined"}`);
|
|
76
|
-
}
|
|
77
|
-
const publicClient = createPublicClient({ transport: http(options.rpcUrl) });
|
|
78
|
-
const confidentialBalanceAbi = [
|
|
79
|
-
{
|
|
80
|
-
inputs: [{ internalType: "address", name: "account", type: "address" }],
|
|
81
|
-
name: "confidentialBalanceOf",
|
|
82
|
-
outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
|
|
83
|
-
stateMutability: "view",
|
|
84
|
-
type: "function"
|
|
85
|
-
}
|
|
86
|
-
];
|
|
87
|
-
const handle = await publicClient.readContract({
|
|
88
|
-
address: options.tokenAddress,
|
|
89
|
-
abi: confidentialBalanceAbi,
|
|
90
|
-
functionName: "confidentialBalanceOf",
|
|
91
|
-
args: [account]
|
|
92
|
-
});
|
|
93
|
-
const shouldDecrypt = options.decrypt ?? true;
|
|
94
|
-
if (!shouldDecrypt) {
|
|
95
|
-
return { handle };
|
|
96
|
-
}
|
|
97
|
-
if (!options.signer) {
|
|
98
|
-
throw new Error("Missing signer for decryption");
|
|
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
|
-
}
|
|
112
|
-
if (handle === ZERO_HANDLE) {
|
|
113
|
-
return { handle, balance: 0n };
|
|
114
|
-
}
|
|
115
|
-
const balance = await userDecryptEuint64(options.relayer, handle, options.tokenAddress, options.signer);
|
|
116
|
-
return { handle, balance };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
65
|
// src/transfer-web.ts
|
|
120
|
-
import { createPublicClient
|
|
66
|
+
import { createPublicClient, decodeEventLog, getAddress as getAddress2, http, isAddress, isHex, isAddressEqual } from "viem";
|
|
121
67
|
async function viewConfidentialTransferAmounts(options) {
|
|
122
|
-
if (!
|
|
68
|
+
if (!isAddress(options.tokenAddress)) {
|
|
123
69
|
throw new Error(`Invalid token address: ${options.tokenAddress}`);
|
|
124
70
|
}
|
|
125
71
|
if (!isHex(options.txHash, { strict: true })) {
|
|
126
72
|
throw new Error(`Invalid transaction hash: ${options.txHash}`);
|
|
127
73
|
}
|
|
128
|
-
if (options.from && !
|
|
74
|
+
if (options.from && !isAddress(options.from)) {
|
|
129
75
|
throw new Error(`Invalid from address: ${options.from}`);
|
|
130
76
|
}
|
|
131
|
-
if (options.to && !
|
|
77
|
+
if (options.to && !isAddress(options.to)) {
|
|
132
78
|
throw new Error(`Invalid to address: ${options.to}`);
|
|
133
79
|
}
|
|
134
|
-
const publicClient =
|
|
80
|
+
const publicClient = createPublicClient({ transport: http(options.rpcUrl) });
|
|
135
81
|
const receipt = await publicClient.getTransactionReceipt({
|
|
136
82
|
hash: options.txHash
|
|
137
83
|
});
|
|
138
|
-
const tokenAddress =
|
|
139
|
-
const fromFilter = options.from ?
|
|
140
|
-
const toFilter = options.to ?
|
|
141
|
-
const logs = receipt.logs.filter((log) =>
|
|
84
|
+
const tokenAddress = getAddress2(options.tokenAddress);
|
|
85
|
+
const fromFilter = options.from ? getAddress2(options.from) : void 0;
|
|
86
|
+
const toFilter = options.to ? getAddress2(options.to) : void 0;
|
|
87
|
+
const logs = receipt.logs.filter((log) => getAddress2(log.address) === tokenAddress);
|
|
142
88
|
const transfers = [];
|
|
143
89
|
for (const log of logs) {
|
|
144
90
|
try {
|
|
@@ -150,8 +96,8 @@ async function viewConfidentialTransferAmounts(options) {
|
|
|
150
96
|
});
|
|
151
97
|
const args = decoded.args;
|
|
152
98
|
const entry = {
|
|
153
|
-
holder:
|
|
154
|
-
payee:
|
|
99
|
+
holder: getAddress2(args.holder),
|
|
100
|
+
payee: getAddress2(args.payee),
|
|
155
101
|
maxClearAmount: BigInt(args.maxClearAmount),
|
|
156
102
|
resourceHash: args.resourceHash,
|
|
157
103
|
nonce: args.nonce,
|
|
@@ -168,10 +114,10 @@ async function viewConfidentialTransferAmounts(options) {
|
|
|
168
114
|
if (!options.signer) {
|
|
169
115
|
throw new Error("Missing signer for decryption");
|
|
170
116
|
}
|
|
171
|
-
const signerAddress =
|
|
172
|
-
const holderAddress =
|
|
173
|
-
const payeeAddress =
|
|
174
|
-
if (!
|
|
117
|
+
const signerAddress = getAddress2(options.signer.address);
|
|
118
|
+
const holderAddress = getAddress2(entry.holder);
|
|
119
|
+
const payeeAddress = getAddress2(entry.payee);
|
|
120
|
+
if (!isAddressEqual(signerAddress, holderAddress) && !isAddressEqual(signerAddress, payeeAddress)) {
|
|
175
121
|
const [holderObserver, payeeObserver] = await Promise.all([
|
|
176
122
|
publicClient.readContract({
|
|
177
123
|
address: tokenAddress,
|
|
@@ -186,7 +132,7 @@ async function viewConfidentialTransferAmounts(options) {
|
|
|
186
132
|
args: [payeeAddress]
|
|
187
133
|
})
|
|
188
134
|
]);
|
|
189
|
-
const allowed = holderObserver &&
|
|
135
|
+
const allowed = holderObserver && isAddressEqual(holderObserver, signerAddress) || payeeObserver && isAddressEqual(payeeObserver, signerAddress);
|
|
190
136
|
if (!allowed) {
|
|
191
137
|
throw new Error(confidentialErrorCodes.observerNotAuthorized);
|
|
192
138
|
}
|
|
@@ -208,6 +154,62 @@ async function viewConfidentialTransferAmounts(options) {
|
|
|
208
154
|
return transfers;
|
|
209
155
|
}
|
|
210
156
|
|
|
157
|
+
// src/token-web.ts
|
|
158
|
+
import { getAddress as getAddress5, isAddress as isAddress4 } from "viem";
|
|
159
|
+
|
|
160
|
+
// src/balance-web.ts
|
|
161
|
+
import { createPublicClient as createPublicClient2, getAddress as getAddress3, http as http2, isAddress as isAddress2, isAddressEqual as isAddressEqual2 } from "viem";
|
|
162
|
+
var ZERO_HANDLE = "0x" + "00".repeat(32);
|
|
163
|
+
async function viewConfidentialBalance(options) {
|
|
164
|
+
if (!isAddress2(options.tokenAddress)) {
|
|
165
|
+
throw new Error(`Invalid token address: ${options.tokenAddress}`);
|
|
166
|
+
}
|
|
167
|
+
const account = options.account ?? options.signer?.address;
|
|
168
|
+
if (!account || !isAddress2(account)) {
|
|
169
|
+
throw new Error(`Invalid account address: ${account ?? "undefined"}`);
|
|
170
|
+
}
|
|
171
|
+
const publicClient = createPublicClient2({ transport: http2(options.rpcUrl) });
|
|
172
|
+
const confidentialBalanceAbi = [
|
|
173
|
+
{
|
|
174
|
+
inputs: [{ internalType: "address", name: "account", type: "address" }],
|
|
175
|
+
name: "confidentialBalanceOf",
|
|
176
|
+
outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
|
|
177
|
+
stateMutability: "view",
|
|
178
|
+
type: "function"
|
|
179
|
+
}
|
|
180
|
+
];
|
|
181
|
+
const handle = await publicClient.readContract({
|
|
182
|
+
address: options.tokenAddress,
|
|
183
|
+
abi: confidentialBalanceAbi,
|
|
184
|
+
functionName: "confidentialBalanceOf",
|
|
185
|
+
args: [account]
|
|
186
|
+
});
|
|
187
|
+
const shouldDecrypt = options.decrypt ?? true;
|
|
188
|
+
if (!shouldDecrypt) {
|
|
189
|
+
return { handle };
|
|
190
|
+
}
|
|
191
|
+
if (!options.signer) {
|
|
192
|
+
throw new Error("Missing signer for decryption");
|
|
193
|
+
}
|
|
194
|
+
const signerAddress = getAddress3(options.signer.address);
|
|
195
|
+
if (!isAddressEqual2(signerAddress, getAddress3(account))) {
|
|
196
|
+
const observer = await publicClient.readContract({
|
|
197
|
+
address: options.tokenAddress,
|
|
198
|
+
abi: confidentialTokenAbi,
|
|
199
|
+
functionName: "observer",
|
|
200
|
+
args: [account]
|
|
201
|
+
});
|
|
202
|
+
if (!observer || !isAddressEqual2(observer, signerAddress)) {
|
|
203
|
+
throw new Error(confidentialErrorCodes.observerNotAuthorized);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (handle === ZERO_HANDLE) {
|
|
207
|
+
return { handle, balance: 0n };
|
|
208
|
+
}
|
|
209
|
+
const balance = await userDecryptEuint64(options.relayer, handle, options.tokenAddress, options.signer);
|
|
210
|
+
return { handle, balance };
|
|
211
|
+
}
|
|
212
|
+
|
|
211
213
|
// src/observer-web.ts
|
|
212
214
|
import { getAddress as getAddress4, isAddress as isAddress3 } from "viem";
|
|
213
215
|
async function setObserverWeb(options) {
|
|
@@ -229,10 +231,130 @@ async function setObserverWeb(options) {
|
|
|
229
231
|
account: options.signer ?? options.walletClient.account ?? null
|
|
230
232
|
});
|
|
231
233
|
}
|
|
234
|
+
|
|
235
|
+
// src/token-web.ts
|
|
236
|
+
function assertAddress(value, label) {
|
|
237
|
+
if (!isAddress4(value)) {
|
|
238
|
+
throw new Error(`Invalid ${label}: ${value}`);
|
|
239
|
+
}
|
|
240
|
+
return getAddress5(value);
|
|
241
|
+
}
|
|
242
|
+
function resolveSignerAddress(signer, walletClient) {
|
|
243
|
+
if (signer?.address) {
|
|
244
|
+
return assertAddress(signer.address, "signer address");
|
|
245
|
+
}
|
|
246
|
+
const account = walletClient.account;
|
|
247
|
+
if (typeof account === "string") {
|
|
248
|
+
return assertAddress(account, "wallet address");
|
|
249
|
+
}
|
|
250
|
+
if (account && typeof account === "object" && "address" in account && account.address) {
|
|
251
|
+
return assertAddress(account.address, "wallet address");
|
|
252
|
+
}
|
|
253
|
+
throw new Error("Missing signer address");
|
|
254
|
+
}
|
|
255
|
+
function resolveWriteAccount(signer, walletClient) {
|
|
256
|
+
if (signer?.address) {
|
|
257
|
+
return assertAddress(signer.address, "signer address");
|
|
258
|
+
}
|
|
259
|
+
const account = walletClient.account;
|
|
260
|
+
if (typeof account === "string") {
|
|
261
|
+
return assertAddress(account, "wallet address");
|
|
262
|
+
}
|
|
263
|
+
return account ?? null;
|
|
264
|
+
}
|
|
265
|
+
var ConfidentialTokenWeb = class {
|
|
266
|
+
constructor(config) {
|
|
267
|
+
this.rpcUrl = config.rpcUrl;
|
|
268
|
+
this.tokenAddress = config.tokenAddress;
|
|
269
|
+
this.relayer = config.relayer;
|
|
270
|
+
this.walletClient = config.walletClient;
|
|
271
|
+
this.signer = config.signer;
|
|
272
|
+
}
|
|
273
|
+
async balanceOf(account) {
|
|
274
|
+
const result = await viewConfidentialBalance({
|
|
275
|
+
rpcUrl: this.rpcUrl,
|
|
276
|
+
tokenAddress: this.tokenAddress,
|
|
277
|
+
account,
|
|
278
|
+
relayer: this.relayer,
|
|
279
|
+
signer: this.signer,
|
|
280
|
+
decrypt: true
|
|
281
|
+
});
|
|
282
|
+
return result.balance ?? 0n;
|
|
283
|
+
}
|
|
284
|
+
async setObserver(account, observer) {
|
|
285
|
+
return setObserverWeb({
|
|
286
|
+
tokenAddress: this.tokenAddress,
|
|
287
|
+
account,
|
|
288
|
+
observer,
|
|
289
|
+
signer: this.signer?.address ? assertAddress(this.signer.address, "signer address") : void 0,
|
|
290
|
+
walletClient: this.walletClient
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
async transfer(to, amount) {
|
|
294
|
+
const tokenAddress = assertAddress(this.tokenAddress, "token address");
|
|
295
|
+
const toAddress = assertAddress(to, "recipient address");
|
|
296
|
+
const fromAddress = resolveSignerAddress(this.signer, this.walletClient);
|
|
297
|
+
if (!Number.isSafeInteger(amount)) {
|
|
298
|
+
throw new Error("Transfer amount must be a safe integer");
|
|
299
|
+
}
|
|
300
|
+
const { handle, inputProof } = await createEncryptedAmountInput(
|
|
301
|
+
this.relayer,
|
|
302
|
+
tokenAddress,
|
|
303
|
+
fromAddress,
|
|
304
|
+
amount
|
|
305
|
+
);
|
|
306
|
+
return this.walletClient.writeContract({
|
|
307
|
+
address: tokenAddress,
|
|
308
|
+
abi: confidentialTokenAbi,
|
|
309
|
+
functionName: "confidentialTransfer",
|
|
310
|
+
args: [toAddress, handle, inputProof],
|
|
311
|
+
chain: this.walletClient.chain ?? null,
|
|
312
|
+
account: resolveWriteAccount(this.signer, this.walletClient)
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
async wrap(to, amount) {
|
|
316
|
+
const tokenAddress = assertAddress(this.tokenAddress, "token address");
|
|
317
|
+
const toAddress = assertAddress(to, "recipient address");
|
|
318
|
+
const normalizedAmount = BigInt(normalizeAmount(amount));
|
|
319
|
+
return this.walletClient.writeContract({
|
|
320
|
+
address: tokenAddress,
|
|
321
|
+
abi: confidentialTokenAbi,
|
|
322
|
+
functionName: "wrap",
|
|
323
|
+
args: [toAddress, normalizedAmount],
|
|
324
|
+
chain: this.walletClient.chain ?? null,
|
|
325
|
+
account: resolveWriteAccount(this.signer, this.walletClient)
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
async unwrap(from, to, amount) {
|
|
329
|
+
const tokenAddress = assertAddress(this.tokenAddress, "token address");
|
|
330
|
+
const fromAddress = assertAddress(from, "from address");
|
|
331
|
+
const toAddress = assertAddress(to, "to address");
|
|
332
|
+
const signerAddress = resolveSignerAddress(this.signer, this.walletClient);
|
|
333
|
+
if (!Number.isSafeInteger(amount)) {
|
|
334
|
+
throw new Error("Unwrap amount must be a safe integer");
|
|
335
|
+
}
|
|
336
|
+
const { handle, inputProof } = await createEncryptedAmountInput(
|
|
337
|
+
this.relayer,
|
|
338
|
+
tokenAddress,
|
|
339
|
+
signerAddress,
|
|
340
|
+
amount
|
|
341
|
+
);
|
|
342
|
+
return this.walletClient.writeContract({
|
|
343
|
+
address: tokenAddress,
|
|
344
|
+
abi: confidentialTokenAbi,
|
|
345
|
+
functionName: "unwrap",
|
|
346
|
+
args: [fromAddress, toAddress, handle, inputProof],
|
|
347
|
+
chain: this.walletClient.chain ?? null,
|
|
348
|
+
account: resolveWriteAccount(this.signer, this.walletClient)
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
};
|
|
232
352
|
export {
|
|
353
|
+
ConfidentialTokenWeb,
|
|
233
354
|
SepoliaConfig,
|
|
234
355
|
confidentialErrorCodes,
|
|
235
356
|
confidentialPaymentTypes,
|
|
357
|
+
ConfidentialTokenWeb as confidentialToken,
|
|
236
358
|
confidentialTokenAbi,
|
|
237
359
|
createEncryptedAmountInput,
|
|
238
360
|
createNonce,
|
|
@@ -241,9 +363,6 @@ export {
|
|
|
241
363
|
initSDK,
|
|
242
364
|
normalizeAmount,
|
|
243
365
|
publicDecrypt,
|
|
244
|
-
setObserver,
|
|
245
|
-
setObserverWeb,
|
|
246
366
|
userDecryptEuint64,
|
|
247
|
-
viewConfidentialBalance,
|
|
248
367
|
viewConfidentialTransferAmounts
|
|
249
368
|
};
|