@vaultkeepr/smart-account 0.1.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/LICENSE +21 -0
- package/dist/index.d.mts +159 -0
- package/dist/index.d.ts +159 -0
- package/dist/index.js +684 -0
- package/dist/index.mjs +635 -0
- package/package.json +41 -0
- package/src/index.ts +73 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,684 @@
|
|
|
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
|
+
CID_REGISTRY_ABI: () => CID_REGISTRY_ABI,
|
|
24
|
+
clearIdentity: () => clearIdentity,
|
|
25
|
+
clearSmartAccountCache: () => clearSmartAccountCache,
|
|
26
|
+
computeSmartAccountAddress: () => computeSmartAccountAddress,
|
|
27
|
+
createVaultSmartAccount: () => createVaultSmartAccount,
|
|
28
|
+
flushCidOnChainSync: () => flushCidOnChainSync,
|
|
29
|
+
formatSecretKeyForDisplay: () => formatSecretKeyForDisplay,
|
|
30
|
+
generateSecretKey: () => generateSecretKey,
|
|
31
|
+
getCachedSmartAccountAddress: () => getCachedSmartAccountAddress,
|
|
32
|
+
getCachedSmartAccountMode: () => getCachedSmartAccountMode,
|
|
33
|
+
getIdentityAddress: () => getIdentityAddress,
|
|
34
|
+
getIdentityMode: () => getIdentityMode,
|
|
35
|
+
getIdentityOwner: () => getIdentityOwner,
|
|
36
|
+
getIdentitySmartAccount: () => getIdentitySmartAccount,
|
|
37
|
+
getIdentitySnapshot: () => getIdentitySnapshot,
|
|
38
|
+
getOnChainSyncStatus: () => getOnChainSyncStatus,
|
|
39
|
+
getOwnerFromPassword: () => getOwnerFromPassword,
|
|
40
|
+
getPimlicoApiKey: () => getPimlicoApiKey,
|
|
41
|
+
getTargetChain: () => getTargetChain,
|
|
42
|
+
initIdentityFromPassword: () => initIdentityFromPassword,
|
|
43
|
+
initIdentityFromSigner: () => initIdentityFromSigner,
|
|
44
|
+
isCidRegistryConfigured: () => isCidRegistryConfigured,
|
|
45
|
+
isIdentityConnected: () => isIdentityConnected,
|
|
46
|
+
isSmartAccountConfigured: () => isSmartAccountConfigured,
|
|
47
|
+
isValidSecretKey: () => isValidSecretKey,
|
|
48
|
+
parseSecretKeyInput: () => parseSecretKeyInput,
|
|
49
|
+
publishCidOnChain: () => publishCidOnChain,
|
|
50
|
+
readCidOnChain: () => readCidOnChain,
|
|
51
|
+
resetOnChainSync: () => resetOnChainSync,
|
|
52
|
+
scheduleCidOnChainSync: () => scheduleCidOnChainSync,
|
|
53
|
+
signMessageWithIdentity: () => signMessageWithIdentity,
|
|
54
|
+
subscribeIdentity: () => subscribeIdentity,
|
|
55
|
+
useIdentity: () => useIdentity
|
|
56
|
+
});
|
|
57
|
+
module.exports = __toCommonJS(index_exports);
|
|
58
|
+
|
|
59
|
+
// src/kernel.ts
|
|
60
|
+
var import_viem = require("viem");
|
|
61
|
+
var import_accounts = require("permissionless/accounts");
|
|
62
|
+
var import_pimlico = require("permissionless/clients/pimlico");
|
|
63
|
+
var import_account_abstraction = require("viem/account-abstraction");
|
|
64
|
+
|
|
65
|
+
// src/config.ts
|
|
66
|
+
var import_chains = require("viem/chains");
|
|
67
|
+
var TARGET_CHAIN = import_chains.base;
|
|
68
|
+
function getPimlicoApiKey() {
|
|
69
|
+
let key = "";
|
|
70
|
+
try {
|
|
71
|
+
key = process.env.PIMLICO_API_KEY || "";
|
|
72
|
+
} catch {
|
|
73
|
+
}
|
|
74
|
+
if (key) return key;
|
|
75
|
+
try {
|
|
76
|
+
key = process.env.NEXT_PUBLIC_PIMLICO_API_KEY || "";
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
return key;
|
|
80
|
+
}
|
|
81
|
+
function getPimlicoUrl() {
|
|
82
|
+
let proxyUrl = "";
|
|
83
|
+
try {
|
|
84
|
+
proxyUrl = process.env.PIMLICO_PROXY_URL || "";
|
|
85
|
+
} catch {
|
|
86
|
+
}
|
|
87
|
+
if (!proxyUrl) {
|
|
88
|
+
try {
|
|
89
|
+
proxyUrl = process.env.NEXT_PUBLIC_PIMLICO_PROXY_URL || "";
|
|
90
|
+
} catch {
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (!proxyUrl) {
|
|
94
|
+
try {
|
|
95
|
+
proxyUrl = process.env.EXPO_PUBLIC_PIMLICO_PROXY_URL || "";
|
|
96
|
+
} catch {
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (proxyUrl) {
|
|
100
|
+
return proxyUrl;
|
|
101
|
+
}
|
|
102
|
+
throw new Error(
|
|
103
|
+
"[SmartAccount] Aucun proxy bundler configure. Configurez PIMLICO_PROXY_URL (le fallback direct api.pimlico.io a ete supprime pour ne pas exposer la cle API dans le bundle client)."
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
function isSmartAccountConfigured() {
|
|
107
|
+
let proxyUrl = "";
|
|
108
|
+
try {
|
|
109
|
+
proxyUrl = process.env.PIMLICO_PROXY_URL || "";
|
|
110
|
+
} catch {
|
|
111
|
+
}
|
|
112
|
+
if (!proxyUrl) {
|
|
113
|
+
try {
|
|
114
|
+
proxyUrl = process.env.NEXT_PUBLIC_PIMLICO_PROXY_URL || "";
|
|
115
|
+
} catch {
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!proxyUrl) {
|
|
119
|
+
try {
|
|
120
|
+
proxyUrl = process.env.EXPO_PUBLIC_PIMLICO_PROXY_URL || "";
|
|
121
|
+
} catch {
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return !!proxyUrl;
|
|
125
|
+
}
|
|
126
|
+
function getTargetChain() {
|
|
127
|
+
return TARGET_CHAIN;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/kernel.ts
|
|
131
|
+
var import_logger = require("@vaultkeepr/logger");
|
|
132
|
+
async function computeSmartAccountAddress(ownerAccount) {
|
|
133
|
+
const publicClient = (0, import_viem.createPublicClient)({
|
|
134
|
+
chain: TARGET_CHAIN,
|
|
135
|
+
transport: (0, import_viem.http)()
|
|
136
|
+
});
|
|
137
|
+
const kernelAccount = await (0, import_accounts.toKernelSmartAccount)({
|
|
138
|
+
client: publicClient,
|
|
139
|
+
owners: [ownerAccount],
|
|
140
|
+
entryPoint: {
|
|
141
|
+
address: import_account_abstraction.entryPoint07Address,
|
|
142
|
+
version: "0.7"
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return kernelAccount.address;
|
|
146
|
+
}
|
|
147
|
+
var _cached = null;
|
|
148
|
+
var _cachedKey = null;
|
|
149
|
+
async function createVaultSmartAccount(params) {
|
|
150
|
+
const { ownerAccount, mode } = params;
|
|
151
|
+
const cacheKey = `${mode}:${ownerAccount.address.toLowerCase()}`;
|
|
152
|
+
if (_cached && _cachedKey === cacheKey) return _cached;
|
|
153
|
+
const publicClient = (0, import_viem.createPublicClient)({
|
|
154
|
+
chain: TARGET_CHAIN,
|
|
155
|
+
transport: (0, import_viem.http)()
|
|
156
|
+
});
|
|
157
|
+
const kernelAccount = await (0, import_accounts.toKernelSmartAccount)({
|
|
158
|
+
client: publicClient,
|
|
159
|
+
owners: [ownerAccount],
|
|
160
|
+
entryPoint: {
|
|
161
|
+
address: import_account_abstraction.entryPoint07Address,
|
|
162
|
+
version: "0.7"
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
const pimlicoUrl = getPimlicoUrl();
|
|
166
|
+
const pimlicoClient = (0, import_pimlico.createPimlicoClient)({
|
|
167
|
+
transport: (0, import_viem.http)(pimlicoUrl),
|
|
168
|
+
entryPoint: {
|
|
169
|
+
address: import_account_abstraction.entryPoint07Address,
|
|
170
|
+
version: "0.7"
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
const bundlerClient = (0, import_account_abstraction.createBundlerClient)({
|
|
174
|
+
account: kernelAccount,
|
|
175
|
+
chain: TARGET_CHAIN,
|
|
176
|
+
transport: (0, import_viem.http)(pimlicoUrl),
|
|
177
|
+
paymaster: {
|
|
178
|
+
async getPaymasterData(parameters) {
|
|
179
|
+
return pimlicoClient.getPaymasterData(parameters);
|
|
180
|
+
},
|
|
181
|
+
async getPaymasterStubData(parameters) {
|
|
182
|
+
return pimlicoClient.getPaymasterStubData(parameters);
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
userOperation: {
|
|
186
|
+
async estimateFeesPerGas() {
|
|
187
|
+
return (await pimlicoClient.getUserOperationGasPrice()).fast;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
const result = {
|
|
192
|
+
address: kernelAccount.address,
|
|
193
|
+
ownerAddress: ownerAccount.address,
|
|
194
|
+
ownerAccount,
|
|
195
|
+
client: bundlerClient,
|
|
196
|
+
chain: TARGET_CHAIN,
|
|
197
|
+
mode
|
|
198
|
+
};
|
|
199
|
+
_cached = result;
|
|
200
|
+
_cachedKey = cacheKey;
|
|
201
|
+
import_logger.logger.debug(
|
|
202
|
+
`[SmartAccount] Kernel cr\xE9\xE9 (${mode}) \u2014 SA: ${(0, import_logger.redactAddress)(kernelAccount.address)} | Owner: ${(0, import_logger.redactAddress)(ownerAccount.address)}`
|
|
203
|
+
);
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
function getCachedSmartAccountAddress() {
|
|
207
|
+
return _cached?.address ?? null;
|
|
208
|
+
}
|
|
209
|
+
function getCachedSmartAccountMode() {
|
|
210
|
+
return _cached?.mode ?? null;
|
|
211
|
+
}
|
|
212
|
+
function clearSmartAccountCache() {
|
|
213
|
+
_cached = null;
|
|
214
|
+
_cachedKey = null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/owner.ts
|
|
218
|
+
var import_core = require("@vaultkeepr/core");
|
|
219
|
+
async function getOwnerFromPassword(password, secretKey) {
|
|
220
|
+
return (0, import_core.getHiddenWalletFromPassword)(password, secretKey);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/cidRegistry.ts
|
|
224
|
+
var import_viem2 = require("viem");
|
|
225
|
+
var import_logger2 = require("@vaultkeepr/logger");
|
|
226
|
+
var CID_REGISTRY_ABI = [
|
|
227
|
+
{
|
|
228
|
+
name: "setCid",
|
|
229
|
+
type: "function",
|
|
230
|
+
stateMutability: "nonpayable",
|
|
231
|
+
inputs: [{ name: "cid", type: "string" }],
|
|
232
|
+
outputs: []
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
name: "deleteCid",
|
|
236
|
+
type: "function",
|
|
237
|
+
stateMutability: "nonpayable",
|
|
238
|
+
inputs: [],
|
|
239
|
+
outputs: []
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
name: "getCid",
|
|
243
|
+
type: "function",
|
|
244
|
+
stateMutability: "view",
|
|
245
|
+
inputs: [{ name: "account", type: "address" }],
|
|
246
|
+
outputs: [{ name: "", type: "string" }]
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
name: "hasCid",
|
|
250
|
+
type: "function",
|
|
251
|
+
stateMutability: "view",
|
|
252
|
+
inputs: [{ name: "account", type: "address" }],
|
|
253
|
+
outputs: [{ name: "", type: "bool" }]
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
name: "getUpdatedAt",
|
|
257
|
+
type: "function",
|
|
258
|
+
stateMutability: "view",
|
|
259
|
+
inputs: [{ name: "account", type: "address" }],
|
|
260
|
+
outputs: [{ name: "", type: "uint256" }]
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
name: "nextAllowedUpdate",
|
|
264
|
+
type: "function",
|
|
265
|
+
stateMutability: "view",
|
|
266
|
+
inputs: [{ name: "account", type: "address" }],
|
|
267
|
+
outputs: [{ name: "", type: "uint256" }]
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
name: "cooldownSeconds",
|
|
271
|
+
type: "function",
|
|
272
|
+
stateMutability: "view",
|
|
273
|
+
inputs: [],
|
|
274
|
+
outputs: [{ name: "", type: "uint256" }]
|
|
275
|
+
}
|
|
276
|
+
];
|
|
277
|
+
function getCidRegistryAddress() {
|
|
278
|
+
let addr = "";
|
|
279
|
+
try {
|
|
280
|
+
addr = process.env.CID_REGISTRY_ADDRESS || "";
|
|
281
|
+
} catch {
|
|
282
|
+
}
|
|
283
|
+
if (addr) return addr;
|
|
284
|
+
try {
|
|
285
|
+
addr = process.env.NEXT_PUBLIC_CID_REGISTRY_ADDRESS || "";
|
|
286
|
+
} catch {
|
|
287
|
+
}
|
|
288
|
+
if (addr) return addr;
|
|
289
|
+
try {
|
|
290
|
+
addr = process.env.EXPO_PUBLIC_CID_REGISTRY_ADDRESS || "";
|
|
291
|
+
} catch {
|
|
292
|
+
}
|
|
293
|
+
if (addr) return addr;
|
|
294
|
+
return "0x0000000000000000000000000000000000000000";
|
|
295
|
+
}
|
|
296
|
+
function isCidRegistryConfigured() {
|
|
297
|
+
return getCidRegistryAddress() !== "0x0000000000000000000000000000000000000000" && isSmartAccountConfigured();
|
|
298
|
+
}
|
|
299
|
+
async function publishCidOnChain(smartAccount, cid) {
|
|
300
|
+
if (!isCidRegistryConfigured()) {
|
|
301
|
+
import_logger2.logger.warn(
|
|
302
|
+
"[CidRegistry] Non configur\xE9, skip on-chain publish"
|
|
303
|
+
);
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
const calldata = (0, import_viem2.encodeFunctionData)({
|
|
308
|
+
abi: CID_REGISTRY_ABI,
|
|
309
|
+
functionName: "setCid",
|
|
310
|
+
args: [cid]
|
|
311
|
+
});
|
|
312
|
+
const userOpHash = await smartAccount.client.sendUserOperation({
|
|
313
|
+
calls: [
|
|
314
|
+
{
|
|
315
|
+
to: getCidRegistryAddress(),
|
|
316
|
+
data: calldata,
|
|
317
|
+
value: BigInt(0)
|
|
318
|
+
}
|
|
319
|
+
]
|
|
320
|
+
});
|
|
321
|
+
import_logger2.logger.debug(
|
|
322
|
+
`[CidRegistry] UserOp soumise: ${userOpHash}`
|
|
323
|
+
);
|
|
324
|
+
const receipt = await smartAccount.client.waitForUserOperationReceipt({
|
|
325
|
+
hash: userOpHash
|
|
326
|
+
});
|
|
327
|
+
import_logger2.logger.debug(
|
|
328
|
+
`[CidRegistry] CID publi\xE9 on-chain \u2014 tx: ${receipt.receipt.transactionHash} | CID: ${cid.slice(0, 20)}...`
|
|
329
|
+
);
|
|
330
|
+
return receipt.receipt.transactionHash;
|
|
331
|
+
} catch (error) {
|
|
332
|
+
import_logger2.logger.error("[CidRegistry] Erreur:", error instanceof Error ? error.message : error);
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
async function readCidOnChain(smartAccountAddress) {
|
|
337
|
+
if (!isCidRegistryConfigured()) return null;
|
|
338
|
+
try {
|
|
339
|
+
const publicClient = (0, import_viem2.createPublicClient)({
|
|
340
|
+
chain: TARGET_CHAIN,
|
|
341
|
+
transport: (0, import_viem2.http)()
|
|
342
|
+
});
|
|
343
|
+
const cid = await publicClient.readContract({
|
|
344
|
+
address: getCidRegistryAddress(),
|
|
345
|
+
abi: CID_REGISTRY_ABI,
|
|
346
|
+
functionName: "getCid",
|
|
347
|
+
args: [smartAccountAddress]
|
|
348
|
+
});
|
|
349
|
+
return cid && cid.length > 0 ? cid : null;
|
|
350
|
+
} catch (error) {
|
|
351
|
+
import_logger2.logger.error("[CidRegistry] Erreur lecture:", error instanceof Error ? error.message : error);
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// src/onChainSync.ts
|
|
357
|
+
var import_logger3 = require("@vaultkeepr/logger");
|
|
358
|
+
var DEFAULT_DEBOUNCE_MS = 5 * 60 * 1e3;
|
|
359
|
+
var _pendingCid = null;
|
|
360
|
+
var _pendingSmartAccount = null;
|
|
361
|
+
var _debounceTimer = null;
|
|
362
|
+
var _lastPublishedCid = null;
|
|
363
|
+
var _lastPublishTime = 0;
|
|
364
|
+
var _isPublishing = false;
|
|
365
|
+
function scheduleCidOnChainSync(smartAccount, cid, debounceMs = DEFAULT_DEBOUNCE_MS) {
|
|
366
|
+
if (!isCidRegistryConfigured()) return;
|
|
367
|
+
if (cid === _lastPublishedCid) {
|
|
368
|
+
import_logger3.logger.debug("[OnChainSync] CID identique, skip");
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const sa = smartAccount ?? _pendingSmartAccount;
|
|
372
|
+
if (!sa) {
|
|
373
|
+
import_logger3.logger.warn("[OnChainSync] Pas de Smart Account disponible, skip");
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
_pendingCid = cid;
|
|
377
|
+
_pendingSmartAccount = sa;
|
|
378
|
+
if (_debounceTimer) {
|
|
379
|
+
clearTimeout(_debounceTimer);
|
|
380
|
+
}
|
|
381
|
+
_debounceTimer = setTimeout(() => {
|
|
382
|
+
_executeSync();
|
|
383
|
+
}, debounceMs);
|
|
384
|
+
import_logger3.logger.debug(
|
|
385
|
+
`[OnChainSync] CID planifi\xE9 dans ${(debounceMs / 1e3).toFixed(0)}s : ${cid.slice(0, 20)}...`
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
async function flushCidOnChainSync() {
|
|
389
|
+
import_logger3.logger.debug(
|
|
390
|
+
"[OnChainSync] flush demand\xE9 \u2014 pending:",
|
|
391
|
+
_pendingCid?.slice(0, 20),
|
|
392
|
+
"SA:",
|
|
393
|
+
!!_pendingSmartAccount
|
|
394
|
+
);
|
|
395
|
+
if (!_pendingCid || !_pendingSmartAccount) {
|
|
396
|
+
import_logger3.logger.debug("[OnChainSync] Rien \xE0 flush");
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
if (_debounceTimer) {
|
|
400
|
+
clearTimeout(_debounceTimer);
|
|
401
|
+
_debounceTimer = null;
|
|
402
|
+
}
|
|
403
|
+
import_logger3.logger.debug("[OnChainSync] Flush imm\xE9diat...");
|
|
404
|
+
return _executeSync();
|
|
405
|
+
}
|
|
406
|
+
function getOnChainSyncStatus() {
|
|
407
|
+
return {
|
|
408
|
+
hasPending: _pendingCid !== null && _pendingCid !== _lastPublishedCid,
|
|
409
|
+
pendingCid: _pendingCid,
|
|
410
|
+
lastPublishedCid: _lastPublishedCid,
|
|
411
|
+
lastPublishTime: _lastPublishTime,
|
|
412
|
+
isPublishing: _isPublishing
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
function resetOnChainSync() {
|
|
416
|
+
if (_debounceTimer) {
|
|
417
|
+
clearTimeout(_debounceTimer);
|
|
418
|
+
_debounceTimer = null;
|
|
419
|
+
}
|
|
420
|
+
_pendingCid = null;
|
|
421
|
+
_pendingSmartAccount = null;
|
|
422
|
+
_lastPublishedCid = null;
|
|
423
|
+
_lastPublishTime = 0;
|
|
424
|
+
_isPublishing = false;
|
|
425
|
+
}
|
|
426
|
+
async function _executeSync() {
|
|
427
|
+
if (!_pendingCid || !_pendingSmartAccount || _isPublishing) return false;
|
|
428
|
+
if (_pendingCid === _lastPublishedCid) {
|
|
429
|
+
_pendingCid = null;
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
_isPublishing = true;
|
|
433
|
+
const cid = _pendingCid;
|
|
434
|
+
const sa = _pendingSmartAccount;
|
|
435
|
+
try {
|
|
436
|
+
import_logger3.logger.debug(`[OnChainSync] Publication on-chain: ${cid.slice(0, 20)}...`);
|
|
437
|
+
const txHash = await publishCidOnChain(sa, cid);
|
|
438
|
+
if (txHash) {
|
|
439
|
+
_lastPublishedCid = cid;
|
|
440
|
+
_lastPublishTime = Date.now();
|
|
441
|
+
_pendingCid = null;
|
|
442
|
+
import_logger3.logger.debug(
|
|
443
|
+
`[OnChainSync] \u2705 CID publi\xE9 on-chain : ${cid.slice(0, 20)}...`
|
|
444
|
+
);
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
import_logger3.logger.warn("[OnChainSync] Publication \xE9chou\xE9e (null txHash)");
|
|
448
|
+
return false;
|
|
449
|
+
} catch (error) {
|
|
450
|
+
import_logger3.logger.error("[OnChainSync] Erreur publication:", error instanceof Error ? error.message : error);
|
|
451
|
+
return false;
|
|
452
|
+
} finally {
|
|
453
|
+
_isPublishing = false;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// src/identity.ts
|
|
458
|
+
var import_logger4 = require("@vaultkeepr/logger");
|
|
459
|
+
var _state = {
|
|
460
|
+
owner: null,
|
|
461
|
+
smartAccount: null,
|
|
462
|
+
mode: null,
|
|
463
|
+
initialized: false
|
|
464
|
+
};
|
|
465
|
+
var _listeners = /* @__PURE__ */ new Set();
|
|
466
|
+
function _notify() {
|
|
467
|
+
for (const fn of _listeners) {
|
|
468
|
+
try {
|
|
469
|
+
fn(_state);
|
|
470
|
+
} catch {
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
async function initIdentityFromPassword(password, secretKey) {
|
|
475
|
+
const owner = await getOwnerFromPassword(password, secretKey);
|
|
476
|
+
let sa = null;
|
|
477
|
+
if (isSmartAccountConfigured()) {
|
|
478
|
+
try {
|
|
479
|
+
sa = await createVaultSmartAccount({
|
|
480
|
+
ownerAccount: owner,
|
|
481
|
+
mode: "password"
|
|
482
|
+
});
|
|
483
|
+
} catch (e) {
|
|
484
|
+
import_logger4.logger.warn("[Identity] SA init failed (non-blocking):", e.message);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
_state = {
|
|
488
|
+
owner,
|
|
489
|
+
smartAccount: sa,
|
|
490
|
+
mode: "password",
|
|
491
|
+
initialized: true
|
|
492
|
+
};
|
|
493
|
+
_notify();
|
|
494
|
+
import_logger4.logger.debug(
|
|
495
|
+
`[Identity] Connect\xE9 (password) \u2014 adresse: ${(0, import_logger4.redactAddress)(owner.address)}` + (sa ? ` | SA: ${(0, import_logger4.redactAddress)(sa.address)}` : "") + (secretKey ? ` | SK: \u2713` : " | no secretKey")
|
|
496
|
+
);
|
|
497
|
+
return {
|
|
498
|
+
address: owner.address,
|
|
499
|
+
smartAccountAddress: sa?.address ?? null
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
async function initIdentityFromSigner(signer, mode = "passkey") {
|
|
503
|
+
let sa = null;
|
|
504
|
+
if (isSmartAccountConfigured()) {
|
|
505
|
+
try {
|
|
506
|
+
sa = await createVaultSmartAccount({ ownerAccount: signer, mode });
|
|
507
|
+
} catch (e) {
|
|
508
|
+
import_logger4.logger.warn("[Identity] SA init failed (non-blocking):", e.message);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
_state = {
|
|
512
|
+
owner: signer,
|
|
513
|
+
smartAccount: sa,
|
|
514
|
+
mode,
|
|
515
|
+
initialized: true
|
|
516
|
+
};
|
|
517
|
+
_notify();
|
|
518
|
+
import_logger4.logger.info(
|
|
519
|
+
`[Identity] Connected (${mode}) \u2014 address: ${signer.address}` + (sa ? ` | SA: ${sa.address}` : "")
|
|
520
|
+
);
|
|
521
|
+
return {
|
|
522
|
+
address: signer.address,
|
|
523
|
+
smartAccountAddress: sa?.address ?? null
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
function clearIdentity() {
|
|
527
|
+
_state = {
|
|
528
|
+
owner: null,
|
|
529
|
+
smartAccount: null,
|
|
530
|
+
mode: null,
|
|
531
|
+
initialized: false
|
|
532
|
+
};
|
|
533
|
+
clearSmartAccountCache();
|
|
534
|
+
_notify();
|
|
535
|
+
import_logger4.logger.info("[Identity] Disconnected");
|
|
536
|
+
}
|
|
537
|
+
function getIdentityAddress() {
|
|
538
|
+
return _state.owner?.address ?? null;
|
|
539
|
+
}
|
|
540
|
+
function isIdentityConnected() {
|
|
541
|
+
return _state.initialized && _state.owner !== null;
|
|
542
|
+
}
|
|
543
|
+
function getIdentityOwner() {
|
|
544
|
+
return _state.owner;
|
|
545
|
+
}
|
|
546
|
+
function getIdentitySmartAccount() {
|
|
547
|
+
return _state.smartAccount;
|
|
548
|
+
}
|
|
549
|
+
function getIdentityMode() {
|
|
550
|
+
return _state.mode;
|
|
551
|
+
}
|
|
552
|
+
async function signMessageWithIdentity(message) {
|
|
553
|
+
if (!_state.owner) {
|
|
554
|
+
throw new Error("[Identity] Pas connect\xE9 \u2014 impossible de signer");
|
|
555
|
+
}
|
|
556
|
+
return _state.owner.signMessage({ message });
|
|
557
|
+
}
|
|
558
|
+
function subscribeIdentity(listener) {
|
|
559
|
+
_listeners.add(listener);
|
|
560
|
+
return () => {
|
|
561
|
+
_listeners.delete(listener);
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
function getIdentitySnapshot() {
|
|
565
|
+
return _state;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// src/useIdentity.ts
|
|
569
|
+
var import_react = require("react");
|
|
570
|
+
var _emptyState = {
|
|
571
|
+
owner: null,
|
|
572
|
+
smartAccount: null,
|
|
573
|
+
mode: null,
|
|
574
|
+
initialized: false
|
|
575
|
+
};
|
|
576
|
+
function getServerSnapshot() {
|
|
577
|
+
return _emptyState;
|
|
578
|
+
}
|
|
579
|
+
function useIdentity() {
|
|
580
|
+
const state = (0, import_react.useSyncExternalStore)(
|
|
581
|
+
subscribeIdentity,
|
|
582
|
+
getIdentitySnapshot,
|
|
583
|
+
getServerSnapshot
|
|
584
|
+
);
|
|
585
|
+
const signMessage = (0, import_react.useCallback)(
|
|
586
|
+
async (message) => {
|
|
587
|
+
return signMessageWithIdentity(message);
|
|
588
|
+
},
|
|
589
|
+
[]
|
|
590
|
+
);
|
|
591
|
+
const disconnect = (0, import_react.useCallback)(() => {
|
|
592
|
+
clearIdentity();
|
|
593
|
+
}, []);
|
|
594
|
+
const notifyOnChainSync = (0, import_react.useCallback)(
|
|
595
|
+
(cid) => {
|
|
596
|
+
const sa = getIdentitySmartAccount();
|
|
597
|
+
if (!sa) return;
|
|
598
|
+
scheduleCidOnChainSync(sa, cid);
|
|
599
|
+
},
|
|
600
|
+
[]
|
|
601
|
+
);
|
|
602
|
+
const cleanupOnLock = (0, import_react.useCallback)(async () => {
|
|
603
|
+
await flushCidOnChainSync().catch(() => {
|
|
604
|
+
});
|
|
605
|
+
clearSmartAccountCache();
|
|
606
|
+
resetOnChainSync();
|
|
607
|
+
clearIdentity();
|
|
608
|
+
}, []);
|
|
609
|
+
const flushOnChainSync = (0, import_react.useCallback)(async () => {
|
|
610
|
+
await flushCidOnChainSync().catch(() => {
|
|
611
|
+
});
|
|
612
|
+
}, []);
|
|
613
|
+
return {
|
|
614
|
+
address: state.owner?.address ?? null,
|
|
615
|
+
isConnected: state.initialized && state.owner !== null,
|
|
616
|
+
mode: state.mode,
|
|
617
|
+
smartAccountAddress: state.smartAccount?.address ?? null,
|
|
618
|
+
signMessage,
|
|
619
|
+
disconnect,
|
|
620
|
+
notifyOnChainSync,
|
|
621
|
+
cleanupOnLock,
|
|
622
|
+
flushOnChainSync
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// src/secretKey.ts
|
|
627
|
+
var import_viem3 = require("viem");
|
|
628
|
+
function generateSecretKey() {
|
|
629
|
+
const bytes = new Uint8Array(32);
|
|
630
|
+
if (typeof globalThis.crypto?.getRandomValues === "function") {
|
|
631
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
632
|
+
} else {
|
|
633
|
+
for (let i = 0; i < 32; i++) {
|
|
634
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return (0, import_viem3.bytesToHex)(bytes).slice(2);
|
|
638
|
+
}
|
|
639
|
+
function isValidSecretKey(key) {
|
|
640
|
+
return typeof key === "string" && /^[0-9a-f]{64}$/i.test(key);
|
|
641
|
+
}
|
|
642
|
+
function formatSecretKeyForDisplay(key) {
|
|
643
|
+
const clean = key.replace(/[^0-9a-f]/gi, "").toLowerCase();
|
|
644
|
+
return clean.match(/.{1,8}/g)?.join("-") ?? clean;
|
|
645
|
+
}
|
|
646
|
+
function parseSecretKeyInput(input) {
|
|
647
|
+
return input.replace(/[-\s]/g, "").toLowerCase();
|
|
648
|
+
}
|
|
649
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
650
|
+
0 && (module.exports = {
|
|
651
|
+
CID_REGISTRY_ABI,
|
|
652
|
+
clearIdentity,
|
|
653
|
+
clearSmartAccountCache,
|
|
654
|
+
computeSmartAccountAddress,
|
|
655
|
+
createVaultSmartAccount,
|
|
656
|
+
flushCidOnChainSync,
|
|
657
|
+
formatSecretKeyForDisplay,
|
|
658
|
+
generateSecretKey,
|
|
659
|
+
getCachedSmartAccountAddress,
|
|
660
|
+
getCachedSmartAccountMode,
|
|
661
|
+
getIdentityAddress,
|
|
662
|
+
getIdentityMode,
|
|
663
|
+
getIdentityOwner,
|
|
664
|
+
getIdentitySmartAccount,
|
|
665
|
+
getIdentitySnapshot,
|
|
666
|
+
getOnChainSyncStatus,
|
|
667
|
+
getOwnerFromPassword,
|
|
668
|
+
getPimlicoApiKey,
|
|
669
|
+
getTargetChain,
|
|
670
|
+
initIdentityFromPassword,
|
|
671
|
+
initIdentityFromSigner,
|
|
672
|
+
isCidRegistryConfigured,
|
|
673
|
+
isIdentityConnected,
|
|
674
|
+
isSmartAccountConfigured,
|
|
675
|
+
isValidSecretKey,
|
|
676
|
+
parseSecretKeyInput,
|
|
677
|
+
publishCidOnChain,
|
|
678
|
+
readCidOnChain,
|
|
679
|
+
resetOnChainSync,
|
|
680
|
+
scheduleCidOnChainSync,
|
|
681
|
+
signMessageWithIdentity,
|
|
682
|
+
subscribeIdentity,
|
|
683
|
+
useIdentity
|
|
684
|
+
});
|