chainlesschain 0.37.9 → 0.37.11

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 (84) hide show
  1. package/README.md +309 -19
  2. package/bin/chainlesschain.js +4 -0
  3. package/package.json +1 -1
  4. package/src/commands/a2a.js +374 -0
  5. package/src/commands/audit.js +286 -0
  6. package/src/commands/auth.js +387 -0
  7. package/src/commands/bi.js +240 -0
  8. package/src/commands/browse.js +184 -0
  9. package/src/commands/cowork.js +317 -0
  10. package/src/commands/did.js +376 -0
  11. package/src/commands/economy.js +375 -0
  12. package/src/commands/encrypt.js +233 -0
  13. package/src/commands/evolution.js +398 -0
  14. package/src/commands/export.js +125 -0
  15. package/src/commands/git.js +215 -0
  16. package/src/commands/hmemory.js +273 -0
  17. package/src/commands/hook.js +260 -0
  18. package/src/commands/import.js +259 -0
  19. package/src/commands/init.js +184 -0
  20. package/src/commands/instinct.js +202 -0
  21. package/src/commands/llm.js +155 -4
  22. package/src/commands/lowcode.js +320 -0
  23. package/src/commands/mcp.js +302 -0
  24. package/src/commands/memory.js +282 -0
  25. package/src/commands/note.js +187 -0
  26. package/src/commands/org.js +505 -0
  27. package/src/commands/p2p.js +274 -0
  28. package/src/commands/plugin.js +451 -0
  29. package/src/commands/sandbox.js +366 -0
  30. package/src/commands/search.js +237 -0
  31. package/src/commands/session.js +238 -0
  32. package/src/commands/skill.js +254 -201
  33. package/src/commands/sync.js +249 -0
  34. package/src/commands/tokens.js +214 -0
  35. package/src/commands/wallet.js +416 -0
  36. package/src/commands/workflow.js +359 -0
  37. package/src/commands/zkp.js +277 -0
  38. package/src/index.js +93 -1
  39. package/src/lib/a2a-protocol.js +371 -0
  40. package/src/lib/agent-coordinator.js +273 -0
  41. package/src/lib/agent-economy.js +369 -0
  42. package/src/lib/app-builder.js +377 -0
  43. package/src/lib/audit-logger.js +364 -0
  44. package/src/lib/bi-engine.js +299 -0
  45. package/src/lib/bm25-search.js +322 -0
  46. package/src/lib/browser-automation.js +216 -0
  47. package/src/lib/cowork/ab-comparator-cli.js +180 -0
  48. package/src/lib/cowork/code-knowledge-graph-cli.js +232 -0
  49. package/src/lib/cowork/debate-review-cli.js +144 -0
  50. package/src/lib/cowork/decision-kb-cli.js +153 -0
  51. package/src/lib/cowork/project-style-analyzer-cli.js +168 -0
  52. package/src/lib/cowork-adapter.js +106 -0
  53. package/src/lib/crypto-manager.js +246 -0
  54. package/src/lib/did-manager.js +270 -0
  55. package/src/lib/ensure-utf8.js +59 -0
  56. package/src/lib/evolution-system.js +508 -0
  57. package/src/lib/git-integration.js +220 -0
  58. package/src/lib/hierarchical-memory.js +471 -0
  59. package/src/lib/hook-manager.js +387 -0
  60. package/src/lib/instinct-manager.js +190 -0
  61. package/src/lib/knowledge-exporter.js +302 -0
  62. package/src/lib/knowledge-importer.js +293 -0
  63. package/src/lib/llm-providers.js +325 -0
  64. package/src/lib/mcp-client.js +413 -0
  65. package/src/lib/memory-manager.js +211 -0
  66. package/src/lib/note-versioning.js +244 -0
  67. package/src/lib/org-manager.js +424 -0
  68. package/src/lib/p2p-manager.js +317 -0
  69. package/src/lib/pdf-parser.js +96 -0
  70. package/src/lib/permission-engine.js +374 -0
  71. package/src/lib/plan-mode.js +333 -0
  72. package/src/lib/plugin-manager.js +430 -0
  73. package/src/lib/project-detector.js +53 -0
  74. package/src/lib/response-cache.js +156 -0
  75. package/src/lib/sandbox-v2.js +503 -0
  76. package/src/lib/service-container.js +183 -0
  77. package/src/lib/session-manager.js +189 -0
  78. package/src/lib/skill-loader.js +274 -0
  79. package/src/lib/sync-manager.js +347 -0
  80. package/src/lib/token-tracker.js +200 -0
  81. package/src/lib/wallet-manager.js +348 -0
  82. package/src/lib/workflow-engine.js +503 -0
  83. package/src/lib/zkp-engine.js +241 -0
  84. package/src/repl/agent-repl.js +259 -124
