UNPKG

1.32 kBJavaScriptView Raw
1var fs = require('fs')
2 , path = require('path')
3 , prompt = require('cli-prompt')
4 , zlib = require('zlib')
5 , tar = require('tar')
6 , pempal = require('pempal')
7
8module.exports = function (inPath, outDir, options) {
9 if (!inPath) inPath = 'salty.pem'
10 if (!outDir) outDir = options.parent.wallet
11 if (options.parent.force) return withCheck()
12 try {
13 fs.statSync(outDir)
14 }
15 catch (e) {
16 if (e && e.code === 'ENOENT') return withCheck()
17 throw e
18 }
19 throw new Error('Refusing to overwrite ' + outDir + '. Use --force to ignore this.')
20 function withCheck () {
21 var str = fs.readFileSync(inPath, {encoding: 'utf8'})
22 if (str.indexOf('4,ENCRYPTED') !== 1) {
23 prompt.password('Enter passphrase: ', function (passphrase) {
24 var parsedPem = pempal.decode(str, {tag: 'SALTY WALLET', passphrase: passphrase})
25 withParsed(parsedPem.body)
26 })
27 }
28 else {
29 var parsedPem = pempal.decode(str, {tag: 'SALTY WALLET'})
30 withParsed(parsedPem.body)
31 }
32 }
33 function withParsed (buf) {
34 var extractStream = tar.Extract({path: outDir, mode: parseInt('0700', 8)})
35 var gunzipStream = zlib.createGunzip()
36 extractStream.on('end', function () {
37 console.log('Restored to', outDir)
38 })
39 gunzipStream.pipe(extractStream)
40 gunzipStream.end(buf)
41 }
42}
\No newline at end of file