1 | var prompt = require('cli-prompt')
|
2 | , fs = require('fs')
|
3 | , loadWallet = require('../utils/loadWallet')
|
4 | , libWallet = require('../lib/wallet')
|
5 | , path = require('path')
|
6 |
|
7 | module.exports = function (options) {
|
8 | var walletDir = options.parent.wallet
|
9 | try {
|
10 | var stat = fs.statSync(path.join(walletDir, 'id_salty'))
|
11 | }
|
12 | catch (e) {
|
13 | if (e.code === 'ENOENT') {
|
14 | if (e && e.code === 'ENOENT') {
|
15 | process.stderr.write('Creating wallet...\n')
|
16 | }
|
17 | try {
|
18 | fs.mkdirSync(options.parent.wallet, parseInt('0700', 8))
|
19 | }
|
20 | catch (e) {}
|
21 | return doInit()
|
22 | }
|
23 | throw e
|
24 | }
|
25 | process.stderr.write('Wallet exists. Update it? (y/n): ')
|
26 | prompt(null, function (resp) {
|
27 | if (resp.match(/^y/i)) {
|
28 | loadWallet(walletDir, function (err, wallet) {
|
29 | if (err) throw err
|
30 | doInit(wallet)
|
31 | })
|
32 | }
|
33 | else {
|
34 | console.error('Cancelled.')
|
35 | }
|
36 | }, function (err) {
|
37 | throw err
|
38 | })
|
39 |
|
40 | function doInit (wallet) {
|
41 | var q = []
|
42 | if (options.regen) {
|
43 | q.push({
|
44 | label: 'Are you SURE you want a new decryption key? Your old key will be gone forever! (y/n)',
|
45 | key: 'sure',
|
46 | validate: function (val) {
|
47 | if (!val.match(/^y/i)) {
|
48 | console.error('Cancelled.')
|
49 | process.exit(1)
|
50 | }
|
51 | }
|
52 | })
|
53 | }
|
54 | q = q.concat([
|
55 | {
|
56 | label: 'Your name',
|
57 | key: 'name',
|
58 | default: wallet && wallet.pubkey.name
|
59 | },
|
60 | {
|
61 | label: 'Your email address',
|
62 | key: 'email',
|
63 | default: wallet && wallet.pubkey.email
|
64 | },
|
65 | {
|
66 | label: 'Create a passphrase',
|
67 | key: 'passphrase',
|
68 | type: 'password'
|
69 | },
|
70 | {
|
71 | label: 'Verify passphrase',
|
72 | key: 'passphrase2',
|
73 | type: 'password',
|
74 | validate: function (val) {
|
75 | var ret = val === this.passphrase
|
76 | if (!ret) process.stderr.write('Passphrase did not match!\n')
|
77 | return ret
|
78 | }
|
79 | }
|
80 | ])
|
81 | prompt.multi(q, function (info) {
|
82 | var isUpdate = !!wallet
|
83 | if (isUpdate) {
|
84 | wallet.pubkey.name = info.name === 'NULL' ? null : info.name
|
85 | wallet.pubkey.email = info.email === 'NULL' ? null : info.email
|
86 | if (options.regen) {
|
87 | wallet.regen()
|
88 | }
|
89 | }
|
90 | else {
|
91 | wallet = libWallet.create(info)
|
92 | }
|
93 | var str = wallet.toPEM(info.passphrase)
|
94 | fs.writeFileSync(path.join(walletDir, 'id_salty'), str + '\n', {mode: parseInt('0600', 8)})
|
95 | fs.writeFileSync(path.join(walletDir, 'id_salty.pub'), wallet.pubkey.toString() + '\n', {mode: parseInt('0644', 8)})
|
96 | if (isUpdate) {
|
97 | console.log('\nWallet updated at ' + walletDir)
|
98 | }
|
99 | else {
|
100 | console.log('\nWallet created at ' + walletDir)
|
101 | console.log('Hint: Share this string with your peers so they can\n\tsalty import \'<pubkey>\'')
|
102 | console.log('...allowing them to `salty encrypt` messages to you!\n\n\t' + wallet.pubkey.toString() + '\n')
|
103 | }
|
104 | }, function (err) {
|
105 | throw err
|
106 | })
|
107 | }
|
108 | } |
\ | No newline at end of file |