@@ -0,0 +1,348 @@
1
+ /**
2
+ * Wallet Manager — Digital wallet and asset management for CLI.
3
+ * Uses Node.js crypto for key generation. Blockchain operations
4
+ * are local-only (no real chain interaction without bridge).
5
+ */
6
+
7
+ import crypto from "crypto";
8
+
9
+ /**
10
+ * Ensure wallet tables exist.
11
+ */
12
+ export function ensureWalletTables(db) {
13
+ db.exec(`
14
+ CREATE TABLE IF NOT EXISTS wallets (
15
+ address TEXT PRIMARY KEY,
16
+ name TEXT,
17
+ wallet_type TEXT DEFAULT 'standard',
18
+ public_key TEXT NOT NULL,
19
+ encrypted_key TEXT NOT NULL,
20
+ balance TEXT DEFAULT '0',
21
+ is_default INTEGER DEFAULT 0,
22
+ created_at TEXT DEFAULT (datetime('now')),
23
+ updated_at TEXT DEFAULT (datetime('now'))
24
+ )
25
+ `);
26
+ db.exec(`
27
+ CREATE TABLE IF NOT EXISTS digital_assets (
28
+ id TEXT PRIMARY KEY,
29
+ wallet_address TEXT NOT NULL,
30
+ asset_type TEXT NOT NULL,
31
+ name TEXT NOT NULL,
32
+ description TEXT,
33
+ metadata TEXT,
34
+ amount TEXT DEFAULT '1',
35
+ status TEXT DEFAULT 'active',
36
+ created_at TEXT DEFAULT (datetime('now')),
37
+ updated_at TEXT DEFAULT (datetime('now'))
38
+ )
39
+ `);
40
+ db.exec(`
41
+ CREATE TABLE IF NOT EXISTS transactions (
42
+ id TEXT PRIMARY KEY,
43
+ from_address TEXT,
44
+ to_address TEXT,
45
+ asset_id TEXT,
46
+ amount TEXT NOT NULL,
47
+ tx_type TEXT NOT NULL,
48
+ status TEXT DEFAULT 'pending',
49
+ metadata TEXT,
50
+ created_at TEXT DEFAULT (datetime('now'))
51
+ )
52
+ `);
53
+ }
54
+
55
+ /**
56
+ * Generate a wallet address from public key.
57
+ */
58
+ export function generateAddress(publicKeyHex) {
59
+ const hash = crypto
60
+ .createHash("sha256")
61
+ .update(Buffer.from(publicKeyHex, "hex"))
62
+ .digest();
63
+ return `0x${hash.toString("hex").slice(0, 40)}`;
64
+ }
65
+
66
+ /**
67
+ * Create a new wallet.
68
+ */
69
+ export function createWallet(db, name, password) {
70
+ ensureWalletTables(db);
71
+
72
+ const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519", {
73
+ publicKeyEncoding: { type: "spki", format: "der" },
74
+ privateKeyEncoding: { type: "pkcs8", format: "der" },
75
+ });
76
+
77
+ const publicKeyHex = publicKey.toString("hex");
78
+ const address = generateAddress(publicKeyHex);
79
+
80
+ // Encrypt private key with password
81
+ const salt = crypto.randomBytes(16);
82
+ const key = crypto.pbkdf2Sync(
83
+ password || "default",
84
+ salt,
85
+ 100000,
86
+ 32,
87
+ "sha256",
88
+ );
89
+ const iv = crypto.randomBytes(12);
90
+ const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
91
+ const encrypted = Buffer.concat([cipher.update(privateKey), cipher.final()]);
92
+ const tag = cipher.getAuthTag();
93
+
94
+ const encryptedKey = JSON.stringify({
95
+ salt: salt.toString("hex"),
96
+ iv: iv.toString("hex"),
97
+ data: encrypted.toString("hex"),
98
+ tag: tag.toString("hex"),
99
+ });
100
+
101
+ // First wallet is default
102
+ const count = db.prepare("SELECT COUNT(*) as c FROM wallets").get().c;
103
+ const isDefault = count === 0 ? 1 : 0;
104
+
105
+ db.prepare(
106
+ `INSERT INTO wallets (address, name, wallet_type, public_key, encrypted_key, balance, is_default)
107
+ VALUES (?, ?, ?, ?, ?, ?, ?)`,
108
+ ).run(
109
+ address,
110
+ name || null,
111
+ "standard",
112
+ publicKeyHex,
113
+ encryptedKey,
114
+ "0",
115
+ isDefault,
116
+ );
117
+
118
+ return {
119
+ address,
120
+ name,
121
+ walletType: "standard",
122
+ publicKey: publicKeyHex,
123
+ balance: "0",
124
+ isDefault: isDefault === 1,
125
+ };
126
+ }
127
+
128
+ /**
129
+ * Get a wallet by address.
130
+ */
131
+ export function getWallet(db, address) {
132
+ ensureWalletTables(db);
133
+ return db.prepare("SELECT * FROM wallets WHERE address = ?").get(address);
134
+ }
135
+
136
+ /**
137
+ * Get the default wallet.
138
+ */
139
+ export function getDefaultWallet(db) {
140
+ ensureWalletTables(db);
141
+ return db.prepare("SELECT * FROM wallets WHERE is_default = 1").get();
142
+ }
143
+
144
+ /**
145
+ * Get all wallets.
146
+ */
147
+ export function getAllWallets(db) {
148
+ ensureWalletTables(db);
149
+ return db
150
+ .prepare("SELECT * FROM wallets ORDER BY is_default DESC, created_at DESC")
151
+ .all();
152
+ }
153
+
154
+ /**
155
+ * Set a wallet as default.
156
+ */
157
+ export function setDefaultWallet(db, address) {
158
+ ensureWalletTables(db);
159
+ const wallet = getWallet(db, address);
160
+ if (!wallet) return false;
161
+
162
+ db.prepare("UPDATE wallets SET is_default = 0 WHERE address LIKE ?").run("%");
163
+ db.prepare("UPDATE wallets SET is_default = 1 WHERE address = ?").run(
164
+ address,
165
+ );
166
+ return true;
167
+ }
168
+
169
+ /**
170
+ * Delete a wallet.
171
+ */
172
+ export function deleteWallet(db, address) {
173
+ ensureWalletTables(db);
174
+ const result = db
175
+ .prepare("DELETE FROM wallets WHERE address = ?")
176
+ .run(address);
177
+ if (result.changes > 0) {
178
+ // Promote next wallet to default
179
+ const next = db
180
+ .prepare("SELECT address FROM wallets ORDER BY created_at ASC LIMIT 1")
181
+ .get();
182
+ if (next) {
183
+ db.prepare("UPDATE wallets SET is_default = 1 WHERE address = ?").run(
184
+ next.address,
185
+ );
186
+ }
187
+ }
188
+ return result.changes > 0;
189
+ }
190
+
191
+ /**
192
+ * Get wallet balance.
193
+ */
194
+ export function getBalance(db, address) {
195
+ ensureWalletTables(db);
196
+ const wallet = getWallet(db, address);
197
+ if (!wallet) return null;
198
+ return { address, balance: wallet.balance, name: wallet.name };
199
+ }
200
+
201
+ /**
202
+ * Create a digital asset.
203
+ */
204
+ export function createAsset(
205
+ db,
206
+ walletAddress,
207
+ assetType,
208
+ name,
209
+ description,
210
+ metadata,
211
+ ) {
212
+ ensureWalletTables(db);
213
+ const wallet = getWallet(db, walletAddress);
214
+ if (!wallet) throw new Error(`Wallet not found: ${walletAddress}`);
215
+
216
+ const id = `asset-${crypto.randomBytes(8).toString("hex")}`;
217
+
218
+ db.prepare(
219
+ `INSERT INTO digital_assets (id, wallet_address, asset_type, name, description, metadata, amount, status)
220
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
221
+ ).run(
222
+ id,
223
+ walletAddress,
224
+ assetType,
225
+ name,
226
+ description || null,
227
+ metadata ? JSON.stringify(metadata) : null,
228
+ "1",
229
+ "active",
230
+ );
231
+
232
+ return {
233
+ id,
234
+ walletAddress,
235
+ assetType,
236
+ name,
237
+ description,
238
+ amount: "1",
239
+ status: "active",
240
+ };
241
+ }
242
+
243
+ /**
244
+ * Get assets for a wallet.
245
+ */
246
+ export function getAssets(db, walletAddress) {
247
+ ensureWalletTables(db);
248
+ return db
249
+ .prepare(
250
+ "SELECT * FROM digital_assets WHERE wallet_address = ? ORDER BY created_at DESC",
251
+ )
252
+ .all(walletAddress);
253
+ }
254
+
255
+ /**
256
+ * Get all assets across all wallets.
257
+ */
258
+ export function getAllAssets(db) {
259
+ ensureWalletTables(db);
260
+ return db
261
+ .prepare("SELECT * FROM digital_assets ORDER BY created_at DESC")
262
+ .all();
263
+ }
264
+
265
+ /**
266
+ * Get asset by ID.
267
+ */
268
+ export function getAsset(db, assetId) {
269
+ ensureWalletTables(db);
270
+ return db.prepare("SELECT * FROM digital_assets WHERE id = ?").get(assetId);
271
+ }
272
+
273
+ /**
274
+ * Transfer an asset to another wallet.
275
+ */
276
+ export function transferAsset(db, assetId, toAddress, amount) {
277
+ ensureWalletTables(db);
278
+ const asset = getAsset(db, assetId);
279
+ if (!asset) throw new Error(`Asset not found: ${assetId}`);
280
+
281
+ const txId = `tx-${crypto.randomBytes(8).toString("hex")}`;
282
+ const fromAddress = asset.wallet_address;
283
+ const txAmount = amount || asset.amount;
284
+
285
+ db.prepare(
286
+ `INSERT INTO transactions (id, from_address, to_address, asset_id, amount, tx_type, status)
287
+ VALUES (?, ?, ?, ?, ?, ?, ?)`,
288
+ ).run(
289
+ txId,
290
+ fromAddress,
291
+ toAddress,
292
+ assetId,
293
+ txAmount,
294
+ "transfer",
295
+ "confirmed",
296
+ );
297
+
298
+ // Update asset ownership
299
+ db.prepare("UPDATE digital_assets SET wallet_address = ? WHERE id = ?").run(
300
+ toAddress,
301
+ assetId,
302
+ );
303
+
304
+ return {
305
+ txId,
306
+ from: fromAddress,
307
+ to: toAddress,
308
+ assetId,
309
+ amount: txAmount,
310
+ status: "confirmed",
311
+ };
312
+ }
313
+
314
+ /**
315
+ * Get transaction history.
316
+ */
317
+ export function getTransactions(db, options = {}) {
318
+ ensureWalletTables(db);
319
+ const { address, limit = 50 } = options;
320
+
321
+ if (address) {
322
+ return db
323
+ .prepare(
324
+ "SELECT * FROM transactions WHERE from_address = ? OR to_address = ? ORDER BY created_at DESC LIMIT ?",
325
+ )
326
+ .all(address, address, limit);
327
+ }
328
+
329
+ return db
330
+ .prepare("SELECT * FROM transactions ORDER BY created_at DESC LIMIT ?")
331
+ .all(limit);
332
+ }
333
+
334
+ /**
335
+ * Get wallet summary.
336
+ */
337
+ export function getWalletSummary(db) {
338
+ ensureWalletTables(db);
339
+ const wallets = db.prepare("SELECT COUNT(*) as c FROM wallets").get();
340
+ const assets = db.prepare("SELECT COUNT(*) as c FROM digital_assets").get();
341
+ const txns = db.prepare("SELECT COUNT(*) as c FROM transactions").get();
342
+
343
+ return {
344
+ walletCount: wallets?.c || 0,
345
+ assetCount: assets?.c || 0,
346
+ transactionCount: txns?.c || 0,
347
+ };
348
+ }