@cogcoin/client 1.1.4 → 1.1.5

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.
Files changed (83) hide show
  1. package/README.md +4 -5
  2. package/dist/bitcoind/progress/tty-renderer.js +3 -2
  3. package/dist/bitcoind/service.js +1 -1
  4. package/dist/cli/command-registry.d.ts +39 -0
  5. package/dist/cli/command-registry.js +1132 -0
  6. package/dist/cli/commands/client-admin.js +6 -56
  7. package/dist/cli/commands/mining-admin.js +9 -32
  8. package/dist/cli/commands/mining-read.js +15 -56
  9. package/dist/cli/commands/mining-runtime.js +258 -57
  10. package/dist/cli/commands/service-runtime.js +1 -64
  11. package/dist/cli/commands/status.js +2 -15
  12. package/dist/cli/commands/update.js +6 -21
  13. package/dist/cli/commands/wallet-admin.js +18 -120
  14. package/dist/cli/commands/wallet-mutation.js +4 -7
  15. package/dist/cli/commands/wallet-read.js +31 -138
  16. package/dist/cli/context.js +2 -4
  17. package/dist/cli/mining-format.js +8 -2
  18. package/dist/cli/mutation-command-groups.d.ts +11 -11
  19. package/dist/cli/mutation-command-groups.js +9 -18
  20. package/dist/cli/mutation-json.d.ts +1 -17
  21. package/dist/cli/mutation-json.js +1 -28
  22. package/dist/cli/mutation-success.d.ts +0 -1
  23. package/dist/cli/mutation-success.js +0 -19
  24. package/dist/cli/output.d.ts +1 -10
  25. package/dist/cli/output.js +52 -481
  26. package/dist/cli/parse.d.ts +1 -1
  27. package/dist/cli/parse.js +38 -695
  28. package/dist/cli/runner.js +28 -113
  29. package/dist/cli/types.d.ts +7 -8
  30. package/dist/cli/update-notifier.js +1 -1
  31. package/dist/cli/wallet-format.js +1 -1
  32. package/dist/wallet/lifecycle/managed-core.d.ts +23 -0
  33. package/dist/wallet/lifecycle/managed-core.js +257 -0
  34. package/dist/wallet/lifecycle/repair-mining.d.ts +49 -0
  35. package/dist/wallet/lifecycle/repair-mining.js +304 -0
  36. package/dist/wallet/lifecycle/repair-runtime.d.ts +36 -0
  37. package/dist/wallet/lifecycle/repair-runtime.js +206 -0
  38. package/dist/wallet/lifecycle/repair.d.ts +11 -0
  39. package/dist/wallet/lifecycle/repair.js +368 -0
  40. package/dist/wallet/lifecycle/setup.d.ts +16 -0
  41. package/dist/wallet/lifecycle/setup.js +430 -0
  42. package/dist/wallet/lifecycle/types.d.ts +125 -0
  43. package/dist/wallet/lifecycle/types.js +1 -0
  44. package/dist/wallet/lifecycle.d.ts +4 -165
  45. package/dist/wallet/lifecycle.js +3 -1656
  46. package/dist/wallet/mining/candidate.d.ts +60 -0
  47. package/dist/wallet/mining/candidate.js +290 -0
  48. package/dist/wallet/mining/competitiveness.d.ts +22 -0
  49. package/dist/wallet/mining/competitiveness.js +640 -0
  50. package/dist/wallet/mining/control.js +7 -251
  51. package/dist/wallet/mining/cycle.d.ts +39 -0
  52. package/dist/wallet/mining/cycle.js +542 -0
  53. package/dist/wallet/mining/engine-state.d.ts +66 -0
  54. package/dist/wallet/mining/engine-state.js +211 -0
  55. package/dist/wallet/mining/engine-types.d.ts +173 -0
  56. package/dist/wallet/mining/engine-types.js +1 -0
  57. package/dist/wallet/mining/engine-utils.d.ts +7 -0
  58. package/dist/wallet/mining/engine-utils.js +75 -0
  59. package/dist/wallet/mining/events.d.ts +2 -0
  60. package/dist/wallet/mining/events.js +19 -0
  61. package/dist/wallet/mining/lifecycle.d.ts +71 -0
  62. package/dist/wallet/mining/lifecycle.js +355 -0
  63. package/dist/wallet/mining/projection.d.ts +61 -0
  64. package/dist/wallet/mining/projection.js +319 -0
  65. package/dist/wallet/mining/publish.d.ts +79 -0
  66. package/dist/wallet/mining/publish.js +614 -0
  67. package/dist/wallet/mining/runner.d.ts +12 -418
  68. package/dist/wallet/mining/runner.js +274 -3433
  69. package/dist/wallet/mining/supervisor.d.ts +134 -0
  70. package/dist/wallet/mining/supervisor.js +558 -0
  71. package/dist/wallet/mining/visualizer-sync.d.ts +42 -0
  72. package/dist/wallet/mining/visualizer-sync.js +166 -0
  73. package/dist/wallet/mining/visualizer.d.ts +1 -0
  74. package/dist/wallet/mining/visualizer.js +33 -18
  75. package/dist/wallet/reset.d.ts +1 -1
  76. package/dist/wallet/reset.js +35 -11
  77. package/dist/wallet/runtime.d.ts +0 -6
  78. package/dist/wallet/runtime.js +2 -38
  79. package/dist/wallet/tx/common.d.ts +18 -0
  80. package/dist/wallet/tx/common.js +40 -26
  81. package/package.json +1 -1
  82. package/dist/wallet/state/seed-index.d.ts +0 -43
  83. package/dist/wallet/state/seed-index.js +0 -151
