diff --git a/lib/expressapp.js b/lib/expressapp.js index 07f9de3..71f920f 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -7,7 +7,7 @@ var express = require('express'); var querystring = require('querystring'); var bodyParser = require('body-parser') -var CopayServer = require('./server'); +var WalletService = require('./server'); log.debug = log.verbose; log.level = 'debug'; @@ -18,7 +18,7 @@ ExpressApp.start = function(opts) { opts = opts || {}; - CopayServer.initialize(opts.CopayServer); + WalletService.initialize(opts.WalletService); var app = express(); app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); @@ -48,7 +48,7 @@ ExpressApp.start = function(opts) { var router = express.Router(); function returnError(err, res, req) { - if (err instanceof CopayServer.ClientError) { + if (err instanceof WalletService.ClientError) { var status = (err.code == 'NOTAUTHORIZED') ? 401 : 400; log.error('Err: ' + status + ':' + req.url + ' :' + err.code + ':' + err.message); @@ -84,7 +84,7 @@ ExpressApp.start = function(opts) { function getServerWithAuth(req, res, cb) { var credentials = getCredentials(req); if (!credentials) - return returnError(new CopayServer.ClientError({ + return returnError(new WalletService.ClientError({ code: 'NOTAUTHORIZED' }), res, req); @@ -93,14 +93,14 @@ ExpressApp.start = function(opts) { message: req.method.toLowerCase() + '|' + req.url + '|' + JSON.stringify(req.body), signature: credentials.signature, }; - CopayServer.getInstanceWithAuth(auth, function(err, server) { + WalletService.getInstanceWithAuth(auth, function(err, server) { if (err) return returnError(err, res, req); return cb(server); }); }; router.post('/v1/wallets/', function(req, res) { - var server = CopayServer.getInstance(); + var server = WalletService.getInstance(); server.createWallet(req.body, function(err, walletId) { if (err) return returnError(err, res, req); @@ -112,7 +112,7 @@ ExpressApp.start = function(opts) { router.post('/v1/wallets/:id/copayers/', function(req, res) { req.body.walletId = req.params['id']; - var server = CopayServer.getInstance(); + var server = WalletService.getInstance(); server.joinWallet(req.body, function(err, result) { if (err) return returnError(err, res, req); @@ -247,7 +247,7 @@ ExpressApp.start = function(opts) { // TODO: DEBUG only! router.get('/v1/dump', function(req, res) { - var server = CopayServer.getInstance(); + var server = WalletService.getInstance(); server.storage._dump(function() { res.end(); }); diff --git a/lib/server.js b/lib/server.js index 3a8822c..8675d11 100644 --- a/lib/server.js +++ b/lib/server.js @@ -32,7 +32,7 @@ var storage, blockExplorer; * Creates an instance of the Copay server. * @constructor */ -function CopayServer() { +function WalletService() { if (!initialized) throw new Error('Server not initialized'); @@ -41,7 +41,7 @@ function CopayServer() { this.notifyTicker = 0; }; -nodeutil.inherits(CopayServer, events.EventEmitter); +nodeutil.inherits(WalletService, events.EventEmitter); /** @@ -50,15 +50,15 @@ nodeutil.inherits(CopayServer, events.EventEmitter); * @param {Storage} [opts.storage] - The storage provider. * @param {Storage} [opts.blockExplorer] - The blockExporer provider. */ -CopayServer.initialize = function(opts) { +WalletService.initialize = function(opts) { opts = opts || {}; storage = opts.storage ||  new Storage(); blockExplorer = opts.blockExplorer; initialized = true; }; -CopayServer.getInstance = function() { - return new CopayServer(); +WalletService.getInstance = function() { + return new WalletService(); }; /** @@ -68,12 +68,12 @@ CopayServer.getInstance = function() { * @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) { +WalletService.getInstanceWithAuth = function(opts, cb) { if (!Utils.checkRequired(opts, ['copayerId', 'message', 'signature'])) return cb(new ClientError('Required argument missing')); - var server = new CopayServer(); + var server = new WalletService(); server.storage.fetchCopayerLookup(opts.copayerId, function(err, copayer) { if (err) return cb(err); if (!copayer) return cb(new ClientError('NOTAUTHORIZED', 'Copayer not found')); @@ -98,7 +98,7 @@ CopayServer.getInstanceWithAuth = function(opts, cb) { * @param {string} opts.pubKey - Public key to verify copayers joining have access to the wallet secret. * @param {string} [opts.network = 'livenet'] - The Bitcoin network for this wallet. */ -CopayServer.prototype.createWallet = function(opts, cb) { +WalletService.prototype.createWallet = function(opts, cb) { var self = this, pubKey; @@ -138,7 +138,7 @@ CopayServer.prototype.createWallet = function(opts, cb) { * @param {Object} opts * @returns {Object} wallet */ -CopayServer.prototype.getWallet = function(opts, cb) { +WalletService.prototype.getWallet = function(opts, cb) { var self = this; self.storage.fetchWallet(self.walletId, function(err, wallet) { @@ -155,7 +155,7 @@ CopayServer.prototype.getWallet = function(opts, cb) { * @param signature * @param pubKey */ -CopayServer.prototype._verifySignature = function(text, signature, pubKey) { +WalletService.prototype._verifySignature = function(text, signature, pubKey) { return WalletUtils.verifyMessage(text, signature, pubKey); }; @@ -166,7 +166,7 @@ CopayServer.prototype._verifySignature = function(text, signature, pubKey) { * @param type * @param data */ -CopayServer.prototype._notify = function(type, data) { +WalletService.prototype._notify = function(type, data) { var self = this; log.debug('Notification', type, data); @@ -192,7 +192,7 @@ CopayServer.prototype._notify = function(type, data) { * @param {number} opts.xPubKey - Extended Public Key for this copayer. * @param {number} opts.xPubKeySignature - Signature of xPubKey using the wallet pubKey. */ -CopayServer.prototype.joinWallet = function(opts, cb) { +WalletService.prototype.joinWallet = function(opts, cb) { var self = this; if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature'])) @@ -253,7 +253,7 @@ CopayServer.prototype.joinWallet = function(opts, cb) { * @param {Object} opts * @returns {Address} address */ -CopayServer.prototype.createAddress = function(opts, cb) { +WalletService.prototype.createAddress = function(opts, cb) { var self = this; Utils.runLocked(self.walletId, cb, function(cb) { @@ -279,7 +279,7 @@ CopayServer.prototype.createAddress = function(opts, cb) { * @param {Object} opts * @returns {Address[]} */ -CopayServer.prototype.getAddresses = function(opts, cb) { +WalletService.prototype.getAddresses = function(opts, cb) { var self = this; self.storage.fetchAddresses(self.walletId, function(err, addresses) { @@ -296,7 +296,7 @@ CopayServer.prototype.getAddresses = function(opts, cb) { * @param {string} opts.signature - The signature of message to verify. * @returns {truthy} The result of the verification. */ -CopayServer.prototype.verifyMessageSignature = function(opts, cb) { +WalletService.prototype.verifyMessageSignature = function(opts, cb) { var self = this; if (!Utils.checkRequired(opts, ['message', 'signature'])) @@ -313,7 +313,7 @@ CopayServer.prototype.verifyMessageSignature = function(opts, cb) { }; -CopayServer.prototype._getBlockExplorer = function(provider, network) { +WalletService.prototype._getBlockExplorer = function(provider, network) { var url; if (this.blockExplorer) @@ -340,7 +340,7 @@ CopayServer.prototype._getBlockExplorer = function(provider, network) { * _getUtxos * */ -CopayServer.prototype._getUtxos = function(cb) { +WalletService.prototype._getUtxos = function(cb) { var self = this; @@ -403,7 +403,7 @@ CopayServer.prototype._getUtxos = function(cb) { * @param {Object} opts * @returns {Object} balance - Total amount & locked amount. */ -CopayServer.prototype.getBalance = function(opts, cb) { +WalletService.prototype.getBalance = function(opts, cb) { var self = this; self._getUtxos(function(err, utxos) { @@ -425,7 +425,7 @@ CopayServer.prototype.getBalance = function(opts, cb) { }; -CopayServer.prototype._selectUtxos = function(txp, utxos) { +WalletService.prototype._selectUtxos = function(txp, utxos) { var i = 0; var total = 0; var selected = []; @@ -463,7 +463,7 @@ CopayServer.prototype._selectUtxos = function(txp, utxos) { * @param {string} opts.proposalSignature - S(toAddress + '|' + amount + '|' + message). Used by other copayers to verify the proposal. Optional in 1-of-1 wallets. * @returns {TxProposal} Transaction proposal. */ -CopayServer.prototype.createTx = function(opts, cb) { +WalletService.prototype.createTx = function(opts, cb) { var self = this; if (!Utils.checkRequired(opts, ['toAddress', 'amount', 'proposalSignature'])) @@ -545,7 +545,7 @@ CopayServer.prototype.createTx = function(opts, cb) { * @param {string} opts.id - The tx id. * @returns {Object} txProposal */ -CopayServer.prototype.getTx = function(opts, cb) { +WalletService.prototype.getTx = function(opts, cb) { var self = this; self.storage.fetchTx(self.walletId, opts.id, function(err, txp) { @@ -563,7 +563,7 @@ CopayServer.prototype.getTx = function(opts, cb) { * @param cb * @return {undefined} */ -CopayServer.prototype.removeWallet = function(opts, cb) { +WalletService.prototype.removeWallet = function(opts, cb) { var self = this; Utils.runLocked(self.walletId, cb, function(cb) { @@ -578,7 +578,7 @@ CopayServer.prototype.removeWallet = function(opts, cb) { * @param {string} opts.txProposalId - The tx id. * @return {undefined} */ -CopayServer.prototype.removePendingTx = function(opts, cb) { +WalletService.prototype.removePendingTx = function(opts, cb) { var self = this; if (!Utils.checkRequired(opts, ['txProposalId'])) @@ -610,7 +610,7 @@ CopayServer.prototype.removePendingTx = function(opts, cb) { }; -CopayServer.prototype._broadcastTx = function(txp, cb) { +WalletService.prototype._broadcastTx = function(txp, cb) { var raw; try { raw = txp.getRawTx(); @@ -629,7 +629,7 @@ CopayServer.prototype._broadcastTx = function(txp, cb) { * @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) */ -CopayServer.prototype.signTx = function(opts, cb) { +WalletService.prototype.signTx = function(opts, cb) { var self = this; if (!Utils.checkRequired(opts, ['txProposalId', 'signatures'])) @@ -699,7 +699,7 @@ CopayServer.prototype.signTx = function(opts, cb) { * @param {Object} opts * @param {string} opts.txProposalId - The identifier of the transaction. */ -CopayServer.prototype.broadcastTx = function(opts, cb) { +WalletService.prototype.broadcastTx = function(opts, cb) { var self = this; if (!Utils.checkRequired(opts, ['txProposalId'])) @@ -744,7 +744,7 @@ CopayServer.prototype.broadcastTx = function(opts, cb) { * @param {string} opts.txProposalId - The identifier of the transaction. * @param {string} [opts.reason] - A message to other copayers explaining the rejection. */ -CopayServer.prototype.rejectTx = function(opts, cb) { +WalletService.prototype.rejectTx = function(opts, cb) { var self = this; if (!Utils.checkRequired(opts, ['txProposalId'])) @@ -791,7 +791,7 @@ CopayServer.prototype.rejectTx = function(opts, cb) { * @param {Object} opts * @returns {TxProposal[]} Transaction proposal. */ -CopayServer.prototype.getPendingTxs = function(opts, cb) { +WalletService.prototype.getPendingTxs = function(opts, cb) { var self = this; self.storage.fetchPendingTxs(self.walletId, function(err, txps) { @@ -810,7 +810,7 @@ CopayServer.prototype.getPendingTxs = function(opts, cb) { * @param {Object} opts.limit * @returns {TxProposal[]} Transaction proposals, first newer */ -CopayServer.prototype.getTxs = function(opts, cb) { +WalletService.prototype.getTxs = function(opts, cb) { var self = this; self.storage.fetchTxs(self.walletId, opts, function(err, txps) { if (err) return cb(err); @@ -829,7 +829,7 @@ CopayServer.prototype.getTxs = function(opts, cb) { * @param {Object} opts.reverse (default false) * @returns {Notification[]} Notifications */ -CopayServer.prototype.getNotifications = function(opts, cb) { +WalletService.prototype.getNotifications = function(opts, cb) { var self = this; self.storage.fetchNotifications(self.walletId, opts, function(err, notifications) { if (err) return cb(err); @@ -841,5 +841,5 @@ CopayServer.prototype.getNotifications = function(opts, cb) { -module.exports = CopayServer; +module.exports = WalletService; module.exports.ClientError = ClientError; diff --git a/test/integration/clientApi.js b/test/integration/clientApi.js index 33645b7..ce9925a 100644 --- a/test/integration/clientApi.js +++ b/test/integration/clientApi.js @@ -132,7 +132,7 @@ describe('client API ', function() { db: db }); app = ExpressApp.start({ - CopayServer: { + WalletService: { storage: storage, blockExplorer: blockExplorerMock, } diff --git a/test/integration/server.js b/test/integration/server.js index fe4097d..d64fc6e 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -17,14 +17,14 @@ var Storage = require('../../lib/storage'); var Wallet = require('../../lib/model/wallet'); var Address = require('../../lib/model/address'); var Copayer = require('../../lib/model/copayer'); -var CopayServer = require('../../lib/server'); +var WalletService = require('../../lib/server'); var TestData = require('../testdata'); var helpers = {}; helpers.getAuthServer = function(copayerId, cb) { - var signatureStub = sinon.stub(CopayServer.prototype, '_verifySignature'); + var signatureStub = sinon.stub(WalletService.prototype, '_verifySignature'); signatureStub.returns(true); - CopayServer.getInstanceWithAuth({ + WalletService.getInstanceWithAuth({ copayerId: copayerId, message: 'dummy', signature: 'dummy', @@ -36,7 +36,7 @@ helpers.getAuthServer = function(copayerId, cb) { }; helpers.createAndJoinWallet = function(m, n, cb) { - var server = new CopayServer(); + var server = new WalletService(); var copayerIds = []; var offset = helpers.offset || 0; @@ -196,7 +196,7 @@ describe('Copay server', function() { storage = new Storage({ db: db }); - CopayServer.initialize({ + WalletService.initialize({ storage: storage }); helpers.offset = 0; @@ -218,7 +218,7 @@ describe('Copay server', function() { var message = 'hola'; var sig = WalletUtils.signMessage(message, priv); - CopayServer.getInstanceWithAuth({ + WalletService.getInstanceWithAuth({ copayerId: wallet.copayers[0].id, message: message, signature: sig, @@ -230,7 +230,7 @@ describe('Copay server', function() { }); it('should fail when requesting for non-existent copayer', function(done) { - CopayServer.getInstanceWithAuth({ + WalletService.getInstanceWithAuth({ copayerId: 'ads', message: TestData.message.text, signature: TestData.message.signature, @@ -243,7 +243,7 @@ describe('Copay server', function() { it('should fail when message signature cannot be verified', function(done) { helpers.createAndJoinWallet(1, 2, function(s, wallet) { - CopayServer.getInstanceWithAuth({ + WalletService.getInstanceWithAuth({ copayerId: wallet.copayers[0].id, message: 'dummy', signature: 'dummy', @@ -259,7 +259,7 @@ describe('Copay server', function() { describe('#createWallet', function() { var server; beforeEach(function() { - server = new CopayServer(); + server = new WalletService(); }); it('should create and store wallet', function(done) { @@ -337,7 +337,7 @@ describe('Copay server', function() { describe('#joinWallet', function() { var server, walletId; beforeEach(function(done) { - server = new CopayServer(); + server = new WalletService(); var walletOpts = { name: 'my wallet', m: 2, @@ -587,7 +587,7 @@ describe('Copay server', function() { describe('Wallet not complete tests', function() { it('should fail to create address when wallet is not complete', function(done) { - var server = new CopayServer(); + var server = new WalletService(); var walletOpts = { name: 'my wallet', m: 2, @@ -617,7 +617,7 @@ describe('Copay server', function() { }); it('should fail to create tx when wallet is not complete', function(done) { - var server = new CopayServer(); + var server = new WalletService(); var walletOpts = { name: 'my wallet', m: 2,