From ac3037f1d438571ade571b6d0997e19adef0371b Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 18 Feb 2015 03:34:11 -0300 Subject: [PATCH] add recreate --- bit-wallet/bit | 1 + bit-wallet/bit-recreate | 25 +++++++++++++++++++++++++ lib/client/api.js | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100755 bit-wallet/bit-recreate diff --git a/bit-wallet/bit b/bit-wallet/bit index 40b36d9..6b9a031 100755 --- a/bit-wallet/bit +++ b/bit-wallet/bit @@ -18,6 +18,7 @@ program .command('export', 'export wallet critical data') .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') .parse(process.argv); diff --git a/bit-wallet/bit-recreate b/bit-wallet/bit-recreate new file mode 100755 index 0000000..3c035bc --- /dev/null +++ b/bit-wallet/bit-recreate @@ -0,0 +1,25 @@ +#!/usr/bin/env node + +var _ = require('lodash'); +var program = require('commander'); +var ClientLib = require('../lib/client'); +var utils = require('./cli-utils'); + +program + .version('0.0.1') + .option('-c, --config [file]', 'Wallet config filename') + .option('-h, --host [host]', 'Bitcore Wallet Service URL (eg: http://localhost:3001/copay/api') + .usage('[options] walletname') + .description('Creates a wallet on the remove server given the local information') + .parse(process.argv); + +var args = program.args; +if (!args[0]) + program.help(); + +var walletName = args[0]; +var client = utils.getClient(program); +client.reCreateWallet(walletName, function(err) { + utils.die(err); + console.log(' * Wallet Created.'); +}); diff --git a/lib/client/api.js b/lib/client/api.js index 80b5d36..167df6e 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -215,6 +215,39 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb }); }; + +API.prototype.reCreateWallet = function(walletName, cb) { + var self = this; + this._loadAndCheck(function(err, data) { + if (err) return cb(err); + + var walletPrivKey = new Bitcore.PrivateKey(); + var args = { + name: walletName, + m: data.m, + n: data.n, + pubKey: walletPrivKey.toPublicKey().toString(), + network: data.network, + }; + var url = '/v1/wallets/'; + self._doPostRequest(url, args, {}, function(err, body) { + if (err) return cb(err); + + var walletId = body.walletId; + + var secret = WalletUtils.toSecret(walletId, walletPrivKey, data.network); + var i = 0; + async.each(data.publicKeyRing, function(xpub, next) { + var copayerName = 'recovered Copayer #' + i; + self._doJoinWallet(walletId, walletPrivKey, data.publicKeyRing[i++], copayerName, next); + }, function(err) { + return cb(err); + }); + }); + }); +}; + + API.prototype.joinWallet = function(secret, copayerName, cb) { var self = this; @@ -345,7 +378,7 @@ API.prototype.import = function(str, cb) { data.publicKeyRing.push(xPubKey); data.copayerId = WalletUtils.xPubToCopayerId(xPubKey); - data.m = data.publicKeyRing.length; + data.n = data.publicKeyRing.length; data.signingPrivKey = (new Bitcore.HDPrivateKey(data.xPrivKey)).derive('m/1/0').privateKey.toWIF(); data.network = data.xPrivKey.substr(0, 4) === 'tprv' ? 'testnet' : 'livenet'; self.storage.save(data, cb);