decryption 0.5.1 → 0.6.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.
Files changed (117) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -325
  3. package/index.js +8 -64
  4. package/package.json +8 -24
  5. package/commands/account.d.ts +0 -4
  6. package/commands/account.js +0 -470
  7. package/commands/cosmology.d.ts +0 -4
  8. package/commands/cosmology.js +0 -97
  9. package/commands/doctor.d.ts +0 -3
  10. package/commands/doctor.js +0 -42
  11. package/commands/encrypt.d.ts +0 -12
  12. package/commands/encrypt.js +0 -251
  13. package/commands/guard.d.ts +0 -3
  14. package/commands/guard.js +0 -92
  15. package/commands/keychain.d.ts +0 -4
  16. package/commands/keychain.js +0 -92
  17. package/commands/keys.d.ts +0 -4
  18. package/commands/keys.js +0 -74
  19. package/commands/passkey.d.ts +0 -4
  20. package/commands/passkey.js +0 -135
  21. package/commands/salt.d.ts +0 -4
  22. package/commands/salt.js +0 -113
  23. package/commands/secrets.d.ts +0 -4
  24. package/commands/secrets.js +0 -214
  25. package/commands/shamir.d.ts +0 -4
  26. package/commands/shamir.js +0 -62
  27. package/commands/vault.d.ts +0 -7
  28. package/commands/vault.js +0 -382
  29. package/commands/wallet.d.ts +0 -5
  30. package/commands/wallet.js +0 -129
  31. package/commands/workspace.d.ts +0 -8
  32. package/commands/workspace.js +0 -289
  33. package/commands.d.ts +0 -8
  34. package/commands.js +0 -126
  35. package/esm/commands/account.js +0 -466
  36. package/esm/commands/cosmology.js +0 -93
  37. package/esm/commands/doctor.js +0 -38
  38. package/esm/commands/encrypt.js +0 -245
  39. package/esm/commands/guard.js +0 -88
  40. package/esm/commands/keychain.js +0 -88
  41. package/esm/commands/keys.js +0 -70
  42. package/esm/commands/passkey.js +0 -131
  43. package/esm/commands/salt.js +0 -109
  44. package/esm/commands/secrets.js +0 -210
  45. package/esm/commands/shamir.js +0 -58
  46. package/esm/commands/vault.js +0 -376
  47. package/esm/commands/wallet.js +0 -125
  48. package/esm/commands/workspace.js +0 -283
  49. package/esm/commands.js +0 -120
  50. package/esm/index.js +0 -50
  51. package/esm/utils/bytes.js +0 -286
  52. package/esm/utils/dispatch.js +0 -52
  53. package/esm/utils/env.js +0 -44
  54. package/esm/utils/errors.js +0 -44
  55. package/esm/utils/header.js +0 -14
  56. package/esm/utils/io.js +0 -49
  57. package/esm/utils/passphrase.js +0 -49
  58. package/esm/utils/recipients.js +0 -74
  59. package/esm/utils/stash.js +0 -61
  60. package/esm/utils/temp.js +0 -51
  61. package/esm/utils/text.js +0 -2
  62. package/esm/workspace/bundle.js +0 -239
  63. package/esm/workspace/doctor.js +0 -104
  64. package/esm/workspace/git.js +0 -72
  65. package/esm/workspace/guard.js +0 -111
  66. package/esm/workspace/layout.js +0 -106
  67. package/esm/workspace/manifest.js +0 -147
  68. package/esm/workspace/recipients.js +0 -37
  69. package/esm/workspace/snippets.js +0 -54
  70. package/esm/workspace/state.js +0 -46
  71. package/esm/workspace/status.js +0 -93
  72. package/esm/workspace/tar.js +0 -235
  73. package/index.d.ts +0 -12
  74. package/utils/bytes.d.ts +0 -86
  75. package/utils/bytes.js +0 -301
  76. package/utils/dispatch.d.ts +0 -24
  77. package/utils/dispatch.js +0 -58
  78. package/utils/env.d.ts +0 -26
  79. package/utils/env.js +0 -49
  80. package/utils/errors.d.ts +0 -25
  81. package/utils/errors.js +0 -50
  82. package/utils/header.d.ts +0 -4
  83. package/utils/header.js +0 -18
  84. package/utils/io.d.ts +0 -10
  85. package/utils/io.js +0 -57
  86. package/utils/passphrase.d.ts +0 -15
  87. package/utils/passphrase.js +0 -53
  88. package/utils/recipients.d.ts +0 -28
  89. package/utils/recipients.js +0 -80
  90. package/utils/stash.d.ts +0 -40
  91. package/utils/stash.js +0 -76
  92. package/utils/temp.d.ts +0 -10
  93. package/utils/temp.js +0 -59
  94. package/utils/text.d.ts +0 -2
  95. package/utils/text.js +0 -6
  96. package/workspace/bundle.d.ts +0 -50
  97. package/workspace/bundle.js +0 -248
  98. package/workspace/doctor.d.ts +0 -8
  99. package/workspace/doctor.js +0 -108
  100. package/workspace/git.d.ts +0 -21
  101. package/workspace/git.js +0 -83
  102. package/workspace/guard.d.ts +0 -23
  103. package/workspace/guard.js +0 -118
  104. package/workspace/layout.d.ts +0 -47
  105. package/workspace/layout.js +0 -113
  106. package/workspace/manifest.d.ts +0 -52
  107. package/workspace/manifest.js +0 -157
  108. package/workspace/recipients.d.ts +0 -16
  109. package/workspace/recipients.js +0 -45
  110. package/workspace/snippets.d.ts +0 -29
  111. package/workspace/snippets.js +0 -58
  112. package/workspace/state.d.ts +0 -13
  113. package/workspace/state.js +0 -52
  114. package/workspace/status.d.ts +0 -54
  115. package/workspace/status.js +0 -99
  116. package/workspace/tar.d.ts +0 -29
  117. package/workspace/tar.js +0 -240
