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.
- package/LICENSE +21 -0
- package/README.md +17 -325
- package/index.js +8 -64
- package/package.json +8 -24
- package/commands/account.d.ts +0 -4
- package/commands/account.js +0 -470
- package/commands/cosmology.d.ts +0 -4
- package/commands/cosmology.js +0 -97
- package/commands/doctor.d.ts +0 -3
- package/commands/doctor.js +0 -42
- package/commands/encrypt.d.ts +0 -12
- package/commands/encrypt.js +0 -251
- package/commands/guard.d.ts +0 -3
- package/commands/guard.js +0 -92
- package/commands/keychain.d.ts +0 -4
- package/commands/keychain.js +0 -92
- package/commands/keys.d.ts +0 -4
- package/commands/keys.js +0 -74
- package/commands/passkey.d.ts +0 -4
- package/commands/passkey.js +0 -135
- package/commands/salt.d.ts +0 -4
- package/commands/salt.js +0 -113
- package/commands/secrets.d.ts +0 -4
- package/commands/secrets.js +0 -214
- package/commands/shamir.d.ts +0 -4
- package/commands/shamir.js +0 -62
- package/commands/vault.d.ts +0 -7
- package/commands/vault.js +0 -382
- package/commands/wallet.d.ts +0 -5
- package/commands/wallet.js +0 -129
- package/commands/workspace.d.ts +0 -8
- package/commands/workspace.js +0 -289
- package/commands.d.ts +0 -8
- package/commands.js +0 -126
- package/esm/commands/account.js +0 -466
- package/esm/commands/cosmology.js +0 -93
- package/esm/commands/doctor.js +0 -38
- package/esm/commands/encrypt.js +0 -245
- package/esm/commands/guard.js +0 -88
- package/esm/commands/keychain.js +0 -88
- package/esm/commands/keys.js +0 -70
- package/esm/commands/passkey.js +0 -131
- package/esm/commands/salt.js +0 -109
- package/esm/commands/secrets.js +0 -210
- package/esm/commands/shamir.js +0 -58
- package/esm/commands/vault.js +0 -376
- package/esm/commands/wallet.js +0 -125
- package/esm/commands/workspace.js +0 -283
- package/esm/commands.js +0 -120
- package/esm/index.js +0 -50
- package/esm/utils/bytes.js +0 -286
- package/esm/utils/dispatch.js +0 -52
- package/esm/utils/env.js +0 -44
- package/esm/utils/errors.js +0 -44
- package/esm/utils/header.js +0 -14
- package/esm/utils/io.js +0 -49
- package/esm/utils/passphrase.js +0 -49
- package/esm/utils/recipients.js +0 -74
- package/esm/utils/stash.js +0 -61
- package/esm/utils/temp.js +0 -51
- package/esm/utils/text.js +0 -2
- package/esm/workspace/bundle.js +0 -239
- package/esm/workspace/doctor.js +0 -104
- package/esm/workspace/git.js +0 -72
- package/esm/workspace/guard.js +0 -111
- package/esm/workspace/layout.js +0 -106
- package/esm/workspace/manifest.js +0 -147
- package/esm/workspace/recipients.js +0 -37
- package/esm/workspace/snippets.js +0 -54
- package/esm/workspace/state.js +0 -46
- package/esm/workspace/status.js +0 -93
- package/esm/workspace/tar.js +0 -235
- package/index.d.ts +0 -12
- package/utils/bytes.d.ts +0 -86
- package/utils/bytes.js +0 -301
- package/utils/dispatch.d.ts +0 -24
- package/utils/dispatch.js +0 -58
- package/utils/env.d.ts +0 -26
- package/utils/env.js +0 -49
- package/utils/errors.d.ts +0 -25
- package/utils/errors.js +0 -50
- package/utils/header.d.ts +0 -4
- package/utils/header.js +0 -18
- package/utils/io.d.ts +0 -10
- package/utils/io.js +0 -57
- package/utils/passphrase.d.ts +0 -15
- package/utils/passphrase.js +0 -53
- package/utils/recipients.d.ts +0 -28
- package/utils/recipients.js +0 -80
- package/utils/stash.d.ts +0 -40
- package/utils/stash.js +0 -76
- package/utils/temp.d.ts +0 -10
- package/utils/temp.js +0 -59
- package/utils/text.d.ts +0 -2
- package/utils/text.js +0 -6
- package/workspace/bundle.d.ts +0 -50
- package/workspace/bundle.js +0 -248
- package/workspace/doctor.d.ts +0 -8
- package/workspace/doctor.js +0 -108
- package/workspace/git.d.ts +0 -21
- package/workspace/git.js +0 -83
- package/workspace/guard.d.ts +0 -23
- package/workspace/guard.js +0 -118
- package/workspace/layout.d.ts +0 -47
- package/workspace/layout.js +0 -113
- package/workspace/manifest.d.ts +0 -52
- package/workspace/manifest.js +0 -157
- package/workspace/recipients.d.ts +0 -16
- package/workspace/recipients.js +0 -45
- package/workspace/snippets.d.ts +0 -29
- package/workspace/snippets.js +0 -58
- package/workspace/state.d.ts +0 -13
- package/workspace/state.js +0 -52
- package/workspace/status.d.ts +0 -54
- package/workspace/status.js +0 -99
- package/workspace/tar.d.ts +0 -29
- package/workspace/tar.js +0 -240
package/esm/utils/bytes.js
DELETED
|
@@ -1,286 +0,0 @@
|
|
|
1
|
-
import { createWriteStream, readFileSync, realpathSync, statSync } from 'fs';
|
|
2
|
-
import { open, rename } from 'fs/promises';
|
|
3
|
-
import { dirname, resolve } from 'path';
|
|
4
|
-
import { Readable } from 'stream';
|
|
5
|
-
import { pipeline } from 'stream/promises';
|
|
6
|
-
import { CliError, EXIT } from './errors';
|
|
7
|
-
import { discardTemp, tempPathFor, trackTemp, untrackTemp } from './temp';
|
|
8
|
-
/**
|
|
9
|
-
* Byte-level input and output for commands that must not hold a whole file in memory.
|
|
10
|
-
*
|
|
11
|
-
* Everything here is a stream of byte pieces with backpressure: input is a Node `Readable`,
|
|
12
|
-
* transforms are async generators, and output goes through `stream.pipeline`. File output is
|
|
13
|
-
* written to a temporary sibling and renamed over the destination only once every byte has been
|
|
14
|
-
* written and synced. That is what keeps a wrong passphrase, a truncated download or an
|
|
15
|
-
* interrupt from replacing a good file with a partial one.
|
|
16
|
-
*/
|
|
17
|
-
export const IO_CHUNK_SIZE = 64 * 1024;
|
|
18
|
-
const memoryInput = (bytes, stdin = false) => ({
|
|
19
|
-
stdin,
|
|
20
|
-
stream: Readable.from([bytes]),
|
|
21
|
-
close() { },
|
|
22
|
-
});
|
|
23
|
-
const fileInput = async (path) => {
|
|
24
|
-
const unreadable = () => new CliError(`cannot read ${path}`, EXIT.notFound);
|
|
25
|
-
const handle = await open(path, 'r').catch(() => {
|
|
26
|
-
throw unreadable();
|
|
27
|
-
});
|
|
28
|
-
if ((await handle.stat()).isDirectory()) {
|
|
29
|
-
await handle.close();
|
|
30
|
-
throw unreadable();
|
|
31
|
-
}
|
|
32
|
-
// The stream owns the handle from here on: it is closed when the stream ends or is destroyed.
|
|
33
|
-
const stream = handle.createReadStream({ highWaterMark: IO_CHUNK_SIZE });
|
|
34
|
-
return { stdin: false, stream, close: () => void stream.destroy() };
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* stdin as a byte stream.
|
|
38
|
-
*
|
|
39
|
-
* Nothing else may read stdin while it carries the input, which is why the entry point keeps the
|
|
40
|
-
* prompter off a stdin that is not a terminal. A terminal is shared with the prompter, so what
|
|
41
|
-
* is typed there is read in one blocking pass, before control returns to the event loop.
|
|
42
|
-
*/
|
|
43
|
-
const stdinInput = () => {
|
|
44
|
-
if (!process.stdin.isTTY) {
|
|
45
|
-
return { stdin: true, stream: process.stdin, close: () => void process.stdin.pause() };
|
|
46
|
-
}
|
|
47
|
-
try {
|
|
48
|
-
return memoryInput(readFileSync(0), true);
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
throw new CliError('failed to read stdin');
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* Opens the command's input as bytes: `--in <file>`, `-` (stdin), the inline value, then piped
|
|
56
|
-
* stdin — the same order as `readInput`.
|
|
57
|
-
*/
|
|
58
|
-
export const openInput = async (argv, inline) => {
|
|
59
|
-
if (typeof argv.in === 'string' && argv.in.length) {
|
|
60
|
-
return argv.in === '-' ? stdinInput() : fileInput(argv.in);
|
|
61
|
-
}
|
|
62
|
-
if (inline === '-' || argv.stdin)
|
|
63
|
-
return stdinInput();
|
|
64
|
-
if (inline !== undefined && inline !== '')
|
|
65
|
-
return memoryInput(Buffer.from(String(inline), 'utf8'));
|
|
66
|
-
if (!process.stdin.isTTY)
|
|
67
|
-
return stdinInput();
|
|
68
|
-
throw new CliError('no input: pass a value, --in <file>, or pipe on stdin');
|
|
69
|
-
};
|
|
70
|
-
/** stdin can carry the data or the passphrase, never both. */
|
|
71
|
-
export const assertStdinFree = (argv, input) => {
|
|
72
|
-
if (input.stdin && (argv['passphrase-stdin'] || argv.passphraseStdin)) {
|
|
73
|
-
throw new CliError('stdin cannot carry both the input and the passphrase; use --in <file> or --passphrase-file <path>');
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
const NO_BYTES = Buffer.alloc(0);
|
|
77
|
-
const asBuffer = (bytes) => Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.length);
|
|
78
|
-
/**
|
|
79
|
-
* Reads the front of a byte stream, then hands on whatever was not consumed.
|
|
80
|
-
*
|
|
81
|
-
* A command has to look at the first bytes of its input to know what it was given, and a header
|
|
82
|
-
* has no fixed length. This buffers only as much as it is asked for; {@link ByteReader.rest}
|
|
83
|
-
* streams the remainder untouched.
|
|
84
|
-
*/
|
|
85
|
-
export class ByteReader {
|
|
86
|
-
pieces;
|
|
87
|
-
held = NO_BYTES;
|
|
88
|
-
constructor(source) {
|
|
89
|
-
this.pieces =
|
|
90
|
-
Symbol.asyncIterator in source ? source[Symbol.asyncIterator]() : source[Symbol.iterator]();
|
|
91
|
-
}
|
|
92
|
-
/** What has been read from the stream and not yet consumed. */
|
|
93
|
-
get buffered() {
|
|
94
|
-
return this.held;
|
|
95
|
-
}
|
|
96
|
-
/** Buffers one more piece. Resolves to false once the stream has ended. */
|
|
97
|
-
async more() {
|
|
98
|
-
const next = await this.pieces.next();
|
|
99
|
-
if (next.done)
|
|
100
|
-
return false;
|
|
101
|
-
this.held = this.held.length ? Buffer.concat([this.held, next.value]) : asBuffer(next.value);
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
/** Buffers until at least `length` bytes are held, or the stream ends, and returns them. */
|
|
105
|
-
async fill(length) {
|
|
106
|
-
while (this.held.length < length && (await this.more()))
|
|
107
|
-
;
|
|
108
|
-
return this.held;
|
|
109
|
-
}
|
|
110
|
-
/** Drops `length` bytes from the front of the buffer. */
|
|
111
|
-
skip(length) {
|
|
112
|
-
this.held = this.held.subarray(length);
|
|
113
|
-
}
|
|
114
|
-
/** Reads everything that is left into memory. For inputs that are small by construction. */
|
|
115
|
-
async all() {
|
|
116
|
-
while (await this.more())
|
|
117
|
-
;
|
|
118
|
-
const everything = this.held;
|
|
119
|
-
this.held = NO_BYTES;
|
|
120
|
-
return everything;
|
|
121
|
-
}
|
|
122
|
-
/** Everything that is left, as a stream: the buffered bytes, then the rest of the source. */
|
|
123
|
-
async *rest() {
|
|
124
|
-
if (this.held.length) {
|
|
125
|
-
const head = this.held;
|
|
126
|
-
this.held = NO_BYTES;
|
|
127
|
-
yield head;
|
|
128
|
-
}
|
|
129
|
-
for (let next = await this.pieces.next(); !next.done; next = await this.pieces.next()) {
|
|
130
|
-
yield next.value;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
/** Runs `source` through `transform`, one piece at a time. */
|
|
135
|
-
export async function* transformBytes(source, transform) {
|
|
136
|
-
for await (const piece of source) {
|
|
137
|
-
const output = transform.update(piece);
|
|
138
|
-
if (output.length)
|
|
139
|
-
yield output;
|
|
140
|
-
}
|
|
141
|
-
yield transform.finish();
|
|
142
|
-
}
|
|
143
|
-
/** Node streams speak Buffer: a bare Uint8Array reads as a list of numbers to anything that inspects a chunk. */
|
|
144
|
-
async function* asBuffers(source) {
|
|
145
|
-
for await (const piece of source)
|
|
146
|
-
yield asBuffer(piece);
|
|
147
|
-
}
|
|
148
|
-
/** Joins byte streams end to end. */
|
|
149
|
-
export async function* chainBytes(...sources) {
|
|
150
|
-
for (const source of sources)
|
|
151
|
-
yield* source;
|
|
152
|
-
}
|
|
153
|
-
const statOrUndefined = (path) => {
|
|
154
|
-
try {
|
|
155
|
-
return statSync(path);
|
|
156
|
-
}
|
|
157
|
-
catch {
|
|
158
|
-
return undefined;
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
const sync = async (path, flags) => {
|
|
162
|
-
const handle = await open(path, flags);
|
|
163
|
-
try {
|
|
164
|
-
await handle.sync();
|
|
165
|
-
}
|
|
166
|
-
finally {
|
|
167
|
-
await handle.close();
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
/** Forces a file that has already been written to disk. */
|
|
171
|
-
export const syncFile = (path) => sync(path, 'r+');
|
|
172
|
-
/** Best effort: makes a rename durable. Not every platform can fsync a directory. */
|
|
173
|
-
export const syncDirectory = async (path) => {
|
|
174
|
-
try {
|
|
175
|
-
await sync(path, 'r');
|
|
176
|
-
}
|
|
177
|
-
catch {
|
|
178
|
-
// ignored on purpose
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
/**
|
|
182
|
-
* Writes `path` atomically. The bytes go to a temporary file in the same directory, created
|
|
183
|
-
* owner-only; once all of them are written and synced it is renamed over `path`. If `bytes`
|
|
184
|
-
* fails part-way, or the process is interrupted, `path` is left exactly as it was.
|
|
185
|
-
*/
|
|
186
|
-
export const writeFileAtomically = async (path, bytes) => {
|
|
187
|
-
const existing = statOrUndefined(path);
|
|
188
|
-
if (existing?.isDirectory())
|
|
189
|
-
throw new CliError(`cannot write ${path}: it is a directory`);
|
|
190
|
-
if (existing && !existing.isFile()) {
|
|
191
|
-
// A pipe, device or other special file: there is nothing to rename over, so write straight in.
|
|
192
|
-
await pipeline(bytes, createWriteStream(path));
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
// Write through a symlink, as a plain overwrite would, instead of replacing the link.
|
|
196
|
-
const target = existing ? realpathSync(path) : resolve(path);
|
|
197
|
-
const temp = tempPathFor(target);
|
|
198
|
-
const handle = await open(temp, 'wx', 0o600).catch(() => {
|
|
199
|
-
throw new CliError(`cannot write ${path}`);
|
|
200
|
-
});
|
|
201
|
-
trackTemp(temp);
|
|
202
|
-
try {
|
|
203
|
-
// The stream owns the handle: `flush` syncs the file to disk before the handle is closed, and
|
|
204
|
-
// pipeline() only resolves after that, so the rename never exposes bytes that are not there.
|
|
205
|
-
await pipeline(bytes, handle.createWriteStream({ flush: true }));
|
|
206
|
-
await rename(temp, target);
|
|
207
|
-
}
|
|
208
|
-
catch (error) {
|
|
209
|
-
discardTemp(temp);
|
|
210
|
-
throw error;
|
|
211
|
-
}
|
|
212
|
-
untrackTemp(temp);
|
|
213
|
-
await syncDirectory(dirname(target));
|
|
214
|
-
};
|
|
215
|
-
const outputFile = (argv) => typeof argv.out === 'string' && argv.out.length && argv.out !== '-' ? argv.out : undefined;
|
|
216
|
-
/** Fails, before any prompt, when binary output would land on a terminal. */
|
|
217
|
-
export const assertNotTerminal = (argv, options) => {
|
|
218
|
-
if (options.binary && outputFile(argv) === undefined && process.stdout.isTTY) {
|
|
219
|
-
throw new CliError('refusing to write binary ciphertext to a terminal; use --out <file> or redirect stdout');
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
/** How many bytes at the end of `bytes` begin a UTF-8 character without finishing it. */
|
|
223
|
-
const unfinished = (bytes) => {
|
|
224
|
-
for (let back = 1; back <= Math.min(3, bytes.length); back++) {
|
|
225
|
-
const byte = bytes[bytes.length - back];
|
|
226
|
-
if ((byte & 0xc0) === 0x80)
|
|
227
|
-
continue; // a continuation byte: its lead byte is further back
|
|
228
|
-
const length = byte >= 0xf0 ? 4 : byte >= 0xe0 ? 3 : byte >= 0xc0 ? 2 : 1;
|
|
229
|
-
return length > back ? back : 0;
|
|
230
|
-
}
|
|
231
|
-
return 0;
|
|
232
|
-
};
|
|
233
|
-
const isText = (bytes) => {
|
|
234
|
-
if (bytes.includes(0))
|
|
235
|
-
return false;
|
|
236
|
-
try {
|
|
237
|
-
new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
238
|
-
return true;
|
|
239
|
-
}
|
|
240
|
-
catch {
|
|
241
|
-
return false;
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
/**
|
|
245
|
-
* Passes text through, and stops at what a terminal should not be sent: a NUL byte, or bytes that
|
|
246
|
-
* are not UTF-8. Only whole characters are passed on: the start of one that is split between two
|
|
247
|
-
* pieces waits for the rest of it, and is never written if the rest does not come.
|
|
248
|
-
*/
|
|
249
|
-
async function* textOnly(bytes) {
|
|
250
|
-
const refusal = () => new CliError('refusing to write binary data to a terminal; use --out <file> or redirect stdout');
|
|
251
|
-
let pending = new Uint8Array(0);
|
|
252
|
-
for await (const piece of bytes) {
|
|
253
|
-
const joined = pending.length ? Buffer.concat([pending, piece]) : piece;
|
|
254
|
-
const end = joined.length - unfinished(joined);
|
|
255
|
-
const whole = joined.subarray(0, end);
|
|
256
|
-
if (!isText(whole))
|
|
257
|
-
throw refusal();
|
|
258
|
-
pending = joined.slice(end);
|
|
259
|
-
if (whole.length)
|
|
260
|
-
yield whole;
|
|
261
|
-
}
|
|
262
|
-
if (pending.length)
|
|
263
|
-
throw refusal();
|
|
264
|
-
}
|
|
265
|
-
/** Writes `bytes` to `--out <file>` when given (atomically), otherwise to stdout. */
|
|
266
|
-
export const writeBytes = async (argv, bytes, options = {}) => {
|
|
267
|
-
const path = outputFile(argv);
|
|
268
|
-
if (path !== undefined) {
|
|
269
|
-
await writeFileAtomically(path, bytes);
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
assertNotTerminal(argv, options);
|
|
273
|
-
// stdout belongs to the process: it is written to, never ended.
|
|
274
|
-
await pipeline(asBuffers(process.stdout.isTTY ? textOnly(bytes) : bytes), process.stdout, { end: false });
|
|
275
|
-
};
|
|
276
|
-
const NEWLINE = Buffer.from('\n');
|
|
277
|
-
/**
|
|
278
|
-
* Writes decrypted bytes exactly as they were encrypted. Only on a terminal is text that does not
|
|
279
|
-
* end its last line given a newline, so that the prompt does not follow it on the same line.
|
|
280
|
-
*/
|
|
281
|
-
export const writePlaintext = (argv, plaintext) => {
|
|
282
|
-
const shown = outputFile(argv) === undefined && process.stdout.isTTY;
|
|
283
|
-
return writeBytes(argv, shown && plaintext.at(-1) !== NEWLINE[0] ? [plaintext, NEWLINE] : [plaintext]);
|
|
284
|
-
};
|
|
285
|
-
/** Writes text the way {@link writeBytes} writes bytes, ending it with a newline. */
|
|
286
|
-
export const writeText = (argv, text) => writeBytes(argv, [Buffer.from(text.endsWith('\n') ? text : `${text}\n`, 'utf8')]);
|
package/esm/utils/dispatch.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { extractFirst } from 'inquirerer';
|
|
2
|
-
import { CliError } from './errors';
|
|
3
|
-
/**
|
|
4
|
-
* `extractFirst` with the argv type preserved — upstream narrows the rest of argv to
|
|
5
|
-
* `{ _: string[] }`, which loses every flag.
|
|
6
|
-
*/
|
|
7
|
-
export const takeFirst = (argv) => extractFirst(argv);
|
|
8
|
-
/**
|
|
9
|
-
* Dispatches `dcrypt <group> <subcommand>`: extracts the positional subcommand, prints per-command
|
|
10
|
-
* help on `--help`, and prompts for the subcommand when running interactively without one.
|
|
11
|
-
*/
|
|
12
|
-
export const runSubcommand = async (argv, prompter, { name, usage, handlers }) => {
|
|
13
|
-
const { first, newArgv } = takeFirst(argv);
|
|
14
|
-
let subcommand = first;
|
|
15
|
-
if (!subcommand && (argv.help || argv.h)) {
|
|
16
|
-
process.stdout.write(usage);
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
if (subcommand === 'help') {
|
|
20
|
-
process.stdout.write(usage);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (!subcommand) {
|
|
24
|
-
const answer = await prompter.prompt(newArgv, [
|
|
25
|
-
{
|
|
26
|
-
type: 'autocomplete',
|
|
27
|
-
name: 'subcommand',
|
|
28
|
-
message: `${name} — what do you want to do?`,
|
|
29
|
-
options: Object.keys(handlers),
|
|
30
|
-
},
|
|
31
|
-
]);
|
|
32
|
-
subcommand = answer.subcommand;
|
|
33
|
-
}
|
|
34
|
-
const handler = handlers[subcommand];
|
|
35
|
-
if (!handler) {
|
|
36
|
-
process.stdout.write(usage);
|
|
37
|
-
throw new CliError(`unknown ${name} subcommand: ${subcommand}`);
|
|
38
|
-
}
|
|
39
|
-
if (newArgv.help || newArgv.h) {
|
|
40
|
-
process.stdout.write(usage);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
await handler(newArgv, prompter);
|
|
44
|
-
};
|
|
45
|
-
/** Prints `usage` and returns true when the user asked for help. */
|
|
46
|
-
export const wantsHelp = (argv, usage) => {
|
|
47
|
-
if (argv.help || argv.h) {
|
|
48
|
-
process.stdout.write(usage);
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
return false;
|
|
52
|
-
};
|
package/esm/utils/env.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Environment variable input, kept compatible with the cosmology CLI.
|
|
3
|
-
*
|
|
4
|
-
* Every name that CLI read still works — `MNEMONIC`, `SALT`, `ENCRYPTED_SALT`,
|
|
5
|
-
* `KEYCHAIN_ACCOUNT` — alongside `DCRYPT_`-prefixed aliases for use in
|
|
6
|
-
* environments where the bare names would collide.
|
|
7
|
-
*
|
|
8
|
-
* Resolution order everywhere is: explicit flag, then environment, then stdin,
|
|
9
|
-
* then an interactive prompt. Flags always win so a script can override an
|
|
10
|
-
* inherited variable, and argv still never carries a secret.
|
|
11
|
-
*/
|
|
12
|
-
export const ENV_VARS = {
|
|
13
|
-
mnemonic: ['MNEMONIC', 'DCRYPT_MNEMONIC'],
|
|
14
|
-
salt: ['SALT', 'DCRYPT_SALT'],
|
|
15
|
-
encryptedSalt: ['ENCRYPTED_SALT', 'DCRYPT_ENCRYPTED_SALT'],
|
|
16
|
-
keychainAccount: ['KEYCHAIN_ACCOUNT', 'DCRYPT_KEYCHAIN_ACCOUNT'],
|
|
17
|
-
passphrase: ['DCRYPT_PASSPHRASE'],
|
|
18
|
-
accountPassword: ['DCRYPT_ACCOUNT_PASSWORD'],
|
|
19
|
-
authEndpoint: ['DCRYPT_AUTH_ENDPOINT'],
|
|
20
|
-
};
|
|
21
|
-
/** First non-empty value among the names for `input`, or undefined. */
|
|
22
|
-
export const fromEnv = (input, env = process.env) => {
|
|
23
|
-
for (const name of ENV_VARS[input]) {
|
|
24
|
-
const value = env[name];
|
|
25
|
-
if (typeof value === 'string' && value.length)
|
|
26
|
-
return value;
|
|
27
|
-
}
|
|
28
|
-
return undefined;
|
|
29
|
-
};
|
|
30
|
-
/** The keychain namespace, matching the cosmology CLI's `KEYCHAIN_ACCOUNT`. */
|
|
31
|
-
export const keychainAccount = (env = process.env) => fromEnv('keychainAccount', env) ?? 'dcrypt';
|
|
32
|
-
export const envUsage = `
|
|
33
|
-
Environment:
|
|
34
|
-
MNEMONIC A BIP39 mnemonic, for wallet commands
|
|
35
|
-
SALT The salt, for salt and cosmology commands
|
|
36
|
-
ENCRYPTED_SALT The encrypted salt, for the two-layer scheme
|
|
37
|
-
KEYCHAIN_ACCOUNT Keychain namespace (default: dcrypt)
|
|
38
|
-
DCRYPT_PASSPHRASE The passphrase, for unattended runs
|
|
39
|
-
DCRYPT_AUTH_ENDPOINT Constructive auth endpoint, for account commands
|
|
40
|
-
DCRYPT_ACCOUNT_PASSWORD The account password, for unattended sign-in
|
|
41
|
-
|
|
42
|
-
Each also accepts a DCRYPT_-prefixed alias (DCRYPT_MNEMONIC, DCRYPT_SALT, ...).
|
|
43
|
-
Flags take precedence over the environment, which takes precedence over stdin.
|
|
44
|
-
`;
|
package/esm/utils/errors.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { CorruptEnvelopeError, InvalidParametersError, UnsupportedEnvelopeError, WrongPassphraseError, } from '@decryption/core';
|
|
2
|
-
import { NotRecipientError } from '@decryption/keys';
|
|
3
|
-
/**
|
|
4
|
-
* Exit codes, so scripts can tell failure modes apart without parsing messages.
|
|
5
|
-
*
|
|
6
|
-
* | Code | Meaning |
|
|
7
|
-
* |------|---------|
|
|
8
|
-
* | 1 | Usage error — bad arguments, unknown command |
|
|
9
|
-
* | 2 | Wrong passphrase, or authentication failed |
|
|
10
|
-
* | 3 | Corrupt or unsupported input |
|
|
11
|
-
* | 4 | Not found — no such secret, vault, or keychain entry |
|
|
12
|
-
* | 5 | Permission — this identity is not a recipient |
|
|
13
|
-
*/
|
|
14
|
-
export const EXIT = {
|
|
15
|
-
usage: 1,
|
|
16
|
-
auth: 2,
|
|
17
|
-
corrupt: 3,
|
|
18
|
-
notFound: 4,
|
|
19
|
-
permission: 5,
|
|
20
|
-
};
|
|
21
|
-
export class CliError extends Error {
|
|
22
|
-
code;
|
|
23
|
-
constructor(message, code = EXIT.usage) {
|
|
24
|
-
super(message);
|
|
25
|
-
this.name = 'CliError';
|
|
26
|
-
this.code = code;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
/** Maps library errors onto exit codes; unknown errors keep exit code 1. */
|
|
30
|
-
export const exitCodeFor = (error) => {
|
|
31
|
-
if (error instanceof CliError)
|
|
32
|
-
return error.code;
|
|
33
|
-
if (error instanceof WrongPassphraseError)
|
|
34
|
-
return EXIT.auth;
|
|
35
|
-
if (error instanceof CorruptEnvelopeError || error instanceof UnsupportedEnvelopeError) {
|
|
36
|
-
return EXIT.corrupt;
|
|
37
|
-
}
|
|
38
|
-
if (error instanceof NotRecipientError)
|
|
39
|
-
return EXIT.permission;
|
|
40
|
-
if (error instanceof InvalidParametersError)
|
|
41
|
-
return EXIT.usage;
|
|
42
|
-
return EXIT.usage;
|
|
43
|
-
};
|
|
44
|
-
export const messageFor = (error) => error instanceof Error ? error.message : String(error);
|
package/esm/utils/header.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { CorruptEnvelopeError, tryParseFileHeader } from '@decryption/core';
|
|
2
|
-
/** Reads exactly the header off the front of `reader`; the payload stays in the stream. */
|
|
3
|
-
export const readFileHeader = async (reader) => {
|
|
4
|
-
for (;;) {
|
|
5
|
-
const header = tryParseFileHeader(reader.buffered);
|
|
6
|
-
if (header) {
|
|
7
|
-
reader.skip(header.length);
|
|
8
|
-
return header;
|
|
9
|
-
}
|
|
10
|
-
if (!(await reader.more())) {
|
|
11
|
-
throw new CorruptEnvelopeError('envelope is truncated: the header is incomplete');
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
};
|
package/esm/utils/io.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync } from 'fs';
|
|
2
|
-
import { CliError, EXIT } from './errors';
|
|
3
|
-
/** Reads `--in <file>`, `-` (stdin), or the given inline value, in that order. */
|
|
4
|
-
export const readInput = (argv, inline) => {
|
|
5
|
-
if (typeof argv.in === 'string' && argv.in.length) {
|
|
6
|
-
if (argv.in === '-')
|
|
7
|
-
return readStdin();
|
|
8
|
-
try {
|
|
9
|
-
return readFileSync(argv.in, 'utf8');
|
|
10
|
-
}
|
|
11
|
-
catch {
|
|
12
|
-
throw new CliError(`cannot read ${argv.in}`, EXIT.notFound);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
if (inline === '-' || argv.stdin)
|
|
16
|
-
return readStdin();
|
|
17
|
-
if (inline !== undefined && inline !== '')
|
|
18
|
-
return inline;
|
|
19
|
-
if (!process.stdin.isTTY)
|
|
20
|
-
return readStdin();
|
|
21
|
-
throw new CliError('no input: pass a value, --in <file>, or pipe on stdin');
|
|
22
|
-
};
|
|
23
|
-
/** Writes to `--out <file>` when given, otherwise stdout. */
|
|
24
|
-
export const writeOutput = (argv, content) => {
|
|
25
|
-
const text = content.endsWith('\n') ? content : `${content}\n`;
|
|
26
|
-
if (typeof argv.out === 'string' && argv.out.length) {
|
|
27
|
-
writeFileSync(argv.out, text, { mode: 0o600 });
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
process.stdout.write(text);
|
|
31
|
-
};
|
|
32
|
-
/** Emits `--json` output, or the human-readable rendering. */
|
|
33
|
-
export const emit = (argv, data, human) => {
|
|
34
|
-
if (argv.json) {
|
|
35
|
-
writeOutput(argv, JSON.stringify(data, null, 2));
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
writeOutput(argv, human());
|
|
39
|
-
};
|
|
40
|
-
/** True when the command should avoid printing secrets to a terminal. */
|
|
41
|
-
export const isTty = () => Boolean(process.stdout.isTTY);
|
|
42
|
-
export const readStdin = () => {
|
|
43
|
-
try {
|
|
44
|
-
return readFileSync(0, 'utf8');
|
|
45
|
-
}
|
|
46
|
-
catch {
|
|
47
|
-
throw new CliError('failed to read stdin');
|
|
48
|
-
}
|
|
49
|
-
};
|
package/esm/utils/passphrase.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from 'fs';
|
|
2
|
-
import { fromEnv } from './env';
|
|
3
|
-
import { CliError, EXIT } from './errors';
|
|
4
|
-
import { readStdin } from './io';
|
|
5
|
-
/**
|
|
6
|
-
* Resolves a passphrase without ever accepting one on the command line.
|
|
7
|
-
*
|
|
8
|
-
* Order: `--passphrase-file <path>`, `DCRYPT_PASSPHRASE`, `--passphrase-stdin`, then an
|
|
9
|
-
* interactive masked prompt. Passing `--passphrase` is rejected outright: argv is visible
|
|
10
|
-
* to every process on the machine.
|
|
11
|
-
*/
|
|
12
|
-
export const resolvePassphrase = async (argv, prompter, options = {}) => {
|
|
13
|
-
if (argv.passphrase !== undefined || argv.password !== undefined) {
|
|
14
|
-
throw new CliError('refusing to read a passphrase from argv (it is visible in `ps`); use --passphrase-file <path> or --passphrase-stdin');
|
|
15
|
-
}
|
|
16
|
-
const file = argv['passphrase-file'] ?? argv.passphraseFile;
|
|
17
|
-
if (typeof file === 'string' && file.length) {
|
|
18
|
-
try {
|
|
19
|
-
return readFileSync(file, 'utf8').replace(/\r?\n$/, '');
|
|
20
|
-
}
|
|
21
|
-
catch {
|
|
22
|
-
throw new CliError(`cannot read passphrase file ${file}`, EXIT.notFound);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
const fromEnvironment = fromEnv('passphrase');
|
|
26
|
-
if (fromEnvironment !== undefined) {
|
|
27
|
-
return fromEnvironment.replace(/\r?\n$/, '');
|
|
28
|
-
}
|
|
29
|
-
if (argv['passphrase-stdin'] || argv.passphraseStdin) {
|
|
30
|
-
return readStdin().replace(/\r?\n$/, '');
|
|
31
|
-
}
|
|
32
|
-
if (!process.stdin.isTTY) {
|
|
33
|
-
throw new CliError('no passphrase: stdin is not a terminal, so there is nothing to prompt on; use --passphrase-file <path>, DCRYPT_PASSPHRASE or --passphrase-stdin');
|
|
34
|
-
}
|
|
35
|
-
const message = options.message ?? 'Passphrase';
|
|
36
|
-
const { passphrase } = await prompter.prompt({}, [
|
|
37
|
-
{ type: 'password', name: 'passphrase', message, required: true },
|
|
38
|
-
]);
|
|
39
|
-
if (!passphrase)
|
|
40
|
-
throw new CliError('a passphrase is required');
|
|
41
|
-
if (options.confirm) {
|
|
42
|
-
const { confirmation } = await prompter.prompt({}, [
|
|
43
|
-
{ type: 'password', name: 'confirmation', message: 'Confirm passphrase', required: true },
|
|
44
|
-
]);
|
|
45
|
-
if (confirmation !== passphrase)
|
|
46
|
-
throw new CliError('passphrases do not match');
|
|
47
|
-
}
|
|
48
|
-
return passphrase;
|
|
49
|
-
};
|
package/esm/utils/recipients.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { recipientFromString, recipientToString } from '@decryption/keys';
|
|
2
|
-
import { readFileSync } from 'fs';
|
|
3
|
-
import { CliError, EXIT, messageFor } from './errors';
|
|
4
|
-
/**
|
|
5
|
-
* Parses a recipients file: one `dcrypt1…` public key per line, optionally followed by a label.
|
|
6
|
-
* Blank lines and lines starting with `#` are ignored. Keys are public, so the file is safe to
|
|
7
|
-
* commit.
|
|
8
|
-
*
|
|
9
|
-
* ```text
|
|
10
|
-
* # who can open this
|
|
11
|
-
* dcrypt1qxy… ada (laptop)
|
|
12
|
-
* dcrypt1qab… recovery — printed, in the safe
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export const parseRecipients = (text, origin = 'recipients') => {
|
|
16
|
-
const entries = [];
|
|
17
|
-
const seen = new Set();
|
|
18
|
-
text.split(/\r?\n/).forEach((raw, index) => {
|
|
19
|
-
const line = raw.trim();
|
|
20
|
-
if (!line || line.startsWith('#'))
|
|
21
|
-
return;
|
|
22
|
-
const [key, ...rest] = line.split(/\s+/);
|
|
23
|
-
let recipient;
|
|
24
|
-
try {
|
|
25
|
-
recipient = recipientToString(recipientFromString(key));
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
throw new CliError(`${origin}:${index + 1}: ${messageFor(error)}`);
|
|
29
|
-
}
|
|
30
|
-
if (seen.has(recipient))
|
|
31
|
-
return;
|
|
32
|
-
seen.add(recipient);
|
|
33
|
-
entries.push({ recipient, label: rest.join(' '), line: index + 1 });
|
|
34
|
-
});
|
|
35
|
-
return entries;
|
|
36
|
-
};
|
|
37
|
-
export const readRecipientsFile = (path) => {
|
|
38
|
-
let text;
|
|
39
|
-
try {
|
|
40
|
-
text = readFileSync(path, 'utf8');
|
|
41
|
-
}
|
|
42
|
-
catch {
|
|
43
|
-
throw new CliError(`cannot read recipients file ${path}`, EXIT.notFound);
|
|
44
|
-
}
|
|
45
|
-
return parseRecipients(text, path);
|
|
46
|
-
};
|
|
47
|
-
const values = (value, flag) => {
|
|
48
|
-
if (value === undefined)
|
|
49
|
-
return [];
|
|
50
|
-
const list = Array.isArray(value) ? value : [value];
|
|
51
|
-
return list.map((item) => {
|
|
52
|
-
if (typeof item !== 'string' || !item.length)
|
|
53
|
-
throw new CliError(`${flag} needs a value`);
|
|
54
|
-
return item;
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Recipients named on the command line: every `--recipient <dcrypt1…>` and every line of every
|
|
59
|
-
* `--recipients-file <path>`, de-duplicated, in the order given.
|
|
60
|
-
*/
|
|
61
|
-
export const collectRecipients = (argv) => {
|
|
62
|
-
const direct = values(argv.recipient, '--recipient').map((value) => {
|
|
63
|
-
try {
|
|
64
|
-
return recipientToString(recipientFromString(value));
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
throw new CliError(`--recipient ${value}: ${messageFor(error)}`);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
const fromFiles = values(argv['recipients-file'] ?? argv.recipientsFile, '--recipients-file')
|
|
71
|
-
.flatMap((path) => readRecipientsFile(path))
|
|
72
|
-
.map((entry) => entry.recipient);
|
|
73
|
-
return [...new Set([...direct, ...fromFiles])];
|
|
74
|
-
};
|
package/esm/utils/stash.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { decryptFromString, encryptToString } from '@decryption/core';
|
|
2
|
-
import { identityFromString, identityToString, recipientToString } from '@decryption/keys';
|
|
3
|
-
import { appstash, resolve } from 'appstash';
|
|
4
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
5
|
-
import { dirname, join } from 'path';
|
|
6
|
-
import { keychainAccount } from './env';
|
|
7
|
-
import { CliError, EXIT } from './errors';
|
|
8
|
-
/**
|
|
9
|
-
* On-disk layout, shared byte-for-byte with the Electron app:
|
|
10
|
-
*
|
|
11
|
-
* ```text
|
|
12
|
-
* ~/.dcrypt/
|
|
13
|
-
* config/identity.json the user's X25519 identity, encrypted under a passphrase
|
|
14
|
-
* data/keychain.json named secrets, each encrypted under a passphrase
|
|
15
|
-
* (KEYCHAIN_ACCOUNT selects keychain-<account>.json instead)
|
|
16
|
-
* data/vaults/<name>.json team secrets files
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* Nothing here is ever written in plaintext.
|
|
20
|
-
*/
|
|
21
|
-
export const APP_NAME = 'dcrypt';
|
|
22
|
-
export const dirs = () => appstash(APP_NAME, { ensure: true });
|
|
23
|
-
export const identityPath = () => resolve(dirs(), 'config', 'identity.json');
|
|
24
|
-
export const keychainPath = () => {
|
|
25
|
-
const account = keychainAccount();
|
|
26
|
-
const file = account === 'dcrypt' ? 'keychain.json' : `keychain-${account}.json`;
|
|
27
|
-
return resolve(dirs(), 'data', file);
|
|
28
|
-
};
|
|
29
|
-
export const vaultPath = (name) => join(resolve(dirs(), 'data', 'vaults'), `${name}.json`);
|
|
30
|
-
export const hasIdentity = () => existsSync(identityPath());
|
|
31
|
-
export const saveIdentity = (identity, passphrase, kdf = 'moderate') => {
|
|
32
|
-
const stored = {
|
|
33
|
-
version: 1,
|
|
34
|
-
recipient: recipientToString(identity.publicKey),
|
|
35
|
-
encrypted: encryptToString(identityToString(identity), passphrase, { kdf }),
|
|
36
|
-
};
|
|
37
|
-
writeJson(identityPath(), stored);
|
|
38
|
-
return stored;
|
|
39
|
-
};
|
|
40
|
-
export const readStoredIdentity = () => {
|
|
41
|
-
if (!hasIdentity()) {
|
|
42
|
-
throw new CliError('no identity yet — run `dcrypt keys generate`', EXIT.notFound);
|
|
43
|
-
}
|
|
44
|
-
return readJson(identityPath());
|
|
45
|
-
};
|
|
46
|
-
export const loadIdentity = (passphrase) => identityFromString(decryptFromString(readStoredIdentity().encrypted, passphrase));
|
|
47
|
-
export const readKeychain = () => existsSync(keychainPath()) ? readJson(keychainPath()) : { version: 1, entries: {} };
|
|
48
|
-
export const writeKeychain = (keychain) => writeJson(keychainPath(), keychain);
|
|
49
|
-
export const readJson = (path) => {
|
|
50
|
-
try {
|
|
51
|
-
return JSON.parse(readFileSync(path, 'utf8'));
|
|
52
|
-
}
|
|
53
|
-
catch {
|
|
54
|
-
throw new CliError(`cannot read ${path}`, EXIT.corrupt);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
/** Writes JSON with owner-only permissions and a trailing newline. */
|
|
58
|
-
export const writeJson = (path, value) => {
|
|
59
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
60
|
-
writeFileSync(path, JSON.stringify(value, null, 2) + '\n', { mode: 0o600 });
|
|
61
|
-
};
|