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 | fs.mkdirSync(options.parent.wallet, parseInt('0700', 8))
|
18 | return doInit()
|
19 | }
|
20 | throw e
|
21 | }
|
22 | process.stderr.write('Wallet exists. Update it? (y/n): ')
|
23 | prompt(null, function (resp) {
|
24 | if (resp.match(/^y/i)) {
|
25 | loadWallet(walletDir, function (err, wallet) {
|
26 | if (err) throw err
|
27 | doInit(wallet)
|
28 | })
|
29 | }
|
30 | else {
|
31 | console.error('Cancelled.')
|
32 | }
|
33 | })
|
34 |
|
35 | function doInit (wallet) {
|
36 | prompt.multi([
|
37 | {
|
38 | label: 'Your name',
|
39 | key: 'name',
|
40 | default: wallet && wallet.pubkey.name
|
41 | },
|
42 | {
|
43 | label: 'Your email address',
|
44 | key: 'email',
|
45 | default: wallet && wallet.pubkey.email
|
46 | },
|
47 | {
|
48 | label: 'Create a passphrase',
|
49 | key: 'passphrase',
|
50 | type: 'password'
|
51 | },
|
52 | {
|
53 | label: 'Verify passphrase',
|
54 | key: 'passphrase2',
|
55 | type: 'password',
|
56 | validate: function (val) {
|
57 | var ret = val === this.passphrase
|
58 | if (!ret) process.stderr.write('Passphrase did not match!\n')
|
59 | return ret
|
60 | }
|
61 | }
|
62 | ], function (info) {
|
63 | var isUpdate = !!wallet
|
64 | if (!wallet) {
|
65 | wallet = libWallet.create(info)
|
66 | }
|
67 | else {
|
68 | wallet.name = info.name
|
69 | wallet.email = info.email
|
70 | }
|
71 | var str = wallet.toPEM(info.passphrase)
|
72 | fs.writeFileSync(path.join(walletDir, 'id_salty'), str + '\n', {mode: parseInt('0600', 8)})
|
73 | fs.writeFileSync(path.join(walletDir, 'id_salty.pub'), wallet.pubkey.toString() + '\n', {mode: parseInt('0644', 8)})
|
74 | if (isUpdate) {
|
75 | console.log('\nWallet updated at ' + walletDir)
|
76 | }
|
77 | else {
|
78 | console.log('\nWallet created at ' + walletDir)
|
79 | console.log('Hint: Share this string with your peers so they can\n\tsalty import \'<pubkey>\'')
|
80 | console.log('...allowing them to `salty encrypt` messages to you!\n\n\t' + wallet.pubkey.toString() + '\n')
|
81 | }
|
82 | })
|
83 | }
|
84 | } |
\ | No newline at end of file |