Browse Source

move #runLocked to utils.js

activeAddress
Ivan Socolsky 10 years ago
parent
commit
5114b0cc25
  1. 37
      lib/server.js
  2. 20
      lib/utils.js

37
lib/server.js

@ -13,8 +13,7 @@ var PublicKey = Bitcore.PublicKey;
var HDPublicKey = Bitcore.HDPublicKey;
var Explorers = require('bitcore-explorers');
var utils = require('./utils');
var Lock = require('./lock');
var Utils = require('./utils');
var Storage = require('./storage');
var SignUtils = require('./signutils');
@ -56,7 +55,7 @@ CopayServer._emit = function (event) {
CopayServer.prototype.createWallet = function (opts, cb) {
var self = this, pubKey;
utils.checkRequired(opts, ['id', 'name', 'm', 'n', 'pubKey']);
Utils.checkRequired(opts, ['id', 'name', 'm', 'n', 'pubKey']);
if (!Wallet.verifyCopayerLimits(opts.m, opts.n)) return cb('Incorrect m or n value');
var network = opts.network || 'livenet';
if (network != 'livenet' && network != 'testnet') return cb('Invalid network');
@ -101,18 +100,6 @@ CopayServer.prototype.createWallet = function (opts, cb) {
};
CopayServer.prototype._runLocked = function (walletId, cb, task) {
var self = this;
Lock.get(walletId, function (lock) {
var _cb = function () {
cb.apply(null, arguments);
lock.free();
};
task(_cb);
});
};
/**
* Verifies a signature
* @param text
@ -135,9 +122,9 @@ CopayServer.prototype._verifySignature = function (text, signature, pubKey) {
CopayServer.prototype.joinWallet = function (opts, cb) {
var self = this;
utils.checkRequired(opts, ['walletId', 'id', 'name', 'xPubKey', 'xPubKeySignature']);
Utils.checkRequired(opts, ['walletId', 'id', 'name', 'xPubKey', 'xPubKeySignature']);
self._runLocked(opts.walletId, cb, function (cb) {
Utils.runLocked(opts.walletId, cb, function (cb) {
self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err) return cb(err);
@ -182,9 +169,9 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) {
CopayServer.prototype.createAddress = function (opts, cb) {
var self = this;
utils.checkRequired(opts, ['walletId', 'isChange']);
Utils.checkRequired(opts, ['walletId', 'isChange']);
self._runLocked(opts.walletId, cb, function (cb) {
Utils.runLocked(opts.walletId, cb, function (cb) {
self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err) return cb(err);
@ -215,7 +202,7 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) {
CopayServer.prototype.verifyMessageSignature = function (opts, cb) {
var self = this;
utils.checkRequired(opts, ['walletId', 'copayerId', 'message', 'signature']);
Utils.checkRequired(opts, ['walletId', 'copayerId', 'message', 'signature']);
self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err) return cb(err);
@ -297,7 +284,7 @@ CopayServer.prototype._getUtxos = function (opts, cb) {
CopayServer.prototype.getBalance = function (opts, cb) {
var self = this;
utils.checkRequired(opts, 'walletId');
Utils.checkRequired(opts, 'walletId');
self._getUtxos({ walletId: opts.walletId }, function (err, utxos) {
if (err) return cb(err);
@ -350,7 +337,7 @@ CopayServer.prototype._selectUtxos = function (txp, utxos) {
CopayServer.prototype.createTx = function (opts, cb) {
var self = this;
utils.checkRequired(opts, ['walletId', 'copayerId', 'toAddress', 'amount', 'message']);
Utils.checkRequired(opts, ['walletId', 'copayerId', 'toAddress', 'amount', 'message']);
self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err) return cb(err);
@ -399,7 +386,7 @@ CopayServer.prototype._broadcastTx = function (rawTx, cb) {
CopayServer.prototype.signTx = function (opts, cb) {
var self = this;
utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId', 'signature']);
Utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId', 'signature']);
self.fetchTx(opts.walletId, opts.txProposalId, function (err, txp) {
if (err) return cb(err);
@ -439,7 +426,7 @@ CopayServer.prototype.signTx = function (opts, cb) {
CopayServer.prototype.rejectTx = function (opts, cb) {
var self = this;
utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId']);
Utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId']);
self.fetchTx(opts.walletId, opts.txProposalId, function (err, txp) {
if (err) return cb(err);
@ -467,7 +454,7 @@ CopayServer.prototype.rejectTx = function (opts, cb) {
CopayServer.prototype.getPendingTxs = function (opts, cb) {
var self = this;
utils.checkRequired(opts, 'walletId');
Utils.checkRequired(opts, 'walletId');
self.storage.fetchTxs(opts.walletId, function (err, txps) {
if (err) return cb(err);

20
lib/utils.js

@ -1,9 +1,23 @@
var $ = require('preconditions').singleton();
var _ = require('lodash');
var Lock = require('./lock');
var utils = {};
var Utils = {};
utils.checkRequired = function (obj, args) {
Utils.runLocked = function (token, cb, task) {
var self = this;
Lock.get(token, function (lock) {
var _cb = function () {
cb.apply(null, arguments);
lock.free();
};
task(_cb);
});
};
Utils.checkRequired = function (obj, args) {
args = [].concat(args);
if (!_.isObject(obj)) throw 'Required arguments missing';
_.each(args, function (arg) {
@ -11,4 +25,4 @@ utils.checkRequired = function (obj, args) {
});
};
module.exports = utils;
module.exports = Utils;

Loading…
Cancel
Save