From 673ba2823b58d747cb902545866e9573ab8da369 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 16 Feb 2015 19:54:38 -0300 Subject: [PATCH] working after refactor --- bit-wallet/bit-join | 2 +- lib/bitcoinutils.js | 25 +++++++++++++++++++++++++ lib/client/Verifier.js | 5 ++--- lib/client/api.js | 21 ++++++++++++--------- test/integration/clientApi.js | 3 +-- 5 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 lib/bitcoinutils.js diff --git a/bit-wallet/bit-join b/bit-wallet/bit-join index d30b63a..bbfb314 100755 --- a/bit-wallet/bit-join +++ b/bit-wallet/bit-join @@ -18,7 +18,7 @@ var secret = args[0]; var copayerName = args[1] || process.env.USER; var client = utils.getClient(program); -cli.joinWallet(secret, copayerName, function(err, xx) { +client.joinWallet(secret, copayerName, function(err, xx) { utils.die(err); console.log(' * Wallet Joined.', xx || ''); }); diff --git a/lib/bitcoinutils.js b/lib/bitcoinutils.js new file mode 100644 index 0000000..2a29ea1 --- /dev/null +++ b/lib/bitcoinutils.js @@ -0,0 +1,25 @@ + +var _ = require('lodash'); + +var Bitcore = require('bitcore'); +var BitcoreAddress = Bitcore.Address; + +function BitcoinUtils () {}; + +BitcoinUtils.deriveAddress = function(publicKeyRing, path, m, network) { + + var publicKeys = _.map(publicKeyRing, function(xPubKey) { + var xpub = new Bitcore.HDPublicKey(xPubKey); + return xpub.derive(path).publicKey; + }); + + var bitcoreAddress = BitcoreAddress.createMultisig(publicKeys, m, network); + + return { + address: bitcoreAddress.toString(), + path: path, + publicKeys: _.invoke(publicKeys, 'toString'), + }; +}; + +module.exports = BitcoinUtils; diff --git a/lib/client/Verifier.js b/lib/client/Verifier.js index fdb7f2c..580468b 100644 --- a/lib/client/Verifier.js +++ b/lib/client/Verifier.js @@ -6,9 +6,8 @@ var BitcoinUtils = require('../bitcoinutils') function Verifier(opts) {}; Verifier.checkAddress = function(data, address) { - var local = BitcoinUtils.deriveAddress(data.publicKeyRing, address.path, data.m, data.network); - return (local.address == address.address - && JSON.stringify(local.publicKeys) == JSON.stringify(address.publicKeys)); + var local = BitcoinUtils.deriveAddress(data.publicKeyRing, address.path, data.m, data.network); + return (local.address == address.address && JSON.stringify(local.publicKeys) == JSON.stringify(address.publicKeys)); }; module.exports = Verifier; diff --git a/lib/client/api.js b/lib/client/api.js index 39081ef..1bfdd22 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -1,6 +1,7 @@ 'use strict'; var _ = require('lodash'); +var $ = require('preconditions').singleton(); var util = require('util'); var async = require('async'); var log = require('npmlog'); @@ -61,7 +62,7 @@ function API(opts) { }; -API.prototype._loadAndCheck = function() { +API.prototype._loadAndCheck = function(opts) { var data = this.storage.load(); if (!data) { log.error('Wallet file not found.'); @@ -69,11 +70,15 @@ API.prototype._loadAndCheck = function() { } if (data.verified == 'corrupt') { - log.error('The wallet is tagged as corrupt. Some of the copayers cannot be verified to have known the wallet secret.'); - process.exit(1); + throw new Error('The wallet is tagged as corrupt. Some of the copayers cannot be verified to have known the wallet secret.'); } if (data.n > 1) { var pkrComplete = data.publicKeyRing && data.m && data.publicKeyRing.length === data.n; + + if (opts.requireCompletePKR && !pkrComplete) { + throw new Error('Wallet Incomplete, cannot derive address.'); + } + if (!pkrComplete) { log.warn('The file ' + this.filename + ' is incomplete. It will allow you to operate with the wallet but it should not be trusted as a backup. Please wait for all copayers to join the wallet and run the tool with -export flag.') } @@ -135,7 +140,7 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb data = { m: m, n: n, - walletPrivKey: privKey.toString(), + walletPrivKey: privKey.toWIF(), network: network, }; @@ -193,11 +198,12 @@ API.prototype._joinWallet = function(data, secret, copayerName, cb) { this._doPostRequest(url, args, data, function(err, body) { var wallet = body.wallet; data.copayerId = body.copayerId; - data.walletPrivKey = walletPrivKey; + data.walletPrivKey = walletPrivKey.toWIF(); data.signingPrivKey = signingPrivKey.toString(); data.m = wallet.m; data.n = wallet.n; data.publicKeyRing = wallet.publicKeyRing; + data.network = wallet.network, self.storage.save(data); return cb(); @@ -283,13 +289,10 @@ API.prototype.getAddresses = function(cb) { API.prototype.createAddress = function(cb) { var self = this; - var data = this._loadAndCheck(); - $.checkState(data.publicKeyRing.length != data.n, 'Wallet Incomplete, cannot derive address.'); - + var data = this._loadAndCheck({requireCompletePKR: true}); var url = '/v1/addresses/'; this._doPostRequest(url, {}, data, function(err, address) { if (err) return cb(err); - if (!Verifier.checkAddress(data, address)) { return cb(new ServerCompromisedError('Server sent fake address')); } diff --git a/test/integration/clientApi.js b/test/integration/clientApi.js index d39bada..ba8d8f6 100644 --- a/test/integration/clientApi.js +++ b/test/integration/clientApi.js @@ -60,7 +60,6 @@ describe('client API', function() { done(); }); }) - }); it('should detect fake addresses', function(done) { var response = { createdOn: 1424105995, @@ -78,5 +77,5 @@ describe('client API', function() { done(); }); }) - + }); });