bitcore-wallet-service/test/integration.js

1197 lines
36 KiB
JavaScript
Raw Normal View History

2015-01-27 05:18:45 -08:00
'use strict';
var _ = require('lodash');
var async = require('async');
var chai = require('chai');
var sinon = require('sinon');
var should = chai.should();
var levelup = require('levelup');
var memdown = require('memdown');
2015-02-03 05:53:59 -08:00
var Bitcore = require('bitcore');
2015-01-27 05:18:45 -08:00
2015-02-04 11:18:36 -08:00
var Utils = require('../lib/utils');
2015-02-03 05:53:59 -08:00
var SignUtils = require('../lib/signutils');
2015-01-28 05:52:45 -08:00
var Storage = require('../lib/storage');
2015-01-27 05:18:45 -08:00
var Wallet = require('../lib/model/wallet');
2015-01-27 11:40:21 -08:00
var Address = require('../lib/model/address');
2015-01-27 05:18:45 -08:00
var Copayer = require('../lib/model/copayer');
var CopayServer = require('../lib/server');
2015-02-10 08:20:41 -08:00
var TestData = require('./testdata');
2015-02-01 11:50:58 -08:00
2015-01-29 10:00:35 -08:00
var helpers = {};
2015-02-06 23:09:45 -08:00
helpers.getAuthServer = function(copayerId, cb) {
2015-02-06 12:56:51 -08:00
var signatureStub = sinon.stub(CopayServer.prototype, '_verifySignature');
signatureStub.returns(true);
CopayServer.getInstanceWithAuth({
copayerId: copayerId,
message: 'dummy',
signature: 'dummy',
2015-02-06 23:09:45 -08:00
}, function(err, server) {
if (err || !server) throw new Error('Could not login as copayerId ' + copayerId);
2015-02-06 12:56:51 -08:00
signatureStub.restore();
return cb(server);
});
};
2015-02-07 08:13:29 -08:00
helpers.createAndJoinWallet = function(m, n, cb) {
2015-02-06 12:56:51 -08:00
var server = new CopayServer();
2015-02-07 07:48:57 -08:00
var copayerIds = [];
2015-02-06 12:56:51 -08:00
2015-01-29 10:00:35 -08:00
var walletOpts = {
2015-02-07 08:13:29 -08:00
name: 'a wallet',
2015-01-29 10:00:35 -08:00
m: m,
n: n,
2015-02-10 08:20:41 -08:00
pubKey: TestData.keyPair.pub,
2015-01-29 10:00:35 -08:00
};
2015-02-07 08:13:29 -08:00
server.createWallet(walletOpts, function(err, walletId) {
2015-01-29 10:00:35 -08:00
if (err) return cb(err);
2015-02-10 08:20:41 -08:00
async.each(_.range(n), function(i, cb) {
2015-01-29 10:00:35 -08:00
var copayerOpts = {
2015-02-07 08:13:29 -08:00
walletId: walletId,
2015-02-10 08:20:41 -08:00
name: 'copayer ' + (i + 1),
xPubKey: TestData.copayers[i].xPubKey,
xPubKeySignature: TestData.copayers[i].xPubKeySignature,
2015-01-29 10:00:35 -08:00
};
2015-02-02 06:55:03 -08:00
2015-02-07 07:48:57 -08:00
server.joinWallet(copayerOpts, function(err, copayerId) {
copayerIds.push(copayerId);
2015-01-29 10:00:35 -08:00
return cb(err);
});
2015-02-06 23:09:45 -08:00
}, function(err) {
2015-02-06 12:56:51 -08:00
if (err) return new Error('Could not generate wallet');
2015-02-07 07:48:57 -08:00
helpers.getAuthServer(copayerIds[0], function(s) {
2015-02-06 23:09:45 -08:00
s.getWallet({}, function(err, w) {
2015-02-10 08:20:41 -08:00
cb(s, w, _.take(TestData.copayers, w.n));
2015-02-06 12:56:51 -08:00
});
2015-01-29 10:00:35 -08:00
});
});
});
};
2015-02-03 11:46:28 -08:00
helpers.randomTXID = function() {
2015-02-03 18:17:06 -08:00
return Bitcore.crypto.Hash.sha256(new Buffer(Math.random() * 100000)).toString('hex');;
2015-02-03 11:46:28 -08:00
};
2015-02-04 11:18:36 -08:00
helpers.toSatoshi = function(btc) {
if (_.isArray(btc)) {
return _.map(btc, helpers.toSatoshi);
} else {
return Utils.strip(btc * 1e8);
}
};
// Amounts in satoshis
2015-02-03 11:46:28 -08:00
helpers.createUtxos = function(server, wallet, amounts, cb) {
var addresses = [];
async.each(amounts, function(a, next) {
server.createAddress({}, function(err, address) {
2015-02-03 11:46:28 -08:00
addresses.push(address);
next(err);
});
},
function(err) {
amounts = [].concat(amounts);
var i = 0;
2015-02-04 06:43:12 -08:00
var utxos = _.map(amounts, function(amount) {
2015-02-03 11:46:28 -08:00
return {
txid: helpers.randomTXID(),
vout: Math.floor((Math.random() * 10) + 1),
2015-02-04 11:18:36 -08:00
satoshis: amount,
2015-02-03 18:17:06 -08:00
scriptPubKey: addresses[i].getScriptPubKey(wallet.m).toBuffer().toString('hex'),
2015-02-04 16:38:23 -08:00
address: addresses[i++].address,
2015-02-03 11:46:28 -08:00
};
2015-02-04 06:43:12 -08:00
});
2015-02-06 10:15:54 -08:00
return cb(utxos);
});
};
2015-02-04 06:43:12 -08:00
2015-02-06 10:15:54 -08:00
helpers.stubBlockExplorer = function(server, utxos, txid) {
var bc = sinon.stub();
bc.getUnspentUtxos = sinon.stub().callsArgWith(1, null, utxos);
if (txid) {
bc.broadcast = sinon.stub().callsArgWith(1, null, txid);
} else {
bc.broadcast = sinon.stub().callsArgWith(1, 'broadcast error');
}
server._getBlockExplorer = sinon.stub().returns(bc);
2015-01-30 12:37:30 -08:00
};
2015-01-29 10:00:35 -08:00
2015-02-06 10:15:54 -08:00
2015-02-04 06:43:12 -08:00
helpers.clientSign = function(tx, xpriv, n) {
//Derive proper key to sign, for each input
var privs = [],
derived = {};
2015-02-10 08:20:41 -08:00
var xpriv = new Bitcore.HDPrivateKey(TestData.copayers[0].xPrivKey);
2015-02-04 06:43:12 -08:00
_.each(tx.inputs, function(i) {
if (!derived[i.path]) {
2015-02-04 16:38:23 -08:00
derived[i.path] = xpriv.derive(i.path).privateKey;
2015-02-06 10:15:54 -08:00
}
2015-02-04 16:38:23 -08:00
privs.push(derived[i.path]);
2015-02-04 06:43:12 -08:00
});
var t = new Bitcore.Transaction();
_.each(tx.inputs, function(i) {
t.from(i, i.publicKeys, n);
});
t.to(tx.toAddress, tx.amount)
.change(tx.changeAddress)
.sign(privs);
var signatures = [];
_.each(privs, function(p) {
2015-02-06 10:15:54 -08:00
var s = t.getSignatures(p)[0].signature.toDER().toString('hex');
signatures.push(s);
});
2015-02-05 10:50:18 -08:00
//
2015-02-04 06:43:12 -08:00
return signatures;
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature = function(txOpts, privKey) {
2015-02-10 05:22:23 -08:00
var msg = txOpts.toAddress + '|' + txOpts.amount + '|' + txOpts.message;
2015-02-10 08:20:41 -08:00
txOpts.proposalSignature = SignUtils.sign(msg, privKey);
2015-02-10 05:22:23 -08:00
};
2015-01-28 05:52:45 -08:00
var db, storage;
2015-01-27 05:18:45 -08:00
2015-01-29 10:00:35 -08:00
2015-01-27 05:18:45 -08:00
describe('Copay server', function() {
beforeEach(function() {
2015-02-01 11:50:58 -08:00
db = levelup(memdown, {
valueEncoding: 'json'
});
storage = new Storage({
db: db
});
2015-02-06 23:09:45 -08:00
CopayServer.initialize({
storage: storage
});
2015-01-27 05:18:45 -08:00
});
2015-02-06 12:56:51 -08:00
describe.skip('#getInstanceWithAuth', function() {
2015-02-06 23:09:45 -08:00
beforeEach(function() {});
2015-01-27 05:18:45 -08:00
2015-02-06 23:09:45 -08:00
it('should get server instance for existing copayer', function(done) {});
2015-01-27 05:18:45 -08:00
2015-02-06 23:09:45 -08:00
it('should fail when requesting for non-existent copayer', function(done) {});
2015-01-27 05:18:45 -08:00
2015-02-06 23:09:45 -08:00
it('should fail when message signature cannot be verified', function(done) {});
2015-01-27 05:18:45 -08:00
});
describe('#createWallet', function() {
2015-02-06 12:56:51 -08:00
var server;
2015-01-27 05:18:45 -08:00
beforeEach(function() {
2015-02-06 12:56:51 -08:00
server = new CopayServer();
2015-01-27 05:18:45 -08:00
});
it('should create and store wallet', function(done) {
var opts = {
name: 'my wallet',
m: 2,
n: 3,
2015-02-10 08:20:41 -08:00
pubKey: TestData.keyPair.pub,
2015-01-27 05:18:45 -08:00
};
2015-02-07 08:13:29 -08:00
server.createWallet(opts, function(err, walletId) {
2015-01-27 05:18:45 -08:00
should.not.exist(err);
2015-02-07 08:13:29 -08:00
server.storage.fetchWallet(walletId, function(err, wallet) {
2015-01-27 05:18:45 -08:00
should.not.exist(err);
2015-02-07 08:13:29 -08:00
wallet.id.should.equal(walletId);
2015-01-27 05:18:45 -08:00
wallet.name.should.equal('my wallet');
done();
});
});
});
2015-02-08 08:16:41 -08:00
it('should fail to create wallet with no name', function(done) {
var opts = {
name: '',
m: 2,
n: 3,
2015-02-10 08:20:41 -08:00
pubKey: TestData.keyPair.pub,
2015-02-08 08:16:41 -08:00
};
server.createWallet(opts, function(err, walletId) {
should.not.exist(walletId);
err.should.exist;
err.message.should.contain('name');
done();
});
});
2015-02-03 04:40:39 -08:00
it('should fail to create wallet with invalid copayer pairs', function(done) {
2015-02-03 11:46:28 -08:00
var invalidPairs = [{
m: 0,
n: 0
}, {
m: 0,
n: 2
}, {
m: 2,
n: 1
}, {
m: 0,
n: 10
}, {
m: 1,
n: 20
}, {
m: 10,
n: 10
}, ];
2015-02-03 04:40:39 -08:00
var opts = {
id: '123',
name: 'my wallet',
2015-02-10 08:20:41 -08:00
pubKey: TestData.keyPair.pub,
2015-02-03 04:40:39 -08:00
};
2015-02-03 11:46:28 -08:00
async.each(invalidPairs, function(pair, cb) {
2015-02-03 04:40:39 -08:00
opts.m = pair.m;
opts.n = pair.n;
server.createWallet(opts, function(err) {
should.exist(err);
2015-02-04 07:46:31 -08:00
err.message.should.equal('Invalid combination of required copayers / total copayers');
2015-02-03 04:40:39 -08:00
return cb();
});
2015-02-03 11:46:28 -08:00
}, function(err) {
2015-02-03 04:40:39 -08:00
done();
});
2015-02-03 11:46:28 -08:00
});
2015-01-27 05:18:45 -08:00
});
describe('#joinWallet', function() {
2015-02-08 08:36:19 -08:00
var server, walletId;
beforeEach(function(done) {
2015-02-06 12:56:51 -08:00
server = new CopayServer();
2015-01-27 05:18:45 -08:00
var walletOpts = {
name: 'my wallet',
m: 2,
n: 3,
2015-02-10 08:20:41 -08:00
pubKey: TestData.keyPair.pub,
2015-01-27 05:18:45 -08:00
};
2015-02-08 08:36:19 -08:00
server.createWallet(walletOpts, function(err, wId) {
should.not.exist(err);
should.exist.walletId;
walletId = wId;
done();
});
});
2015-02-01 11:50:58 -08:00
2015-02-08 08:36:19 -08:00
it('should join existing wallet', function(done) {
var copayerOpts = {
walletId: walletId,
name: 'me',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[0].xPubKey,
xPubKeySignature: TestData.copayers[0].xPubKeySignature,
2015-02-08 08:36:19 -08:00
};
server.joinWallet(copayerOpts, function(err, copayerId) {
2015-01-27 05:18:45 -08:00
should.not.exist(err);
2015-02-08 08:36:19 -08:00
helpers.getAuthServer(copayerId, function(server) {
server.getWallet({}, function(err, wallet) {
wallet.id.should.equal(walletId);
wallet.copayers.length.should.equal(1);
var copayer = wallet.copayers[0];
copayer.name.should.equal('me');
copayer.id.should.equal(copayerId);
done();
2015-01-27 05:18:45 -08:00
});
});
});
});
2015-02-08 08:36:19 -08:00
it('should fail to join with no name', function(done) {
var copayerOpts = {
walletId: walletId,
name: '',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[0].xPubKey,
xPubKeySignature: TestData.copayers[0].xPubKeySignature,
2015-02-08 08:36:19 -08:00
};
server.joinWallet(copayerOpts, function(err, copayerId) {
should.not.exist(copayerId);
err.should.exist;
err.message.should.contain('name');
done();
});
});
2015-02-01 11:50:58 -08:00
it('should fail to join non-existent wallet', function(done) {
2015-02-07 08:13:29 -08:00
var copayerOpts = {
2015-02-08 08:36:19 -08:00
walletId: '123',
2015-02-07 08:13:29 -08:00
name: 'me',
xPubKey: 'dummy',
xPubKeySignature: 'dummy',
2015-01-27 05:18:45 -08:00
};
2015-02-07 08:13:29 -08:00
server.joinWallet(copayerOpts, function(err) {
should.exist(err);
done();
2015-01-27 05:18:45 -08:00
});
});
2015-02-01 11:50:58 -08:00
it('should fail to join full wallet', function(done) {
2015-02-08 08:36:19 -08:00
helpers.createAndJoinWallet(1, 1, function(s, wallet) {
var copayerOpts = {
walletId: wallet.id,
2015-01-27 05:18:45 -08:00
name: 'me',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[1].xPubKey,
xPubKeySignature: TestData.copayers[1].xPubKeySignature,
2015-01-27 05:18:45 -08:00
};
2015-02-08 08:36:19 -08:00
server.joinWallet(copayerOpts, function(err) {
should.exist(err);
err.code.should.equal('WFULL');
err.message.should.equal('Wallet full');
done();
2015-01-27 05:18:45 -08:00
});
});
});
2015-02-01 11:50:58 -08:00
it('should fail to re-join wallet', function(done) {
2015-02-08 08:36:19 -08:00
var copayerOpts = {
walletId: walletId,
name: 'me',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[0].xPubKey,
xPubKeySignature: TestData.copayers[0].xPubKeySignature,
2015-01-27 05:18:45 -08:00
};
2015-02-08 08:36:19 -08:00
server.joinWallet(copayerOpts, function(err) {
2015-01-27 05:18:45 -08:00
should.not.exist(err);
2015-02-01 11:50:58 -08:00
server.joinWallet(copayerOpts, function(err) {
2015-02-08 08:36:19 -08:00
should.exist(err);
err.code.should.equal('CINWALLET');
err.message.should.equal('Copayer already in wallet');
done();
2015-01-27 05:18:45 -08:00
});
});
});
2015-02-01 11:50:58 -08:00
it('should fail to join with bad formated signature', function(done) {
2015-02-08 08:36:19 -08:00
var copayerOpts = {
walletId: walletId,
name: 'me',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[0].xPubKey,
2015-02-08 08:36:19 -08:00
xPubKeySignature: 'bad sign',
2015-02-01 11:50:58 -08:00
};
2015-02-08 08:36:19 -08:00
server.joinWallet(copayerOpts, function(err) {
err.message.should.equal('Bad request');
done();
2015-02-01 11:50:58 -08:00
});
});
it('should fail to join with null signature', function(done) {
2015-02-08 08:36:19 -08:00
var copayerOpts = {
walletId: walletId,
name: 'me',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[0].xPubKey[0],
2015-02-01 11:50:58 -08:00
};
server.joinWallet(copayerOpts, function(err) {
err.should.exist;
err.message.should.contain('argument missing');
2015-02-08 08:36:19 -08:00
done();
});
2015-02-01 11:50:58 -08:00
});
it('should fail to join with wrong signature', function(done) {
2015-02-08 08:36:19 -08:00
var copayerOpts = {
walletId: walletId,
name: 'me',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[0].xPubKey,
xPubKeySignature: TestData.copayers[1].xPubKeySignature,
2015-02-01 11:50:58 -08:00
};
2015-02-08 08:36:19 -08:00
server.joinWallet(copayerOpts, function(err) {
err.message.should.equal('Bad request');
done();
2015-02-01 11:50:58 -08:00
});
});
2015-02-02 11:16:14 -08:00
it('should set pkr and status = complete on last copayer joining (2-3)', function(done) {
2015-02-07 08:13:29 -08:00
helpers.createAndJoinWallet(2, 3, function(server) {
2015-02-06 12:56:51 -08:00
server.getWallet({}, function(err, wallet) {
2015-01-27 05:18:45 -08:00
should.not.exist(err);
wallet.status.should.equal('complete');
wallet.publicKeyRing.length.should.equal(3);
done();
});
});
2015-02-01 11:50:58 -08:00
});
2015-01-27 05:18:45 -08:00
});
2015-01-28 07:16:19 -08:00
describe('#verifyMessageSignature', function() {
2015-02-06 15:59:59 -08:00
var server, wallet;
beforeEach(function(done) {
2015-02-07 08:13:29 -08:00
helpers.createAndJoinWallet(2, 2, function(s, w) {
2015-02-06 15:59:59 -08:00
server = s;
wallet = w;
done();
});
2015-01-28 07:06:34 -08:00
});
2015-02-01 11:50:58 -08:00
it('should successfully verify message signature', function(done) {
2015-02-06 15:59:59 -08:00
var opts = {
2015-02-10 08:20:41 -08:00
message: TestData.message.text,
signature: TestData.message.signature,
2015-02-06 15:59:59 -08:00
};
server.verifyMessageSignature(opts, function(err, isValid) {
should.not.exist(err);
isValid.should.equal(true);
done();
2015-01-28 07:06:34 -08:00
});
});
2015-02-06 12:56:51 -08:00
it('should fail to verify message signature for different copayer', function(done) {
2015-02-06 15:59:59 -08:00
var opts = {
2015-02-10 08:20:41 -08:00
message: TestData.message.text,
signature: TestData.message.signature,
2015-02-06 15:59:59 -08:00
};
2015-02-07 08:13:29 -08:00
helpers.getAuthServer(wallet.copayers[1].id, function(server) {
2015-02-06 15:59:59 -08:00
server.verifyMessageSignature(opts, function(err, isValid) {
should.not.exist(err);
isValid.should.be.false;
done();
2015-01-28 07:06:34 -08:00
});
});
});
});
2015-01-27 11:40:21 -08:00
describe('#createAddress', function() {
2015-02-06 15:59:59 -08:00
var server, wallet;
beforeEach(function(done) {
2015-02-07 08:13:29 -08:00
helpers.createAndJoinWallet(2, 2, function(s, w) {
2015-02-06 15:59:59 -08:00
server = s;
wallet = w;
done();
});
2015-01-27 05:18:45 -08:00
});
it('should create address', function(done) {
server.createAddress({}, function(err, address) {
2015-02-06 15:59:59 -08:00
should.not.exist(err);
address.should.exist;
2015-02-10 08:20:41 -08:00
address.address.should.equal('38Jf1QE7ddXscW76ACgJrNkMWBwDAgMm6M');
2015-02-06 15:59:59 -08:00
address.path.should.equal('m/2147483647/0/0');
done();
2015-02-02 11:32:13 -08:00
});
});
it('should fail to create address when wallet is not complete', function(done) {
var server = new CopayServer();
var walletOpts = {
name: 'my wallet',
m: 2,
n: 3,
2015-02-10 08:20:41 -08:00
pubKey: TestData.keyPair.pub,
};
server.createWallet(walletOpts, function(err, walletId) {
should.not.exist(err);
var copayerOpts = {
walletId: walletId,
name: 'me',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[0].xPubKey,
xPubKeySignature: TestData.copayers[0].xPubKeySignature,
};
server.joinWallet(copayerOpts, function(err, copayerId) {
should.not.exist(err);
helpers.getAuthServer(copayerId, function(server) {
server.createAddress({}, function(err, address) {
should.not.exist(address);
err.should.exist;
err.message.should.contain('not complete');
done();
});
});
});
});
});
2015-02-07 06:15:54 -08:00
it('should create many addresses on simultaneous requests', function(done) {
2015-02-08 13:35:19 -08:00
var N = 5;
async.map(_.range(N), function(i, cb) {
server.createAddress({}, cb);
2015-02-06 15:59:59 -08:00
}, function(err, addresses) {
2015-02-08 13:35:19 -08:00
addresses.length.should.equal(N);
_.each(_.range(N), function(i) {
addresses[i].path.should.equal('m/2147483647/0/' + i);
});
2015-02-06 15:59:59 -08:00
// No two identical addresses
2015-02-08 13:35:19 -08:00
_.uniq(_.pluck(addresses, 'address')).length.should.equal(N);
2015-02-06 15:59:59 -08:00
done();
});
});
2015-02-03 12:32:40 -08:00
2015-02-08 15:46:02 -08:00
it('should not create address if unable to store it', function(done) {
sinon.stub(server.storage, 'storeAddressAndWallet').yields('dummy error');
server.createAddress({}, function(err, address) {
2015-02-06 15:59:59 -08:00
err.should.exist;
should.not.exist(address);
2015-02-03 12:32:40 -08:00
2015-02-06 15:59:59 -08:00
server.getAddresses({}, function(err, addresses) {
addresses.length.should.equal(0);
2015-02-08 15:46:02 -08:00
server.storage.storeAddressAndWallet.restore();
server.createAddress({}, function(err, address) {
2015-02-06 15:59:59 -08:00
should.not.exist(err);
address.should.exist;
2015-02-10 08:20:41 -08:00
address.address.should.equal('38Jf1QE7ddXscW76ACgJrNkMWBwDAgMm6M');
address.path.should.equal('m/2147483647/0/0');
2015-02-06 15:59:59 -08:00
done();
2015-02-03 12:32:40 -08:00
});
});
});
});
2015-01-27 05:18:45 -08:00
});
2015-01-27 11:40:21 -08:00
describe('#createTx', function() {
2015-02-10 08:20:41 -08:00
var server, wallet, copayerPriv;
2015-01-27 11:40:21 -08:00
beforeEach(function(done) {
2015-02-10 08:20:41 -08:00
helpers.createAndJoinWallet(2, 2, function(s, w, c) {
2015-02-06 12:56:51 -08:00
server = s;
2015-02-03 05:53:59 -08:00
wallet = w;
2015-02-10 08:20:41 -08:00
copayerPriv = c;
server.createAddress({}, function(err, address) {
2015-01-27 11:40:21 -08:00
done();
});
});
});
2015-02-06 12:51:21 -08:00
it('should create a tx', function(done) {
2015-02-04 11:18:36 -08:00
helpers.createUtxos(server, wallet, helpers.toSatoshi([100, 200]), function(utxos) {
2015-02-06 10:15:54 -08:00
helpers.stubBlockExplorer(server, utxos);
2015-02-03 11:46:28 -08:00
var txOpts = {
2015-02-03 18:17:06 -08:00
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
2015-02-04 11:18:36 -08:00
amount: helpers.toSatoshi(80),
2015-02-03 11:46:28 -08:00
message: 'some message',
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-03 18:17:06 -08:00
2015-02-03 11:46:28 -08:00
server.createTx(txOpts, function(err, tx) {
2015-01-28 12:40:37 -08:00
should.not.exist(err);
2015-02-03 11:46:28 -08:00
tx.should.exist;
2015-02-08 14:10:06 -08:00
tx.message.should.equal('some message');
2015-02-03 11:46:28 -08:00
tx.isAccepted().should.equal.false;
tx.isRejected().should.equal.false;
2015-02-06 12:56:51 -08:00
server.getPendingTxs({}, function(err, txs) {
2015-01-30 13:29:46 -08:00
should.not.exist(err);
2015-02-03 11:46:28 -08:00
txs.length.should.equal(1);
2015-02-06 12:56:51 -08:00
server.getBalance({}, function(err, balance) {
2015-02-03 11:46:28 -08:00
should.not.exist(err);
2015-02-04 11:18:36 -08:00
balance.totalAmount.should.equal(helpers.toSatoshi(300));
balance.lockedAmount.should.equal(helpers.toSatoshi(100));
2015-02-03 11:46:28 -08:00
done();
});
2015-01-30 13:29:46 -08:00
});
2015-01-28 12:40:37 -08:00
});
2015-01-27 11:40:21 -08:00
});
});
2015-01-30 12:37:30 -08:00
it('should fail to create tx when wallet is not complete', function(done) {
var server = new CopayServer();
var walletOpts = {
name: 'my wallet',
m: 2,
n: 3,
2015-02-10 08:20:41 -08:00
pubKey: TestData.keyPair.pub,
};
server.createWallet(walletOpts, function(err, walletId) {
should.not.exist(err);
var copayerOpts = {
walletId: walletId,
name: 'me',
2015-02-10 08:20:41 -08:00
xPubKey: TestData.copayers[0].xPubKey,
xPubKeySignature: TestData.copayers[0].xPubKeySignature,
};
server.joinWallet(copayerOpts, function(err, copayerId) {
should.not.exist(err);
helpers.getAuthServer(copayerId, function(server, wallet) {
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(80),
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
server.createTx(txOpts, function(err, tx) {
should.not.exist(tx);
err.should.exist;
err.message.should.contain('not complete');
done();
});
});
});
});
});
2015-02-07 06:15:54 -08:00
2015-02-08 14:10:06 -08:00
it('should fail to create tx for address invalid address', function(done) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([100, 200]), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = {
toAddress: 'invalid address',
amount: helpers.toSatoshi(80),
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-08 14:10:06 -08:00
server.createTx(txOpts, function(err, tx) {
should.not.exist(tx);
err.should.exist;
err.code.should.equal('INVALIDADDRESS');
err.message.should.equal('Invalid address');
done();
});
});
});
it('should fail to create tx for address of different network', function(done) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([100, 200]), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = {
toAddress: 'myE38JHdxmQcTJGP1ZiX4BiGhDxMJDvLJD', // testnet
amount: helpers.toSatoshi(80),
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-08 14:10:06 -08:00
server.createTx(txOpts, function(err, tx) {
should.not.exist(tx);
err.should.exist;
err.code.should.equal('INVALIDADDRESS');
err.message.should.equal('Incorrect address network');
done();
});
});
});
2015-02-08 08:16:41 -08:00
2015-02-03 18:17:06 -08:00
it('should fail to create tx when insufficient funds', function(done) {
2015-02-06 10:15:54 -08:00
helpers.createUtxos(server, wallet, helpers.toSatoshi([100]), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
2015-02-03 18:17:06 -08:00
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
2015-02-04 11:18:36 -08:00
amount: helpers.toSatoshi(120),
2015-02-03 18:17:06 -08:00
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-03 18:17:06 -08:00
server.createTx(txOpts, function(err, tx) {
err.code.should.equal('INSUFFICIENTFUNDS');
err.message.should.equal('Insufficient funds');
2015-02-06 12:56:51 -08:00
server.getPendingTxs({}, function(err, txs) {
2015-02-03 18:17:06 -08:00
should.not.exist(err);
txs.length.should.equal(0);
2015-02-06 12:56:51 -08:00
server.getBalance({}, function(err, balance) {
2015-02-03 18:17:06 -08:00
should.not.exist(err);
balance.lockedAmount.should.equal(0);
2015-02-04 11:18:36 -08:00
balance.totalAmount.should.equal(10000000000);
2015-02-03 18:17:06 -08:00
done();
});
});
});
});
});
2015-02-08 08:16:41 -08:00
it.skip('should fail to create tx when insufficient funds for fee', function(done) {});
it.skip('should fail to create tx for dust amount', function(done) {});
2015-02-03 18:17:06 -08:00
it('should create tx when there is a pending tx and enough UTXOs', function(done) {
2015-02-04 11:18:36 -08:00
helpers.createUtxos(server, wallet, helpers.toSatoshi([10.1, 10.2, 10.3]), function(utxos) {
2015-02-06 10:15:54 -08:00
helpers.stubBlockExplorer(server, utxos);
2015-02-03 18:17:06 -08:00
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
2015-02-04 11:18:36 -08:00
amount: helpers.toSatoshi(12),
2015-02-03 18:17:06 -08:00
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-03 18:17:06 -08:00
server.createTx(txOpts, function(err, tx) {
should.not.exist(err);
tx.should.exist;
var txOpts2 = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: 8,
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts2, copayerPriv[0].privKey);
2015-02-03 18:17:06 -08:00
server.createTx(txOpts2, function(err, tx) {
should.not.exist(err);
tx.should.exist;
2015-02-06 12:56:51 -08:00
server.getPendingTxs({}, function(err, txs) {
2015-02-03 18:17:06 -08:00
should.not.exist(err);
txs.length.should.equal(2);
2015-02-06 12:56:51 -08:00
server.getBalance({}, function(err, balance) {
2015-02-03 18:17:06 -08:00
should.not.exist(err);
2015-02-04 11:18:36 -08:00
balance.totalAmount.should.equal(3060000000);
balance.lockedAmount.should.equal(3060000000);
2015-02-03 18:17:06 -08:00
done();
});
});
});
});
});
});
it('should fail to create tx when there is a pending tx and not enough UTXOs', function(done) {
2015-02-04 11:18:36 -08:00
helpers.createUtxos(server, wallet, helpers.toSatoshi([10.1, 10.2, 10.3]), function(utxos) {
2015-02-06 10:15:54 -08:00
helpers.stubBlockExplorer(server, utxos);
2015-02-03 18:17:06 -08:00
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
2015-02-04 11:18:36 -08:00
amount: helpers.toSatoshi(12),
2015-02-03 18:17:06 -08:00
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-03 18:17:06 -08:00
server.createTx(txOpts, function(err, tx) {
should.not.exist(err);
tx.should.exist;
var txOpts2 = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
2015-02-04 11:18:36 -08:00
amount: helpers.toSatoshi(24),
2015-02-03 18:17:06 -08:00
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts2, copayerPriv[0].privKey);
2015-02-03 18:17:06 -08:00
server.createTx(txOpts2, function(err, tx) {
err.code.should.equal('INSUFFICIENTFUNDS');
err.message.should.equal('Insufficient funds');
2015-02-03 18:17:06 -08:00
should.not.exist(tx);
2015-02-06 12:56:51 -08:00
server.getPendingTxs({}, function(err, txs) {
2015-02-03 18:17:06 -08:00
should.not.exist(err);
txs.length.should.equal(1);
2015-02-06 12:56:51 -08:00
server.getBalance({}, function(err, balance) {
2015-02-03 18:17:06 -08:00
should.not.exist(err);
2015-02-04 11:18:36 -08:00
balance.totalAmount.should.equal(helpers.toSatoshi(30.6));
balance.lockedAmount.should.equal(helpers.toSatoshi(20.3));
2015-02-03 18:17:06 -08:00
done();
});
});
});
});
});
});
2015-02-08 13:29:58 -08:00
it('should create tx using different UTXOs for simultaneous requests', function(done) {
2015-02-08 13:35:19 -08:00
var N = 5;
helpers.createUtxos(server, wallet, helpers.toSatoshi(_.times(N, function() {
2015-02-08 13:29:58 -08:00
return 100;
})), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
server.getBalance({}, function(err, balance) {
should.not.exist(err);
2015-02-08 13:35:19 -08:00
balance.totalAmount.should.equal(helpers.toSatoshi(N * 100));
2015-02-08 13:29:58 -08:00
balance.lockedAmount.should.equal(helpers.toSatoshi(0));
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(80),
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-08 13:35:19 -08:00
async.map(_.range(N), function(i, cb) {
2015-02-08 13:29:58 -08:00
server.createTx(txOpts, function(err, tx) {
cb(err, tx);
});
}, function(err) {
server.getPendingTxs({}, function(err, txs) {
should.not.exist(err);
2015-02-08 13:35:19 -08:00
txs.length.should.equal(N);
_.uniq(_.pluck(txs, 'changeAddress')).length.should.equal(N);
2015-02-08 13:29:58 -08:00
server.getBalance({}, function(err, balance) {
should.not.exist(err);
2015-02-08 13:35:19 -08:00
balance.totalAmount.should.equal(helpers.toSatoshi(N * 100));
2015-02-08 13:29:58 -08:00
balance.lockedAmount.should.equal(balance.totalAmount);
done();
});
});
});
});
});
});
2015-01-27 11:40:21 -08:00
});
2015-02-04 06:43:12 -08:00
2015-02-04 11:18:36 -08:00
describe('#signTx', function() {
2015-02-10 08:20:41 -08:00
var server, wallet, copayerPriv, txid;
2015-02-04 06:43:12 -08:00
beforeEach(function(done) {
2015-02-10 08:20:41 -08:00
helpers.createAndJoinWallet(2, 2, function(s, w, c) {
2015-02-06 12:56:51 -08:00
server = s;
2015-02-04 06:43:12 -08:00
wallet = w;
2015-02-10 08:20:41 -08:00
copayerPriv = c;
server.createAddress({}, function(err, address) {
2015-02-04 11:18:36 -08:00
helpers.createUtxos(server, wallet, helpers.toSatoshi([1, 2, 3, 4, 5, 6, 7, 8]), function(utxos) {
2015-02-06 10:15:54 -08:00
helpers.stubBlockExplorer(server, utxos);
2015-02-04 06:43:12 -08:00
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
2015-02-04 11:18:36 -08:00
amount: helpers.toSatoshi(10),
2015-02-04 06:43:12 -08:00
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-04 06:43:12 -08:00
server.createTx(txOpts, function(err, tx) {
should.not.exist(err);
tx.should.exist;
txid = tx.id;
done();
});
});
});
});
});
2015-02-06 23:09:45 -08:00
it('should sign a TX with multiple inputs, different paths', function(done) {
2015-02-06 12:56:51 -08:00
server.getPendingTxs({}, function(err, txs) {
2015-02-04 06:43:12 -08:00
var tx = txs[0];
tx.id.should.equal(txid);
2015-02-10 08:20:41 -08:00
var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey, wallet.n);
2015-02-04 11:18:36 -08:00
server.signTx({
txProposalId: txid,
signatures: signatures,
}, function(err) {
2015-02-05 10:50:18 -08:00
should.not.exist(err);
2015-02-04 11:18:36 -08:00
done();
});
2015-02-05 10:50:18 -08:00
});
});
2015-02-04 06:43:12 -08:00
2015-02-06 23:09:45 -08:00
it('should fail if one signature is broken', function(done) {
2015-02-06 12:56:51 -08:00
server.getPendingTxs({}, function(err, txs) {
2015-02-05 10:50:18 -08:00
var tx = txs[0];
tx.id.should.equal(txid);
2015-02-10 08:20:41 -08:00
var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey, wallet.n);
2015-02-06 10:15:54 -08:00
signatures[0] = 1;
2015-02-05 10:50:18 -08:00
server.signTx({
txProposalId: txid,
signatures: signatures,
}, function(err) {
err.message.should.contain('signatures');
done();
});
2015-02-04 06:43:12 -08:00
});
2015-02-05 10:50:18 -08:00
});
2015-02-07 08:13:29 -08:00
it('should fail on invalid signature', function(done) {
2015-02-06 12:56:51 -08:00
server.getPendingTxs({}, function(err, txs) {
2015-02-05 10:50:18 -08:00
var tx = txs[0];
tx.id.should.equal(txid);
2015-02-04 06:43:12 -08:00
2015-02-05 10:50:18 -08:00
var signatures = ['11', '22', '33', '44'];
server.signTx({
txProposalId: txid,
signatures: signatures,
}, function(err) {
err.message.should.contain('signatures');
done();
});
});
2015-02-04 06:43:12 -08:00
});
});
2015-02-06 10:15:54 -08:00
describe('#signTx and broadcast', function() {
2015-02-10 08:20:41 -08:00
var server, wallet, copayerPriv, utxos;
2015-02-06 10:15:54 -08:00
beforeEach(function(done) {
2015-02-10 08:20:41 -08:00
helpers.createAndJoinWallet(1, 1, function(s, w, c) {
2015-02-06 12:56:51 -08:00
server = s;
2015-02-06 10:15:54 -08:00
wallet = w;
2015-02-10 08:20:41 -08:00
copayerPriv = c;
server.createAddress({}, function(err, address) {
2015-02-06 10:15:54 -08:00
helpers.createUtxos(server, wallet, helpers.toSatoshi([1, 2, 3, 4, 5, 6, 7, 8]), function(inutxos) {
utxos = inutxos;
done();
});
});
});
});
it('should sign and broadcast a tx', function(done) {
helpers.stubBlockExplorer(server, utxos, '1122334455');
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(10),
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-06 10:15:54 -08:00
server.createTx(txOpts, function(err, txp) {
should.not.exist(err);
txp.should.exist;
var txpid = txp.id;
2015-02-06 13:22:52 -08:00
server.getPendingTxs({}, function(err, txps) {
2015-02-06 10:15:54 -08:00
var txp = txps[0];
txp.id.should.equal(txpid);
2015-02-10 08:20:41 -08:00
var signatures = helpers.clientSign(txp, TestData.copayers[0].xPrivKey, wallet.n);
2015-02-06 10:15:54 -08:00
server.signTx({
txProposalId: txpid,
signatures: signatures,
}, function(err, txp) {
should.not.exist(err);
txp.status.should.equal('broadcasted');
txp.txid.should.equal('1122334455');
done();
});
});
});
});
it('should keep tx as *accepted* if unable to broadcast it', function(done) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(10),
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-06 10:15:54 -08:00
server.createTx(txOpts, function(err, txp) {
should.not.exist(err);
txp.should.exist;
var txpid = txp.id;
2015-02-06 13:22:52 -08:00
server.getPendingTxs({}, function(err, txps) {
2015-02-06 10:15:54 -08:00
var txp = txps[0];
txp.id.should.equal(txpid);
2015-02-10 08:20:41 -08:00
var signatures = helpers.clientSign(txp, TestData.copayers[0].xPrivKey, wallet.n);
2015-02-06 10:15:54 -08:00
server.signTx({
txProposalId: txpid,
signatures: signatures,
}, function(err, txp) {
err.should.contain('broadcast');
2015-02-06 13:22:52 -08:00
server.getPendingTxs({}, function(err, txps) {
should.not.exist(err);
2015-02-08 06:20:22 -08:00
txps.length.should.equal(1);
var txp = txps[0];
txp.status.should.equal('accepted');
should.not.exist(txp.txid);
done();
2015-02-06 13:22:52 -08:00
});
2015-02-06 10:15:54 -08:00
});
});
});
});
});
2015-02-07 06:15:54 -08:00
2015-02-08 08:16:41 -08:00
describe('Tx proposal workflow', function() {
2015-02-10 08:20:41 -08:00
var server, wallet, copayerPriv, utxos;
beforeEach(function(done) {
2015-02-10 08:20:41 -08:00
helpers.createAndJoinWallet(2, 3, function(s, w, c) {
server = s;
wallet = w;
2015-02-10 08:20:41 -08:00
copayerPriv = c;
server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([1, 2, 3, 4, 5, 6, 7, 8]), function(inutxos) {
utxos = inutxos;
done();
});
});
});
});
it('other copayers should see pending proposal created by one copayer', function(done) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(10),
message: 'some message',
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
server.createTx(txOpts, function(err, txp) {
should.not.exist(err);
should.exist.txp;
helpers.getAuthServer(wallet.copayers[1].id, function(server2, wallet) {
server2.getPendingTxs({}, function(err, txps) {
should.not.exist(err);
txps.length.should.equal(1);
txps[0].id.should.equal(txp.id);
2015-02-08 13:29:58 -08:00
txps[0].message.should.equal('some message');
done();
});
});
});
});
2015-02-07 06:15:54 -08:00
2015-02-08 08:16:41 -08:00
it.skip('tx proposals should not be broadcast until quorum is reached', function(done) {
});
2015-02-07 06:15:54 -08:00
2015-02-07 08:13:29 -08:00
it.skip('tx proposals should accept as many rejections as possible without finally rejecting', function(done) {});
2015-02-07 06:15:54 -08:00
2015-02-07 08:13:29 -08:00
it.skip('proposal creator should be able to delete proposal if there are no other signatures', function(done) {});
2015-02-07 06:15:54 -08:00
});
2015-02-06 23:09:45 -08:00
describe('#getTxs', function() {
2015-02-10 08:20:41 -08:00
var server, wallet, copayerPriv, clock;
2015-02-06 23:09:45 -08:00
beforeEach(function(done) {
if (server)
2015-02-07 09:15:04 -08:00
return done();
this.timeout(5000);
2015-02-06 23:09:45 -08:00
console.log('\tCreating TXS...');
clock = sinon.useFakeTimers();
2015-02-10 08:20:41 -08:00
helpers.createAndJoinWallet(1, 1, function(s, w, c) {
2015-02-06 23:09:45 -08:00
server = s;
wallet = w;
2015-02-10 08:20:41 -08:00
copayerPriv = c;
server.createAddress({}, function(err, address) {
2015-02-06 23:09:45 -08:00
helpers.createUtxos(server, wallet, helpers.toSatoshi(_.range(10)), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(0.1),
};
2015-02-10 08:20:41 -08:00
helpers.addProposalSignature(txOpts, copayerPriv[0].privKey);
2015-02-06 23:09:45 -08:00
async.eachSeries(_.range(10), function(i, next) {
clock.tick(10000);
server.createTx(txOpts, function(err, tx) {
next();
});
}, function(err) {
return done(err);
});
2015-02-06 23:09:45 -08:00
});
});
});
});
afterEach(function() {
clock.restore();
});
it('should pull 4 txs, down to to time 60', function(done) {
server.getTxs({
minTs: 60,
limit: 8
}, function(err, txps) {
should.not.exist(err);
2015-02-07 08:13:29 -08:00
var times = _.pluck(txps, 'createdOn');
times.should.deep.equal([90, 80, 70, 60]);
2015-02-06 23:09:45 -08:00
done();
});
});
it('should pull the first 5 txs', function(done) {
server.getTxs({
maxTs: 50,
limit: 5
}, function(err, txps) {
should.not.exist(err);
2015-02-07 08:13:29 -08:00
var times = _.pluck(txps, 'createdOn');
times.should.deep.equal([50, 40, 30, 20, 10]);
2015-02-06 23:09:45 -08:00
done();
});
});
it('should pull the last 4 txs', function(done) {
server.getTxs({
limit: 4
}, function(err, txps) {
should.not.exist(err);
2015-02-07 08:13:29 -08:00
var times = _.pluck(txps, 'createdOn');
times.should.deep.equal([90, 80, 70, 60]);
2015-02-06 23:09:45 -08:00
done();
});
});
it('should pull all txs', function(done) {
2015-02-07 08:13:29 -08:00
server.getTxs({}, function(err, txps) {
2015-02-06 23:09:45 -08:00
should.not.exist(err);
2015-02-07 08:13:29 -08:00
var times = _.pluck(txps, 'createdOn');
times.should.deep.equal([90, 80, 70, 60, 50, 40, 30, 20, 10]);
2015-02-06 23:09:45 -08:00
done();
});
});
it('should txs from times 50 to 70', function(done) {
server.getTxs({
minTs: 50,
maxTs: 70,
}, function(err, txps) {
should.not.exist(err);
2015-02-07 08:13:29 -08:00
var times = _.pluck(txps, 'createdOn');
times.should.deep.equal([70, 60, 50]);
2015-02-06 23:09:45 -08:00
done();
});
});
});
2015-02-09 13:07:15 -08:00
describe('#removeWallet', function() {
var server, wallet, clock;
beforeEach(function(done) {
helpers.createAndJoinWallet(1, 1, function(s, w) {
server = s;
wallet = w;
server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, helpers.toSatoshi(_.range(2)), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(0.1),
};
async.eachSeries(_.range(2), function(i, next) {
server.createTx(txOpts, function(err, tx) {
next();
});
}, done);
});
});
});
});
it('should delete a wallet', function(done) {
var i = 0;
var count = function() {
return ++i;
};
server.storage._dump(function() {
i.should.above(1);
server.removeWallet({}, function(err) {
i = 0;
server.storage._dump(function() {
i.should.equal(0);
done();
}, count);
});
}, count);
});
// creates 2 wallet, and deletes only 1.
it('should delete a wallet, and only that wallet', function(done) {
var i = 0;
var db = [];
var cat = function(data) {
db.push(data);
};
server.storage._dump(function() {
var before = _.clone(db);
db.length.should.above(1);
helpers.createAndJoinWallet(2, 3, function(s, w) {
server = s;
wallet = w;
server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, helpers.toSatoshi(_.range(2)), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(0.1),
};
async.eachSeries(_.range(2), function(i, next) {
server.createTx(txOpts, function(err, tx) {
next();
});
}, function() {
server.removeWallet({}, function(err) {
db=[];
server.storage._dump(function() {
var after = _.clone(db);
after.should.deep.equal(before);
done();
}, cat);
});
}, cat);
});
});
});
}, cat);
});
});
2015-01-27 05:18:45 -08:00
});