diff --git a/bit-wallet/bit-create b/bit-wallet/bit-create index ebbe5ee..75f2b7b 100755 --- a/bit-wallet/bit-create +++ b/bit-wallet/bit-create @@ -27,10 +27,14 @@ try { utils.die(ex); } -var client = utils.getClient(program); -client.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) { - utils.die(err); - console.log(' * ' + _.capitalize(network) + ' Wallet Created.'); - if (secret) - console.log(' - Secret to share:\n\t' + secret); +utils.getClient(program, function (client) { + client.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) { + utils.die(err); + console.log(' * ' + _.capitalize(network) + ' Wallet Created.'); + utils.saveClient(program, client, function () { + if (secret) { + console.log(' - Secret to share:\n\t' + secret); + } + }); + }); }); diff --git a/bit-wallet/bit-join b/bit-wallet/bit-join index fb169b6..6834b08 100755 --- a/bit-wallet/bit-join +++ b/bit-wallet/bit-join @@ -17,8 +17,10 @@ if (!args[0]) var secret = args[0]; var copayerName = args[1] || process.env.USER; -var client = utils.getClient(program); -client.joinWallet(secret, copayerName, function(err, xx) { - utils.die(err); - console.log(' * Wallet Joined.', xx || ''); +utils.getClient(program, function (client) { + client.joinWallet(secret, copayerName, function(err, wallet) { + utils.die(err); + console.log(' * Wallet Joined.', wallet.name); + utils.saveClient(program, client, function () {}); + }); }); diff --git a/bit-wallet/cli-utils.js b/bit-wallet/cli-utils.js index f6c65e8..44e7b02 100644 --- a/bit-wallet/cli-utils.js +++ b/bit-wallet/cli-utils.js @@ -1,5 +1,6 @@ var _ = require('lodash'); var Client = require('../lib/client'); +var FileStorage = require('./filestorage'); var read = require('read') var Utils = function() {}; @@ -39,55 +40,66 @@ Utils.confirmationId = function(copayer) { return parseInt(copayer.xPubKeySignature.substr(-4), 16).toString().substr(-4); } -Utils.getClient = function(args) { - var storage = new Client.FileStorage({ +Utils.getClient = function(args, cb) { + var storage = new FileStorage({ filename: args.file || process.env['BIT_FILE'], }); - var c = new Client({ - storage: storage, + var client = new Client({ baseUrl: args.host || process.env['BIT_HOST'], verbose: args.verbose, }); - - - if (args.nopasswd) - c.setNopasswdAccess(args.nopasswd); - - var setPassword; - c.on('needPassword', function(cb) { - if (args.password) { - return cb(args.password); - } else { - if (setPassword) - return cb(setPassword); - - read({ - prompt: 'Password for ' + args.file + ' : ', - silent: true - }, function(er, password) { - setPassword = password; - return cb(password); - }) + storage.load(function(err, walletData) { + if (err && err.code != 'ENOENT') die(err); + if (walletData) { + client.import(walletData); } + return cb(client); }); +}; - c.on('needNewPassword', function(cb) { - if (args.password) { - return cb(args.password); - } else { - read({ - prompt: 'New Password: ', - silent: true - }, function(er, password) { - return cb(password); - }) - } +Utils.saveClient = function(args, client, cb) { + var storage = new FileStorage({ + filename: args.file || process.env['BIT_FILE'], + }); + var str = client.export(); + storage.save(str, function(err) { + die(err); + return cb(); }); +}; +// var setPassword; +// c.on('needPassword', function(cb) { +// if (args.password) { +// return cb(args.password); +// } else { +// if (setPassword) +// return cb(setPassword); + +// read({ +// prompt: 'Password for ' + args.file + ' : ', +// silent: true +// }, function(er, password) { +// setPassword = password; +// return cb(password); +// }) +// } +// }); + +// c.on('needNewPassword', function(cb) { +// if (args.password) { +// return cb(args.password); +// } else { +// read({ +// prompt: 'New Password: ', +// silent: true +// }, function(er, password) { +// return cb(password); +// }) +// } +// }); - return c; -} Utils.findOneTxProposal = function(txps, id) { var matches = _.filter(txps, function(tx) { diff --git a/bit-wallet/filestorage.js b/bit-wallet/filestorage.js new file mode 100644 index 0000000..021a774 --- /dev/null +++ b/bit-wallet/filestorage.js @@ -0,0 +1,30 @@ +var fs = require('fs') + +function FileStorage(opts) { + if (!opts.filename) { + throw new Error('Please set wallet filename'); + } + this.filename = opts.filename; + this.fs = opts.fs || fs; +}; + +FileStorage.prototype.getName = function() { + return this.filename; +}; + +FileStorage.prototype.save = function(data, cb) { + this.fs.writeFile(this.filename, JSON.stringify(data), cb); +}; + +FileStorage.prototype.load = function(cb) { + this.fs.readFile(this.filename, 'utf8', function(err, data) { + if (err) return cb(err); + try { + data = JSON.parse(data); + } catch (e) {} + return cb(null, data); + }); +}; + + +module.exports = FileStorage; diff --git a/lib/client/api.js b/lib/client/api.js index 8f07729..97635a3 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -145,16 +145,18 @@ API.prototype.import = function(str, opts) { } } + var credentials; try { if (opts.compressed) { - this.credentials = Credentials.importCompressed(input); + credentials = Credentials.importCompressed(input); // TODO: complete missing fields that live on the server only such as: walletId, walletName, copayerName } else { - this.credentials = Credentials.fromObj(JSON.parse(input)); + credentials = Credentials.fromObj(JSON.parse(input)); } } catch (ex) { throw new Error('Error importing from source'); } + this.credentials = credentials; }; API.prototype.toString = function(password) {