forked from alexbosworth/balanceofsatoshis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt_ciphertext.js
61 lines (48 loc) · 1.42 KB
/
decrypt_ciphertext.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
const asyncAuto = require('async/auto');
const {returnResult} = require('asyncjs-util');
/** Decrypt ciphertext that has been encrypted to GPG keys
{
cipher: <Encrypted Text String>
spawn: <Spawn Function>
}
@returns via cbk or Promise
{
clear: <Clear Text String>
}
*/
module.exports = ({cipher, spawn}, cbk) => {
return new Promise((resolve, reject) => {
return asyncAuto({
// Check arguments
validate: cbk => {
if (!cipher) {
return cbk([400, 'ExpectedCiphertextToDecrypt']);
}
if (!spawn) {
return cbk([400, 'ExpectedSpawnFunctionToDecryptCiphertext']);
}
return cbk();
},
// Decrypt the ciphertext
decrypt: ['validate', ({}, cbk) => {
const datas = [];
const decrypt = spawn('gpg', ['-d']);
decrypt.stdin.setEncoding('utf-8');
decrypt.stdout.on('data', data => datas.push(data));
decrypt.stdout.on('error', err => cbk([503, 'DecryptionFail', {err}]));
decrypt.stdout.on('end', () => {
if (!datas.length) {
return cbk([503, 'FailedToDecrypt']);
}
return cbk(null, {
clear: Buffer.concat(datas).toString('utf8').trim(),
});
});
decrypt.stdin.write(`${cipher}`);
decrypt.stdin.end();
return;
}],
},
returnResult({reject, resolve, of: 'decrypt'}, cbk));
});
};