secrez 1.1.1 → 1.1.2
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/README.md +414 -291
- package/bin/secrez.js +50 -47
- package/coverage.report +63 -305
- package/package.json +10 -12
- package/src/Command.js +78 -57
- package/src/PreCommand.js +74 -70
- package/src/Welcome.js +144 -134
- package/src/cliConfig.js +14 -14
- package/src/commands/Alias.js +123 -100
- package/src/commands/Bash.js +10 -12
- package/src/commands/Cat.js +117 -107
- package/src/commands/Cd.js +39 -42
- package/src/commands/Chat.js +75 -63
- package/src/commands/Conf.js +123 -99
- package/src/commands/Contacts.js +189 -171
- package/src/commands/Copy.js +132 -113
- package/src/commands/Courier.js +123 -105
- package/src/commands/Ds.js +88 -76
- package/src/commands/Edit.js +122 -103
- package/src/commands/Export.js +153 -114
- package/src/commands/Find.js +115 -110
- package/src/commands/Help.js +20 -23
- package/src/commands/Import.js +296 -225
- package/src/commands/Lcat.js +36 -39
- package/src/commands/Lcd.js +38 -39
- package/src/commands/Lls.js +58 -55
- package/src/commands/Lpwd.js +20 -24
- package/src/commands/Ls.js +107 -97
- package/src/commands/Mkdir.js +35 -38
- package/src/commands/Mv.js +147 -114
- package/src/commands/Paste.js +68 -65
- package/src/commands/Pwd.js +18 -23
- package/src/commands/Quit.js +22 -24
- package/src/commands/Rm.js +78 -70
- package/src/commands/Shell.js +31 -32
- package/src/commands/Ssh.js +77 -63
- package/src/commands/Tag.js +133 -112
- package/src/commands/Totp.js +166 -136
- package/src/commands/Touch.js +169 -56
- package/src/commands/Use.js +44 -41
- package/src/commands/Ver.js +16 -18
- package/src/commands/Whoami.js +34 -37
- package/src/commands/chat/Contacts.js +41 -44
- package/src/commands/chat/Help.js +20 -23
- package/src/commands/chat/Join.js +59 -55
- package/src/commands/chat/Leave.js +16 -22
- package/src/commands/chat/Quit.js +19 -24
- package/src/commands/chat/Send.js +58 -57
- package/src/commands/chat/Show.js +60 -51
- package/src/commands/chat/Whoami.js +18 -22
- package/src/commands/index.js +20 -22
- package/src/index.js +3 -3
- package/src/prompts/ChatPrompt.js +87 -82
- package/src/prompts/ChatPromptMock.js +11 -17
- package/src/prompts/CommandPrompt.js +146 -138
- package/src/prompts/Completion.js +64 -69
- package/src/prompts/MainPrompt.js +84 -77
- package/src/prompts/MainPromptMock.js +19 -30
- package/src/prompts/MultiEditorPrompt.js +21 -22
- package/src/prompts/SigintManager.js +21 -24
- package/src/utils/AliasManager.js +16 -18
- package/src/utils/ContactManager.js +15 -17
- package/src/utils/Fido2Client.js +59 -49
- package/src/utils/HelpProto.js +130 -117
- package/src/utils/Logger.js +48 -50
- package/.eslintignore +0 -0
- package/.eslintrc +0 -33
- package/.jshintrc +0 -3
package/src/commands/Export.js
CHANGED
@@ -1,197 +1,236 @@
|
|
1
|
-
const fs = require(
|
2
|
-
const path = require(
|
3
|
-
const chalk = require(
|
1
|
+
const fs = require("fs-extra");
|
2
|
+
const path = require("path");
|
3
|
+
const chalk = require("chalk");
|
4
4
|
|
5
|
-
const Crypto = require(
|
6
|
-
const {sleep} = require(
|
5
|
+
const Crypto = require("@secrez/crypto");
|
6
|
+
const { sleep } = require("@secrez/utils");
|
7
7
|
|
8
|
-
const {Node, FileCipher} = require(
|
9
|
-
|
10
|
-
class Export extends require('../Command') {
|
8
|
+
const { Node, FileCipher } = require("@secrez/fs");
|
11
9
|
|
10
|
+
class Export extends require("../Command") {
|
12
11
|
setHelpAndCompletion() {
|
13
12
|
this.cliConfig.completion.export = {
|
14
13
|
_func: this.selfCompletion(this),
|
15
|
-
_self: this
|
16
|
-
}
|
17
|
-
this.cliConfig.completion.help.export = true
|
14
|
+
_self: this,
|
15
|
+
};
|
16
|
+
this.cliConfig.completion.help.export = true;
|
18
17
|
this.optionDefinitions = [
|
19
18
|
{
|
20
|
-
name:
|
21
|
-
alias:
|
22
|
-
type: Boolean
|
19
|
+
name: "help",
|
20
|
+
alias: "h",
|
21
|
+
type: Boolean,
|
23
22
|
},
|
24
23
|
{
|
25
|
-
name:
|
26
|
-
completionType:
|
27
|
-
alias:
|
24
|
+
name: "path",
|
25
|
+
completionType: "file",
|
26
|
+
alias: "p",
|
28
27
|
defaultOption: true,
|
29
|
-
type: String
|
28
|
+
type: String,
|
30
29
|
},
|
31
30
|
{
|
32
|
-
name:
|
33
|
-
alias:
|
34
|
-
type: Boolean
|
31
|
+
name: "version",
|
32
|
+
alias: "v",
|
33
|
+
type: Boolean,
|
35
34
|
},
|
36
35
|
{
|
37
|
-
name:
|
38
|
-
alias:
|
39
|
-
type: Number
|
36
|
+
name: "duration",
|
37
|
+
alias: "d",
|
38
|
+
type: Number,
|
40
39
|
},
|
41
40
|
{
|
42
|
-
name:
|
43
|
-
alias:
|
44
|
-
type: Boolean
|
41
|
+
name: "encrypt",
|
42
|
+
alias: "e",
|
43
|
+
type: Boolean,
|
45
44
|
},
|
46
45
|
{
|
47
|
-
name:
|
48
|
-
alias:
|
46
|
+
name: "contacts",
|
47
|
+
alias: "c",
|
49
48
|
multiple: true,
|
50
|
-
type: String
|
49
|
+
type: String,
|
51
50
|
},
|
52
51
|
{
|
53
|
-
name:
|
52
|
+
name: "public-keys",
|
54
53
|
multiple: true,
|
55
|
-
type: String
|
54
|
+
type: String,
|
56
55
|
},
|
57
56
|
{
|
58
|
-
name:
|
59
|
-
type: String
|
57
|
+
name: "password",
|
58
|
+
type: String,
|
60
59
|
},
|
61
60
|
{
|
62
|
-
name:
|
63
|
-
type: Boolean
|
64
|
-
}
|
65
|
-
]
|
61
|
+
name: "include-me",
|
62
|
+
type: Boolean,
|
63
|
+
},
|
64
|
+
];
|
66
65
|
}
|
67
66
|
|
68
67
|
help() {
|
69
68
|
return {
|
70
69
|
description: [
|
71
|
-
|
72
|
-
|
70
|
+
"Export encrypted data to the OS in the current local folder",
|
71
|
+
"Files and folders are decrypted during the process.",
|
73
72
|
],
|
74
73
|
examples: [
|
75
|
-
[
|
76
|
-
[
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
[
|
81
|
-
[
|
82
|
-
|
83
|
-
|
84
|
-
|
74
|
+
["export seed.json", "decrypts and copies seed.json to the disk"],
|
75
|
+
[
|
76
|
+
"export seed.json -d 30",
|
77
|
+
"export seed.json and remove it from disk after 30 seconds",
|
78
|
+
],
|
79
|
+
["export ethKeys -v 8uW3", "exports version 8uW3 of the file"],
|
80
|
+
[
|
81
|
+
"export seed.json -e",
|
82
|
+
'asks for a password and encrypts seed.json before exporting it. The final file will have the extension ".secrez"',
|
83
|
+
],
|
84
|
+
[
|
85
|
+
'export seed.json -e --password "some strong password"',
|
86
|
+
"uses the typed password to encrypt seed.json before exporting it",
|
87
|
+
],
|
88
|
+
[
|
89
|
+
"export seed.json -ec bob alice",
|
90
|
+
"encrypts seed.json using a key shared with the contacts Bob and Alice, before exporting it",
|
91
|
+
],
|
92
|
+
[
|
93
|
+
"export seed.json -e --public-keys TCpDvTiVpHwNiS....",
|
94
|
+
"encrypts seed.json using shared keys generated from the specified public keys",
|
95
|
+
],
|
96
|
+
[
|
97
|
+
"export seed.json -e --include-me",
|
98
|
+
"encrypts seed.json also using your key",
|
99
|
+
],
|
100
|
+
],
|
101
|
+
};
|
85
102
|
}
|
86
103
|
|
87
104
|
async export(options = {}) {
|
88
|
-
let efs = this.externalFs
|
89
|
-
let fileCipher = new FileCipher(this.secrez)
|
90
|
-
let cat = this.prompt.commands.cat
|
91
|
-
let lpwd = this.prompt.commands.lpwd
|
92
|
-
let originalPath = options.path
|
93
|
-
let data = await this.internalFs.getTreeIndexAndPath(options.path)
|
94
|
-
options.path = data.path
|
95
|
-
let tree = data.tree
|
96
|
-
let p = tree.getNormalizedPath(options.path)
|
97
|
-
let file = tree.root.getChildFromPath(p)
|
105
|
+
let efs = this.externalFs;
|
106
|
+
let fileCipher = new FileCipher(this.secrez);
|
107
|
+
let cat = this.prompt.commands.cat;
|
108
|
+
let lpwd = this.prompt.commands.lpwd;
|
109
|
+
let originalPath = options.path;
|
110
|
+
let data = await this.internalFs.getTreeIndexAndPath(options.path);
|
111
|
+
options.path = data.path;
|
112
|
+
let tree = data.tree;
|
113
|
+
let p = tree.getNormalizedPath(options.path);
|
114
|
+
let file = tree.root.getChildFromPath(p);
|
98
115
|
if (Node.isFile(file)) {
|
99
|
-
let entry = (
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
let
|
107
|
-
let
|
108
|
-
|
109
|
-
|
116
|
+
let entry = (
|
117
|
+
await cat.cat({
|
118
|
+
path: originalPath,
|
119
|
+
version: options.version,
|
120
|
+
unformatted: true,
|
121
|
+
})
|
122
|
+
)[0];
|
123
|
+
let dir = await lpwd.lpwd();
|
124
|
+
let newPath = path.join(
|
125
|
+
dir,
|
126
|
+
path.basename(p) +
|
127
|
+
(options.encrypt ? ".secrez" + (Node.isBinary(entry) ? "b" : "") : "")
|
128
|
+
);
|
129
|
+
let name = await efs.getVersionedBasename(newPath);
|
130
|
+
let content = entry.content;
|
131
|
+
if (Node.isBinary(entry) && typeof content === "string") {
|
132
|
+
content = Crypto.bs64.decode(content);
|
110
133
|
}
|
111
134
|
if (options.encrypt) {
|
112
|
-
const myPublicKey = this.secrez.getPublicKey()
|
135
|
+
const myPublicKey = this.secrez.getPublicKey();
|
113
136
|
if (options.publicKeys) {
|
114
|
-
if (
|
115
|
-
options.
|
137
|
+
if (
|
138
|
+
options.includeMe &&
|
139
|
+
options.publicKeys.indexOf(myPublicKey) === -1
|
140
|
+
) {
|
141
|
+
options.publicKeys.push(myPublicKey);
|
116
142
|
}
|
117
143
|
} else if (options.contacts) {
|
118
|
-
options.publicKeys = await this.getContactsPublicKeys(options)
|
144
|
+
options.publicKeys = await this.getContactsPublicKeys(options);
|
119
145
|
if (options.includeMe) {
|
120
|
-
options.publicKeys.push(myPublicKey)
|
146
|
+
options.publicKeys.push(myPublicKey);
|
121
147
|
}
|
122
148
|
} else if (options.includeMe) {
|
123
|
-
options.publicKeys = [myPublicKey]
|
149
|
+
options.publicKeys = [myPublicKey];
|
124
150
|
} else {
|
125
|
-
let pwd =
|
126
|
-
|
127
|
-
|
128
|
-
|
151
|
+
let pwd =
|
152
|
+
options.password ||
|
153
|
+
(await this.useInput({
|
154
|
+
type: "password",
|
155
|
+
message: "Type the password",
|
156
|
+
}));
|
129
157
|
if (!pwd) {
|
130
|
-
throw new Error(
|
158
|
+
throw new Error("Operation canceled");
|
131
159
|
}
|
132
|
-
let pwd2 =
|
133
|
-
|
134
|
-
|
135
|
-
|
160
|
+
let pwd2 =
|
161
|
+
options.password ||
|
162
|
+
(await this.useInput({
|
163
|
+
type: "password",
|
164
|
+
message: "Retype it",
|
165
|
+
}));
|
136
166
|
if (!pwd2) {
|
137
|
-
throw new Error(
|
167
|
+
throw new Error("Operation canceled");
|
138
168
|
}
|
139
169
|
if (pwd !== pwd2) {
|
140
|
-
throw new Error(
|
170
|
+
throw new Error("The two password do not match");
|
141
171
|
}
|
142
|
-
options.password = pwd
|
172
|
+
options.password = pwd;
|
143
173
|
}
|
144
|
-
content = fileCipher.encryptFile(content, options).join(
|
174
|
+
content = fileCipher.encryptFile(content, options).join(",");
|
145
175
|
}
|
146
|
-
let fn = path.join(dir, name)
|
147
|
-
await fs.writeFile(fn, content)
|
176
|
+
let fn = path.join(dir, name);
|
177
|
+
await fs.writeFile(fn, content);
|
148
178
|
if (options.duration) {
|
149
|
-
this.deleteFromDisk(fn, options.duration)
|
179
|
+
this.deleteFromDisk(fn, options.duration);
|
150
180
|
}
|
151
|
-
return name
|
181
|
+
return name;
|
152
182
|
} else {
|
153
|
-
throw new Error(
|
183
|
+
throw new Error("Cannot export a folder");
|
154
184
|
}
|
155
185
|
}
|
156
186
|
|
157
187
|
async getContactsPublicKeys(options) {
|
158
|
-
let contacts = await this.prompt.commands.contacts.contacts({
|
159
|
-
|
188
|
+
let contacts = await this.prompt.commands.contacts.contacts({
|
189
|
+
list: true,
|
190
|
+
asIs: true,
|
191
|
+
});
|
192
|
+
let publicKeys = [];
|
160
193
|
for (let contact of contacts) {
|
161
194
|
if (options.contacts.indexOf(contact[0]) !== -1) {
|
162
|
-
publicKeys.push(contact[1].publicKey)
|
195
|
+
publicKeys.push(contact[1].publicKey);
|
163
196
|
}
|
164
197
|
}
|
165
|
-
return publicKeys
|
198
|
+
return publicKeys;
|
166
199
|
}
|
167
200
|
|
168
201
|
async deleteFromDisk(fn, duration) {
|
169
|
-
await sleep(1000 * duration)
|
202
|
+
await sleep(1000 * duration);
|
170
203
|
if (await fs.pathExists(fn)) {
|
171
|
-
fs.unlink(fn)
|
204
|
+
fs.unlink(fn);
|
172
205
|
}
|
173
206
|
}
|
174
207
|
|
175
208
|
async exec(options = {}) {
|
176
209
|
if (options.help) {
|
177
|
-
return this.showHelp()
|
210
|
+
return this.showHelp();
|
178
211
|
}
|
179
212
|
try {
|
180
|
-
this.validate(options)
|
181
|
-
let name = await this.export(options)
|
182
|
-
this.Logger.grey(
|
183
|
-
this.Logger.reset(name)
|
184
|
-
if (
|
185
|
-
|
186
|
-
|
213
|
+
this.validate(options);
|
214
|
+
let name = await this.export(options);
|
215
|
+
this.Logger.grey("Exported file:");
|
216
|
+
this.Logger.reset(name);
|
217
|
+
if (
|
218
|
+
options.encrypt &&
|
219
|
+
!options.password &&
|
220
|
+
!options.includeMe &&
|
221
|
+
!this.alerted
|
222
|
+
) {
|
223
|
+
this.Logger.yellow(
|
224
|
+
chalk.red("One time alert: ") +
|
225
|
+
"Only the users for which you encrypted the data can decrypt it; not even you can decrypt the exported data. Be careful!"
|
226
|
+
);
|
227
|
+
this.alerted = true;
|
187
228
|
}
|
188
229
|
} catch (e) {
|
189
|
-
this.Logger.red(e.message)
|
230
|
+
this.Logger.red(e.message);
|
190
231
|
}
|
191
|
-
await this.prompt.run()
|
232
|
+
await this.prompt.run();
|
192
233
|
}
|
193
234
|
}
|
194
235
|
|
195
|
-
module.exports = Export
|
196
|
-
|
197
|
-
|
236
|
+
module.exports = Export;
|