bitcore-wallet-service/test/integration/clientApi.js

1221 lines
40 KiB
JavaScript
Raw Normal View History

2015-02-16 10:04:25 -08:00
'use strict';
var _ = require('lodash');
var chai = require('chai');
var sinon = require('sinon');
var should = chai.should();
2015-02-19 06:00:14 -08:00
var levelup = require('levelup');
var memdown = require('memdown');
2015-02-19 07:32:10 -08:00
var async = require('async');
2015-02-19 06:00:14 -08:00
var request = require('supertest');
2015-02-16 10:04:25 -08:00
var Client = require('../../lib/client');
var API = Client.API;
var Bitcore = require('bitcore');
var WalletUtils = require('../../lib/walletutils');
2015-02-19 06:00:14 -08:00
var ExpressApp = require('../../lib/expressapp');
var Storage = require('../../lib/storage');
2015-02-16 10:04:25 -08:00
2015-02-19 07:32:10 -08:00
var helpers = {};
helpers.getRequest = function(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) {
r.set(k, v);
})
}
if (!_.isEmpty(args.body)) {
r.send(args.body);
};
r.end(function(err, res) {
return cb(err, res, res.body);
2015-02-16 10:04:25 -08:00
});
2015-02-19 07:32:10 -08:00
};
};
helpers.createAndJoinWallet = function(clients, m, n, cb) {
2015-02-19 12:38:48 -08:00
clients[0].createWallet('wallet name', 'creator', m, n, 'testnet',
2015-02-19 07:32:10 -08:00
function(err, secret) {
if (err) return cb(err);
if (n == 1) return cb();
should.exist(secret);
2015-02-19 07:45:28 -08:00
async.each(_.range(n - 1), function(i, cb) {
2015-02-19 07:32:10 -08:00
clients[i + 1].joinWallet(secret, 'copayer ' + (i + 1), function(err, result) {
should.not.exist(err);
return cb(err);
});
}, function(err) {
2015-02-19 12:38:48 -08:00
if (err) return cb(err);
return cb(null, {
m: m,
n: n,
secret: secret,
});
2015-02-19 07:32:10 -08:00
});
2015-02-16 10:04:25 -08:00
});
2015-02-19 07:32:10 -08:00
};
2015-02-19 06:00:14 -08:00
2015-02-19 12:38:48 -08:00
2015-02-19 07:32:10 -08:00
var fsmock = {};
var content = {};
fsmock.readFile = function(name, enc, cb) {
if (!content || _.isEmpty(content[name]))
return cb('empty');
return cb(null, content[name]);
};
fsmock.writeFile = function(name, data, cb) {
content[name] = data;
return cb();
};
2015-02-19 12:38:48 -08:00
fsmock.reset = function() {
content = {};
};
fsmock._get = function(name) {
return content[name];
};
2015-02-21 15:34:44 -08:00
fsmock._set = function(name, data) {
return content[name] = data;
};
2015-02-19 12:38:48 -08:00
var blockExplorerMock = {};
2015-02-19 13:11:57 -08:00
blockExplorerMock.utxos = [];
2015-02-19 12:38:48 -08:00
blockExplorerMock.getUnspentUtxos = function(dummy, cb) {
2015-02-19 13:11:57 -08:00
var ret = _.map(blockExplorerMock.utxos || [], function(x) {
2015-02-19 15:19:01 -08:00
var y = _.clone(x);
y.toObject = function() {
2015-02-19 12:38:48 -08:00
return this;
};
2015-02-19 15:19:01 -08:00
return y;
2015-02-19 12:38:48 -08:00
});
return cb(null, ret);
};
blockExplorerMock.setUtxo = function(address, amount, m) {
2015-02-19 13:11:57 -08:00
blockExplorerMock.utxos.push({
2015-02-19 12:38:48 -08:00
txid: Bitcore.crypto.Hash.sha256(new Buffer(Math.random() * 100000)).toString('hex'),
vout: Math.floor((Math.random() * 10) + 1),
amount: amount,
address: address.address,
2015-02-19 13:11:57 -08:00
scriptPubKey: Bitcore.Script.buildMultisigOut(address.publicKeys, m).toScriptHashOut().toString(),
2015-02-19 12:38:48 -08:00
});
};
2015-02-19 13:11:57 -08:00
blockExplorerMock.broadcast = function(raw, cb) {
blockExplorerMock.lastBroadcasted = raw;
return cb(null, (new Bitcore.Transaction(raw)).id);
};
2015-02-19 12:38:48 -08:00
blockExplorerMock.reset = function() {
2015-02-19 13:11:57 -08:00
blockExplorerMock.utxos = [];
2015-02-19 12:38:48 -08:00
};
2015-02-19 07:32:10 -08:00
describe('client API ', function() {
2015-02-19 12:38:48 -08:00
var clients, app;
2015-02-19 07:32:10 -08:00
beforeEach(function() {
clients = [];
2015-02-19 07:45:28 -08:00
var db = levelup(memdown, {
valueEncoding: 'json'
});
var storage = new Storage({
db: db
});
2015-02-19 12:38:48 -08:00
app = ExpressApp.start({
2015-02-20 12:32:19 -08:00
WalletService: {
2015-02-19 12:38:48 -08:00
storage: storage,
blockExplorer: blockExplorerMock,
2015-02-21 22:46:47 -08:00
},
disableLogs: true,
2015-02-19 07:45:28 -08:00
});
2015-02-19 07:32:10 -08:00
// Generates 5 clients
_.each(_.range(5), function(i) {
var storage = new Client.FileStorage({
filename: 'client' + i,
fs: fsmock,
});
var client = new Client({
storage: storage,
});
2015-02-19 07:45:28 -08:00
2015-02-19 07:32:10 -08:00
client.request = helpers.getRequest(app);
clients.push(client);
2015-02-19 06:00:14 -08:00
});
2015-02-19 12:38:48 -08:00
fsmock.reset();
blockExplorerMock.reset();
2015-02-19 07:32:10 -08:00
});
2015-02-21 22:46:47 -08:00
describe('Server internals', function() {
it('should allow cors', function(done) {
clients[0]._doRequest('options', '/', null, {}, 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);
app = ExpressApp.start({
WalletService: {
storage: s,
blockExplorer: blockExplorerMock,
},
disableLogs: true,
});
var s2 = sinon.stub();
s2.load = sinon.stub().yields(null);
var client = new Client({
storage: s2,
});
client.request = helpers.getRequest(app);
client.createWallet('1', '2', 1, 1, 'testnet',
function(err) {
err.code.should.equal('ERROR');
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);
app = ExpressApp.start({
WalletService: {
storage: s,
blockExplorer: blockExplorerMock,
},
disableLogs: true,
});
var s2 = sinon.stub();
s2.load = sinon.stub().yields(null);
var client = new Client({
storage: s2,
});
client.request = helpers.getRequest(app);
client.createWallet('1', '2', 1, 1, 'testnet',
function(err) {
err.code.should.equal('ERROR');
done();
});
});
});
2015-02-23 15:11:07 -08:00
describe.skip('Storage Encryption', function() {
it('should check balance in a 1-1 ', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err) {
should.not.exist(err);
clients[0].getBalance(function(err, x) {
should.not.exist(err);
var wcd = JSON.parse(fsmock._get('client0'));
console.log('[clientApi.js.236]', wcd); //TODO
done();
})
});
});
});
2015-02-19 12:38:48 -08:00
describe('Wallet Creation', function() {
2015-02-19 07:32:10 -08:00
it('should check balance in a 1-1 ', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err) {
should.not.exist(err);
clients[0].getBalance(function(err, x) {
should.not.exist(err);
done();
})
});
2015-02-19 06:00:14 -08:00
});
2015-02-21 15:20:58 -08:00
it('should be able to complete wallets in copayer that joined later', function(done) {
2015-02-19 07:32:10 -08:00
helpers.createAndJoinWallet(clients, 2, 3, function(err) {
should.not.exist(err);
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();
})
})
})
});
2015-02-19 06:00:14 -08:00
});
2015-02-16 21:20:04 -08:00
2015-02-19 12:38:48 -08:00
it('should not allow to join a full wallet ', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err, w) {
2015-02-16 21:20:04 -08:00
should.not.exist(err);
2015-02-19 12:38:48 -08:00
should.exist(w.secret);
clients[4].joinWallet(w.secret, 'copayer', function(err, result) {
2015-02-19 15:04:05 -08:00
err.code.should.contain('WFULL');
2015-02-19 12:38:48 -08:00
done();
});
});
});
2015-02-21 20:35:25 -08:00
it('should fail with an invalid secret', function(done) {
2015-02-22 06:16:29 -08:00
// Invalid
2015-02-21 20:35:25 -08:00
clients[0].joinWallet('dummy', 'copayer', function(err, result) {
err.message.should.contain('Invalid secret');
// Right length, invalid char for base 58
2015-02-22 06:16:29 -08:00
clients[0].joinWallet('DsZbqNQQ9LrTKU8EknR7gFKyCQMPg2UUHNPZ1BzM5EbJwjRZaUNBfNtdWLluuFc0f7f7sTCkh7T', 'copayer', function(err, result) {
2015-02-21 20:35:25 -08:00
err.message.should.contain('Invalid secret');
done();
});
});
});
it('should fail with an unknown secret', function(done) {
// Unknown walletId
2015-02-22 06:16:29 -08:00
var oldSecret = '3bJKRn1HkQTpwhVaJMaJ22KwsjN24ML9uKfkSrP7iDuq91vSsTEygfGMMpo6kWLp1pXG9wZSKcT';
2015-02-19 12:38:48 -08:00
clients[0].joinWallet(oldSecret, 'copayer', function(err, result) {
2015-02-19 15:04:05 -08:00
err.code.should.contain('BADREQUEST');
2015-02-16 21:20:04 -08:00
done();
});
2015-02-17 07:39:11 -08:00
});
2015-02-19 12:38:48 -08:00
it('should reject wallets with bad signatures', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err) {
should.not.exist(err);
2015-02-16 21:20:04 -08:00
2015-02-19 12:38:48 -08:00
// Get right response
2015-02-23 15:11:07 -08:00
clients[0]._load({}, function(err, data) {
2015-02-19 12:38:48 -08:00
var url = '/v1/wallets/';
clients[0]._doGetRequest(url, data, function(err, x) {
2015-02-16 21:20:04 -08:00
2015-02-19 12:38:48 -08:00
// Tamper data
x.wallet.copayers[0].xPubKey = x.wallet.copayers[1].xPubKey;
2015-02-16 21:20:04 -08:00
2015-02-19 12:38:48 -08:00
// Tamper response
clients[1]._doGetRequest = sinon.stub().yields(null, x);
2015-02-16 21:20:04 -08:00
2015-02-19 12:38:48 -08:00
clients[1].getBalance(function(err, x) {
2015-02-19 15:04:05 -08:00
err.code.should.contain('SERVERCOMPROMISED');
2015-02-19 12:38:48 -08:00
done();
});
});
});
2015-02-16 21:20:04 -08:00
});
2015-02-17 07:39:11 -08:00
});
2015-02-16 21:20:04 -08:00
2015-02-19 12:38:48 -08:00
it('should reject wallets with missing signatures', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err) {
should.not.exist(err);
2015-02-16 21:20:04 -08:00
2015-02-19 12:38:48 -08:00
// Get right response
2015-02-23 15:11:07 -08:00
var data = clients[0]._load({}, function(err, data) {
2015-02-19 12:38:48 -08:00
var url = '/v1/wallets/';
clients[0]._doGetRequest(url, data, function(err, x) {
// Tamper data
delete x.wallet.copayers[1].xPubKey;
// Tamper response
clients[1]._doGetRequest = sinon.stub().yields(null, x);
clients[1].getBalance(function(err, x) {
2015-02-19 15:04:05 -08:00
err.code.should.contain('SERVERCOMPROMISED');
2015-02-19 12:38:48 -08:00
done();
});
});
});
2015-02-16 21:20:04 -08:00
});
2015-02-17 07:39:11 -08:00
});
2015-02-16 21:20:04 -08:00
2015-02-19 12:38:48 -08:00
it('should reject wallets missing caller"s pubkey', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err) {
should.not.exist(err);
// Get right response
2015-02-23 15:11:07 -08:00
var data = clients[0]._load({}, function(err, data) {
2015-02-19 12:38:48 -08:00
var url = '/v1/wallets/';
clients[0]._doGetRequest(url, data, function(err, x) {
// Tamper data. Replace caller's pubkey
x.wallet.copayers[1].xPubKey = (new Bitcore.HDPrivateKey()).publicKey;
// Add a correct signature
x.wallet.copayers[1].xPubKeySignature = WalletUtils.signMessage(
x.wallet.copayers[1].xPubKey, data.walletPrivKey),
// Tamper response
clients[1]._doGetRequest = sinon.stub().yields(null, x);
clients[1].getBalance(function(err, x) {
2015-02-19 15:04:05 -08:00
err.code.should.contain('SERVERCOMPROMISED');
2015-02-19 12:38:48 -08:00
done();
});
});
});
2015-02-16 21:20:04 -08:00
});
2015-02-17 07:39:11 -08:00
});
2015-02-19 12:38:48 -08:00
});
2015-02-17 06:48:19 -08:00
2015-02-22 17:15:53 -08:00
describe('Access control', function() {
2015-02-21 15:34:44 -08:00
it('should not be able to create address if not rwPubKey', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err) {
should.not.exist(err);
var data = JSON.parse(fsmock._get('client0'));
delete data.rwPrivKey;
fsmock._set('client0', JSON.stringify(data));
data.rwPrivKey = null;
2015-02-23 15:11:07 -08:00
// Overwrite client's API auth checks
clients[0]._processWcdAfterRead = function(rawData, xx, cb) {
return cb(null, rawData);
};
2015-02-21 15:34:44 -08:00
clients[0].createAddress(function(err, x0) {
err.code.should.equal('NOTAUTHORIZED');
done();
});
});
});
2015-02-21 17:53:00 -08:00
it('should not be able to create address from a ro export', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err) {
should.not.exist(err);
clients[0].export({
access: 'readonly'
}, function(err, str) {
should.not.exist(err);
clients[1].import(str, function(err, wallet) {
should.not.exist(err);
2015-02-23 09:09:04 -08:00
2015-02-23 15:11:07 -08:00
// Overwrite client's API auth checks
clients[1]._processWcdAfterRead = function(rawData, xx, cb) {
return cb(null, rawData);
};
2015-02-21 17:53:00 -08:00
clients[1].createAddress(function(err, x0) {
err.code.should.equal('NOTAUTHORIZED');
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
done();
});
});
});
});
});
});
it('should be able to create address from a rw export', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err) {
should.not.exist(err);
clients[0].export({
access: 'readwrite'
}, function(err, str) {
should.not.exist(err);
clients[1].import(str, function(err, wallet) {
should.not.exist(err);
clients[1].createAddress(function(err, x0) {
should.not.exist(err);
done();
});
});
});
});
});
2015-02-21 15:34:44 -08:00
2015-02-21 17:53:00 -08:00
it('should not be able to create tx proposals from a rw export', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err, w) {
should.not.exist(err);
clients[0].export({
access: 'readwrite'
}, function(err, str) {
clients[1].import(str, function(err, wallet) {
should.not.exist(err);
clients[1].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 1, 1);
var opts = {
amount: 10000000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
};
clients[1].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
2015-02-23 15:11:07 -08:00
// Overwrite client's API auth checks
clients[1]._processWcdAfterRead = function(rawData, xx, cb) {
return cb(null, rawData);
};
2015-02-21 17:53:00 -08:00
clients[1].signTxProposal(x, function(err, tx) {
2015-02-23 15:11:07 -08:00
console.log('[clientApi.js.456:err:]',err); //TODO
2015-02-21 17:53:00 -08:00
err.code.should.be.equal('BADSIGNATURES');
clients[1].getTxProposals({}, function(err, txs) {
should.not.exist(err);
txs[0].status.should.equal('pending');
done();
});
});
});
});
});
});
});
});
2015-02-22 17:15:53 -08:00
});
2015-02-23 09:09:04 -08:00
describe('Air gapped related flows', function() {
2015-02-21 22:46:47 -08:00
it('should be able get Tx proposals from a file', function(done) {
helpers.createAndJoinWallet(clients, 1, 2, function(err, w) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 1, 1);
var opts = {
amount: 10000000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
};
clients[1].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
clients[1].getTxProposals({
getRawTxps: true
}, function(err, txs, rawTxps) {
should.not.exist(err);
2015-02-22 19:43:39 -08:00
clients[0].parseTxProposals({
txps: rawTxps
}, function(err, txs2) {
2015-02-21 22:46:47 -08:00
should.not.exist(err);
txs[0].should.deep.equal(txs2[0]);
done();
});
});
});
});
});
});
it('should detect fakes from Tx proposals file', function(done) {
helpers.createAndJoinWallet(clients, 1, 2, function(err, w) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 1, 1);
var opts = {
amount: 10000000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
};
clients[1].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
clients[1].getTxProposals({
getRawTxps: true
}, function(err, txs, rawTxps) {
should.not.exist(err);
//Tamper
rawTxps[0].amount++;
2015-02-22 19:43:39 -08:00
clients[0].parseTxProposals({
txps: rawTxps
}, function(err, txs2) {
2015-02-21 22:46:47 -08:00
err.code.should.equal('SERVERCOMPROMISED');
done();
});
});
});
});
});
});
2015-02-22 19:43:39 -08:00
2015-02-23 09:09:04 -08:00
it('should create from proxy from airgapped', function(done) {
2015-02-23 10:36:35 -08:00
var airgapped = clients[0];
var proxy = clients[1];
airgapped.generateKey('testnet', function(err) {
2015-02-22 17:15:53 -08:00
should.not.exist(err);
2015-02-23 10:36:35 -08:00
airgapped.export({
2015-02-23 09:09:04 -08:00
access: 'readwrite'
}, function(err, str) {
2015-02-23 10:36:35 -08:00
proxy.import(str, function(err) {
2015-02-22 17:15:53 -08:00
should.not.exist(err);
2015-02-21 22:46:47 -08:00
2015-02-23 10:36:35 -08:00
proxy.createWallet('1', '2', 1, 1, 'testnet',
2015-02-23 09:09:04 -08:00
function(err) {
2015-02-22 17:15:53 -08:00
should.not.exist(err);
2015-02-23 09:09:04 -08:00
// should keep cpub
var c0 = JSON.parse(fsmock._get('client0'));
var c1 = JSON.parse(fsmock._get('client1'));
_.each(['copayerId', 'network', 'publicKeyRing',
'roPrivKey', 'rwPrivKey'
], function(k) {
c0[k].should.deep.equal(c1[k]);
2015-02-22 19:43:39 -08:00
});
2015-02-23 09:09:04 -08:00
done();
2015-02-22 17:15:53 -08:00
});
2015-02-23 09:09:04 -08:00
});
});
});
});
it('should join from proxy from airgapped', function(done) {
2015-02-23 10:36:35 -08:00
var airgapped = clients[0];
var proxy = clients[1];
var other = clients[2]; // Other copayer
airgapped.generateKey('testnet', function(err) {
2015-02-23 09:09:04 -08:00
should.not.exist(err);
2015-02-23 10:36:35 -08:00
airgapped.export({
2015-02-23 09:09:04 -08:00
access: 'readwrite'
}, function(err, str) {
2015-02-23 10:36:35 -08:00
proxy.import(str, function(err) {
2015-02-23 09:09:04 -08:00
should.not.exist(err);
2015-02-23 10:36:35 -08:00
other.createWallet('1', '2', 1, 2, 'testnet', function(err, secret) {
2015-02-23 09:09:04 -08:00
should.not.exist(err);
2015-02-23 10:36:35 -08:00
proxy.joinWallet(secret, 'john', function(err) {
2015-02-23 09:09:04 -08:00
should.not.exist(err);
// should keep cpub
var c0 = JSON.parse(fsmock._get('client0'));
var c1 = JSON.parse(fsmock._get('client1'));
_.each(['copayerId', 'network', 'publicKeyRing',
'roPrivKey', 'rwPrivKey'
], function(k) {
c0[k].should.deep.equal(c1[k]);
});
done();
})
2015-02-22 17:15:53 -08:00
});
});
});
});
});
2015-02-23 09:09:04 -08:00
2015-02-22 19:43:39 -08:00
it('should be able export signatures and sign later from a ro client',
function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err, w) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 1, 1);
blockExplorerMock.setUtxo(x0, 1, 2);
var opts = {
amount: 150000000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
};
clients[0].sendTxProposal(opts, function(err, txp) {
should.not.exist(err);
clients[0].getSignatures(txp, function(err, signatures) {
should.not.exist(err);
signatures.length.should.equal(txp.inputs.length);
signatures[0].length.should.above(62 * 2);
txp.signatures = signatures;
// Make client RO
var data = JSON.parse(fsmock._get('client0'));
delete data.xPrivKey;
fsmock._set('client0', JSON.stringify(data));
clients[0].signTxProposal(txp, function(err, txp) {
should.not.exist(err);
txp.status.should.equal('broadcasted');
done();
});
});
});
});
});
});
2015-02-21 17:53:00 -08:00
});
2015-02-17 06:48:19 -08:00
2015-02-19 12:38:48 -08:00
describe('Address Creation', function() {
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(err) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
clients[1].createAddress(function(err, x1) {
should.not.exist(err);
should.exist(x1.address);
clients[2].createAddress(function(err, x2) {
should.not.exist(err);
should.exist(x2.address);
done();
});
});
});
});
});
it('should see balance on address created by others', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err, w) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
blockExplorerMock.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);
clients[1].getBalance(function(err, bal1) {
bal1.totalAmount.should.equal(10 * 1e8);
bal1.lockedAmount.should.equal(0);
done();
});
});
});
2015-02-17 06:48:19 -08:00
});
2015-02-17 07:39:11 -08:00
});
2015-02-20 06:23:28 -08:00
it('should detect fake addresses', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err) {
should.not.exist(err);
// Get right response
2015-02-23 15:11:07 -08:00
clients[0]._load({}, function(err, data) {
2015-02-20 06:23:28 -08:00
var url = '/v1/addresses/';
clients[0]._doPostRequest(url, {}, data, function(err, address) {
// Tamper data
address.address = '2N86pNEpREGpwZyHVC5vrNUCbF9nM1Geh4K';
// Tamper response
clients[1]._doPostRequest = sinon.stub().yields(null, address);
// Grab real response
clients[1].createAddress(function(err, x0) {
err.code.should.contain('SERVERCOMPROMISED');
done();
});
});
});
});
});
it('should detect fake public keys', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err) {
should.not.exist(err);
// Get right response
2015-02-23 15:11:07 -08:00
clients[0]._load({}, function(err, data) {
2015-02-20 06:23:28 -08:00
var url = '/v1/addresses/';
clients[0]._doPostRequest(url, {}, data, function(err, address) {
// Tamper data
2015-02-20 06:52:01 -08:00
address.publicKeys = ['0322defe0c3eb9fcd8bc01878e6dbca7a6846880908d214b50a752445040cc5c54',
'02bf3aadc17131ca8144829fa1883c1ac0a8839067af4bca47a90ccae63d0d8037'
];
2015-02-20 06:23:28 -08:00
// Tamper response
clients[1]._doPostRequest = sinon.stub().yields(null, address);
// Grab real response
clients[1].createAddress(function(err, x0) {
err.code.should.contain('SERVERCOMPROMISED');
done();
});
});
});
});
});
2015-02-16 21:20:04 -08:00
});
2015-02-17 06:52:29 -08:00
2015-02-19 12:38:48 -08:00
describe('Wallet Backups and Mobility', function() {
2015-02-17 06:52:29 -08:00
2015-02-19 12:38:48 -08:00
it('round trip #import #export', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err, w) {
should.not.exist(err);
2015-02-23 09:09:04 -08:00
clients[1].export({}, function(err, str) {
2015-02-19 12:38:48 -08:00
should.not.exist(err);
2015-02-23 09:09:04 -08:00
var original = JSON.parse(fsmock._get('client1'));
2015-02-19 12:38:48 -08:00
clients[2].import(str, function(err, wallet) {
should.not.exist(err);
var clone = JSON.parse(fsmock._get('client2'));
delete original.walletPrivKey; // no need to persist it.
clone.should.deep.equal(original);
done();
});
2015-02-17 06:52:29 -08:00
2015-02-19 12:38:48 -08:00
});
});
});
it('should recreate a wallet, create addresses and receive money', function(done) {
2015-02-23 09:09:04 -08:00
var backup = '["tprv8ZgxMBicQKsPehCdj4HM1MZbKVXBFt5Dj9nQ44M99EdmdiUfGtQBDTSZsKmzdUrB1vEuP6ipuoa39UXwPS2CvnjE1erk5aUjc5vQZkWvH4B",2,2,["tpubD6NzVbkrYhZ4XCNDPDtyRWPxvJzvTkvUE2cMPB8jcUr9Dkicv6cYQmA18DBAid6eRK1BGCU9nzgxxVdQUGLYJ34XsPXPW4bxnH4PH6oQBF3"],"sd0kzXmlXBgTGHrKaBW4aA=="]';
2015-02-19 12:38:48 -08:00
clients[0].import(backup, function(err, wallet) {
2015-02-17 06:52:29 -08:00
should.not.exist(err);
2015-02-19 12:38:48 -08:00
clients[0].reCreateWallet('pepe', function(err, wallet) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
blockExplorerMock.setUtxo(x0, 10, 2);
clients[0].getBalance(function(err, bal0) {
should.not.exist(err);
bal0.totalAmount.should.equal(10 * 1e8);
bal0.lockedAmount.should.equal(0);
done();
});
});
});
2015-02-17 06:52:29 -08:00
});
2015-02-17 07:39:11 -08:00
});
2015-02-19 12:38:48 -08:00
});
2015-02-17 07:39:11 -08:00
2015-02-19 12:38:48 -08:00
2015-02-20 07:25:21 -08:00
describe('Transaction Proposals Creation and Locked funds', function() {
2015-02-20 06:52:01 -08:00
it('Should lock and release funds', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
blockExplorerMock.setUtxo(x0, 1, 2);
blockExplorerMock.setUtxo(x0, 1, 2);
var opts = {
amount: 120000000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
2015-02-20 06:52:01 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
clients[0].sendTxProposal(opts, function(err, y) {
err.code.should.contain('INSUFFICIENTFUNDS');
clients[0].rejectTxProposal(x, 'no', function(err, z) {
should.not.exist(err);
z.status.should.equal('rejected');
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
done();
});
});
});
});
});
});
});
it('Should keep message and refusal texts', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 10, 2);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'some message',
2015-02-20 06:52:01 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
clients[1].rejectTxProposal(x, 'rejection comment', function(err, tx1) {
2015-02-20 06:52:01 -08:00
should.not.exist(err);
2015-02-21 15:20:58 -08:00
2015-02-20 06:52:01 -08:00
clients[2].getTxProposals({}, function(err, txs) {
should.not.exist(err);
txs[0].message.should.equal('some message');
txs[0].actions[0].comment.should.equal('rejection comment');
2015-02-20 06:52:01 -08:00
done();
});
});
});
});
});
});
it('Should encrypt proposal message', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 10, 2);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'some message',
};
var spy = sinon.spy(clients[0], '_doPostRequest');
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
spy.calledOnce.should.be.true;
JSON.stringify(spy.getCall(0).args).should.not.contain('some message');
done();
});
});
});
});
2015-02-20 10:11:30 -08:00
it('Should encrypt proposal refusal comment', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 10, 2);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
var spy = sinon.spy(clients[1], '_doPostRequest');
clients[1].rejectTxProposal(x, 'rejection comment', function(err, tx1) {
should.not.exist(err);
spy.calledOnce.should.be.true;
JSON.stringify(spy.getCall(0).args).should.not.contain('rejection comment');
done();
});
});
});
});
});
2015-02-20 06:52:01 -08:00
it('should detect fake tx proposals (wrong signature)', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 10, 2);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello',
2015-02-20 06:52:01 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
// Get right response
2015-02-23 15:11:07 -08:00
clients[0]._load({}, function(err, data) {
2015-02-20 06:52:01 -08:00
var url = '/v1/txproposals/';
clients[0]._doGetRequest(url, data, function(err, txps) {
// Tamper data
txps[0].proposalSignature = '304402206e4a1db06e00068582d3be41cfc795dcf702451c132581e661e7241ef34ca19202203e17598b4764913309897d56446b51bc1dcd41a25d90fdb5f87a6b58fe3a6920';
// Tamper response
clients[0]._doGetRequest = sinon.stub().yields(null, txps);
// Grab real response
clients[0].getTxProposals({}, function(err, txps) {
should.exist(err);
err.code.should.contain('SERVERCOMPROMISED');
done();
});
});
});
});
});
});
});
it('should detect fake tx proposals (tampered amount)', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 10, 2);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello',
2015-02-20 06:52:01 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
// Get right response
2015-02-23 15:11:07 -08:00
clients[0]._load({}, function(err, data) {
2015-02-20 06:52:01 -08:00
var url = '/v1/txproposals/';
clients[0]._doGetRequest(url, data, function(err, txps) {
// Tamper data
txps[0].amount = 100000;
// Tamper response
clients[0]._doGetRequest = sinon.stub().yields(null, txps);
// Grab real response
clients[0].getTxProposals({}, function(err, txps) {
should.exist(err);
err.code.should.contain('SERVERCOMPROMISED');
done();
});
});
});
});
});
});
});
it('should detect fake tx proposals (change address not it wallet)', function(done) {
helpers.createAndJoinWallet(clients, 2, 2, function(err) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 10, 2);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello',
2015-02-20 06:52:01 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
// Get right response
2015-02-23 15:11:07 -08:00
clients[0]._load({}, function(err, data) {
2015-02-20 06:52:01 -08:00
var url = '/v1/txproposals/';
clients[0]._doGetRequest(url, data, function(err, txps) {
// Tamper data
txps[0].changeAddress.address = 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5';
// Tamper response
clients[0]._doGetRequest = sinon.stub().yields(null, txps);
// Grab real response
clients[0].getTxProposals({}, function(err, txps) {
should.exist(err);
err.code.should.contain('SERVERCOMPROMISED');
done();
});
});
});
});
});
});
});
2015-02-22 08:04:23 -08:00
it('Should return only main addresses (case 1)', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err, w) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
blockExplorerMock.setUtxo(x0, 1, 1);
var opts = {
amount: 10000000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
clients[0].getMainAddresses({}, function(err, addr) {
should.not.exist(err);
addr.length.should.equal(1);
done();
});
});
});
});
});
it('Should return only main addresses (case 2)', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err, w) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
2015-02-22 17:15:53 -08:00
clients[0].getMainAddresses({
doNotVerify: true
}, function(err, addr) {
2015-02-22 08:04:23 -08:00
should.not.exist(err);
addr.length.should.equal(2);
done();
});
});
});
});
});
2015-02-20 06:52:01 -08:00
});
2015-02-19 15:04:05 -08:00
describe('Transactions Signatures and Rejection', function() {
2015-02-20 10:24:49 -08:00
this.timeout(5000);
2015-02-19 12:38:48 -08:00
it('Send and broadcast in 1-1 wallet', function(done) {
helpers.createAndJoinWallet(clients, 1, 1, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
2015-02-19 13:11:57 -08:00
blockExplorerMock.setUtxo(x0, 1, 1);
2015-02-19 12:38:48 -08:00
var opts = {
2015-02-19 16:37:13 -08:00
amount: 10000000,
2015-02-19 12:38:48 -08:00
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
2015-02-19 12:38:48 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
2015-02-19 12:47:51 -08:00
x.requiredRejections.should.equal(1);
x.requiredSignatures.should.equal(1);
x.status.should.equal('pending');
x.changeAddress.path.should.equal('m/2147483647/1/0');
2015-02-19 13:11:57 -08:00
clients[0].signTxProposal(x, function(err, tx) {
should.not.exist(err);
tx.status.should.equal('broadcasted');
tx.txid.should.equal((new Bitcore.Transaction(blockExplorerMock.lastBroadcasted)).id);
2015-02-19 12:47:51 -08:00
done();
});
2015-02-19 12:38:48 -08:00
});
});
});
});
2015-02-19 12:47:51 -08:00
it('Send and broadcast in 2-3 wallet', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
blockExplorerMock.setUtxo(x0, 10, 1);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
2015-02-19 12:47:51 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
2015-02-22 19:43:39 -08:00
clients[0].getStatus(function(err, st) {
should.not.exist(err);
var x = st.pendingTxps[0];
x.status.should.equal('pending');
x.requiredRejections.should.equal(2);
x.requiredSignatures.should.equal(2);
var w = st.wallet;
w.copayers.length.should.equal(3);
w.status.should.equal('complete');
var b = st.balance;
b.totalAmount.should.equal(1000000000);
b.lockedAmount.should.equal(1000000000);
clients[0].signTxProposal(x, function(err, tx) {
should.not.exist(err, err);
tx.status.should.equal('pending');
clients[1].signTxProposal(x, function(err, tx) {
should.not.exist(err);
tx.status.should.equal('broadcasted');
tx.txid.should.equal((new Bitcore.Transaction(blockExplorerMock.lastBroadcasted)).id);
done();
});
2015-02-22 18:00:11 -08:00
});
2015-02-19 12:47:51 -08:00
});
});
});
});
});
2015-02-19 13:11:57 -08:00
it('Send, reject, 2 signs and broadcast in 2-3 wallet', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
blockExplorerMock.setUtxo(x0, 10, 1);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
2015-02-19 13:11:57 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
x.status.should.equal('pending');
x.requiredRejections.should.equal(2);
x.requiredSignatures.should.equal(2);
clients[0].rejectTxProposal(x, 'wont sign', function(err, tx) {
2015-02-19 13:11:57 -08:00
should.not.exist(err, err);
tx.status.should.equal('pending');
clients[1].signTxProposal(x, function(err, tx) {
should.not.exist(err);
clients[2].signTxProposal(x, function(err, tx) {
should.not.exist(err);
tx.status.should.equal('broadcasted');
tx.txid.should.equal((new Bitcore.Transaction(blockExplorerMock.lastBroadcasted)).id);
done();
});
});
});
});
});
});
});
it('Send, reject in 3-4 wallet', function(done) {
helpers.createAndJoinWallet(clients, 3, 4, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
blockExplorerMock.setUtxo(x0, 10, 1);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
2015-02-19 13:11:57 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
x.status.should.equal('pending');
x.requiredRejections.should.equal(2);
x.requiredSignatures.should.equal(3);
clients[0].rejectTxProposal(x, 'wont sign', function(err, tx) {
2015-02-19 13:11:57 -08:00
should.not.exist(err, err);
tx.status.should.equal('pending');
clients[1].signTxProposal(x, function(err, tx) {
should.not.exist(err);
tx.status.should.equal('pending');
clients[2].rejectTxProposal(x, 'me neither', function(err, tx) {
2015-02-19 13:11:57 -08:00
should.not.exist(err);
tx.status.should.equal('rejected');
done();
});
});
});
});
});
});
});
2015-02-19 13:47:17 -08:00
it('Should not allow to reject or sign twice', function(done) {
helpers.createAndJoinWallet(clients, 2, 3, function(err, w) {
clients[0].createAddress(function(err, x0) {
should.not.exist(err);
should.exist(x0.address);
blockExplorerMock.setUtxo(x0, 10, 1);
var opts = {
amount: 10000,
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
message: 'hello 1-1',
2015-02-19 13:47:17 -08:00
};
clients[0].sendTxProposal(opts, function(err, x) {
should.not.exist(err);
x.status.should.equal('pending');
x.requiredRejections.should.equal(2);
x.requiredSignatures.should.equal(2);
clients[0].signTxProposal(x, function(err, tx) {
should.not.exist(err, err);
tx.status.should.equal('pending');
clients[0].signTxProposal(x, function(err, tx) {
2015-02-19 15:04:05 -08:00
err.code.should.contain('CVOTED');
2015-02-19 13:47:17 -08:00
clients[1].rejectTxProposal(x, 'xx', function(err, tx) {
should.not.exist(err);
clients[1].rejectTxProposal(x, 'xx', function(err, tx) {
2015-02-19 15:04:05 -08:00
err.code.should.contain('CVOTED');
2015-02-19 13:47:17 -08:00
done();
});
});
});
});
});
});
});
});
2015-02-17 07:39:11 -08:00
});
2015-02-16 10:04:25 -08:00
});