diff --git a/bit-wallet/bit b/bit-wallet/bit index d1069d5..fbd840b 100755 --- a/bit-wallet/bit +++ b/bit-wallet/bit @@ -19,6 +19,8 @@ program .command('import', 'import wallet critical data') .command('confirm', 'show copayer\'s data for confirmation') .command('recreate', 'recreate a wallet on a remove server given local infomation') + .command('txproposals', 'list transactions proposals') + .command('genkey', 'generates extended private key for later wallet usage') .parse(process.argv); diff --git a/bit-wallet/bit-genkey b/bit-wallet/bit-genkey new file mode 100755 index 0000000..83b7164 --- /dev/null +++ b/bit-wallet/bit-genkey @@ -0,0 +1,20 @@ +#!/usr/bin/env node + +var _ = require('lodash'); +var program = require('commander'); +var Client = require('../lib/client'); +var utils = require('./cli-utils'); +program = utils.configureCommander(program); + +program + .option('-t, --testnet', 'Create a Testnet Extended Private Key') + .parse(process.argv); + +var args = program.args; +var client = utils.getClient(program); +var network = program.testnet ? 'testnet' : 'livenet'; + +client.generateKey(network, function(err) { + utils.die(err); + console.log(' * ' + _.capitalize(network) + ' Extended Private Key Created.'); +}); diff --git a/lib/client/api.js b/lib/client/api.js index cec3b83..63d84cb 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -228,21 +228,27 @@ API.prototype._initData = function(network, walletPrivKey, m, n) { var xPubKey = (new Bitcore.HDPublicKey(xPrivKey)).toString(); var roPrivKey = xPrivKey.derive('m/1/0').privateKey; var rwPrivKey = xPrivKey.derive('m/1/1').privateKey; - var sharedEncryptingKey = WalletUtils.privateKeyToAESKey(walletPrivKey); var copayerId = WalletUtils.xPubToCopayerId(xPubKey); + var data = { copayerId: copayerId, xPrivKey: xPrivKey.toString(), publicKeyRing: [xPubKey], network: network, - m: m, - n: n, roPrivKey: roPrivKey.toWIF(), rwPrivKey: rwPrivKey.toWIF(), - walletPrivKey: walletPrivKey.toWIF(), - sharedEncryptingKey: sharedEncryptingKey, }; + + if (walletPrivKey) { + var sharedEncryptingKey = WalletUtils.privateKeyToAESKey(walletPrivKey); + data.walletPrivKey = walletPrivKey.toWIF(); + data.sharedEncryptingKey = sharedEncryptingKey; + } + + if (m) data.m = m; + if (n) data.n = n; + return data; }; @@ -260,6 +266,22 @@ API.prototype._doJoinWallet = function(walletId, walletPrivKey, xPubKey, copayer }); }; +API.prototype.generateKey = function(network, cb) { + var self = this; + network = network || 'livenet'; + if (!_.contains(['testnet', 'livenet'], network)) + return cb('Invalid network'); + + this.storage.load(function(err, data) { + if (data) + return cb(self.storage.getName() + ' already contains a wallet'); + + var data = self._initData(network); + self.storage.save(data, function(err) { + return cb(err, null); + }); + }); +}; API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) { var self = this; @@ -471,7 +493,7 @@ API.prototype.export = function(opts, cb) { opts = opts || {}; var access = opts.access || 'full'; - this._loadAndCheck({}, function(err, data) { + this._load(function(err, data) { if (err) return cb(err); var v = []; @@ -532,9 +554,12 @@ API.prototype.import = function(str, cb) { data.rwPrivKey = xpriv.derive('m/1/1').privateKey.toWIF(); } - data.n = data.publicKeyRing.length; + var dataIsComplete = !!data.m; + + if (dataIsComplete) + data.n = data.publicKeyRing.length; - if (!data.copayerId || !data.n || !data.m) + if (!data.publicKeyRing) return cb('Invalid source data'); data.network = data.publicKeyRing[0].substr(0, 4) == 'tpub' ? 'testnet' : 'livenet';