-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·227 lines (193 loc) · 6.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env node
require("babel-polyfill")
const co = require('co')
const fs = require('fs')
const uuidv4 = require('uuid/v4')
const promisify = require('sb-promisify').default
const TrezorSession = require('trezor-session')
const {random32ByteBuffer} = require('./entropy')
const clipboard = require('./clipboard')()
const trez = require('./trez-format')
const argv = require('yargs').usage(`
Trez - File encryption program making use of Trezor hardware wallet security.
Standard input is used for trezor prompts (pin, passphrase).
`)
.option('clipboard-save', {describe: 'Save next clipboard copy to an encrypted file (clears the clipboard).', type: 'string', alias: 's'})
.option('clipboard-load', {describe: 'Load the clipboard with decrypted data.', type: 'string', alias: 'l'})
.option('force', {describe: 'Force overwrite file', type: 'boolean', alias: 'f'})
.option('check', {describe: 'Check a trez file', type: 'string', alias: 'k'})
.example('$0 --clipboard-save [myfile.txt.trez, omit to generate filename]')
.example('$0 --clipboard-load myfile.txt.trez')
.example('$0 myfile.txt newfile.trez', 'Encrypt to newfile.trez')
.example('$0 myfile.txt', 'Encrypt to myfile.txt.trez')
.example('$0 myfile.txt.trez', 'Decrypt to myfile.txt')
.example('$0 myfile.txt.trez -', 'Decrypt to standard out')
.example('$0 myfile.txt.trez /safe/myfile.txt', 'Decrypt')
.help('help').alias('help', 'h').alias('help', '?').argv
const files = argv._
const clipboardToFile = argv['clipboard-save']
const clipboardFromFile = argv['clipboard-load']
const check = argv['check']
const force = argv['force']
// The implementation below, based on arguments will define only one:
// readCipherText or readPlainText
/* Return {function} returns Buffer */
let readCipherText
/** Return {function} return Buffer */
let readPlainText
/**
Required
@arg ciperOrPlain<Buffer>
@return {undefined|Promise} complete
*/
let saveBuffer
/** @return {undefined|Promise} complete */
let onSuccess = () => {}
if(check) {
if(!fs.existsSync(check)) {
console.error('File does not exist: ' + check)
process.exit(1)
}
const data = fs.readFileSync(check)
const {validData, validHeader} = trez.check(data)
if(!validData || !validHeader) {
console.error(`Invalid file: ${check}${validData ? '' : ', invalid data'}${validHeader ? '': ', invalid header'}`)
process.exit(1)
}
console.error(`OK!`)
process.exit(0)
}
if(files.length && (clipboardToFile || clipboardFromFile)) {
console.error('Please work with files or the clipboard but not both')
process.exit(1)
}
if(files.length > 2) {
console.error('Expecting only 2 files, instead got ' + files.length)
process.exit(1)
}
const saveFileOrStdout = fn => buf => {
if(fn.trim() === '-') {
process.stdout.write(buf.toString('binary'))
return
}
console.error('Writing ' + fn)
fs.writeFileSync(fn, buf)
}
if(files.length) {
let [f1, f2] = files
if(!fs.existsSync(f1)) {
console.error('File does not exist: ' + f1)
process.exit(1)
}
const data = fs.readFileSync(f1)
let isSourceEncrypted
try {
trez.dissect(data)
isSourceEncrypted = true
} catch(error) {
isSourceEncrypted = false
}
if(f2 == null) {
if(isSourceEncrypted) {
f2 = f1.replace(/.trez$/, '')
if(f1 === f2) {
f2 = `${uuidv4()}.trez`
}
} else {
f2 = f1 + '.trez'
}
}
if(fs.existsSync(f2)) {
if(!force) {
console.error('File exist, use --force to overwrite: ' + f2)
process.exit(1)
}
}
if(isSourceEncrypted) {
// decrypt trez => plainbuf
readCipherText = () => fs.readFileSync(f1)
saveBuffer = saveFileOrStdout(f2)
} else {
readPlainText = () => fs.readFileSync(f1)
saveBuffer = saveFileOrStdout(f2)
}
}
// clipboard to file
if(clipboardToFile != null) {
const fn = clipboardToFile.trim() === '' ? `${uuidv4()}.trez` : clipboardToFile
if(fs.existsSync(fn) && !force) {
console.error('File exist, use --force to overwrite: ' + fn)
process.exit(1)
}
saveBuffer = saveFileOrStdout(fn)
readPlainText = () => {
console.error('Checking clipboard for new data. Copy it but do not paste. I\'ll encrypt, save, then erase..')
return promisify(clipboard.nextClip)().then(clip => Buffer.from(clip, 'utf-8'))
}
onSuccess = () => {
console.error('Erasing clipboard')
clipboard.nextClipClear()
}
}
// file to clipboard
if(clipboardFromFile != null) {
if(/^-?$/.test(clipboardFromFile.trim())) { // if dash or empty
console.error('--clipboard-load requires a file name')
process.exit(1)
}
if(!fs.existsSync(clipboardFromFile)) {
console.error('File does not exist: ' + clipboardFromFile)
process.exit(1)
}
readCipherText = () => loadFile(clipboardFromFile)
saveBuffer = buf => clipboard.push(buf.toString('utf-8'))
}
if(!readCipherText && !readPlainText) {
console.error('Nothing to do.')
console.error('Try -? for help')
process.exit(1)
}
const trezorSession = TrezorSession()
const errorExit = (error) => {
console.error(error)
process.exit(1)
}
trezorSession(
(err, session) => err ?
errorExit(err) : co.wrap(main)(session).catch(errorExit)
)
function loadFile(fn) {
fn = fn.trim()
let buf
if(fn === '-') {
// trezor prompts interfere
console.error('data source can not be an standard input')
process.exit(1)
} else if(fn === '') {
console.error('input file name is required')
process.exit(1)
}
return fs.readFileSync(fn)
}
process.on('exit', function() {
trezorSession.close()
})
function* main(session) {
if(readPlainText) {
const entropy = () => {
console.error('Gathering entropy..')
return random32ByteBuffer()
}
const readData = () => Promise.resolve(readPlainText())
const config = {saveLabel: true, entropy}
const cypherbuf = yield trez.encrypt(session, readData, config)
yield Promise.resolve(saveBuffer(cypherbuf))
yield Promise.resolve(onSuccess())
} else if(readCipherText) {
const readData = () => Promise.resolve(readCipherText())
const plainbuf = yield trez.decrypt(session, readData)
yield Promise.resolve(saveBuffer(plainbuf))
yield Promise.resolve(onSuccess())
}
process.exit()
}