Browse Source

refactor server & tests

activeAddress
Ivan Socolsky 10 years ago
parent
commit
ddbfcbe7f5
  1. 149
      lib/server.js
  2. 335
      test/integration.js

149
lib/server.js

@ -25,25 +25,56 @@ var Address = require('./model/address');
var TxProposal = require('./model/txproposal'); var TxProposal = require('./model/txproposal');
var initialized = false;
var storage;
/** /**
* Creates an instance of the Copay server. * Creates an instance of the Copay server.
* @constructor * @constructor
*/
function CopayServer() {
if (!initialized) throw new Error('Server not initialized');
this.storage = storage;
};
/**
* Initializes global settings for all instances.
* @param {Object} opts * @param {Object} opts
* @param {Storage} [opts.storage] - The storage provider. * @param {Storage} [opts.storage] - The storage provider.
*/ */
function CopayServer(opts) { CopayServer.initialize = function (opts) {
opts = opts || {}; opts = opts || {};
this.storage = opts.storage ||  new Storage(); storage = opts.storage ||  new Storage();
initialized = true;
}; };
inherits(CopayServer, events.EventEmitter); /**
* Gets an instance of the server after authenticating the copayer.
* @param {Object} opts
* @param {string} opts.copayerId - The copayer id making the request.
* @param {string} opts.message - The contents of the request to be signed.
* @param {string} opts.signature - Signature of message to be verified using the copayer's signingPubKey.
*/
CopayServer.getInstanceWithAuth = function (opts, cb) {
Utils.checkRequired(opts, ['copayerId', 'message', 'signature']);
var server = new CopayServer();
server.storage.fetchCopayerLookup(opts.copayerId, function (err, copayer) {
if (err) return cb(err);
if (!copayer) return cb('Copayer not found');
var isValid = server._verifySignature(opts.message, opts.signature, copayer.signingPubKey);
if (!isValid) return cb('Invalid signature');
CopayServer._emit = function(event) { server.copayerId = opts.copayerId;
var args = Array.prototype.slice.call(arguments); server.walletId = copayer.walletId;
log.debug('Emitting: ', args); return cb(null, server);
this.emit.apply(this, arguments); });
}; };
/** /**
* Creates a new wallet. * Creates a new wallet.
* @param {Object} opts * @param {Object} opts
@ -89,13 +120,12 @@ CopayServer.prototype.createWallet = function(opts, cb) {
/** /**
* Retrieves a wallet from storage. * Retrieves a wallet from storage.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.id - The wallet id.
* @returns {Object} wallet * @returns {Object} wallet
*/ */
CopayServer.prototype.getWallet = function(opts, cb) { CopayServer.prototype.getWallet = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchWallet(opts.id, function(err, wallet) { self.storage.fetchWallet(self.walletId, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!wallet) return cb(new ClientError('Wallet not found')); if (!wallet) return cb(new ClientError('Wallet not found'));
return cb(null, wallet); return cb(null, wallet);
@ -128,10 +158,9 @@ CopayServer.prototype.joinWallet = function(opts, cb) {
Utils.checkRequired(opts, ['walletId', 'id', 'name', 'xPubKey', 'xPubKeySignature']); Utils.checkRequired(opts, ['walletId', 'id', 'name', 'xPubKey', 'xPubKeySignature']);
Utils.runLocked(opts.walletId, cb, function(cb) { Utils.runLocked(opts.walletId, cb, function(cb) {
self.getWallet({ self.storage.fetchWallet(opts.walletId, function(err, wallet) {
id: opts.walletId
}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!wallet) return cb(new ClientError('Wallet not found'));
if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) { if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) {
return cb(new ClientError()); return cb(new ClientError());
@ -160,12 +189,8 @@ CopayServer.prototype.joinWallet = function(opts, cb) {
}; };
/** /**
*
* TODO: How this is going to be authenticated?
*
* Creates a new address. * Creates a new address.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {truthy} opts.isChange - Indicates whether this is a regular address or a change address. * @param {truthy} opts.isChange - Indicates whether this is a regular address or a change address.
* @returns {Address} address * @returns {Address} address
*/ */
@ -173,12 +198,10 @@ CopayServer.prototype.createAddress = function(opts, cb) {
var self = this; var self = this;
var isChange = opts.isChange || false; var isChange = opts.isChange || false;
Utils.checkRequired(opts, ['walletId', 'isChange']); Utils.checkRequired(opts, ['isChange']);
Utils.runLocked(opts.walletId, cb, function(cb) { Utils.runLocked(self.walletId, cb, function(cb) {
self.getWallet({ self.getWallet({}, function(err, wallet) {
id: opts.walletId
}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
var address = wallet.createAddress(opts.isChange); var address = wallet.createAddress(opts.isChange);
@ -203,13 +226,12 @@ CopayServer.prototype.createAddress = function(opts, cb) {
/** /**
* Get all addresses. * Get all addresses.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @returns {Address[]} * @returns {Address[]}
*/ */
CopayServer.prototype.getAddresses = function(opts, cb) { CopayServer.prototype.getAddresses = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchAddresses(opts.walletId, function(err, addresses) { self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err); if (err) return cb(err);
return cb(null, addresses); return cb(null, addresses);
@ -219,8 +241,6 @@ CopayServer.prototype.getAddresses = function(opts, cb) {
/** /**
* Verifies that a given message was actually sent by an authorized copayer. * Verifies that a given message was actually sent by an authorized copayer.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {string} opts.copayerId - The wallet id.
* @param {string} opts.message - The message to verify. * @param {string} opts.message - The message to verify.
* @param {string} opts.signature - The signature of message to verify. * @param {string} opts.signature - The signature of message to verify.
* @returns {truthy} The result of the verification. * @returns {truthy} The result of the verification.
@ -228,15 +248,12 @@ CopayServer.prototype.getAddresses = function(opts, cb) {
CopayServer.prototype.verifyMessageSignature = function(opts, cb) { CopayServer.prototype.verifyMessageSignature = function(opts, cb) {
var self = this; var self = this;
Utils.checkRequired(opts, ['walletId', 'copayerId', 'message', 'signature']); Utils.checkRequired(opts, ['message', 'signature']);
self.getWallet({ self.getWallet({}, function(err, wallet) {
id: opts.walletId
}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
var copayer = wallet.getCopayer(opts.copayerId); var copayer = wallet.getCopayer(self.copayerId);
if (!copayer) return cb(new ClientError('Copayer not found'));
var isValid = self._verifySignature(opts.message, opts.signature, copayer.signingPubKey); var isValid = self._verifySignature(opts.message, opts.signature, copayer.signingPubKey);
return cb(null, isValid); return cb(null, isValid);
@ -267,14 +284,13 @@ CopayServer.prototype._getBlockExplorer = function(provider, network) {
/** /**
* _getUtxos * _getUtxos
* *
* @param opts.walletId
*/ */
CopayServer.prototype._getUtxos = function(opts, cb) { CopayServer.prototype._getUtxos = function(cb) {
var self = this; var self = this;
// Get addresses for this wallet // Get addresses for this wallet
self.storage.fetchAddresses(opts.walletId, function(err, addresses) { self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err); if (err) return cb(err);
if (addresses.length == 0) return cb(new ClientError('The wallet has no addresses')); if (addresses.length == 0) return cb(new ClientError('The wallet has no addresses'));
@ -286,9 +302,7 @@ CopayServer.prototype._getUtxos = function(opts, cb) {
bc.getUnspentUtxos(addressStrs, function(err, utxos) { bc.getUnspentUtxos(addressStrs, function(err, utxos) {
if (err) return cb(err); if (err) return cb(err);
self.getPendingTxs({ self.getPendingTxs({}, function(err, txps) {
walletId: opts.walletId
}, function(err, txps) {
if (err) return cb(err); if (err) return cb(err);
var inputs = _.chain(txps) var inputs = _.chain(txps)
@ -326,17 +340,12 @@ CopayServer.prototype._getUtxos = function(opts, cb) {
/** /**
* Creates a new transaction proposal. * Creates a new transaction proposal.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @returns {Object} balance - Total amount & locked amount. * @returns {Object} balance - Total amount & locked amount.
*/ */
CopayServer.prototype.getBalance = function(opts, cb) { CopayServer.prototype.getBalance = function(opts, cb) {
var self = this; var self = this;
Utils.checkRequired(opts, 'walletId'); self._getUtxos(function(err, utxos) {
self._getUtxos({
walletId: opts.walletId
}, function(err, utxos) {
if (err) return cb(err); if (err) return cb(err);
var balance = {}; var balance = {};
@ -355,7 +364,7 @@ CopayServer.prototype.getBalance = function(opts, cb) {
}; };
// TODO: should be in Utils
CopayServer.prototype._inputSatoshis = function(i) { CopayServer.prototype._inputSatoshis = function(i) {
return i.amount ? Utils.strip(i.amount * 1e8) : i.satoshis; return i.amount ? Utils.strip(i.amount * 1e8) : i.satoshis;
}; };
@ -383,8 +392,6 @@ CopayServer.prototype._selectUtxos = function(txp, utxos) {
/** /**
* Creates a new transaction proposal. * Creates a new transaction proposal.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {string} opts.copayerId - The wallet id.
* @param {string} opts.toAddress - Destination address. * @param {string} opts.toAddress - Destination address.
* @param {number} opts.amount - Amount to transfer in satoshi. * @param {number} opts.amount - Amount to transfer in satoshi.
* @param {string} opts.message - A message to attach to this transaction. * @param {string} opts.message - A message to attach to this transaction.
@ -393,21 +400,17 @@ CopayServer.prototype._selectUtxos = function(txp, utxos) {
CopayServer.prototype.createTx = function(opts, cb) { CopayServer.prototype.createTx = function(opts, cb) {
var self = this; var self = this;
Utils.checkRequired(opts, ['walletId', 'copayerId', 'toAddress', 'amount', 'message']); Utils.checkRequired(opts, ['toAddress', 'amount', 'message']);
// TODO? // TODO?
// Check some parameters like: // Check some parameters like:
// amount > dust // amount > dust
self.getWallet({ self.getWallet({}, function(err, wallet) {
id: opts.walletId
}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
self._getUtxos({ self._getUtxos(function(err, utxos) {
walletId: wallet.id
}, function(err, utxos) {
if (err) return cb(err); if (err) return cb(err);
var changeAddress = wallet.createAddress(true).address; var changeAddress = wallet.createAddress(true).address;
@ -417,7 +420,7 @@ CopayServer.prototype.createTx = function(opts, cb) {
}); });
var txp = new TxProposal({ var txp = new TxProposal({
creatorId: opts.copayerId, creatorId: self.copayerId,
toAddress: opts.toAddress, toAddress: opts.toAddress,
amount: opts.amount, amount: opts.amount,
changeAddress: changeAddress, changeAddress: changeAddress,
@ -446,14 +449,13 @@ CopayServer.prototype.createTx = function(opts, cb) {
/** /**
* Retrieves a tx from storage. * Retrieves a tx from storage.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {string} opts.id - The tx id. * @param {string} opts.id - The tx id.
* @returns {Object} txProposal * @returns {Object} txProposal
*/ */
CopayServer.prototype.getTx = function(opts, cb) { CopayServer.prototype.getTx = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchTx(opts.walletId, opts.id, function(err, txp) { self.storage.fetchTx(self.walletId, opts.id, function(err, txp) {
if (err) return cb(err); if (err) return cb(err);
if (!txp) return cb(new ClientError('Transaction proposal not found')); if (!txp) return cb(new ClientError('Transaction proposal not found'));
return cb(null, txp); return cb(null, txp);
@ -471,23 +473,18 @@ CopayServer.prototype._broadcastTx = function(txp, cb) {
/** /**
* Sign a transaction proposal. * Sign a transaction proposal.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {string} opts.copayerId - The wallet id.
* @param {string} opts.txProposalId - The identifier of the transaction. * @param {string} opts.txProposalId - The identifier of the transaction.
* @param {string} opts.signatures - The signatures of the inputs of this tx for this copayer (in apperance order) * @param {string} opts.signatures - The signatures of the inputs of this tx for this copayer (in apperance order)
*/ */
CopayServer.prototype.signTx = function(opts, cb) { CopayServer.prototype.signTx = function(opts, cb) {
var self = this; var self = this;
Utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId', 'signatures']); Utils.checkRequired(opts, ['txProposalId', 'signatures']);
self.getWallet({ self.getWallet({}, function(err, wallet) {
id: opts.walletId
}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
self.getTx({ self.getTx({
walletId: opts.walletId,
id: opts.txProposalId id: opts.txProposalId
}, function(err, txp) { }, function(err, txp) {
if (err) return cb(err); if (err) return cb(err);
@ -500,22 +497,22 @@ CopayServer.prototype.signTx = function(opts, cb) {
if (txp.status != 'pending') if (txp.status != 'pending')
return cb(new ClientError('TXNOTPENDING', 'The transaction proposal is not pending')); return cb(new ClientError('TXNOTPENDING', 'The transaction proposal is not pending'));
var copayer = wallet.getCopayer(opts.copayerId); var copayer = wallet.getCopayer(self.copayerId);
if (!txp.checkSignatures(opts.signatures, copayer.xPubKey)) if (!txp.checkSignatures(opts.signatures, copayer.xPubKey))
return cb(new ClientError('BADSIGNATURES', 'Bad signatures')); return cb(new ClientError('BADSIGNATURES', 'Bad signatures'));
txp.sign(opts.copayerId, opts.signatures); txp.sign(self.copayerId, opts.signatures);
self.storage.storeTx(opts.walletId, txp, function(err) { self.storage.storeTx(self.walletId, txp, function(err) {
if (err) return cb(err); if (err) return cb(err);
if (txp.status == 'accepted') { if (txp.status == 'accepted') {
self._broadcastTx(txp, function(err, txid) { self._broadcastTx(txp, function(err, txid) {
if (err) return cb(err, txp); if (err) return cb(err);
txp.setBroadcasted(txid); txp.setBroadcasted(txid);
self.storage.storeTx(opts.walletId, txp, function(err) { self.storage.storeTx(self.walletId, txp, function(err) {
if (err) return cb(err); if (err) return cb(err);
return cb(null, txp); return cb(null, txp);
@ -532,31 +529,28 @@ CopayServer.prototype.signTx = function(opts, cb) {
/** /**
* Reject a transaction proposal. * Reject a transaction proposal.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {string} opts.copayerId - The wallet id.
* @param {string} opts.txProposalId - The identifier of the transaction. * @param {string} opts.txProposalId - The identifier of the transaction.
* @param {string} [opts.reason] - A message to other copayers explaining the rejection. * @param {string} [opts.reason] - A message to other copayers explaining the rejection.
*/ */
CopayServer.prototype.rejectTx = function(opts, cb) { CopayServer.prototype.rejectTx = function(opts, cb) {
var self = this; var self = this;
Utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId']); Utils.checkRequired(opts, ['txProposalId']);
self.getTx({ self.getTx({
walletId: opts.walletId,
id: opts.txProposalId id: opts.txProposalId
}, function(err, txp) { }, function(err, txp) {
if (err) return cb(err); if (err) return cb(err);
if (!txp) return cb(new ClientError('Transaction proposal not found')); if (!txp) return cb(new ClientError('Transaction proposal not found'));
var action = _.find(txp.actions, { var action = _.find(txp.actions, {
copayerId: opts.copayerId copayerId: self.copayerId
}); });
if (action) return cb(new ClientError('CVOTED', 'Copayer already voted on this transaction proposal')); if (action) return cb(new ClientError('CVOTED', 'Copayer already voted on this transaction proposal'));
if (txp.status != 'pending') return cb(new ClientError('TXNOTPENDING', 'The transaction proposal is not pending')); if (txp.status != 'pending') return cb(new ClientError('TXNOTPENDING', 'The transaction proposal is not pending'));
txp.reject(opts.copayerId); txp.reject(self.copayerId);
self.storage.storeTx(opts.walletId, txp, function(err) { self.storage.storeTx(self.walletId, txp, function(err) {
if (err) return cb(err); if (err) return cb(err);
return cb(); return cb();
@ -567,15 +561,12 @@ CopayServer.prototype.rejectTx = function(opts, cb) {
/** /**
* Retrieves all pending transaction proposals. * Retrieves all pending transaction proposals.
* @param {Object} opts * @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @returns {TxProposal[]} Transaction proposal. * @returns {TxProposal[]} Transaction proposal.
*/ */
CopayServer.prototype.getPendingTxs = function(opts, cb) { CopayServer.prototype.getPendingTxs = function(opts, cb) {
var self = this; var self = this;
Utils.checkRequired(opts, 'walletId'); self.storage.fetchTxs(self.walletId, function(err, txps) {
self.storage.fetchTxs(opts.walletId, function(err, txps) {
if (err) return cb(err); if (err) return cb(err);
var pending = _.filter(txps, { var pending = _.filter(txps, {

335
test/integration.js

@ -60,7 +60,22 @@ var aTextSignature = '3045022100addd20e5413865d65d561ad2979f2289a40d52594b1f8048
var helpers = {}; var helpers = {};
helpers.getAuthServer = function (copayerId, cb) {
var signatureStub = sinon.stub(CopayServer.prototype, '_verifySignature');
signatureStub.returns(true);
CopayServer.getInstanceWithAuth({
copayerId: copayerId,
message: 'dummy',
signature: 'dummy',
}, function (err, server) {
signatureStub.restore();
return cb(server);
});
};
helpers.createAndJoinWallet = function(id, m, n, cb) { helpers.createAndJoinWallet = function(id, m, n, cb) {
var server = new CopayServer();
var walletOpts = { var walletOpts = {
id: id, id: id,
name: id + ' wallet', name: id + ' wallet',
@ -84,14 +99,13 @@ helpers.createAndJoinWallet = function(id, m, n, cb) {
server.joinWallet(copayerOpts, function(err) { server.joinWallet(copayerOpts, function(err) {
return cb(err); return cb(err);
}); });
}, function(err) { }, function (err) {
if (err) return cb(err); if (err) return new Error('Could not generate wallet');
server.getWallet({ helpers.getAuthServer('1', function (s) {
id: id, s.getWallet({}, function (err, w) {
includeCopayers: true cb(s, w);
}, function(err, wallet) { });
return cb(err, wallet);
}); });
}); });
}); });
@ -114,10 +128,8 @@ helpers.toSatoshi = function(btc) {
helpers.createUtxos = function(server, wallet, amounts, cb) { helpers.createUtxos = function(server, wallet, amounts, cb) {
var addresses = []; var addresses = [];
async.each(amounts, function(a, next) { async.each(amounts, function(a, next) {
server.createAddress({ server.createAddress({
walletId: wallet.id,
isChange: false, isChange: false,
}, function(err, address) { }, function(err, address) {
addresses.push(address); addresses.push(address);
@ -191,7 +203,6 @@ helpers.clientSign = function(tx, xpriv, n) {
}; };
var db, storage; var db, storage;
var server;
describe('Copay server', function() { describe('Copay server', function() {
@ -202,96 +213,27 @@ describe('Copay server', function() {
storage = new Storage({ storage = new Storage({
db: db db: db
}); });
CopayServer.initialize({ storage: storage });
}); });
describe('#getWallet', function() { describe.skip('#getInstanceWithAuth', function() {
beforeEach(function() { beforeEach(function() {
server = new CopayServer({
storage: storage,
});
}); });
it('should get existing wallet', function(done) { it('should get server instance for existing copayer', function(done) {
});
var w1 = new Wallet({
id: '123',
name: 'my wallet',
m: 2,
n: 3,
pubKey: aPubKey,
});
var w2 = new Wallet({
id: '234',
name: 'my wallet 2',
m: 3,
n: 4,
pubKey: aPubKey,
});
db.batch([{
type: 'put',
key: 'wallet-123',
value: w1,
}, {
type: 'put',
key: 'wallet-234',
value: w2,
}]);
server.getWallet({ it('should fail when requesting for non-existent copayer', function(done) {
id: '123',
includeCopayers: true
}, function(err, wallet) {
should.not.exist(err);
wallet.id.should.equal('123');
wallet.name.should.equal('my wallet');
wallet.status.should.equal('pending');
wallet.copayers.length.should.equal(0);
done();
});
}); });
it('should fail when requesting non-existent wallet', function(done) { it('should fail when message signature cannot be verified', function(done) {
var w1 = new Wallet({
id: '123',
name: 'my wallet',
m: 2,
n: 3,
pubKey: aPubKey,
});
var w2 = new Wallet({
id: '234',
name: 'my wallet 2',
m: 3,
n: 4,
pubKey: aPubKey,
});
db.batch([{
type: 'put',
key: 'wallet-123',
value: w1,
}, {
type: 'put',
key: 'wallet-234',
value: w2,
}]);
server.getWallet({
id: '345'
}, function(err, wallet) {
should.exist(err);
err.message.should.equal('Wallet not found');
done();
});
}); });
}); });
describe('#createWallet', function() { describe('#createWallet', function() {
var server;
beforeEach(function() { beforeEach(function() {
server = new CopayServer({ server = new CopayServer();
storage: storage,
});
}); });
it('should create and store wallet', function(done) { it('should create and store wallet', function(done) {
@ -304,9 +246,7 @@ describe('Copay server', function() {
}; };
server.createWallet(opts, function(err) { server.createWallet(opts, function(err) {
should.not.exist(err); should.not.exist(err);
server.getWallet({ server.storage.fetchWallet('123', function(err, wallet) {
id: '123'
}, function(err, wallet) {
should.not.exist(err); should.not.exist(err);
wallet.id.should.equal('123'); wallet.id.should.equal('123');
wallet.name.should.equal('my wallet'); wallet.name.should.equal('my wallet');
@ -325,9 +265,7 @@ describe('Copay server', function() {
}; };
server.createWallet(opts, function(err) { server.createWallet(opts, function(err) {
should.not.exist(err); should.not.exist(err);
server.getWallet({ server.storage.fetchWallet('123', function(err, wallet) {
id: '123'
}, function(err, wallet) {
should.not.exist(err); should.not.exist(err);
wallet.id.should.equal('123'); wallet.id.should.equal('123');
wallet.name.should.equal('my wallet'); wallet.name.should.equal('my wallet');
@ -379,10 +317,9 @@ describe('Copay server', function() {
}); });
describe('#joinWallet', function() { describe('#joinWallet', function() {
var server;
beforeEach(function() { beforeEach(function() {
server = new CopayServer({ server = new CopayServer();
storage: storage,
});
}); });
it('should join existing wallet', function(done) { it('should join existing wallet', function(done) {
@ -405,16 +342,15 @@ describe('Copay server', function() {
}; };
server.joinWallet(copayerOpts, function(err) { server.joinWallet(copayerOpts, function(err) {
should.not.exist(err); should.not.exist(err);
server.getWallet({ helpers.getAuthServer('999', function (server) {
id: '123', server.getWallet({}, function(err, wallet) {
includeCopayers: true wallet.id.should.equal('123');
}, function(err, wallet) { wallet.copayers.length.should.equal(1);
wallet.id.should.equal('123'); var copayer = wallet.copayers[0];
wallet.copayers.length.should.equal(1); copayer.id.should.equal('999');
var copayer = wallet.copayers[0]; copayer.name.should.equal('me');
copayer.id.should.equal('999'); done();
copayer.name.should.equal('me'); });
done();
}); });
}); });
}); });
@ -470,15 +406,15 @@ describe('Copay server', function() {
}; };
server.joinWallet(copayer1Opts, function(err) { server.joinWallet(copayer1Opts, function(err) {
should.not.exist(err); should.not.exist(err);
server.getWallet({ helpers.getAuthServer('111', function (server) {
id: '123' server.getWallet({}, function(err, wallet) {
}, function(err, wallet) { wallet.status.should.equal('complete');
wallet.status.should.equal('complete'); server.joinWallet(copayer2Opts, function(err) {
server.joinWallet(copayer2Opts, function(err) { should.exist(err);
should.exist(err); err.code.should.equal('WFULL');
err.code.should.equal('WFULL'); err.message.should.equal('Wallet full');
err.message.should.equal('Wallet full'); done();
done(); });
}); });
}); });
}); });
@ -590,10 +526,8 @@ describe('Copay server', function() {
}); });
it('should set pkr and status = complete on last copayer joining (2-3)', function(done) { it('should set pkr and status = complete on last copayer joining (2-3)', function(done) {
helpers.createAndJoinWallet('123', 2, 3, function(err, wallet) { helpers.createAndJoinWallet('123', 2, 3, function(server) {
server.getWallet({ server.getWallet({}, function(err, wallet) {
id: '123'
}, function(err, wallet) {
should.not.exist(err); should.not.exist(err);
wallet.status.should.equal('complete'); wallet.status.should.equal('complete');
wallet.publicKeyRing.length.should.equal(3); wallet.publicKeyRing.length.should.equal(3);
@ -606,16 +540,11 @@ describe('Copay server', function() {
describe('#verifyMessageSignature', function() { describe('#verifyMessageSignature', function() {
beforeEach(function() { beforeEach(function() {
server = new CopayServer({
storage: storage,
});
}); });
it('should successfully verify message signature', function(done) { it('should successfully verify message signature', function(done) {
helpers.createAndJoinWallet('123', 2, 2, function(err, wallet) { helpers.createAndJoinWallet('123', 2, 2, function(server) {
var opts = { var opts = {
walletId: '123',
copayerId: '1',
message: aText, message: aText,
signature: aTextSignature, signature: aTextSignature,
}; };
@ -627,17 +556,18 @@ describe('Copay server', function() {
}); });
}); });
it('should fail to verify message signature when copayer does not exist', function(done) { it('should fail to verify message signature for different copayer', function(done) {
helpers.createAndJoinWallet('123', 2, 2, function(err, wallet) { helpers.createAndJoinWallet('123', 2, 2, function() {
var opts = { var opts = {
walletId: '123', message: aText,
copayerId: '999', signature: aTextSignature,
message: 'hello world',
signature: 'dummy',
}; };
server.verifyMessageSignature(opts, function(err, isValid) { helpers.getAuthServer('2', function (server) {
err.message.should.equal('Copayer not found'); server.verifyMessageSignature(opts, function(err, isValid) {
done(); should.not.exist(err);
isValid.should.be.false;
done();
});
}); });
}); });
}); });
@ -645,15 +575,11 @@ describe('Copay server', function() {
describe('#createAddress', function() { describe('#createAddress', function() {
beforeEach(function() { beforeEach(function() {
server = new CopayServer({
storage: storage,
});
}); });
it('should create main address', function(done) { it('should create main address', function(done) {
helpers.createAndJoinWallet('123', 2, 2, function(err, wallet) { helpers.createAndJoinWallet('123', 2, 2, function(server) {
server.createAddress({ server.createAddress({
walletId: '123',
isChange: false, isChange: false,
}, function(err, address) { }, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -667,9 +593,8 @@ describe('Copay server', function() {
it('should create change address', function(done) { it('should create change address', function(done) {
helpers.createAndJoinWallet('123', 2, 2, function(err, wallet) { helpers.createAndJoinWallet('123', 2, 2, function(server) {
server.createAddress({ server.createAddress({
walletId: '123',
isChange: true, isChange: true,
}, function(err, address) { }, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -682,10 +607,9 @@ describe('Copay server', function() {
}); });
it('should create many addresses on simultaneous requests', function(done) { it('should create many addresses on simultaneous requests', function(done) {
helpers.createAndJoinWallet('123', 2, 2, function(err, wallet) { helpers.createAndJoinWallet('123', 2, 2, function(server) {
async.map(_.range(10), function(i, cb) { async.map(_.range(10), function(i, cb) {
server.createAddress({ server.createAddress({
walletId: '123',
isChange: false, isChange: false,
}, cb); }, cb);
}, function(err, addresses) { }, function(err, addresses) {
@ -700,26 +624,22 @@ describe('Copay server', function() {
}); });
it('should not create address if unable to store wallet', function(done) { it('should not create address if unable to store wallet', function(done) {
helpers.createAndJoinWallet('123', 2, 2, function(err, wallet) { helpers.createAndJoinWallet('123', 2, 2, function(server) {
var storeWalletStub = sinon.stub(server.storage, 'storeWallet'); var storeWalletStub = sinon.stub(server.storage, 'storeWallet');
storeWalletStub.yields('dummy error'); storeWalletStub.yields('dummy error');
server.createAddress({ server.createAddress({
walletId: '123',
isChange: true, isChange: true,
}, function(err, address) { }, function(err, address) {
err.should.exist; err.should.exist;
should.not.exist(address); should.not.exist(address);
server.getAddresses({ server.getAddresses({}, function(err, addresses) {
walletId: '123'
}, function(err, addresses) {
addresses.length.should.equal(0); addresses.length.should.equal(0);
server.storage.storeWallet.restore(); server.storage.storeWallet.restore();
server.createAddress({ server.createAddress({
walletId: '123',
isChange: true, isChange: true,
}, function(err, address) { }, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -734,26 +654,22 @@ describe('Copay server', function() {
}); });
it('should not create address if unable to store addresses', function(done) { it('should not create address if unable to store addresses', function(done) {
helpers.createAndJoinWallet('123', 2, 2, function(err, wallet) { helpers.createAndJoinWallet('123', 2, 2, function(server) {
var storeAddressStub = sinon.stub(server.storage, 'storeAddress'); var storeAddressStub = sinon.stub(server.storage, 'storeAddress');
storeAddressStub.yields('dummy error'); storeAddressStub.yields('dummy error');
server.createAddress({ server.createAddress({
walletId: '123',
isChange: true, isChange: true,
}, function(err, address) { }, function(err, address) {
err.should.exist; err.should.exist;
should.not.exist(address); should.not.exist(address);
server.getAddresses({ server.getAddresses({}, function(err, addresses) {
walletId: '123'
}, function(err, addresses) {
addresses.length.should.equal(0); addresses.length.should.equal(0);
server.storage.storeAddress.restore(); server.storage.storeAddress.restore();
server.createAddress({ server.createAddress({
walletId: '123',
isChange: true, isChange: true,
}, function(err, address) { }, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -769,15 +685,12 @@ describe('Copay server', function() {
}); });
describe('#createTx', function() { describe('#createTx', function() {
var wallet; var server, wallet;
beforeEach(function(done) { beforeEach(function(done) {
server = new CopayServer({ helpers.createAndJoinWallet('123', 2, 2, function(s, w) {
storage: storage, server = s;
});
helpers.createAndJoinWallet('123', 2, 2, function(err, w) {
wallet = w; wallet = w;
server.createAddress({ server.createAddress({
walletId: '123',
isChange: false, isChange: false,
}, function(err, address) { }, function(err, address) {
done(); done();
@ -786,14 +699,9 @@ describe('Copay server', function() {
}); });
it('should create tx', function(done) { it('should create tx', function(done) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([100, 200]), function(utxos) { helpers.createUtxos(server, wallet, helpers.toSatoshi([100, 200]), function(utxos) {
helpers.stubBlockExplorer(server, utxos); helpers.stubBlockExplorer(server, utxos);
var txOpts = { var txOpts = {
copayerId: '1',
walletId: '123',
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(80), amount: helpers.toSatoshi(80),
message: 'some message', message: 'some message',
@ -806,14 +714,10 @@ describe('Copay server', function() {
tx.should.exist; tx.should.exist;
tx.isAccepted().should.equal.false; tx.isAccepted().should.equal.false;
tx.isRejected().should.equal.false; tx.isRejected().should.equal.false;
server.getPendingTxs({ server.getPendingTxs({}, function(err, txs) {
walletId: '123'
}, function(err, txs) {
should.not.exist(err); should.not.exist(err);
txs.length.should.equal(1); txs.length.should.equal(1);
server.getBalance({ server.getBalance({}, function(err, balance) {
walletId: '123'
}, function(err, balance) {
should.not.exist(err); should.not.exist(err);
balance.totalAmount.should.equal(helpers.toSatoshi(300)); balance.totalAmount.should.equal(helpers.toSatoshi(300));
balance.lockedAmount.should.equal(helpers.toSatoshi(100)); balance.lockedAmount.should.equal(helpers.toSatoshi(100));
@ -825,13 +729,9 @@ describe('Copay server', function() {
}); });
it('should fail to create tx when insufficient funds', function(done) { it('should fail to create tx when insufficient funds', function(done) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([100]), function(utxos) { helpers.createUtxos(server, wallet, helpers.toSatoshi([100]), function(utxos) {
helpers.stubBlockExplorer(server, utxos); helpers.stubBlockExplorer(server, utxos);
var txOpts = { var txOpts = {
copayerId: '1',
walletId: '123',
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(120), amount: helpers.toSatoshi(120),
message: 'some message', message: 'some message',
@ -842,14 +742,10 @@ describe('Copay server', function() {
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
err.code.should.equal('INSUFFICIENTFUNDS'); err.code.should.equal('INSUFFICIENTFUNDS');
err.message.should.equal('Insufficient funds'); err.message.should.equal('Insufficient funds');
server.getPendingTxs({ server.getPendingTxs({}, function(err, txs) {
walletId: '123'
}, function(err, txs) {
should.not.exist(err); should.not.exist(err);
txs.length.should.equal(0); txs.length.should.equal(0);
server.getBalance({ server.getBalance({}, function(err, balance) {
walletId: '123'
}, function(err, balance) {
should.not.exist(err); should.not.exist(err);
balance.lockedAmount.should.equal(0); balance.lockedAmount.should.equal(0);
balance.totalAmount.should.equal(10000000000); balance.totalAmount.should.equal(10000000000);
@ -861,13 +757,9 @@ describe('Copay server', function() {
}); });
it('should create tx when there is a pending tx and enough UTXOs', function(done) { it('should create tx when there is a pending tx and enough UTXOs', function(done) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([10.1, 10.2, 10.3]), function(utxos) { helpers.createUtxos(server, wallet, helpers.toSatoshi([10.1, 10.2, 10.3]), function(utxos) {
helpers.stubBlockExplorer(server, utxos); helpers.stubBlockExplorer(server, utxos);
var txOpts = { var txOpts = {
copayerId: '1',
walletId: '123',
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(12), amount: helpers.toSatoshi(12),
message: 'some message', message: 'some message',
@ -879,8 +771,6 @@ describe('Copay server', function() {
tx.should.exist; tx.should.exist;
var txOpts2 = { var txOpts2 = {
copayerId: '1',
walletId: '123',
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: 8, amount: 8,
message: 'some message 2', message: 'some message 2',
@ -890,14 +780,10 @@ describe('Copay server', function() {
server.createTx(txOpts2, function(err, tx) { server.createTx(txOpts2, function(err, tx) {
should.not.exist(err); should.not.exist(err);
tx.should.exist; tx.should.exist;
server.getPendingTxs({ server.getPendingTxs({}, function(err, txs) {
walletId: '123'
}, function(err, txs) {
should.not.exist(err); should.not.exist(err);
txs.length.should.equal(2); txs.length.should.equal(2);
server.getBalance({ server.getBalance({}, function(err, balance) {
walletId: '123'
}, function(err, balance) {
should.not.exist(err); should.not.exist(err);
balance.totalAmount.should.equal(3060000000); balance.totalAmount.should.equal(3060000000);
balance.lockedAmount.should.equal(3060000000); balance.lockedAmount.should.equal(3060000000);
@ -911,11 +797,8 @@ describe('Copay server', function() {
it('should fail to create tx when there is a pending tx and not enough UTXOs', function(done) { it('should fail to create tx when there is a pending tx and not enough UTXOs', function(done) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([10.1, 10.2, 10.3]), function(utxos) { helpers.createUtxos(server, wallet, helpers.toSatoshi([10.1, 10.2, 10.3]), function(utxos) {
helpers.stubBlockExplorer(server, utxos); helpers.stubBlockExplorer(server, utxos);
var txOpts = { var txOpts = {
copayerId: '1',
walletId: '123',
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(12), amount: helpers.toSatoshi(12),
message: 'some message', message: 'some message',
@ -927,8 +810,6 @@ describe('Copay server', function() {
tx.should.exist; tx.should.exist;
var txOpts2 = { var txOpts2 = {
copayerId: '1',
walletId: '123',
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(24), amount: helpers.toSatoshi(24),
message: 'some message 2', message: 'some message 2',
@ -939,14 +820,10 @@ describe('Copay server', function() {
err.code.should.equal('INSUFFICIENTFUNDS'); err.code.should.equal('INSUFFICIENTFUNDS');
err.message.should.equal('Insufficient funds'); err.message.should.equal('Insufficient funds');
should.not.exist(tx); should.not.exist(tx);
server.getPendingTxs({ server.getPendingTxs({}, function(err, txs) {
walletId: '123'
}, function(err, txs) {
should.not.exist(err); should.not.exist(err);
txs.length.should.equal(1); txs.length.should.equal(1);
server.getBalance({ server.getBalance({}, function(err, balance) {
walletId: '123'
}, function(err, balance) {
should.not.exist(err); should.not.exist(err);
balance.totalAmount.should.equal(helpers.toSatoshi(30.6)); balance.totalAmount.should.equal(helpers.toSatoshi(30.6));
balance.lockedAmount.should.equal(helpers.toSatoshi(20.3)); balance.lockedAmount.should.equal(helpers.toSatoshi(20.3));
@ -961,23 +838,18 @@ describe('Copay server', function() {
}); });
describe('#signTx', function() { describe('#signTx', function() {
var wallet, txid; var server, wallet, txid;
beforeEach(function(done) { beforeEach(function(done) {
server = new CopayServer({ helpers.createAndJoinWallet('123', 2, 2, function(s, w) {
storage: storage, server = s;
});
helpers.createAndJoinWallet('123', 2, 2, function(err, w) {
wallet = w; wallet = w;
server.createAddress({ server.createAddress({
walletId: '123',
isChange: false, isChange: false,
}, function(err, address) { }, function(err, address) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([1, 2, 3, 4, 5, 6, 7, 8]), function(utxos) { helpers.createUtxos(server, wallet, helpers.toSatoshi([1, 2, 3, 4, 5, 6, 7, 8]), function(utxos) {
helpers.stubBlockExplorer(server, utxos); helpers.stubBlockExplorer(server, utxos);
var txOpts = { var txOpts = {
copayerId: '1',
walletId: '123',
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(10), amount: helpers.toSatoshi(10),
message: 'some message', message: 'some message',
@ -995,17 +867,13 @@ describe('Copay server', function() {
}); });
}); });
it('should sign a TX with multiple inputs, different paths', function(done) { it('should sign a TX with multiple inputs, different paths', function(done) {
server.getPendingTxs({ server.getPendingTxs({}, function(err, txs) {
walletId: '123'
}, function(err, txs) {
var tx = txs[0]; var tx = txs[0];
tx.id.should.equal(txid); tx.id.should.equal(txid);
var signatures = helpers.clientSign(tx, someXPrivKey[0], wallet.n); var signatures = helpers.clientSign(tx, someXPrivKey[0], wallet.n);
server.signTx({ server.signTx({
walletId: '123',
copayerId: '1',
txProposalId: txid, txProposalId: txid,
signatures: signatures, signatures: signatures,
}, function(err) { }, function(err) {
@ -1015,10 +883,8 @@ describe('Copay server', function() {
}); });
}); });
it('should fail if one signature is broken', function(done) { it('should fail if one signature is broken', function(done) {
server.getPendingTxs({ server.getPendingTxs({}, function(err, txs) {
walletId: '123'
}, function(err, txs) {
var tx = txs[0]; var tx = txs[0];
tx.id.should.equal(txid); tx.id.should.equal(txid);
@ -1026,8 +892,6 @@ describe('Copay server', function() {
signatures[0] = 1; signatures[0] = 1;
server.signTx({ server.signTx({
walletId: '123',
copayerId: '1',
txProposalId: txid, txProposalId: txid,
signatures: signatures, signatures: signatures,
}, function(err) { }, function(err) {
@ -1036,17 +900,13 @@ describe('Copay server', function() {
}); });
}); });
}); });
it('should fail on invalids signature', function(done) { it('should fail on invalids signature', function(done) {
server.getPendingTxs({ server.getPendingTxs({}, function(err, txs) {
walletId: '123'
}, function(err, txs) {
var tx = txs[0]; var tx = txs[0];
tx.id.should.equal(txid); tx.id.should.equal(txid);
var signatures = ['11', '22', '33', '44']; var signatures = ['11', '22', '33', '44'];
server.signTx({ server.signTx({
walletId: '123',
copayerId: '1',
txProposalId: txid, txProposalId: txid,
signatures: signatures, signatures: signatures,
}, function(err) { }, function(err) {
@ -1060,16 +920,13 @@ describe('Copay server', function() {
describe('#signTx and broadcast', function() { describe('#signTx and broadcast', function() {
var wallet, utxos; var server, wallet, utxos;
beforeEach(function(done) { beforeEach(function(done) {
server = new CopayServer({ helpers.createAndJoinWallet('123', 1, 1, function(s, w) {
storage: storage, server = s;
});
helpers.createAndJoinWallet('123', 1, 1, function(err, w) {
wallet = w; wallet = w;
server.createAddress({ server.createAddress({
walletId: '123',
isChange: false, isChange: false,
}, function(err, address) { }, function(err, address) {
helpers.createUtxos(server, wallet, helpers.toSatoshi([1, 2, 3, 4, 5, 6, 7, 8]), function(inutxos) { helpers.createUtxos(server, wallet, helpers.toSatoshi([1, 2, 3, 4, 5, 6, 7, 8]), function(inutxos) {
@ -1142,8 +999,6 @@ describe('Copay server', function() {
txp.id.should.equal(txpid); txp.id.should.equal(txpid);
var signatures = helpers.clientSign(txp, someXPrivKey[0], wallet.n); var signatures = helpers.clientSign(txp, someXPrivKey[0], wallet.n);
server.signTx({ server.signTx({
walletId: '123',
copayerId: '1',
txProposalId: txpid, txProposalId: txpid,
signatures: signatures, signatures: signatures,
}, function(err, txp) { }, function(err, txp) {

Loading…
Cancel
Save