bitcore-wallet-client
Version:
Client for bitcore-wallet-service
1,451 lines (1,339 loc) • 152 kB
JavaScript
'use strict';
var _ = require('lodash');
var $ = require('preconditions').singleton();
var chai = require('chai');
chai.config.includeStack = true;
var sinon = require('sinon');
var should = chai.should();
var async = require('async');
var request = require('supertest');
var Uuid = require('uuid');
var tingodb = require('tingodb')({
memStore: true
});
var log = require('../lib/log');
var Bitcore = require('bitcore-lib');
var BitcorePayPro = require('bitcore-payment-protocol');
var BWS = require('bitcore-wallet-service');
var Common = require('../lib/common');
var Constants = Common.Constants;
var Utils = Common.Utils;
var Client = require('../lib');
var ExpressApp = BWS.ExpressApp;
var Storage = BWS.Storage;
var TestData = require('./testdata');
var ImportData = require('./legacyImportData.js');
var Errors = require('../lib/errors');
var helpers = {};
helpers.toSatoshi = function(btc) {
if (_.isArray(btc)) {
return _.map(btc, helpers.toSatoshi);
} else {
return parseFloat((btc * 1e8).toPrecision(12));
}
};
helpers.getRequest = function(app) {
$.checkArgument(app);
return function(args, cb) {
var req = request(app);
var r = req[args.method](args.relUrl);
if (args.headers) {
_.each(args.headers, function(v, k) {
if (k && v) {
r.set(k, v);
}
});
}
if (!_.isEmpty(args.body)) {
r.send(args.body);
};
r.end(function(err, res) {
return cb(err, res, res.body);
});
};
};
helpers.newClient = function(app) {
$.checkArgument(app);
return new Client({
request: helpers.getRequest(app),
});
};
helpers.newDb = function() {
this.dbCounter = (this.dbCounter || 0) + 1;
return new tingodb.Db('./db/test' + this.dbCounter, {});
};
helpers.generateUtxos = function(scriptType, publicKeyRing, path, requiredSignatures, amounts) {
var amounts = [].concat(amounts);
var utxos = _.map(amounts, function(amount, i) {
var address = Utils.deriveAddress(scriptType, publicKeyRing, path, requiredSignatures, 'testnet');
var scriptPubKey;
switch (scriptType) {
case Constants.SCRIPT_TYPES.P2SH:
scriptPubKey = Bitcore.Script.buildMultisigOut(address.publicKeys, requiredSignatures).toScriptHashOut();
break;
case Constants.SCRIPT_TYPES.P2PKH:
scriptPubKey = Bitcore.Script.buildPublicKeyHashOut(address.address);
break;
}
should.exist(scriptPubKey);
var obj = {
txid: Bitcore.crypto.Hash.sha256(new Buffer(i)).toString('hex'),
vout: 100,
satoshis: helpers.toSatoshi(amount),
scriptPubKey: scriptPubKey.toBuffer().toString('hex'),
address: address.address,
path: path,
publicKeys: address.publicKeys,
};
return obj;
});
return utxos;
};
helpers.createAndJoinWallet = function(clients, m, n, cb) {
clients[0].seedFromRandomWithMnemonic({
network: 'testnet'
});
clients[0].createWallet('wallet name', 'creator', m, n, {
network: 'testnet'
}, function(err, secret) {
should.not.exist(err);
if (n > 1) {
should.exist(secret);
}
async.series([
function(next) {
async.each(_.range(1, n), function(i, cb) {
clients[i].seedFromRandomWithMnemonic({
network: 'testnet'
});
clients[i].joinWallet(secret, 'copayer ' + i, {}, cb);
}, next);
},
function(next) {
async.each(_.range(n), function(i, cb) {
clients[i].openWallet(cb);
}, next);
},
],
function(err) {
should.not.exist(err);
return cb({
m: m,
n: n,
secret: secret,
});
});
});
};
helpers.tamperResponse = function(clients, method, url, args, tamper, cb) {
clients = [].concat(clients);
// Use first client to get a clean response from server
clients[0]._doRequest(method, url, args, function(err, result) {
should.not.exist(err);
tamper(result);
// Return tampered data for every client in the list
_.each(clients, function(client) {
client._doRequest = sinon.stub().withArgs(method, url).yields(null, result);
});
return cb();
});
};
var blockchainExplorerMock = {};
blockchainExplorerMock.getUnspentUtxos = function(addresses, cb) {
var selected = _.filter(blockchainExplorerMock.utxos, function(utxo) {
return _.contains(addresses, utxo.address);
});
return cb(null, selected);
};
blockchainExplorerMock.setUtxo = function(address, amount, m, confirmations) {
var scriptPubKey;
switch (address.type) {
case Constants.SCRIPT_TYPES.P2SH:
scriptPubKey = address.publicKeys ? Bitcore.Script.buildMultisigOut(address.publicKeys, m).toScriptHashOut() : '';
break;
case Constants.SCRIPT_TYPES.P2PKH:
scriptPubKey = Bitcore.Script.buildPublicKeyHashOut(address.address);
break;
}
should.exist(scriptPubKey);
blockchainExplorerMock.utxos.push({
txid: Bitcore.crypto.Hash.sha256(new Buffer(Math.random() * 100000)).toString('hex'),
vout: Math.floor((Math.random() * 10) + 1),
amount: amount,
address: address.address,
scriptPubKey: scriptPubKey.toBuffer().toString('hex'),
confirmations: _.isUndefined(confirmations) ? Math.floor((Math.random() * 100) + 1) : +confirmations,
});
};
blockchainExplorerMock.broadcast = function(raw, cb) {
blockchainExplorerMock.lastBroadcasted = raw;
return cb(null, (new Bitcore.Transaction(raw)).id);
};
blockchainExplorerMock.setHistory = function(txs) {
blockchainExplorerMock.txHistory = txs;
};
blockchainExplorerMock.getTransactions = function(addresses, from, to, cb) {
var list = [].concat(blockchainExplorerMock.txHistory);
list = _.slice(list, from, to);
return cb(null, list);
};
blockchainExplorerMock.getAddressActivity = function(address, cb) {
var activeAddresses = _.pluck(blockchainExplorerMock.utxos || [], 'address');
return cb(null, _.contains(activeAddresses, address));
};
blockchainExplorerMock.setFeeLevels = function(levels) {
blockchainExplorerMock.feeLevels = levels;
};
blockchainExplorerMock.estimateFee = function(nbBlocks, cb) {
return cb(null, {
feePerKB: blockchainExplorerMock.feeLevels[nbBlocks] / 1e8
});
};
blockchainExplorerMock.reset = function() {
blockchainExplorerMock.utxos = [];
blockchainExplorerMock.txHistory = [];
blockchainExplorerMock.feeLevels = [];
};
describe('client API', function() {
var clients, app, sandbox;
var i = 0;
beforeEach(function(done) {
var storage = new Storage({
db: helpers.newDb(),
});
var expressApp = new ExpressApp();
expressApp.start({
storage: storage,
blockchainExplorer: blockchainExplorerMock,
disableLogs: true,
},
function() {
app = expressApp.app;
// Generates 5 clients
clients = _.map(_.range(5), function(i) {
return helpers.newClient(app);
});
blockchainExplorerMock.reset();
sandbox = sinon.sandbox.create();
if (!process.env.BWC_SHOW_LOGS) {
sandbox.stub(log, 'warn');
sandbox.stub(log, 'info');
sandbox.stub(log, 'error');
}
done();
});
});
afterEach(function(done) {
sandbox.restore();
done();
});
describe('Client Internals', function() {
it('should expose bitcore', function() {
should.exist(Client.Bitcore);
should.exist(Client.Bitcore.HDPublicKey);
});
});
describe('Server internals', function() {
it('should allow cors', function(done) {
clients[0].credentials = {};
clients[0]._doRequest('options', '/', {}, function(err, x, headers) {
headers['access-control-allow-origin'].should.equal('*');
should.exist(headers['access-control-allow-methods']);
should.exist(headers['access-control-allow-headers']);
done();
});
});
it('should handle critical errors', function(done) {
var s = sinon.stub();
s.storeWallet = sinon.stub().yields('bigerror');
s.fetchWallet = sinon.stub().yields(null);
var expressApp = new ExpressApp();
expressApp.start({
storage: s,
blockchainExplorer: blockchainExplorerMock,
disableLogs: true,
}, function() {
var s2 = sinon.stub();
s2.load = sinon.stub().yields(null);
var client = helpers.newClient(app);
client.storage = s2;
client.createWallet('1', '2', 1, 1, {
network: 'testnet'
},
function(err) {
err.should.be.an.instanceOf(Error);
err.message.should.equal('bigerror');
done();
});
});
});
it('should handle critical errors (Case2)', function(done) {
var s = sinon.stub();
s.storeWallet = sinon.stub().yields({
code: 501,
message: 'wow'
});
s.fetchWallet = sinon.stub().yields(null);
var expressApp = new ExpressApp();
expressApp.start({
storage: s,
blockchainExplorer: blockchainExplorerMock,
disableLogs: true,
}, function() {
var s2 = sinon.stub();
s2.load = sinon.stub().yields(null);
var client = helpers.newClient(app);
client.storage = s2;
client.createWallet('1', '2', 1, 1, {
network: 'testnet'
},
function(err) {
err.should.be.an.instanceOf(Error);
err.message.should.equal('wow');
done();
});
});
});
it('should handle critical errors (Case3)', function(done) {
var s = sinon.stub();
s.storeWallet = sinon.stub().yields({
code: 404,
message: 'wow'
});
s.fetchWallet = sinon.stub().yields(null);
var expressApp = new ExpressApp();
expressApp.start({
storage: s,
blockchainExplorer: blockchainExplorerMock,
disableLogs: true,
}, function() {
var s2 = sinon.stub();
s2.load = sinon.stub().yields(null);
var client = helpers.newClient(app);
client.storage = s2;
client.createWallet('1', '2', 1, 1, {
network: 'testnet'
},
function(err) {
err.should.be.an.instanceOf(Errors.NOT_FOUND);
done();
});
});
});
it('should handle critical errors (Case4)', function(done) {
var body = {
code: 999,
message: 'unexpected body'
};
var ret = Client._parseError(body);
ret.should.be.an.instanceOf(Error);
ret.message.should.equal('999');
done();
});
it('should handle critical errors (Case5)', function(done) {
var err = 'some error';
var res, body; // leave them undefined to simulate no-response
var requestStub = function(args, cb) {
cb(err, res, body);
};
var request = sinon.stub(clients[0], 'request', requestStub);
clients[0].createWallet('wallet name', 'creator', 1, 2, {
network: 'testnet'
}, function(err, secret) {
should.exist(err);
err.should.be.an.instanceOf(Errors.CONNECTION_ERROR);
request.restore();
done();
});
});
});
describe('Build & sign txs', function() {
var masterPrivateKey = 'tprv8ZgxMBicQKsPdPLE72pfSo7CvzTsWddGHdwSuMNrcerr8yQZKdaPXiRtP9Ew8ueSe9M7jS6RJsp4DiAVS2xmyxcCC9kZV6X1FMsX7EQX2R5';
var derivedPrivateKey = {
'BIP44': new Bitcore.HDPrivateKey(masterPrivateKey).derive("m/44'/1'/0'").toString(),
'BIP45': new Bitcore.HDPrivateKey(masterPrivateKey).derive("m/45'").toString(),
'BIP48': new Bitcore.HDPrivateKey(masterPrivateKey).derive("m/48'/1'/0'").toString(),
};
describe('#buildTx', function() {
it('should build a tx correctly (BIP44)', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]);
var txp = {
version: '2.0.0',
inputs: utxos,
toAddress: toAddress,
amount: 1200,
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1],
fee: 10050,
derivationStrategy: 'BIP44',
addressType: 'P2PKH',
};
var t = Client.buildTx(txp);
var bitcoreError = t.getSerializationError({
disableIsFullySigned: true,
disableSmallFees: true,
disableLargeFees: true,
});
should.not.exist(bitcoreError);
t.getFee().should.equal(10050);
});
it('should build a tx correctly (BIP48)', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP48']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]);
var txp = {
version: '2.0.0',
inputs: utxos,
toAddress: toAddress,
amount: 1200,
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1],
fee: 10050,
derivationStrategy: 'BIP48',
addressType: 'P2PKH',
};
var t = Client.buildTx(txp);
var bitcoreError = t.getSerializationError({
disableIsFullySigned: true,
disableSmallFees: true,
disableLargeFees: true,
});
should.not.exist(bitcoreError);
t.getFee().should.equal(10050);
});
it('should build a legacy (v1.*) tx correctly', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP45']),
}];
var utxos = helpers.generateUtxos('P2SH', publicKeyRing, 'm/2147483647/0/0', 1, [1000, 2000]);
var txp = {
version: '1.0.1',
inputs: utxos,
toAddress: toAddress,
amount: 1200,
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1],
feePerKb: 40000,
fee: 10050,
derivationStrategy: 'BIP45',
addressType: 'P2SH',
};
var t = Client.buildTx(txp);
var bitcoreError = t.getSerializationError({
disableIsFullySigned: true,
disableSmallFees: true,
disableLargeFees: true,
});
should.not.exist(bitcoreError);
t.getFee().should.equal(40000);
});
it('should protect from creating excessive fee', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1, 2]);
var txp = {
inputs: utxos,
toAddress: toAddress,
amount: 1.5e8,
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1],
fee: 1.2e8,
derivationStrategy: 'BIP44',
addressType: 'P2PKH',
};
var x = Utils.newBitcoreTransaction;
Utils.newBitcoreTransaction = function() {
return {
from: sinon.stub(),
to: sinon.stub(),
change: sinon.stub(),
outputs: [{
satoshis: 1000,
}],
fee: sinon.stub(),
}
};
(function() {
var t = Client.buildTx(txp);
}).should.throw('Illegal State');
Utils.newBitcoreTransaction = x;
});
it('should build a tx with multiple outputs', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]);
var txp = {
inputs: utxos,
type: 'multiple_outputs',
outputs: [{
toAddress: toAddress,
amount: 800,
message: 'first output'
}, {
toAddress: toAddress,
amount: 900,
message: 'second output'
}],
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1, 2],
fee: 10000,
derivationStrategy: 'BIP44',
addressType: 'P2PKH',
};
var t = Client.buildTx(txp);
var bitcoreError = t.getSerializationError({
disableIsFullySigned: true,
});
should.not.exist(bitcoreError);
});
it('should build a tx with provided output scripts', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [0.001]);
var txp = {
inputs: utxos,
type: 'external',
outputs: [{
"toAddress": "18433T2TSgajt9jWhcTBw4GoNREA6LpX3E",
"amount": 700,
"script": "512103ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff210314a96cd6f5a20826070173fe5b7e9797f21fc8ca4a55bcb2d2bde99f55dd352352ae"
}, {
"amount": 600,
"script": "76a9144d5bd54809f846dc6b1a14cbdd0ac87a3c66f76688ac"
}, {
"amount": 0,
"script": "6a1e43430102fa9213bc243af03857d0f9165e971153586d3915201201201210"
}],
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1, 2, 3],
fee: 10000,
derivationStrategy: 'BIP44',
addressType: 'P2PKH',
};
var t = Client.buildTx(txp);
var bitcoreError = t.getSerializationError({
disableIsFullySigned: true,
});
should.not.exist(bitcoreError);
t.outputs.length.should.equal(4);
t.outputs[0].script.toHex().should.equal(txp.outputs[0].script);
t.outputs[0].satoshis.should.equal(txp.outputs[0].amount);
t.outputs[1].script.toHex().should.equal(txp.outputs[1].script);
t.outputs[1].satoshis.should.equal(txp.outputs[1].amount);
t.outputs[2].script.toHex().should.equal(txp.outputs[2].script);
t.outputs[2].satoshis.should.equal(txp.outputs[2].amount);
var changeScript = Bitcore.Script.fromAddress(txp.changeAddress.address).toHex();
t.outputs[3].script.toHex().should.equal(changeScript);
});
it('should fail if provided output has no either toAddress or script', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [0.001]);
var txp = {
inputs: utxos,
type: 'external',
outputs: [{
"amount": 700,
}, {
"amount": 600,
"script": "76a9144d5bd54809f846dc6b1a14cbdd0ac87a3c66f76688ac"
}, {
"amount": 0,
"script": "6a1e43430102fa9213bc243af03857d0f9165e971153586d3915201201201210"
}],
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1, 2, 3],
fee: 10000,
derivationStrategy: 'BIP44',
addressType: 'P2PKH',
};
(function() {
var t = Client.buildTx(txp);
}).should.throw('Output should have either toAddress or script specified');
txp.outputs[0].toAddress = "18433T2TSgajt9jWhcTBw4GoNREA6LpX3E";
var t = Client.buildTx(txp);
var bitcoreError = t.getSerializationError({
disableIsFullySigned: true,
});
should.not.exist(bitcoreError);
delete txp.outputs[0].toAddress;
txp.outputs[0].script = "512103ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff210314a96cd6f5a20826070173fe5b7e9797f21fc8ca4a55bcb2d2bde99f55dd352352ae";
t = Client.buildTx(txp);
var bitcoreError = t.getSerializationError({
disableIsFullySigned: true,
});
should.not.exist(bitcoreError);
});
});
describe('#signTxp', function() {
it('should sign BIP45 P2SH correctly', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP45']),
}];
var utxos = helpers.generateUtxos('P2SH', publicKeyRing, 'm/2147483647/0/0', 1, [1000, 2000]);
var txp = {
inputs: utxos,
toAddress: toAddress,
amount: 1200,
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1],
fee: 10000,
derivationStrategy: 'BIP45',
addressType: 'P2SH',
};
var signatures = Client.signTxp(txp, derivedPrivateKey['BIP45']);
signatures.length.should.be.equal(utxos.length);
});
it('should sign BIP44 P2PKH correctly', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]);
var txp = {
inputs: utxos,
toAddress: toAddress,
amount: 1200,
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1],
fee: 10000,
derivationStrategy: 'BIP44',
addressType: 'P2PKH',
};
var signatures = Client.signTxp(txp, derivedPrivateKey['BIP44']);
signatures.length.should.be.equal(utxos.length);
});
it('should sign multiple-outputs proposal correctly', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]);
var txp = {
inputs: utxos,
type: 'multiple_outputs',
outputs: [{
toAddress: toAddress,
amount: 800,
message: 'first output'
}, {
toAddress: toAddress,
amount: 900,
message: 'second output'
}],
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1, 2],
fee: 10000,
derivationStrategy: 'BIP44',
addressType: 'P2PKH',
};
var signatures = Client.signTxp(txp, derivedPrivateKey['BIP44']);
signatures.length.should.be.equal(utxos.length);
});
it('should sign proposal with provided output scripts correctly', function() {
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx';
var publicKeyRing = [{
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']),
}];
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [0.001]);
var txp = {
inputs: utxos,
type: 'external',
outputs: [{
"amount": 700,
"script": "512103ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff210314a96cd6f5a20826070173fe5b7e9797f21fc8ca4a55bcb2d2bde99f55dd352352ae"
}, {
"amount": 600,
"script": "76a9144d5bd54809f846dc6b1a14cbdd0ac87a3c66f76688ac"
}, {
"amount": 0,
"script": "6a1e43430102fa9213bc243af03857d0f9165e971153586d3915201201201210"
}],
changeAddress: {
address: changeAddress
},
requiredSignatures: 1,
outputOrder: [0, 1, 2, 3],
fee: 10000,
derivationStrategy: 'BIP44',
addressType: 'P2PKH',
};
var signatures = Client.signTxp(txp, derivedPrivateKey['BIP44']);
signatures.length.should.be.equal(utxos.length);
});
});
});
describe('Wallet secret round trip', function() {
it('should create secret and parse secret', function() {
var i = 0;
while (i++ < 100) {
var walletId = Uuid.v4();
var walletPrivKey = new Bitcore.PrivateKey();
var network = i % 2 == 0 ? 'testnet' : 'livenet';
var secret = Client._buildSecret(walletId, walletPrivKey, network);
var result = Client.parseSecret(secret);
result.walletId.should.equal(walletId);
result.walletPrivKey.toString().should.equal(walletPrivKey.toString());
result.network.should.equal(network);
};
});
it('should fail on invalid secret', function() {
(function() {
Client.parseSecret('invalidSecret');
}).should.throw('Invalid secret');
});
it('should create secret and parse secret from string ', function() {
var walletId = Uuid.v4();
var walletPrivKey = new Bitcore.PrivateKey();
var network = 'testnet';
var secret = Client._buildSecret(walletId, walletPrivKey.toString(), network);
var result = Client.parseSecret(secret);
result.walletId.should.equal(walletId);
result.walletPrivKey.toString().should.equal(walletPrivKey.toString());
result.network.should.equal(network);
});
});
describe('Notification polling', function() {
var clock, interval;
beforeEach(function() {
clock = sinon.useFakeTimers(1234000, 'Date');
});
afterEach(function() {
clock.restore();
});
it('should fetch notifications at intervals', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function() {
clients[0].on('notification', function(data) {
notifications.push(data);
});
var notifications = [];
clients[0]._fetchLatestNotifications(5, function() {
_.pluck(notifications, 'type').should.deep.equal(['NewCopayer', 'WalletComplete']);
clock.tick(2000);
notifications = [];
clients[0]._fetchLatestNotifications(5, function() {
notifications.length.should.equal(0);
clock.tick(2000);
clients[1].createAddress(function(err, x) {
should.not.exist(err);
clients[0]._fetchLatestNotifications(5, function() {
_.pluck(notifications, 'type').should.deep.equal(['NewAddress']);
clock.tick(2000);
notifications = [];
clients[0]._fetchLatestNotifications(5, function() {
notifications.length.should.equal(0);
clients[1].createAddress(function(err, x) {
should.not.exist(err);
clock.tick(60 * 1000);
clients[0]._fetchLatestNotifications(5, function() {
notifications.length.should.equal(0);
done();
});
});
});
});
});
});
});
});
});
});
describe('Wallet Creation', function() {
it('should check balance in a 1-1 ', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function() {
clients[0].getBalance({}, function(err, balance) {
should.not.exist(err);
balance.totalAmount.should.equal(0);
balance.availableAmount.should.equal(0);
balance.lockedAmount.should.equal(0);
balance.totalBytesToSendMax.should.equal(0);
done();
})
});
});
it('should be able to complete wallet in copayer that joined later', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function() {
clients[0].getBalance({}, function(err, x) {
should.not.exist(err);
clients[1].getBalance({}, function(err, x) {
should.not.exist(err);
clients[2].getBalance({}, function(err, x) {
should.not.exist(err);
done();
})
})
})
});
});
it('should fire event when wallet is complete', function(done) {
var checks = 0;
clients[0].on('walletCompleted', function(wallet) {
wallet.name.should.equal('wallet name');
wallet.status.should.equal('complete');
clients[0].isComplete().should.equal(true);
clients[0].credentials.isComplete().should.equal(true);
if (++checks == 2) done();
});
clients[0].createWallet('wallet name', 'creator', 2, 2, {
network: 'testnet'
}, function(err, secret) {
should.not.exist(err);
clients[0].isComplete().should.equal(false);
clients[0].credentials.isComplete().should.equal(false);
clients[1].joinWallet(secret, 'guest', {}, function(err) {
should.not.exist(err);
clients[0].openWallet(function(err, walletStatus) {
should.not.exist(err);
should.exist(walletStatus);
_.difference(_.pluck(walletStatus.copayers, 'name'), ['creator', 'guest']).length.should.equal(0);
if (++checks == 2) done();
});
});
});
});
it('should fill wallet info in a incomplete wallets', function(done) {
clients[0].seedFromRandomWithMnemonic();
clients[0].createWallet('XXX', 'creator', 2, 3, {}, function(err, secret) {
should.not.exist(err);
clients[1].seedFromMnemonic(clients[0].getMnemonic());
clients[1].openWallet(function(err) {
clients[1].credentials.walletName.should.equal('XXX');
clients[1].credentials.m.should.equal(2);
clients[1].credentials.n.should.equal(3);
should.not.exist(err);
done();
});
});
});
it('should not allow to join a full wallet ', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(w) {
should.exist(w.secret);
clients[4].joinWallet(w.secret, 'copayer', {}, function(err, result) {
err.should.be.an.instanceOf(Errors.WALLET_FULL);
done();
});
});
});
it('should fail with an invalid secret', function(done) {
// Invalid
clients[0].joinWallet('dummy', 'copayer', {}, function(err, result) {
err.message.should.contain('Invalid secret');
// Right length, invalid char for base 58
clients[0].joinWallet('DsZbqNQQ9LrTKU8EknR7gFKyCQMPg2UUHNPZ1BzM5EbJwjRZaUNBfNtdWLluuFc0f7f7sTCkh7T', 'copayer', {}, function(err, result) {
err.message.should.contain('Invalid secret');
done();
});
});
});
it('should fail with an unknown secret', function(done) {
// Unknown walletId
var oldSecret = '3bJKRn1HkQTpwhVaJMaJ22KwsjN24ML9uKfkSrP7iDuq91vSsTEygfGMMpo6kWLp1pXG9wZSKcT';
clients[0].joinWallet(oldSecret, 'copayer', {}, function(err, result) {
err.should.be.an.instanceOf(Errors.WALLET_NOT_FOUND);
done();
});
});
it('should detect wallets with bad signatures', function(done) {
// Do not complete clients[1] pkr
var openWalletStub = sinon.stub(clients[1], 'openWallet').yields();
helpers.createAndJoinWallet(clients, 2, 3, function() {
helpers.tamperResponse([clients[0], clients[1]], 'get', '/v1/wallets/', {}, function(status) {
status.wallet.copayers[0].xPubKey = status.wallet.copayers[1].xPubKey;
}, function() {
openWalletStub.restore();
clients[1].openWallet(function(err, x) {
err.should.be.an.instanceOf(Errors.SERVER_COMPROMISED);
done();
});
});
});
});
it('should detect wallets with missing signatures', function(done) {
// Do not complete clients[1] pkr
var openWalletStub = sinon.stub(clients[1], 'openWallet').yields();
helpers.createAndJoinWallet(clients, 2, 3, function() {
helpers.tamperResponse([clients[0], clients[1]], 'get', '/v1/wallets/', {}, function(status) {
delete status.wallet.copayers[1].xPubKey;
}, function() {
openWalletStub.restore();
clients[1].openWallet(function(err, x) {
err.should.be.an.instanceOf(Errors.SERVER_COMPROMISED);
done();
});
});
});
});
it('should detect wallets missing callers pubkey', function(done) {
// Do not complete clients[1] pkr
var openWalletStub = sinon.stub(clients[1], 'openWallet').yields();
helpers.createAndJoinWallet(clients, 2, 3, function() {
helpers.tamperResponse([clients[0], clients[1]], 'get', '/v1/wallets/', {}, function(status) {
// Replace caller's pubkey
status.wallet.copayers[1].xPubKey = (new Bitcore.HDPrivateKey()).publicKey;
// Add a correct signature
status.wallet.copayers[1].xPubKeySignature = Utils.signMessage(
status.wallet.copayers[1].xPubKey.toString(),
clients[0].credentials.walletPrivKey
);
}, function() {
openWalletStub.restore();
clients[1].openWallet(function(err, x) {
err.should.be.an.instanceOf(Errors.SERVER_COMPROMISED);
done();
});
});
});
});
it('should perform a dry join without actually joining', function(done) {
clients[0].createWallet('wallet name', 'creator', 1, 2, {}, function(err, secret) {
should.not.exist(err);
should.exist(secret);
clients[1].joinWallet(secret, 'dummy', {
dryRun: true
}, function(err, wallet) {
should.not.exist(err);
should.exist(wallet);
wallet.status.should.equal('pending');
wallet.copayers.length.should.equal(1);
done();
});
});
});
it('should return wallet status even if wallet is not yet complete', function(done) {
clients[0].createWallet('wallet name', 'creator', 1, 2, {
network: 'testnet'
}, function(err, secret) {
should.not.exist(err);
should.exist(secret);
clients[0].getStatus({}, function(err, status) {
should.not.exist(err);
should.exist(status);
status.wallet.status.should.equal('pending');
should.exist(status.wallet.secret);
status.wallet.secret.should.equal(secret);
done();
});
});
});
it('should return status using v2 version', function(done) {
clients[0].createWallet('wallet name', 'creator', 1, 1, {
network: 'testnet'
}, function(err, secret) {
should.not.exist(err);
clients[0].getStatus({}, function(err, status) {
should.not.exist(err);
should.not.exist(status.wallet.publicKeyRing);
status.wallet.status.should.equal('complete');
done();
});
});
});
it('should return extended status using v2 version', function(done) {
clients[0].createWallet('wallet name', 'creator', 1, 1, {
network: 'testnet'
}, function(err, secret) {
should.not.exist(err);
clients[0].getStatus({
includeExtendedInfo: true
}, function(err, status) {
should.not.exist(err);
status.wallet.publicKeyRing.length.should.equal(1);
status.wallet.status.should.equal('complete');
done();
});
});
});
it('should store walletPrivKey', function(done) {
clients[0].createWallet('wallet name', 'creator', 1, 1, {
network: 'testnet'
}, function(err) {
var key = clients[0].credentials.walletPrivKey;
should.not.exist(err);
clients[0].getStatus({
includeExtendedInfo: true
}, function(err, status) {
should.not.exist(err);
status.wallet.publicKeyRing.length.should.equal(1);
status.wallet.status.should.equal('complete');
var key2 = status.customData.walletPrivKey;
key2.should.be.equal(key2);
done();
});
});
});
it('should set walletPrivKey from BWS', function(done) {
clients[0].createWallet('wallet name', 'creator', 1, 1, {
network: 'testnet'
}, function(err) {
var wkey = clients[0].credentials.walletPrivKey;
var skey = clients[0].credentials.sharedEncryptingKey;
delete clients[0].credentials.walletPrivKey;
delete clients[0].credentials.sharedEncryptingKey;
should.not.exist(err);
clients[0].getStatus({
includeExtendedInfo: true
}, function(err, status) {
should.not.exist(err);
clients[0].credentials.walletPrivKey.should.equal(wkey);
clients[0].credentials.sharedEncryptingKey.should.equal(skey);
done();
});
});
});
it('should prepare wallet with external xpubkey', function(done) {
var client = helpers.newClient(app);
client.seedFromExtendedPublicKey('xpub661MyMwAqRbcGVyYUcHbZi9KNhN9Tdj8qHi9ZdoUXP1VeKiXDGGrE9tSoJKYhGFE2rimteYdwvoP6e87zS5LsgcEvsvdrpPBEmeWz9EeAUq', 'ledger', '1a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f00', {
account: 1,
derivationStrategy: 'BIP48',
});
client.isPrivKeyExternal().should.equal(true);
client.credentials.account.should.equal(1);
client.credentials.derivationStrategy.should.equal('BIP48');
client.credentials.requestPrivKey.should.equal('36a4504f0c6651db30484c2c128304a7ea548ef5935f19ed6af99db8000c75a4');
client.credentials.personalEncryptingKey.should.equal('wYI1597BfOv06NI6Uye3tA==');
client.getPrivKeyExternalSourceName().should.equal('ledger');
done();
});
it('should create a 1-1 wallet with random mnemonic', function(done) {
clients[0].seedFromRandomWithMnemonic();
clients[0].createWallet('wallet name', 'creator', 1, 1, {
network: 'livenet'
},
function(err) {
should.not.exist(err);
clients[0].openWallet(function(err) {
should.not.exist(err);
should.not.exist(err);
clients[0].credentials.network.should.equal('livenet');
clients[0].getMnemonic().split(' ').length.should.equal(12);
done();
});
});
});
it('should create a 1-1 wallet with given mnemonic', function(done) {
var words = 'forget announce travel fury farm alpha chaos choice talent sting eagle supreme';
clients[0].seedFromMnemonic(words);
clients[0].createWallet('wallet name', 'creator', 1, 1, {
network: 'livenet',
derivationStrategy: 'BIP48',
},
function(err) {
should.not.exist(err);
clients[0].openWallet(function(err) {
should.not.exist(err);
should.not.exist(clients[0].getMnemonic()); // mnemonics are *not* stored
clients[0].credentials.xPrivKey.should.equal('xprv9s21ZrQH143K4X2frJxRmGsmef9UfXhmfL4hdTGLm5ruSX46gekuSTspJX63d5nEi9q2wqUgg4KZ4yhSPy13CzVezAH6t6gCox1DN2hXV3L')
done();
});
});
});
it('should create a 2-3 wallet with given mnemonic', function(done) {
var words = 'forget announce travel fury farm alpha chaos choice talent sting eagle supreme';
clients[0].seedFromMnemonic(words);
clients[0].createWallet('wallet name', 'creator', 2, 3, {
network: 'livenet'
},
function(err, secret) {
should.not.exist(err);
should.exist(secret);
clients[0].openWallet(function(err) {
should.not.exist(err);
should.not.exist(clients[0].getMnemonic()); // mnemonics are *not* stored
clients[0].credentials.xPrivKey.should.equal('xprv9s21ZrQH143K4X2frJxRmGsmef9UfXhmfL4hdTGLm5ruSX46gekuSTspJX63d5nEi9q2wqUgg4KZ4yhSPy13CzVezAH6t6gCox1DN2hXV3L')
done();
});
});
});
});
describe('Network fees', function() {
it('should get current fee levels', function(done) {
blockchainExplorerMock.setFeeLevels({
1: 40000,
3: 20000,
10: 18000,
});
clients[0].credentials = {};
clients[0].getFeeLevels('livenet', function(err, levels) {
should.not.exist(err);
should.exist(levels);
_.difference(['priority', 'normal', 'economy'], _.pluck(levels, 'level')).should.be.empty;
done();
});
});
});
describe('Version', function() {
it('should get version of bws', function(done) {
clients[0].credentials = {};
clients[0].getVersion(function(err, version) {
if (err) {
// if bws is older version without getVersion support
err.code.should.equal('NOT_FOUND');
} else {
// if bws is up-to-date
should.exist(version);
should.exist(version.serviceVersion);
version.serviceVersion.should.contain('bws-');
}
done();
});
});
});
describe('Preferences', function() {
it('should save and retrieve preferences', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function() {
clients[0].getPreferences(function(err, preferences) {
should.not.exist(err);
preferences.should.be.empty;
clients[0].savePreferences({
email: 'dummy@dummy.com'
}, function(err) {
should.not.exist(err);
clients[0].getPreferences(function(err, preferences) {
should.not.exist(err);
should.exist(preferences);
preferences.email.should.equal('dummy@dummy.com');
done();
});
});
});
});
});
});
describe('Fiat rates', function() {
it('should get fiat exchange rate', function(done) {
var now = Date.now();
helpers.createAndJoinWallet(clients, 1, 1, function() {
clients[0].getFiatRate({
code: 'USD',
ts: now,
}, function(err, res) {
should.not.exist(err);
should.exist(res);
res.ts.should.equal(now);
should.not.exist(res.rate);
done();
});
});
});
});
describe('Push notifications', function() {
it('should do a post request', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function() {
clients[0]._doRequest = sinon.stub().yields(null, {
statusCode: 200,
});
clients[0].pushNotificationsSubscribe(function(err, res) {
should.not.exist(err);
should.exist(res);
res.statusCode.should.be.equal(200);
done();
});
});
});
it('should do a delete request', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function() {
clients[0]._doRequest = sinon.stub().yields(null);
clients[0].pushNotificationsUnsubscribe(function(err) {
should.not.exist(err);
done();
});
});
});
});
describe('Address Creation', function() {
it('should be able to create address in 1-of-1 wallet', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function() {
clients[0].createAddress(function(err, x) {
should.not.exist(err);
should.exist(x.address);
x.address.charAt(0).should.not.equal('2');
done();
});
});
});
it('should be able to create address in all copayers in a 2-3 wallet', function(done) {
this.timeout(5000);
helpers.createAndJoinWallet(clients, 2, 3, function() {
clients[0].createAddress(function(err, x) {
should.not.exist(err);
should.exist(x.address);
x.address.charAt(0).should.equal('2');
clients[1].createAddress(function(err, x) {
should.not.exist(err);
should.exist(x.address);
clients[2].createAddress(function(err, x) {
should.not.exist(err);
should.exist(x.address);
done();
});
});
});
});
});
it('should see balance on address created by others', function(done) {
this.timeout(5000);
helpers.createAndJoinWallet(clients, 2, 2, function(w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
blockchainExplorerMock.setUtxo(x0, 10, w.m);
clients[0].getBalance({}, function(err, bal0) {
should.not.exist(err);
bal0.totalAmount.should.equal(10 * 1e8);
bal0.lockedAmount.should.equal(0);
bal0.totalBytesToSendMax.should.be.within(300, 400);
clients[1].getBalance({}, function(err, bal1) {
bal1.totalAmount.should.equal(10 * 1e8);
bal1.lockedAmount.should.equal(0);
done();
});
});
});
});
});
it('should detect fake addresses', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function() {
helpers.tamperResponse(clients[0], 'post', '/v1/addresses/', {}, function(address) {
address.address = '2N86pNEpREGpwZyHVC5vrNUCbF9nM1Geh4K';
}, function() {
clients[0].createAddress(function(err, x0) {
err.should.be.an.instanceOf(Errors.SERVER_COMPROMISED);
done();
});
});
});
});
it('should detect fake public keys', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function() {
helpers.tamperResponse(clients[0], 'post', '/v1/addresses/', {}, function(address) {
address.publicKeys = [
'0322defe0c3eb9fcd8bc01878e6dbca7a6846880908d214b50a752445040cc5c54',
'02bf3aadc17131ca8144829fa1883c1ac0a8839067af4bca47a90ccae63d0d8037'
];
}, function() {
clients[0].createAddress(function(err, x0) {
err.should.be.an.instanceOf(Errors.SERVER_COMPROMISED);
done();
});
});
});
});
it('should be able to derive 25 addresses', function(done) {
this.timeout(5000);
var num = 25;
helpers.createAndJoinWallet(clients, 1, 1, function() {
function create(callback) {
clients[0].createAddress({
ignoreMaxGap: true
}, function(err, x) {
should.not.exist(err);
should.exist(x.address);
callback(err, x);
});
}
var tasks = [];
for (var i = 0; i < num; i++) {
tasks.push(create);
}
async.parallel(tasks, function(err, results) {
should.not.exist(err);
results.length.should.equal(num);
done();
});
});
});
});
describe('Notifications', function() {
var clock;
beforeEach(function(done) {
this.timeout(5000);
clock = sinon.useFakeTimers(1234000, 'Date');
helpers.createAndJoinWallet(clients, 2, 2, function() {
clock.tick(25 * 1000);
clients[0].createAddress(function(err, x) {
should.not.exist(err);
clock.tick(25 * 1000);
clients[1].createAddress(function(err, x) {
should.not.exist(err);
done();
});
});
});
});
afterEach(function() {
clock.restore();
});
it('should receive notifications', function(done) {
clients[0].getNotifications({}, function(err, notifications) {
should.not.exist(err);
notifications.length.should.equal(3);
_.pluck(notifications, 'type').should.deep.equal(['NewCopayer', 'WalletComplete', 'NewAddress']);
clients[0].getNotifications({
lastNotificationId: _.last(notifications).id
}, function(err, notifications) {
should.not.exist(err);
notifications.length.should.equal(0, 'should only return unread notifications');
done();
});
});
});
it('should not receive old notifications', function(done) {
clock.tick(61 * 1000); // more than 60 seconds
clients[0].getNotifications({}, function(err, notifications