|
|
@ -36,6 +36,7 @@ var storage; |
|
|
|
function CopayServer() { |
|
|
|
if (!initialized) throw new Error('Server not initialized'); |
|
|
|
this.storage = storage; |
|
|
|
this.notifyTicker = 0; |
|
|
|
}; |
|
|
|
|
|
|
|
nodeutil.inherits(CopayServer, events.EventEmitter); |
|
|
@ -61,7 +62,8 @@ CopayServer.initialize = function(opts) { |
|
|
|
*/ |
|
|
|
CopayServer.getInstanceWithAuth = function(opts, cb) { |
|
|
|
|
|
|
|
if (!Utils.checkRequired(opts, ['copayerId', 'message', 'signature'])) return cb(new ClientError('Required argument missing')); |
|
|
|
if (!Utils.checkRequired(opts, ['copayerId', 'message', 'signature'])) |
|
|
|
return cb(new ClientError('Required argument missing')); |
|
|
|
|
|
|
|
var server = new CopayServer(); |
|
|
|
server.storage.fetchCopayerLookup(opts.copayerId, function(err, copayer) { |
|
|
@ -163,6 +165,7 @@ CopayServer.prototype._notify = function(type, data) { |
|
|
|
var n = new Notification({ |
|
|
|
type: type, |
|
|
|
data: data, |
|
|
|
ticker: this.notifyTicker++, |
|
|
|
}); |
|
|
|
this.storage.storeNotification(walletId, n, function() { |
|
|
|
self.emit(n); |
|
|
@ -180,9 +183,11 @@ CopayServer.prototype._notify = function(type, data) { |
|
|
|
CopayServer.prototype.joinWallet = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature'])) return cb(new ClientError('Required argument missing')); |
|
|
|
if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature'])) |
|
|
|
return cb(new ClientError('Required argument missing')); |
|
|
|
|
|
|
|
if (_.isEmpty(opts.name)) return cb(new ClientError('Invalid copayer name')); |
|
|
|
if (_.isEmpty(opts.name)) |
|
|
|
return cb(new ClientError('Invalid copayer name')); |
|
|
|
|
|
|
|
Utils.runLocked(opts.walletId, cb, function(cb) { |
|
|
|
self.storage.fetchWallet(opts.walletId, function(err, wallet) { |
|
|
@ -196,7 +201,8 @@ CopayServer.prototype.joinWallet = function(opts, cb) { |
|
|
|
if (_.find(wallet.copayers, { |
|
|
|
xPubKey: opts.xPubKey |
|
|
|
})) return cb(new ClientError('CINWALLET', 'Copayer already in wallet')); |
|
|
|
if (wallet.copayers.length == wallet.n) return cb(new ClientError('WFULL', 'Wallet full')); |
|
|
|
if (wallet.copayers.length == wallet.n) |
|
|
|
return cb(new ClientError('WFULL', 'Wallet full')); |
|
|
|
|
|
|
|
var copayer = new Copayer({ |
|
|
|
name: opts.name, |
|
|
@ -209,6 +215,7 @@ CopayServer.prototype.joinWallet = function(opts, cb) { |
|
|
|
self.storage.storeWalletAndUpdateCopayersLookup(wallet, function(err) { |
|
|
|
self._notify('NewCopayer', { |
|
|
|
walletId: opts.walletId, |
|
|
|
copayerId: copayer.id, |
|
|
|
}); |
|
|
|
return cb(err, copayer.id); |
|
|
|
}); |
|
|
@ -227,7 +234,8 @@ CopayServer.prototype.createAddress = function(opts, cb) { |
|
|
|
Utils.runLocked(self.walletId, cb, function(cb) { |
|
|
|
self.getWallet({}, function(err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete')); |
|
|
|
if (!wallet.isComplete()) |
|
|
|
return cb(new ClientError('Wallet is not complete')); |
|
|
|
|
|
|
|
var address = wallet.createAddress(false); |
|
|
|
|
|
|
@ -624,7 +632,7 @@ CopayServer.prototype.signTx = function(opts, cb) { |
|
|
|
self.storage.storeTx(self.walletId, txp, function(err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
self._notify('newOutgoingTx', { |
|
|
|
self._notify('NewOutgoingTx', { |
|
|
|
txProposalId: opts.txProposalId, |
|
|
|
txid: txid |
|
|
|
}); |
|
|
@ -704,6 +712,8 @@ CopayServer.prototype.getPendingTxs = function(opts, cb) { |
|
|
|
|
|
|
|
/** |
|
|
|
* Retrieves pending transaction proposals in the range (maxTs-minTs) |
|
|
|
* Times are in UNIX EPOCH |
|
|
|
* |
|
|
|
* @param {Object} opts.minTs (defaults to 0) |
|
|
|
* @param {Object} opts.maxTs (defaults to now) |
|
|
|
* @param {Object} opts.limit |
|
|
@ -720,10 +730,13 @@ CopayServer.prototype.getTxs = function(opts, cb) { |
|
|
|
|
|
|
|
/** |
|
|
|
* Retrieves notifications in the range (maxTs-minTs). |
|
|
|
* Times are in UNIX EPOCH. Order is assured even for events with the same time |
|
|
|
* |
|
|
|
* @param {Object} opts.minTs (defaults to 0) |
|
|
|
* @param {Object} opts.maxTs (defaults to now) |
|
|
|
* @param {Object} opts.limit |
|
|
|
* @returns {Notification[]} Notifications, first newer |
|
|
|
* @param {Object} opts.reverse (default false) |
|
|
|
* @returns {Notification[]} Notifications |
|
|
|
*/ |
|
|
|
CopayServer.prototype.getNotifications = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|