@@ -1,376 +0,0 @@
1
- import { defaultModulePath, Vault, } from '@decryption/vault';
2
- import { resolve } from 'appstash';
3
- import { existsSync } from 'fs';
4
- import { join } from 'path';
5
- import { runSubcommand, takeFirst } from '../utils/dispatch';
6
- import { CliError, EXIT } from '../utils/errors';
7
- import { emit, readInput, writeOutput } from '../utils/io';
8
- import { resolvePassphrase } from '../utils/passphrase';
9
- import { dirs } from '../utils/stash';
10
- import { resolveKdf } from './encrypt';
11
- export const vaultUsage = `
12
- Vault Command:
13
-
14
- dcrypt vault <subcommand> [OPTIONS]
15
-
16
- The local encrypted vault, shared with the dcrypt desktop app. The whole
17
- database is one encrypted file; every value inside is additionally encrypted.
18
-
19
- Subcommands:
20
- status Show whether a vault exists and where it lives
21
- item list List items (titles and kinds, never values)
22
- item get <title> Print an item's fields (values require --reveal)
23
- item add <title> Add an item; value read from --in or prompted
24
- item rm <title> Move an item to the trash
25
- totp <title> Print the current one-time code for an item
26
- export Export items as JSON ("--format csv" for CSV)
27
- import Import items from a JSON export ("--from csv|bitwarden")
28
-
29
- Options:
30
- --kind <kind> login | note | card | identity | wallet | totp | ssh_key
31
- --field <name> Field name for "item add" (default depends on kind)
32
- --reveal Include secret values in "item get" output
33
- --format <fmt> Export format: json (default) | csv
34
- --from <fmt> Import format: json (default) | csv | bitwarden
35
- --in <file> / --out Read/write files ("-" is stdin/stdout)
36
- --json Machine-readable output
37
- --passphrase-file <p> Read the master password from a file
38
- --passphrase-stdin Read the master password from stdin
39
- --help, -h Show this help message
40
-
41
- Examples:
42
- dcrypt vault item add github --kind login --field password
43
- dcrypt vault item get github --reveal
44
- dcrypt vault totp work-email
45
- dcrypt vault export --out backup.json
46
- `;
47
- const KINDS = ['login', 'note', 'card', 'identity', 'wallet', 'totp', 'ssh_key'];
48
- export const vaultFile = () => join(resolve(dirs(), 'data', 'db'), 'vault.dcrypt');
49
- export const openVault = async (argv, prompter) => {
50
- const passphrase = await resolvePassphrase(argv, prompter, {
51
- message: 'Master password',
52
- });
53
- try {
54
- return await Vault.open({
55
- file: vaultFile(),
56
- passphrase,
57
- modulePath: defaultModulePath(),
58
- kdf: resolveKdf(argv.kdf),
59
- });
60
- }
61
- catch (error) {
62
- if (error instanceof Error && /passphrase/i.test(error.constructor.name)) {
63
- throw new CliError('wrong master password', EXIT.auth);
64
- }
65
- throw error;
66
- }
67
- };
68
- const findItem = async (vault, ref) => {
69
- const items = await vault.listItems();
70
- const item = items.find((candidate) => candidate.id === ref) ??
71
- items.find((candidate) => candidate.title === ref) ??
72
- items.find((candidate) => candidate.title.toLowerCase() === ref.toLowerCase());
73
- if (!item)
74
- throw new CliError(`no item named "${ref}"`, EXIT.notFound);
75
- return item;
76
- };
77
- const status = async (argv) => {
78
- const file = vaultFile();
79
- emit(argv, { exists: existsSync(file), file }, () => existsSync(file) ? `vault: ${file}` : `no vault yet (would be created at ${file})`);
80
- };
81
- const itemList = async (argv, prompter) => {
82
- const vault = await openVault(argv, prompter);
83
- try {
84
- const items = await vault.listItems(typeof argv.kind === 'string' ? { kind: argv.kind } : {});
85
- emit(argv, items.map((item) => ({ id: item.id, kind: item.kind, title: item.title })), () => items.map((item) => `${item.kind.padEnd(9)} ${item.title}`).join('\n') || '(empty)');
86
- }
87
- finally {
88
- await vault.lock();
89
- }
90
- };
91
- const itemGet = async (argv, prompter) => {
92
- const { first, newArgv } = takeFirst(argv);
93
- if (!first)
94
- throw new CliError('an item title is required');
95
- const vault = await openVault(newArgv, prompter);
96
- try {
97
- const item = await findItem(vault, first);
98
- const fields = await vault.listFields(item.id);
99
- const reveal = Boolean(newArgv.reveal);
100
- const values = {};
101
- for (const field of fields) {
102
- values[field.name] =
103
- reveal || !field.concealed ? await vault.revealField(item.id, field.name) : '(concealed)';
104
- }
105
- emit(newArgv, { id: item.id, kind: item.kind, title: item.title, fields: values }, () => [`${item.title} (${item.kind})`, ...Object.entries(values).map(([k, v]) => ` ${k}: ${v}`)].join('\n'));
106
- }
107
- finally {
108
- await vault.lock();
109
- }
110
- };
111
- const DEFAULT_FIELD = {
112
- login: { name: 'password', purpose: 'password' },
113
- totp: { name: 'seed', purpose: 'totp_seed' },
114
- wallet: { name: 'mnemonic', purpose: 'mnemonic' },
115
- ssh_key: { name: 'private_key', purpose: 'private_key' },
116
- };
117
- const itemAdd = async (argv, prompter) => {
118
- const { first, newArgv } = takeFirst(argv);
119
- if (!first)
120
- throw new CliError('an item title is required');
121
- const kind = (typeof newArgv.kind === 'string' ? newArgv.kind : 'login');
122
- if (!KINDS.includes(kind))
123
- throw new CliError(`unknown kind "${kind}"`);
124
- const preset = DEFAULT_FIELD[kind] ?? { name: 'value', purpose: 'text' };
125
- const fieldName = typeof newArgv.field === 'string' ? newArgv.field : preset.name;
126
- const value = readInput(newArgv);
127
- const vault = await openVault(newArgv, prompter);
128
- try {
129
- const item = await vault.createItem(kind, first);
130
- await vault.setField(item.id, fieldName, preset.purpose, value.trim());
131
- emit(newArgv, { id: item.id, title: item.title }, () => `added "${item.title}" (${item.id})`);
132
- }
133
- finally {
134
- await vault.lock();
135
- }
136
- };
137
- const itemRm = async (argv, prompter) => {
138
- const { first, newArgv } = takeFirst(argv);
139
- if (!first)
140
- throw new CliError('an item title is required');
141
- const vault = await openVault(newArgv, prompter);
142
- try {
143
- const item = await findItem(vault, first);
144
- await vault.trashItem(item.id);
145
- emit(newArgv, { id: item.id }, () => `moved "${item.title}" to trash`);
146
- }
147
- finally {
148
- await vault.lock();
149
- }
150
- };
151
- const itemCommand = async (argv, prompter) => runSubcommand(argv, prompter, {
152
- name: 'vault item',
153
- usage: vaultUsage,
154
- handlers: { list: itemList, get: itemGet, add: itemAdd, rm: itemRm },
155
- });
156
- const totp = async (argv, prompter) => {
157
- const { first, newArgv } = takeFirst(argv);
158
- if (!first)
159
- throw new CliError('an item title is required');
160
- const vault = await openVault(newArgv, prompter);
161
- try {
162
- const item = await findItem(vault, first);
163
- const fields = await vault.listFields(item.id);
164
- if (!fields.some((field) => field.purpose === 'totp_seed')) {
165
- throw new CliError(`"${item.title}" has no one-time-code secret`);
166
- }
167
- const numeric = async (name, fallback) => {
168
- if (!fields.some((field) => field.name === name))
169
- return fallback;
170
- const value = Number(await vault.revealField(item.id, name));
171
- return Number.isInteger(value) && value > 0 ? value : fallback;
172
- };
173
- const code = await vault.totpCode(item.id, {
174
- period: await numeric('period', 30),
175
- digits: await numeric('digits', 6),
176
- });
177
- emit(newArgv, { code }, () => code);
178
- }
179
- finally {
180
- await vault.lock();
181
- }
182
- };
183
- const exportItems = async (vault) => {
184
- const items = await vault.listItems();
185
- const result = [];
186
- for (const item of items) {
187
- const fields = {};
188
- for (const field of await vault.listFields(item.id)) {
189
- fields[field.name] = await vault.revealField(item.id, field.name);
190
- }
191
- result.push({
192
- kind: item.kind,
193
- title: item.title,
194
- favorite: item.favorite,
195
- fields,
196
- urls: await vault.listUrls(item.id),
197
- tags: (await vault.listTags(item.id)).map((tag) => tag.name),
198
- });
199
- }
200
- return result;
201
- };
202
- const csvEscape = (value) => /[",\n]/.test(value) ? `"${value.replace(/"/g, '""')}"` : value;
203
- const exportCommand = async (argv, prompter) => {
204
- const format = typeof argv.format === 'string' ? argv.format : 'json';
205
- const vault = await openVault(argv, prompter);
206
- try {
207
- const items = await exportItems(vault);
208
- let output;
209
- if (format === 'csv') {
210
- const rows = items.map((item) => [
211
- item.title,
212
- item.kind,
213
- item.fields.username ?? '',
214
- item.fields.password ?? item.fields.value ?? '',
215
- item.urls[0] ?? '',
216
- item.tags.join(';'),
217
- ]
218
- .map(csvEscape)
219
- .join(','));
220
- output = ['title,kind,username,password,url,tags', ...rows].join('\n') + '\n';
221
- }
222
- else if (format === 'json') {
223
- output = JSON.stringify({ version: 1, items }, null, 2) + '\n';
224
- }
225
- else {
226
- throw new CliError(`unknown export format "${format}"`);
227
- }
228
- writeOutput(argv, output);
229
- if (argv.out && argv.out !== '-') {
230
- process.stderr.write('warning: the export contains plaintext secrets — handle with care\n');
231
- }
232
- }
233
- finally {
234
- await vault.lock();
235
- }
236
- };
237
- const parseCsvLine = (line) => {
238
- const cells = [];
239
- let current = '';
240
- let quoted = false;
241
- for (let i = 0; i < line.length; i++) {
242
- const char = line[i];
243
- if (quoted) {
244
- if (char === '"' && line[i + 1] === '"') {
245
- current += '"';
246
- i++;
247
- }
248
- else if (char === '"') {
249
- quoted = false;
250
- }
251
- else {
252
- current += char;
253
- }
254
- }
255
- else if (char === '"') {
256
- quoted = true;
257
- }
258
- else if (char === ',') {
259
- cells.push(current);
260
- current = '';
261
- }
262
- else {
263
- current += char;
264
- }
265
- }
266
- cells.push(current);
267
- return cells;
268
- };
269
- const parseImport = (raw, from) => {
270
- if (from === 'json') {
271
- const parsed = JSON.parse(raw);
272
- if (!Array.isArray(parsed.items))
273
- throw new CliError('not a dcrypt vault export');
274
- return parsed.items.map((item) => ({
275
- kind: KINDS.includes(item.kind) ? item.kind : 'note',
276
- title: item.title,
277
- fields: item.fields ?? {},
278
- urls: item.urls ?? [],
279
- tags: item.tags ?? [],
280
- }));
281
- }
282
- if (from === 'bitwarden') {
283
- const parsed = JSON.parse(raw);
284
- if (!Array.isArray(parsed.items))
285
- throw new CliError('not a bitwarden export');
286
- return parsed.items.map((entry) => {
287
- const fields = {};
288
- if (entry.login?.username)
289
- fields.username = entry.login.username;
290
- if (entry.login?.password)
291
- fields.password = entry.login.password;
292
- if (entry.login?.totp)
293
- fields.seed = entry.login.totp;
294
- if (entry.notes)
295
- fields.note = entry.notes;
296
- return {
297
- kind: entry.type === 1 ? 'login' : 'note',
298
- title: entry.name,
299
- fields,
300
- urls: entry.login?.uris?.map((u) => u.uri) ?? [],
301
- tags: [],
302
- };
303
- });
304
- }
305
- if (from === 'csv') {
306
- const [header, ...lines] = raw.trim().split(/\r?\n/);
307
- const columns = parseCsvLine(header).map((c) => c.trim().toLowerCase());
308
- return lines.filter(Boolean).map((line) => {
309
- const cells = parseCsvLine(line);
310
- const get = (...names) => {
311
- for (const name of names) {
312
- const index = columns.indexOf(name);
313
- if (index >= 0 && cells[index])
314
- return cells[index];
315
- }
316
- return '';
317
- };
318
- const fields = {};
319
- const username = get('username', 'login_username', 'user');
320
- const password = get('password', 'login_password');
321
- if (username)
322
- fields.username = username;
323
- if (password)
324
- fields.password = password;
325
- const url = get('url', 'login_uri', 'website');
326
- return {
327
- kind: 'login',
328
- title: get('title', 'name', 'account') || url || 'imported',
329
- fields,
330
- urls: url ? [url] : [],
331
- tags: [],
332
- };
333
- });
334
- }
335
- throw new CliError(`unknown import format "${from}"`);
336
- };
337
- const FIELD_PURPOSE = {
338
- username: 'username',
339
- password: 'password',
340
- seed: 'totp_seed',
341
- mnemonic: 'mnemonic',
342
- private_key: 'private_key',
343
- };
344
- const importCommand = async (argv, prompter) => {
345
- const from = typeof argv.from === 'string' ? argv.from : 'json';
346
- const raw = readInput(argv);
347
- const imported = parseImport(raw, from);
348
- const vault = await openVault(argv, prompter);
349
- try {
350
- for (const entry of imported) {
351
- const item = await vault.createItem(entry.kind, entry.title);
352
- for (const [name, value] of Object.entries(entry.fields)) {
353
- await vault.setField(item.id, name, FIELD_PURPOSE[name] ?? 'text', value, name !== 'username');
354
- }
355
- for (const url of entry.urls)
356
- await vault.addUrl(item.id, url);
357
- for (const tag of entry.tags)
358
- await vault.tagItem(item.id, tag);
359
- }
360
- emit(argv, { imported: imported.length }, () => `imported ${imported.length} item(s)`);
361
- }
362
- finally {
363
- await vault.lock();
364
- }
365
- };
366
- export const vaultCommand = async (argv, prompter) => runSubcommand(argv, prompter, {
367
- name: 'vault',
368
- usage: vaultUsage,
369
- handlers: {
370
- status,
371
- item: itemCommand,
372
- totp,
373
- export: exportCommand,
374
- import: importCommand,
375
- },
376
- });
@@ -1,125 +0,0 @@
1
- import { armor, encrypt } from '@decryption/core';
2
- import { assertValidMnemonic, createMnemonic, DEFAULT_NETWORK, deriveAccount, isValidMnemonic, NETWORKS, WORD_COUNTS, } from '@decryption/wallet';
3
- import { runSubcommand, takeFirst, wantsHelp } from '../utils/dispatch';
4
- import { fromEnv } from '../utils/env';
5
- import { CliError } from '../utils/errors';
6
- import { emit, readInput, writeOutput } from '../utils/io';
7
- import { resolvePassphrase } from '../utils/passphrase';
8
- import { resolveKdf } from './encrypt';
9
- export const walletUsage = `
10
- Wallet Command:
11
-
12
- dcrypt wallet <subcommand> [OPTIONS]
13
-
14
- Create BIP39 wallets and derive addresses. Entirely offline: this command never
15
- contacts a network, signs a transaction, or queries a balance.
16
-
17
- Subcommands:
18
- create Generate a mnemonic and show its addresses
19
- address Derive an address from an existing mnemonic
20
- validate Check a mnemonic's wordlist and checksum
21
-
22
- Options:
23
- --words <n> 12, 15, 18, 21 or 24 (default: 24)
24
- --network <id> Network id, repeatable (default: bitcoin)
25
- --account <n> BIP44 account index (default: 0)
26
- --index <n> BIP44 address index (default: 0)
27
- --path <path> Explicit derivation path, overrides --account/--index
28
- --in <file> Read the mnemonic from a file ("-" for stdin)
29
- --encrypt Encrypt the new mnemonic and print the envelope instead
30
- --kdf <profile> interactive | moderate | sensitive (default: moderate)
31
- --out <file> Write output to a file
32
- --json Machine-readable output
33
- --help, -h Show this help message
34
-
35
- Environment:
36
- MNEMONIC The mnemonic, for "address" and "validate"
37
-
38
- Networks:
39
- ${Object.keys(NETWORKS).join(', ')}
40
-
41
- Examples:
42
- dcrypt wallet create --words 12 --network bitcoin --network ethereum
43
- dcrypt wallet create --encrypt --out wallet.dcrypt
44
- dcrypt wallet address --in mnemonic.txt --network bitcoin --index 3
45
- MNEMONIC="..." dcrypt wallet address --network ethereum
46
- echo "$MNEMONIC" | dcrypt wallet validate --in -
47
- `;
48
- const networksOf = (argv) => {
49
- const value = argv.network ?? argv.networks ?? DEFAULT_NETWORK;
50
- return (Array.isArray(value) ? value : [value]).map(String);
51
- };
52
- const derivationOptions = (argv) => ({
53
- account: argv.account === undefined ? 0 : Number(argv.account),
54
- index: argv.index === undefined ? 0 : Number(argv.index),
55
- path: typeof argv.path === 'string' ? argv.path : undefined,
56
- });
57
- const create = async (argv, prompter) => {
58
- const words = Number(argv.words ?? 24);
59
- if (!(words in WORD_COUNTS)) {
60
- throw new CliError(`--words must be one of ${Object.keys(WORD_COUNTS).join(', ')}`);
61
- }
62
- const mnemonic = createMnemonic(words);
63
- const accounts = networksOf(argv).map((network) => deriveAccount(mnemonic, network, derivationOptions(argv)));
64
- if (argv.encrypt) {
65
- const passphrase = await resolvePassphrase(argv, prompter, {
66
- confirm: true,
67
- message: 'Passphrase to encrypt the new mnemonic',
68
- });
69
- const envelope = armor(encrypt(mnemonic, passphrase, { kdf: resolveKdf(argv.kdf) }));
70
- emit(argv, { accounts, mnemonic: envelope }, () => [...accounts.map((account) => `${account.network}\t${account.address}`), '', envelope].join('\n'));
71
- return;
72
- }
73
- emit(argv, { mnemonic, accounts }, () => [
74
- mnemonic,
75
- '',
76
- ...accounts.map((account) => `${account.network}\t${account.path}\t${account.address}`),
77
- ].join('\n'));
78
- };
79
- const address = async (argv, prompter) => {
80
- const mnemonic = await readMnemonic(argv, prompter);
81
- const accounts = networksOf(argv).map((network) => deriveAccount(mnemonic, network, derivationOptions(argv)));
82
- emit(argv, { accounts }, () => accounts.map((account) => `${account.network}\t${account.path}\t${account.address}`).join('\n'));
83
- };
84
- const validate = async (argv, prompter) => {
85
- const mnemonic = await readMnemonic(argv, prompter);
86
- if (argv.json) {
87
- let reason = null;
88
- try {
89
- assertValidMnemonic(mnemonic);
90
- }
91
- catch (error) {
92
- reason = error.message;
93
- }
94
- writeOutput(argv, JSON.stringify({ valid: reason === null, reason }, null, 2));
95
- if (reason)
96
- throw new CliError(reason);
97
- return;
98
- }
99
- assertValidMnemonic(mnemonic);
100
- writeOutput(argv, `valid (${mnemonic.split(' ').length} words)`);
101
- };
102
- const readMnemonic = async (argv, prompter) => {
103
- const { first, newArgv } = takeFirst(argv);
104
- if (first || newArgv.in)
105
- return readInput(newArgv, first).trim();
106
- const envMnemonic = fromEnv('mnemonic');
107
- if (envMnemonic !== undefined)
108
- return envMnemonic.trim();
109
- if (!process.stdin.isTTY)
110
- return readInput(newArgv, first).trim();
111
- const { mnemonic } = await prompter.prompt({}, [
112
- { type: 'password', name: 'mnemonic', message: 'Mnemonic', required: true },
113
- ]);
114
- return String(mnemonic).trim();
115
- };
116
- export const walletCommand = async (argv, prompter) => {
117
- if (wantsHelp(argv, walletUsage) && !argv._.length)
118
- return;
119
- await runSubcommand(argv, prompter, {
120
- name: 'wallet',
121
- usage: walletUsage,
122
- handlers: { create, address, validate },
123
- });
124
- };
125
- export const isMnemonicValid = isValidMnemonic;