working after refactor

This commit is contained in:
Matias Alejo Garcia 2015-02-16 19:54:38 -03:00
parent 57532242a4
commit 673ba2823b
5 changed files with 41 additions and 15 deletions

View File

@ -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 || '');
});

25
lib/bitcoinutils.js Normal file
View File

@ -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;

View File

@ -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;

View File

@ -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'));
}

View File

@ -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();
});
})
});
});