@@ -0,0 +1,368 @@
1
+ import { access, constants } from "node:fs/promises";
2
+ import { attachOrStartIndexerDaemon, probeIndexerDaemon, } from "../../bitcoind/indexer-daemon.js";
3
+ import { createRpcClient } from "../../bitcoind/node.js";
4
+ import { attachOrStartManagedBitcoindService, probeManagedBitcoindService } from "../../bitcoind/service.js";
5
+ import { resolveManagedServicePaths } from "../../bitcoind/service-paths.js";
6
+ import { persistWalletCoinControlStateIfNeeded } from "../coin-control.js";
7
+ import { normalizeWalletDescriptorState } from "../descriptor-normalization.js";
8
+ import { acquireFileLock } from "../fs/lock.js";
9
+ import { clearLegacyWalletLockArtifacts } from "../managed-core-wallet.js";
10
+ import { loadMiningRuntimeStatus } from "../mining/runtime-artifacts.js";
11
+ import { resolveWalletRuntimePathsForTesting } from "../runtime.js";
12
+ import { createDefaultWalletSecretProvider, createWalletSecretReference, } from "../state/provider.js";
13
+ import { loadWalletState } from "../state/storage.js";
14
+ import { recreateManagedCoreWalletReplica, verifyManagedCoreWalletReplica } from "./managed-core.js";
15
+ import { applyRepairStoppedMiningState, cleanupMiningForRepair, persistRepairState, resumeBackgroundMiningAfterRepair, } from "./repair-mining.js";
16
+ import { clearIndexerDaemonArtifacts, clearManagedBitcoindArtifacts, clearOrphanedRepairLocks, ensureIndexerDatabaseHealthy, mapBitcoindCompatibilityToRepairIssue, mapBitcoindRepairHealth, mapIndexerCompatibilityToRepairIssue, verifyIndexerPostRepairHealth, waitForProcessExit, } from "./repair-runtime.js";
17
+ async function pathExists(path) {
18
+ try {
19
+ await access(path, constants.F_OK);
20
+ return true;
21
+ }
22
+ catch {
23
+ return false;
24
+ }
25
+ }
26
+ export async function repairWallet(options) {
27
+ const provider = options.provider ?? createDefaultWalletSecretProvider();
28
+ const nowUnixMs = options.nowUnixMs ?? Date.now();
29
+ const paths = options.paths ?? resolveWalletRuntimePathsForTesting();
30
+ const probeManagedBitcoind = options.probeBitcoindService ?? probeManagedBitcoindService;
31
+ const attachManagedBitcoind = options.attachService ?? attachOrStartManagedBitcoindService;
32
+ const probeManagedIndexerDaemon = options.probeIndexerDaemon ?? probeIndexerDaemon;
33
+ const attachManagedIndexerDaemon = options.attachIndexerDaemon ?? attachOrStartIndexerDaemon;
34
+ await clearOrphanedRepairLocks([
35
+ paths.walletControlLockPath,
36
+ paths.miningControlLockPath,
37
+ ]);
38
+ const controlLock = await acquireFileLock(paths.walletControlLockPath, {
39
+ purpose: "wallet-repair",
40
+ walletRootId: null,
41
+ });
42
+ try {
43
+ let loaded;
44
+ try {
45
+ loaded = await loadWalletState({
46
+ primaryPath: paths.walletStatePath,
47
+ backupPath: paths.walletStateBackupPath,
48
+ }, {
49
+ provider,
50
+ });
51
+ }
52
+ catch {
53
+ throw new Error("local-state-corrupt");
54
+ }
55
+ const recoveredFromBackup = loaded.source === "backup";
56
+ const secretReference = createWalletSecretReference(loaded.state.walletRootId);
57
+ let repairedState = loaded.state;
58
+ let repairStateNeedsPersist = false;
59
+ const servicePaths = resolveManagedServicePaths(options.dataDir, repairedState.walletRootId);
60
+ await clearOrphanedRepairLocks([
61
+ servicePaths.bitcoindLockPath,
62
+ servicePaths.indexerDaemonLockPath,
63
+ ]);
64
+ const preRepairMiningRuntime = await loadMiningRuntimeStatus(paths.miningStatusPath).catch(() => null);
65
+ const miningCleanup = await cleanupMiningForRepair({
66
+ paths,
67
+ state: repairedState,
68
+ snapshot: preRepairMiningRuntime,
69
+ nowUnixMs,
70
+ });
71
+ const miningPreRepairRunMode = miningCleanup.preRepairRunMode;
72
+ if (miningPreRepairRunMode !== "stopped" || preRepairMiningRuntime?.runMode !== "stopped") {
73
+ repairedState = applyRepairStoppedMiningState(repairedState);
74
+ repairStateNeedsPersist = true;
75
+ }
76
+ if (!(options.assumeYes ?? false)) {
77
+ await ensureIndexerDatabaseHealthy({
78
+ databasePath: options.databasePath,
79
+ dataDir: options.dataDir,
80
+ walletRootId: repairedState.walletRootId,
81
+ resetIfNeeded: false,
82
+ });
83
+ }
84
+ let bitcoindServiceAction = "none";
85
+ let bitcoindCompatibilityIssue = "none";
86
+ let managedCoreReplicaAction = "none";
87
+ let indexerDaemonAction = "none";
88
+ let indexerCompatibilityIssue = "none";
89
+ let initialBitcoindProbe = {
90
+ compatibility: "unreachable",
91
+ status: null,
92
+ error: null,
93
+ };
94
+ let resetIndexerDatabase = false;
95
+ let bitcoindHandle = null;
96
+ let bitcoindPostRepairHealth = "unavailable";
97
+ const bitcoindLock = await acquireFileLock(servicePaths.bitcoindLockPath, {
98
+ purpose: "managed-bitcoind-repair",
99
+ walletRootId: repairedState.walletRootId,
100
+ dataDir: options.dataDir,
101
+ });
102
+ try {
103
+ initialBitcoindProbe = await probeManagedBitcoind({
104
+ dataDir: options.dataDir,
105
+ chain: "main",
106
+ startHeight: 0,
107
+ walletRootId: repairedState.walletRootId,
108
+ });
109
+ bitcoindCompatibilityIssue = mapBitcoindCompatibilityToRepairIssue(initialBitcoindProbe.compatibility);
110
+ if (initialBitcoindProbe.compatibility === "service-version-mismatch"
111
+ || initialBitcoindProbe.compatibility === "wallet-root-mismatch"
112
+ || initialBitcoindProbe.compatibility === "runtime-mismatch") {
113
+ const processId = initialBitcoindProbe.status?.processId ?? null;
114
+ if (processId === null) {
115
+ throw new Error("managed_bitcoind_process_id_unavailable");
116
+ }
117
+ try {
118
+ process.kill(processId, "SIGTERM");
119
+ }
120
+ catch (error) {
121
+ if (!(error instanceof Error) || !("code" in error) || error.code !== "ESRCH") {
122
+ throw error;
123
+ }
124
+ }
125
+ await waitForProcessExit(processId, 15_000, "managed_bitcoind_stop_timeout");
126
+ await clearManagedBitcoindArtifacts(servicePaths);
127
+ bitcoindServiceAction = "stopped-incompatible-service";
128
+ }
129
+ else if (initialBitcoindProbe.compatibility === "unreachable") {
130
+ const hasStaleArtifacts = await Promise.all([
131
+ servicePaths.bitcoindStatusPath,
132
+ servicePaths.bitcoindPidPath,
133
+ servicePaths.bitcoindReadyPath,
134
+ servicePaths.bitcoindWalletStatusPath,
135
+ ].map(pathExists));
136
+ if (hasStaleArtifacts.some(Boolean)) {
137
+ await clearManagedBitcoindArtifacts(servicePaths);
138
+ bitcoindServiceAction = "cleared-stale-artifacts";
139
+ }
140
+ }
141
+ else if (initialBitcoindProbe.compatibility === "protocol-error") {
142
+ throw new Error(initialBitcoindProbe.error ?? "managed_bitcoind_protocol_error");
143
+ }
144
+ }
145
+ finally {
146
+ await bitcoindLock.release();
147
+ }
148
+ try {
149
+ bitcoindHandle = await attachManagedBitcoind({
150
+ dataDir: options.dataDir,
151
+ chain: "main",
152
+ startHeight: 0,
153
+ walletRootId: repairedState.walletRootId,
154
+ });
155
+ const bitcoindRpc = (options.rpcFactory ?? createRpcClient)(bitcoindHandle.rpc);
156
+ const normalizedDescriptorState = await normalizeWalletDescriptorState(repairedState, bitcoindRpc);
157
+ if (normalizedDescriptorState.changed) {
158
+ repairedState = normalizedDescriptorState.state;
159
+ repairStateNeedsPersist = true;
160
+ }
161
+ const reconciledCoinControl = await persistWalletCoinControlStateIfNeeded({
162
+ state: repairedState,
163
+ access: {
164
+ provider,
165
+ secretReference,
166
+ },
167
+ paths,
168
+ nowUnixMs,
169
+ replacePrimary: recoveredFromBackup && !repairStateNeedsPersist,
170
+ rpc: (options.rpcFactory ?? createRpcClient)(bitcoindHandle.rpc),
171
+ });
172
+ repairedState = reconciledCoinControl.state;
173
+ if (reconciledCoinControl.changed) {
174
+ repairStateNeedsPersist = false;
175
+ }
176
+ let replica = await verifyManagedCoreWalletReplica(repairedState, options.dataDir, {
177
+ nodeHandle: bitcoindHandle,
178
+ attachService: options.attachService,
179
+ rpcFactory: options.rpcFactory,
180
+ });
181
+ let recreatedManagedCoreWallet = false;
182
+ if (replica.proofStatus !== "ready") {
183
+ repairedState = await recreateManagedCoreWalletReplica(repairedState, provider, paths, options.dataDir, nowUnixMs, {
184
+ attachService: options.attachService,
185
+ rpcFactory: options.rpcFactory,
186
+ });
187
+ recreatedManagedCoreWallet = true;
188
+ managedCoreReplicaAction = "recreated";
189
+ repairStateNeedsPersist = false;
190
+ replica = await verifyManagedCoreWalletReplica(repairedState, options.dataDir, {
191
+ nodeHandle: bitcoindHandle,
192
+ attachService: options.attachService,
193
+ rpcFactory: options.rpcFactory,
194
+ });
195
+ }
196
+ const finalBitcoindStatus = await bitcoindHandle.refreshServiceStatus?.() ?? null;
197
+ const chainInfo = await bitcoindRpc.getBlockchainInfo();
198
+ bitcoindPostRepairHealth = mapBitcoindRepairHealth({
199
+ serviceState: finalBitcoindStatus?.state ?? null,
200
+ catchingUp: chainInfo.blocks < chainInfo.headers,
201
+ replica,
202
+ });
203
+ if (bitcoindServiceAction === "none" && initialBitcoindProbe.compatibility === "unreachable") {
204
+ bitcoindServiceAction = "restarted-compatible-service";
205
+ }
206
+ let initialIndexerDaemonInstanceId = null;
207
+ let preAttachIndexerDaemonInstanceId = null;
208
+ const indexerLock = await acquireFileLock(servicePaths.indexerDaemonLockPath, {
209
+ purpose: "indexer-daemon-repair",
210
+ walletRootId: repairedState.walletRootId,
211
+ dataDir: options.dataDir,
212
+ databasePath: options.databasePath,
213
+ });
214
+ try {
215
+ const initialProbe = await probeManagedIndexerDaemon({
216
+ dataDir: options.dataDir,
217
+ walletRootId: repairedState.walletRootId,
218
+ });
219
+ indexerCompatibilityIssue = mapIndexerCompatibilityToRepairIssue(initialProbe.compatibility);
220
+ initialIndexerDaemonInstanceId = initialProbe.status?.daemonInstanceId ?? null;
221
+ if (initialProbe.compatibility === "compatible") {
222
+ await initialProbe.client?.close().catch(() => undefined);
223
+ }
224
+ else if (initialProbe.compatibility === "service-version-mismatch"
225
+ || initialProbe.compatibility === "wallet-root-mismatch"
226
+ || initialProbe.compatibility === "schema-mismatch") {
227
+ const processId = initialProbe.status?.processId ?? null;
228
+ if (processId === null) {
229
+ throw new Error("indexer_daemon_process_id_unavailable");
230
+ }
231
+ try {
232
+ process.kill(processId, "SIGTERM");
233
+ }
234
+ catch (error) {
235
+ if (!(error instanceof Error) || !("code" in error) || error.code !== "ESRCH") {
236
+ throw error;
237
+ }
238
+ }
239
+ await waitForProcessExit(processId);
240
+ await clearIndexerDaemonArtifacts(servicePaths);
241
+ indexerDaemonAction = "stopped-incompatible-daemon";
242
+ }
243
+ else if (initialProbe.compatibility === "unreachable") {
244
+ const hasStaleArtifacts = await Promise.all([
245
+ servicePaths.indexerDaemonSocketPath,
246
+ servicePaths.indexerDaemonStatusPath,
247
+ ].map(pathExists));
248
+ if (hasStaleArtifacts.some(Boolean)) {
249
+ await clearIndexerDaemonArtifacts(servicePaths);
250
+ indexerDaemonAction = "cleared-stale-artifacts";
251
+ }
252
+ }
253
+ else {
254
+ throw new Error(initialProbe.error ?? "indexer_daemon_protocol_error");
255
+ }
256
+ resetIndexerDatabase = await ensureIndexerDatabaseHealthy({
257
+ databasePath: options.databasePath,
258
+ dataDir: options.dataDir,
259
+ walletRootId: repairedState.walletRootId,
260
+ resetIfNeeded: options.assumeYes ?? false,
261
+ });
262
+ }
263
+ finally {
264
+ await indexerLock.release();
265
+ }
266
+ if (recoveredFromBackup) {
267
+ repairedState = await persistRepairState({
268
+ state: repairedState,
269
+ provider,
270
+ paths,
271
+ nowUnixMs,
272
+ replacePrimary: true,
273
+ });
274
+ repairStateNeedsPersist = false;
275
+ }
276
+ else if (repairStateNeedsPersist) {
277
+ repairedState = await persistRepairState({
278
+ state: repairedState,
279
+ provider,
280
+ paths,
281
+ nowUnixMs,
282
+ });
283
+ repairStateNeedsPersist = false;
284
+ }
285
+ const preAttachProbe = await probeManagedIndexerDaemon({
286
+ dataDir: options.dataDir,
287
+ walletRootId: repairedState.walletRootId,
288
+ });
289
+ if (preAttachProbe.compatibility === "compatible") {
290
+ preAttachIndexerDaemonInstanceId = preAttachProbe.status?.daemonInstanceId ?? null;
291
+ await preAttachProbe.client?.close().catch(() => undefined);
292
+ }
293
+ else if (preAttachProbe.compatibility !== "unreachable") {
294
+ throw new Error(preAttachProbe.error ?? "indexer_daemon_protocol_error");
295
+ }
296
+ const daemon = await attachManagedIndexerDaemon({
297
+ dataDir: options.dataDir,
298
+ databasePath: options.databasePath,
299
+ walletRootId: repairedState.walletRootId,
300
+ });
301
+ try {
302
+ const { health: indexerPostRepairHealth, daemonInstanceId: postRepairDaemonInstanceId, } = await verifyIndexerPostRepairHealth({
303
+ daemon,
304
+ probeIndexerDaemon: probeManagedIndexerDaemon,
305
+ dataDir: options.dataDir,
306
+ walletRootId: repairedState.walletRootId,
307
+ nowUnixMs,
308
+ });
309
+ const restartedIndexerDaemon = indexerDaemonAction !== "none" || preAttachProbe.compatibility === "unreachable";
310
+ if (restartedIndexerDaemon
311
+ && initialIndexerDaemonInstanceId !== null
312
+ && postRepairDaemonInstanceId === initialIndexerDaemonInstanceId) {
313
+ throw new Error("indexer_daemon_repair_identity_not_rotated");
314
+ }
315
+ if (!restartedIndexerDaemon
316
+ && preAttachProbe.compatibility === "compatible"
317
+ && preAttachIndexerDaemonInstanceId !== null
318
+ && postRepairDaemonInstanceId !== preAttachIndexerDaemonInstanceId) {
319
+ throw new Error("indexer_daemon_repair_identity_changed");
320
+ }
321
+ if (indexerDaemonAction === "none" && preAttachProbe.compatibility === "unreachable") {
322
+ indexerDaemonAction = "restarted-compatible-daemon";
323
+ }
324
+ const miningResume = await resumeBackgroundMiningAfterRepair({
325
+ miningPreRepairRunMode,
326
+ provider,
327
+ paths,
328
+ repairedState,
329
+ bitcoindPostRepairHealth,
330
+ indexerPostRepairHealth,
331
+ dataDir: options.dataDir,
332
+ databasePath: options.databasePath,
333
+ startBackgroundMining: options.startBackgroundMining,
334
+ });
335
+ await clearLegacyWalletLockArtifacts(paths.walletRuntimeRoot);
336
+ return {
337
+ walletRootId: repairedState.walletRootId,
338
+ recoveredFromBackup,
339
+ recreatedManagedCoreWallet,
340
+ resetIndexerDatabase,
341
+ bitcoindServiceAction,
342
+ bitcoindCompatibilityIssue,
343
+ managedCoreReplicaAction,
344
+ bitcoindPostRepairHealth,
345
+ indexerDaemonAction,
346
+ indexerCompatibilityIssue,
347
+ indexerPostRepairHealth,
348
+ miningPreRepairRunMode,
349
+ miningResumeAction: miningResume.miningResumeAction,
350
+ miningPostRepairRunMode: miningResume.miningPostRepairRunMode,
351
+ miningResumeError: miningResume.miningResumeError,
352
+ note: resetIndexerDatabase
353
+ ? "Indexer artifacts were reset and may still be catching up."
354
+ : null,
355
+ };
356
+ }
357
+ finally {
358
+ await daemon.close().catch(() => undefined);
359
+ }
360
+ }
361
+ finally {
362
+ await bitcoindHandle?.stop?.().catch(() => undefined);
363
+ }
364
+ }
365
+ finally {
366
+ await controlLock.release();
367
+ }
368
+ }
@@ -0,0 +1,16 @@
1
+ import { type WalletRuntimePaths } from "../runtime.js";
2
+ import { type WalletSecretProvider } from "../state/provider.js";
3
+ import type { WalletInitializationResult, WalletPrompter, WalletSetupDependencies } from "./types.js";
4
+ export declare function initializeWallet(options: {
5
+ dataDir: string;
6
+ provider?: WalletSecretProvider;
7
+ prompter: WalletPrompter;
8
+ nowUnixMs?: number;
9
+ paths?: WalletRuntimePaths;
10
+ } & WalletSetupDependencies): Promise<WalletInitializationResult>;
11
+ export declare function showWalletMnemonic(options: {
12
+ provider?: WalletSecretProvider;
13
+ prompter: WalletPrompter;
14
+ nowUnixMs?: number;
15
+ paths?: WalletRuntimePaths;
16
+ }): Promise<void>;