|
|
@ -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; |
|
|
|