Browse Source

rm CopayServer

activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
1e10c751e9
  1. 16
      lib/expressapp.js
  2. 62
      lib/server.js
  3. 2
      test/integration/clientApi.js
  4. 24
      test/integration/server.js

16
lib/expressapp.js

@ -7,7 +7,7 @@ var express = require('express');
var querystring = require('querystring'); var querystring = require('querystring');
var bodyParser = require('body-parser') var bodyParser = require('body-parser')
var CopayServer = require('./server'); var WalletService = require('./server');
log.debug = log.verbose; log.debug = log.verbose;
log.level = 'debug'; log.level = 'debug';
@ -18,7 +18,7 @@ ExpressApp.start = function(opts) {
opts = opts || {}; opts = opts || {};
CopayServer.initialize(opts.CopayServer); WalletService.initialize(opts.WalletService);
var app = express(); var app = express();
app.use(function(req, res, next) { app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Origin', '*');
@ -48,7 +48,7 @@ ExpressApp.start = function(opts) {
var router = express.Router(); var router = express.Router();
function returnError(err, res, req) { function returnError(err, res, req) {
if (err instanceof CopayServer.ClientError) { if (err instanceof WalletService.ClientError) {
var status = (err.code == 'NOTAUTHORIZED') ? 401 : 400; var status = (err.code == 'NOTAUTHORIZED') ? 401 : 400;
log.error('Err: ' + status + ':' + req.url + ' :' + err.code + ':' + err.message); log.error('Err: ' + status + ':' + req.url + ' :' + err.code + ':' + err.message);
@ -84,7 +84,7 @@ ExpressApp.start = function(opts) {
function getServerWithAuth(req, res, cb) { function getServerWithAuth(req, res, cb) {
var credentials = getCredentials(req); var credentials = getCredentials(req);
if (!credentials) if (!credentials)
return returnError(new CopayServer.ClientError({ return returnError(new WalletService.ClientError({
code: 'NOTAUTHORIZED' code: 'NOTAUTHORIZED'
}), res, req); }), res, req);
@ -93,14 +93,14 @@ ExpressApp.start = function(opts) {
message: req.method.toLowerCase() + '|' + req.url + '|' + JSON.stringify(req.body), message: req.method.toLowerCase() + '|' + req.url + '|' + JSON.stringify(req.body),
signature: credentials.signature, signature: credentials.signature,
}; };
CopayServer.getInstanceWithAuth(auth, function(err, server) { WalletService.getInstanceWithAuth(auth, function(err, server) {
if (err) return returnError(err, res, req); if (err) return returnError(err, res, req);
return cb(server); return cb(server);
}); });
}; };
router.post('/v1/wallets/', function(req, res) { router.post('/v1/wallets/', function(req, res) {
var server = CopayServer.getInstance(); var server = WalletService.getInstance();
server.createWallet(req.body, function(err, walletId) { server.createWallet(req.body, function(err, walletId) {
if (err) return returnError(err, res, req); if (err) return returnError(err, res, req);
@ -112,7 +112,7 @@ ExpressApp.start = function(opts) {
router.post('/v1/wallets/:id/copayers/', function(req, res) { router.post('/v1/wallets/:id/copayers/', function(req, res) {
req.body.walletId = req.params['id']; req.body.walletId = req.params['id'];
var server = CopayServer.getInstance(); var server = WalletService.getInstance();
server.joinWallet(req.body, function(err, result) { server.joinWallet(req.body, function(err, result) {
if (err) return returnError(err, res, req); if (err) return returnError(err, res, req);
@ -247,7 +247,7 @@ ExpressApp.start = function(opts) {
// TODO: DEBUG only! // TODO: DEBUG only!
router.get('/v1/dump', function(req, res) { router.get('/v1/dump', function(req, res) {
var server = CopayServer.getInstance(); var server = WalletService.getInstance();
server.storage._dump(function() { server.storage._dump(function() {
res.end(); res.end();
}); });

62
lib/server.js

@ -32,7 +32,7 @@ var storage, blockExplorer;
* Creates an instance of the Copay server. * Creates an instance of the Copay server.
* @constructor * @constructor
*/ */
function CopayServer() { function WalletService() {
if (!initialized) if (!initialized)
throw new Error('Server not initialized'); throw new Error('Server not initialized');
@ -41,7 +41,7 @@ function CopayServer() {
this.notifyTicker = 0; 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.storage] - The storage provider.
* @param {Storage} [opts.blockExplorer] - The blockExporer provider. * @param {Storage} [opts.blockExplorer] - The blockExporer provider.
*/ */
CopayServer.initialize = function(opts) { WalletService.initialize = function(opts) {
opts = opts || {}; opts = opts || {};
storage = opts.storage ||  new Storage(); storage = opts.storage ||  new Storage();
blockExplorer = opts.blockExplorer; blockExplorer = opts.blockExplorer;
initialized = true; initialized = true;
}; };
CopayServer.getInstance = function() { WalletService.getInstance = function() {
return new CopayServer(); 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.message - The contents of the request to be signed.
* @param {string} opts.signature - Signature of message to be verified using the copayer's signingPubKey. * @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'])) if (!Utils.checkRequired(opts, ['copayerId', 'message', 'signature']))
return cb(new ClientError('Required argument missing')); return cb(new ClientError('Required argument missing'));
var server = new CopayServer(); var server = new WalletService();
server.storage.fetchCopayerLookup(opts.copayerId, function(err, copayer) { server.storage.fetchCopayerLookup(opts.copayerId, function(err, copayer) {
if (err) return cb(err); if (err) return cb(err);
if (!copayer) return cb(new ClientError('NOTAUTHORIZED', 'Copayer not found')); 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.pubKey - Public key to verify copayers joining have access to the wallet secret.
* @param {string} [opts.network = 'livenet'] - The Bitcoin network for this wallet. * @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, var self = this,
pubKey; pubKey;
@ -138,7 +138,7 @@ CopayServer.prototype.createWallet = function(opts, cb) {
* @param {Object} opts * @param {Object} opts
* @returns {Object} wallet * @returns {Object} wallet
*/ */
CopayServer.prototype.getWallet = function(opts, cb) { WalletService.prototype.getWallet = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchWallet(self.walletId, function(err, wallet) { self.storage.fetchWallet(self.walletId, function(err, wallet) {
@ -155,7 +155,7 @@ CopayServer.prototype.getWallet = function(opts, cb) {
* @param signature * @param signature
* @param pubKey * @param pubKey
*/ */
CopayServer.prototype._verifySignature = function(text, signature, pubKey) { WalletService.prototype._verifySignature = function(text, signature, pubKey) {
return WalletUtils.verifyMessage(text, signature, pubKey); return WalletUtils.verifyMessage(text, signature, pubKey);
}; };
@ -166,7 +166,7 @@ CopayServer.prototype._verifySignature = function(text, signature, pubKey) {
* @param type * @param type
* @param data * @param data
*/ */
CopayServer.prototype._notify = function(type, data) { WalletService.prototype._notify = function(type, data) {
var self = this; var self = this;
log.debug('Notification', type, data); 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.xPubKey - Extended Public Key for this copayer.
* @param {number} opts.xPubKeySignature - Signature of xPubKey using the wallet pubKey. * @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; var self = this;
if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature'])) if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature']))
@ -253,7 +253,7 @@ CopayServer.prototype.joinWallet = function(opts, cb) {
* @param {Object} opts * @param {Object} opts
* @returns {Address} address * @returns {Address} address
*/ */
CopayServer.prototype.createAddress = function(opts, cb) { WalletService.prototype.createAddress = function(opts, cb) {
var self = this; var self = this;
Utils.runLocked(self.walletId, cb, function(cb) { Utils.runLocked(self.walletId, cb, function(cb) {
@ -279,7 +279,7 @@ CopayServer.prototype.createAddress = function(opts, cb) {
* @param {Object} opts * @param {Object} opts
* @returns {Address[]} * @returns {Address[]}
*/ */
CopayServer.prototype.getAddresses = function(opts, cb) { WalletService.prototype.getAddresses = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchAddresses(self.walletId, function(err, addresses) { 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. * @param {string} opts.signature - The signature of message to verify.
* @returns {truthy} The result of the verification. * @returns {truthy} The result of the verification.
*/ */
CopayServer.prototype.verifyMessageSignature = function(opts, cb) { WalletService.prototype.verifyMessageSignature = function(opts, cb) {
var self = this; var self = this;
if (!Utils.checkRequired(opts, ['message', 'signature'])) 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; var url;
if (this.blockExplorer) if (this.blockExplorer)
@ -340,7 +340,7 @@ CopayServer.prototype._getBlockExplorer = function(provider, network) {
* _getUtxos * _getUtxos
* *
*/ */
CopayServer.prototype._getUtxos = function(cb) { WalletService.prototype._getUtxos = function(cb) {
var self = this; var self = this;
@ -403,7 +403,7 @@ CopayServer.prototype._getUtxos = function(cb) {
* @param {Object} opts * @param {Object} opts
* @returns {Object} balance - Total amount & locked amount. * @returns {Object} balance - Total amount & locked amount.
*/ */
CopayServer.prototype.getBalance = function(opts, cb) { WalletService.prototype.getBalance = function(opts, cb) {
var self = this; var self = this;
self._getUtxos(function(err, utxos) { 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 i = 0;
var total = 0; var total = 0;
var selected = []; 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. * @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. * @returns {TxProposal} Transaction proposal.
*/ */
CopayServer.prototype.createTx = function(opts, cb) { WalletService.prototype.createTx = function(opts, cb) {
var self = this; var self = this;
if (!Utils.checkRequired(opts, ['toAddress', 'amount', 'proposalSignature'])) if (!Utils.checkRequired(opts, ['toAddress', 'amount', 'proposalSignature']))
@ -545,7 +545,7 @@ CopayServer.prototype.createTx = function(opts, cb) {
* @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) { WalletService.prototype.getTx = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchTx(self.walletId, opts.id, function(err, txp) { self.storage.fetchTx(self.walletId, opts.id, function(err, txp) {
@ -563,7 +563,7 @@ CopayServer.prototype.getTx = function(opts, cb) {
* @param cb * @param cb
* @return {undefined} * @return {undefined}
*/ */
CopayServer.prototype.removeWallet = function(opts, cb) { WalletService.prototype.removeWallet = function(opts, cb) {
var self = this; var self = this;
Utils.runLocked(self.walletId, cb, function(cb) { Utils.runLocked(self.walletId, cb, function(cb) {
@ -578,7 +578,7 @@ CopayServer.prototype.removeWallet = function(opts, cb) {
* @param {string} opts.txProposalId - The tx id. * @param {string} opts.txProposalId - The tx id.
* @return {undefined} * @return {undefined}
*/ */
CopayServer.prototype.removePendingTx = function(opts, cb) { WalletService.prototype.removePendingTx = function(opts, cb) {
var self = this; var self = this;
if (!Utils.checkRequired(opts, ['txProposalId'])) 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; var raw;
try { try {
raw = txp.getRawTx(); 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.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) { WalletService.prototype.signTx = function(opts, cb) {
var self = this; var self = this;
if (!Utils.checkRequired(opts, ['txProposalId', 'signatures'])) if (!Utils.checkRequired(opts, ['txProposalId', 'signatures']))
@ -699,7 +699,7 @@ CopayServer.prototype.signTx = function(opts, cb) {
* @param {Object} opts * @param {Object} opts
* @param {string} opts.txProposalId - The identifier of the transaction. * @param {string} opts.txProposalId - The identifier of the transaction.
*/ */
CopayServer.prototype.broadcastTx = function(opts, cb) { WalletService.prototype.broadcastTx = function(opts, cb) {
var self = this; var self = this;
if (!Utils.checkRequired(opts, ['txProposalId'])) 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.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) { WalletService.prototype.rejectTx = function(opts, cb) {
var self = this; var self = this;
if (!Utils.checkRequired(opts, ['txProposalId'])) if (!Utils.checkRequired(opts, ['txProposalId']))
@ -791,7 +791,7 @@ CopayServer.prototype.rejectTx = function(opts, cb) {
* @param {Object} opts * @param {Object} opts
* @returns {TxProposal[]} Transaction proposal. * @returns {TxProposal[]} Transaction proposal.
*/ */
CopayServer.prototype.getPendingTxs = function(opts, cb) { WalletService.prototype.getPendingTxs = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchPendingTxs(self.walletId, function(err, txps) { self.storage.fetchPendingTxs(self.walletId, function(err, txps) {
@ -810,7 +810,7 @@ CopayServer.prototype.getPendingTxs = function(opts, cb) {
* @param {Object} opts.limit * @param {Object} opts.limit
* @returns {TxProposal[]} Transaction proposals, first newer * @returns {TxProposal[]} Transaction proposals, first newer
*/ */
CopayServer.prototype.getTxs = function(opts, cb) { WalletService.prototype.getTxs = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchTxs(self.walletId, opts, function(err, txps) { self.storage.fetchTxs(self.walletId, opts, function(err, txps) {
if (err) return cb(err); if (err) return cb(err);
@ -829,7 +829,7 @@ CopayServer.prototype.getTxs = function(opts, cb) {
* @param {Object} opts.reverse (default false) * @param {Object} opts.reverse (default false)
* @returns {Notification[]} Notifications * @returns {Notification[]} Notifications
*/ */
CopayServer.prototype.getNotifications = function(opts, cb) { WalletService.prototype.getNotifications = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchNotifications(self.walletId, opts, function(err, notifications) { self.storage.fetchNotifications(self.walletId, opts, function(err, notifications) {
if (err) return cb(err); 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; module.exports.ClientError = ClientError;

2
test/integration/clientApi.js

@ -132,7 +132,7 @@ describe('client API ', function() {
db: db db: db
}); });
app = ExpressApp.start({ app = ExpressApp.start({
CopayServer: { WalletService: {
storage: storage, storage: storage,
blockExplorer: blockExplorerMock, blockExplorer: blockExplorerMock,
} }

24
test/integration/server.js

@ -17,14 +17,14 @@ var Storage = require('../../lib/storage');
var Wallet = require('../../lib/model/wallet'); var Wallet = require('../../lib/model/wallet');
var Address = require('../../lib/model/address'); var Address = require('../../lib/model/address');
var Copayer = require('../../lib/model/copayer'); var Copayer = require('../../lib/model/copayer');
var CopayServer = require('../../lib/server'); var WalletService = require('../../lib/server');
var TestData = require('../testdata'); var TestData = require('../testdata');
var helpers = {}; var helpers = {};
helpers.getAuthServer = function(copayerId, cb) { helpers.getAuthServer = function(copayerId, cb) {
var signatureStub = sinon.stub(CopayServer.prototype, '_verifySignature'); var signatureStub = sinon.stub(WalletService.prototype, '_verifySignature');
signatureStub.returns(true); signatureStub.returns(true);
CopayServer.getInstanceWithAuth({ WalletService.getInstanceWithAuth({
copayerId: copayerId, copayerId: copayerId,
message: 'dummy', message: 'dummy',
signature: 'dummy', signature: 'dummy',
@ -36,7 +36,7 @@ helpers.getAuthServer = function(copayerId, cb) {
}; };
helpers.createAndJoinWallet = function(m, n, cb) { helpers.createAndJoinWallet = function(m, n, cb) {
var server = new CopayServer(); var server = new WalletService();
var copayerIds = []; var copayerIds = [];
var offset = helpers.offset || 0; var offset = helpers.offset || 0;
@ -196,7 +196,7 @@ describe('Copay server', function() {
storage = new Storage({ storage = new Storage({
db: db db: db
}); });
CopayServer.initialize({ WalletService.initialize({
storage: storage storage: storage
}); });
helpers.offset = 0; helpers.offset = 0;
@ -218,7 +218,7 @@ describe('Copay server', function() {
var message = 'hola'; var message = 'hola';
var sig = WalletUtils.signMessage(message, priv); var sig = WalletUtils.signMessage(message, priv);
CopayServer.getInstanceWithAuth({ WalletService.getInstanceWithAuth({
copayerId: wallet.copayers[0].id, copayerId: wallet.copayers[0].id,
message: message, message: message,
signature: sig, signature: sig,
@ -230,7 +230,7 @@ describe('Copay server', function() {
}); });
it('should fail when requesting for non-existent copayer', function(done) { it('should fail when requesting for non-existent copayer', function(done) {
CopayServer.getInstanceWithAuth({ WalletService.getInstanceWithAuth({
copayerId: 'ads', copayerId: 'ads',
message: TestData.message.text, message: TestData.message.text,
signature: TestData.message.signature, signature: TestData.message.signature,
@ -243,7 +243,7 @@ describe('Copay server', function() {
it('should fail when message signature cannot be verified', function(done) { it('should fail when message signature cannot be verified', function(done) {
helpers.createAndJoinWallet(1, 2, function(s, wallet) { helpers.createAndJoinWallet(1, 2, function(s, wallet) {
CopayServer.getInstanceWithAuth({ WalletService.getInstanceWithAuth({
copayerId: wallet.copayers[0].id, copayerId: wallet.copayers[0].id,
message: 'dummy', message: 'dummy',
signature: 'dummy', signature: 'dummy',
@ -259,7 +259,7 @@ describe('Copay server', function() {
describe('#createWallet', function() { describe('#createWallet', function() {
var server; var server;
beforeEach(function() { beforeEach(function() {
server = new CopayServer(); server = new WalletService();
}); });
it('should create and store wallet', function(done) { it('should create and store wallet', function(done) {
@ -337,7 +337,7 @@ describe('Copay server', function() {
describe('#joinWallet', function() { describe('#joinWallet', function() {
var server, walletId; var server, walletId;
beforeEach(function(done) { beforeEach(function(done) {
server = new CopayServer(); server = new WalletService();
var walletOpts = { var walletOpts = {
name: 'my wallet', name: 'my wallet',
m: 2, m: 2,
@ -587,7 +587,7 @@ describe('Copay server', function() {
describe('Wallet not complete tests', function() { describe('Wallet not complete tests', function() {
it('should fail to create address when wallet is not complete', function(done) { it('should fail to create address when wallet is not complete', function(done) {
var server = new CopayServer(); var server = new WalletService();
var walletOpts = { var walletOpts = {
name: 'my wallet', name: 'my wallet',
m: 2, m: 2,
@ -617,7 +617,7 @@ describe('Copay server', function() {
}); });
it('should fail to create tx when wallet is not complete', function(done) { it('should fail to create tx when wallet is not complete', function(done) {
var server = new CopayServer(); var server = new WalletService();
var walletOpts = { var walletOpts = {
name: 'my wallet', name: 'my wallet',
m: 2, m: 2,

Loading…
Cancel
Save