1 | var fs = require('fs')
|
2 | , path = require('path')
|
3 | , tar = require('tar')
|
4 | , zlib = require('zlib')
|
5 | , fstream = require('fstream')
|
6 | , prompt = require('cli-prompt')
|
7 | , pempal = require('pempal')
|
8 |
|
9 | module.exports = function (inDir, outPath, options) {
|
10 | if (!inDir) inDir = options.parent.wallet
|
11 | if (!outPath) outPath = 'salty.pem'
|
12 | if (options.parent.force) return withCheck()
|
13 | try {
|
14 | fs.statSync(outPath)
|
15 | }
|
16 | catch (e) {
|
17 | if (e.code === 'ENOENT') {
|
18 | return withCheck()
|
19 | }
|
20 | }
|
21 | throw new Error('Refusing to overwrite ' + outPath + '. Use --force to ignore this.')
|
22 | function withCheck () {
|
23 | prompt.multi([
|
24 | {
|
25 | label: 'Create a passphrase',
|
26 | key: 'passphrase',
|
27 | type: 'password'
|
28 | },
|
29 | {
|
30 | label: 'Verify passphrase',
|
31 | key: 'passphrase2',
|
32 | type: 'password',
|
33 | validate: function (val) {
|
34 | var ret = val === this.passphrase
|
35 | if (!ret) process.stderr.write('Passphrase did not match!\n')
|
36 | return ret
|
37 | }
|
38 | }
|
39 | ], function (info) {
|
40 | var tarStream = tar.Pack({fromBase: true})
|
41 | var gzipStream = tarStream.pipe(zlib.createGzip())
|
42 | var gzipChunks = []
|
43 | gzipStream.on('data', function (chunk) {
|
44 | gzipChunks.push(chunk)
|
45 | })
|
46 | gzipStream.once('end', function () {
|
47 | var zlibBuffer = Buffer.concat(gzipChunks)
|
48 | var pem = pempal.encode(zlibBuffer, {tag: 'SALTY WALLET', passphrase: info.passphrase})
|
49 | fs.writeFile(outPath, pem + '\n', {mode: parseInt('0644', 8)}, function (err) {
|
50 | if (err) throw err
|
51 | console.log('Saved to', outPath)
|
52 | })
|
53 | })
|
54 | var reader = fstream.Reader({
|
55 | path: inDir,
|
56 | type: 'Directory',
|
57 | sort: 'alpha',
|
58 | mode: parseInt('0700', 8)
|
59 | })
|
60 | reader.pipe(tarStream)
|
61 | }, function (err) {
|
62 | throw err
|
63 | })
|
64 | }
|
65 | } |
\ | No newline at end of file |