|
|
@ -30,8 +30,8 @@ var TxProposal = require('./model/txproposal'); |
|
|
|
* @param {Storage} [opts.storage] - The storage provider. |
|
|
|
*/ |
|
|
|
function CopayServer(opts) { |
|
|
|
opts = opts || {}; |
|
|
|
this.storage = opts.storage || new Storage(); |
|
|
|
opts = opts || {}; |
|
|
|
this.storage = opts.storage || new Storage(); |
|
|
|
}; |
|
|
|
|
|
|
|
inherits(CopayServer, events.EventEmitter); |
|
|
@ -53,12 +53,12 @@ CopayServer._emit = function (event) { |
|
|
|
* @param {string} [opts.network = 'livenet'] - The Bitcoin network for this wallet. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
|
var self = this, pubKey; |
|
|
|
var self = this, 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'); |
|
|
|
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'); |
|
|
|
|
|
|
|
try { |
|
|
|
pubKey = new PublicKey.fromString(opts.pubKey); |
|
|
@ -66,21 +66,21 @@ CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
|
return cb(e.toString()); |
|
|
|
}; |
|
|
|
|
|
|
|
self.storage.fetchWallet(opts.id, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (wallet) return cb('Wallet already exists'); |
|
|
|
|
|
|
|
var wallet = new Wallet({ |
|
|
|
id: opts.id, |
|
|
|
name: opts.name, |
|
|
|
m: opts.m, |
|
|
|
n: opts.n, |
|
|
|
network: network, |
|
|
|
pubKey: pubKey, |
|
|
|
}); |
|
|
|
|
|
|
|
self.storage.storeWallet(wallet, cb); |
|
|
|
}); |
|
|
|
self.storage.fetchWallet(opts.id, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (wallet) return cb('Wallet already exists'); |
|
|
|
|
|
|
|
var wallet = new Wallet({ |
|
|
|
id: opts.id, |
|
|
|
name: opts.name, |
|
|
|
m: opts.m, |
|
|
|
n: opts.n, |
|
|
|
network: network, |
|
|
|
pubKey: pubKey, |
|
|
|
}); |
|
|
|
|
|
|
|
self.storage.storeWallet(wallet, cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -90,13 +90,13 @@ CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
|
* @returns {Object} wallet |
|
|
|
*/ |
|
|
|
CopayServer.prototype.getWallet = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
var self = this; |
|
|
|
|
|
|
|
self.storage.fetchWallet(opts.id, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!wallet) return cb('Wallet not found'); |
|
|
|
return cb(null, wallet); |
|
|
|
}); |
|
|
|
self.storage.fetchWallet(opts.id, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!wallet) return cb('Wallet not found'); |
|
|
|
return cb(null, wallet); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -120,40 +120,40 @@ CopayServer.prototype._verifySignature = function (text, signature, pubKey) { |
|
|
|
* @param {number} opts.xPubKeySignature - Signature of xPubKey using the wallet pubKey. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.joinWallet = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
var self = this; |
|
|
|
|
|
|
|
Utils.checkRequired(opts, ['walletId', 'id', 'name', 'xPubKey', 'xPubKeySignature']); |
|
|
|
Utils.checkRequired(opts, ['walletId', 'id', 'name', 'xPubKey', 'xPubKeySignature']); |
|
|
|
|
|
|
|
Utils.runLocked(opts.walletId, cb, function (cb) { |
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
Utils.runLocked(opts.walletId, cb, function (cb) { |
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) { |
|
|
|
return cb('Bad request'); |
|
|
|
} |
|
|
|
|
|
|
|
if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return cb('Copayer already in wallet'); |
|
|
|
if (wallet.copayers.length == wallet.n) return cb('Wallet full'); |
|
|
|
var copayer = new Copayer({ |
|
|
|
id: opts.id, |
|
|
|
name: opts.name, |
|
|
|
xPubKey: opts.xPubKey, |
|
|
|
xPubKeySignature: opts.xPubKeySignature, |
|
|
|
}); |
|
|
|
|
|
|
|
wallet.addCopayer(copayer); |
|
|
|
|
|
|
|
self.storage.storeWallet(wallet, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return cb('Copayer already in wallet'); |
|
|
|
if (wallet.copayers.length == wallet.n) return cb('Wallet full'); |
|
|
|
var copayer = new Copayer({ |
|
|
|
id: opts.id, |
|
|
|
name: opts.name, |
|
|
|
xPubKey: opts.xPubKey, |
|
|
|
xPubKeySignature: opts.xPubKeySignature, |
|
|
|
}); |
|
|
|
|
|
|
|
wallet.addCopayer(copayer); |
|
|
|
|
|
|
|
self.storage.storeWallet(wallet, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) { |
|
|
|
throw 'not implemented'; |
|
|
|
throw 'not implemented'; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -167,27 +167,27 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) { |
|
|
|
* @returns {Address} address |
|
|
|
*/ |
|
|
|
CopayServer.prototype.createAddress = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
Utils.checkRequired(opts, ['walletId', 'isChange']); |
|
|
|
|
|
|
|
Utils.runLocked(opts.walletId, cb, function (cb) { |
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var index = wallet.addressIndex++; |
|
|
|
self.storage.storeWallet(wallet, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var address = self._doCreateAddress(wallet.publicKeyRing, index, opts.isChange); |
|
|
|
self.storage.storeAddress(opts.walletId, address, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
return cb(null, address); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
var self = this; |
|
|
|
|
|
|
|
Utils.checkRequired(opts, ['walletId', 'isChange']); |
|
|
|
|
|
|
|
Utils.runLocked(opts.walletId, cb, function (cb) { |
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var index = wallet.addressIndex++; |
|
|
|
self.storage.storeWallet(wallet, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var address = self._doCreateAddress(wallet.publicKeyRing, index, opts.isChange); |
|
|
|
self.storage.storeAddress(opts.walletId, address, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
return cb(null, address); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -200,78 +200,78 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) { |
|
|
|
* @returns {truthy} The result of the verification. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.verifyMessageSignature = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
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); |
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var copayer = wallet.getCopayer(opts.copayerId); |
|
|
|
if (!copayer) return cb('Copayer not found'); |
|
|
|
var copayer = wallet.getCopayer(opts.copayerId); |
|
|
|
if (!copayer) return cb('Copayer not found'); |
|
|
|
|
|
|
|
var isValid = self._verifySignature(opts.message, opts.signature, copayer.signingPubKey); |
|
|
|
return cb(null, isValid); |
|
|
|
}); |
|
|
|
var isValid = self._verifySignature(opts.message, opts.signature, copayer.signingPubKey); |
|
|
|
return cb(null, isValid); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
CopayServer.prototype._getBlockExplorer = function (provider, network) { |
|
|
|
var url; |
|
|
|
|
|
|
|
switch (provider) { |
|
|
|
default: |
|
|
|
case 'insight': |
|
|
|
switch (network) { |
|
|
|
default: |
|
|
|
case 'livenet': |
|
|
|
url = 'https://insight.bitpay.com:443'; |
|
|
|
break; |
|
|
|
case 'testnet': |
|
|
|
url = 'https://test-insight.bitpay.com:443' |
|
|
|
break; |
|
|
|
} |
|
|
|
return new Explorers.Insight(url, network); |
|
|
|
break; |
|
|
|
} |
|
|
|
var url; |
|
|
|
|
|
|
|
switch (provider) { |
|
|
|
default: |
|
|
|
case 'insight': |
|
|
|
switch (network) { |
|
|
|
default: |
|
|
|
case 'livenet': |
|
|
|
url = 'https://insight.bitpay.com:443'; |
|
|
|
break; |
|
|
|
case 'testnet': |
|
|
|
url = 'https://test-insight.bitpay.com:443' |
|
|
|
break; |
|
|
|
} |
|
|
|
return new Explorers.Insight(url, network); |
|
|
|
break; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
CopayServer.prototype._getUtxos = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
// Get addresses for this wallet
|
|
|
|
self.storage.fetchAddresses(opts.walletId, function (err, addresses) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (addresses.length == 0) return cb('The wallet has no addresses'); |
|
|
|
|
|
|
|
var addresses = _.pluck(addresses, 'address'); |
|
|
|
|
|
|
|
var bc = self._getBlockExplorer('insight', opts.network); |
|
|
|
bc.getUnspentUtxos(addresses, function (err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
self.getPendingTxs({ walletId: opts.walletId }, function (err, txps) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var inputs = _.chain(txps) |
|
|
|
.pluck('inputs') |
|
|
|
.flatten() |
|
|
|
.map(function (utxo) { return utxo.txid + '|' + utxo.vout }); |
|
|
|
|
|
|
|
var dictionary = _.groupBy(utxos, function (utxo) { |
|
|
|
return utxo.txid + '|' + utxo.vout; |
|
|
|
}); |
|
|
|
|
|
|
|
_.each(inputs, function (input) { |
|
|
|
if (dictionary[input]) { |
|
|
|
dictionary[input].locked = true; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
return cb(null, utxos); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
var self = this; |
|
|
|
|
|
|
|
// Get addresses for this wallet
|
|
|
|
self.storage.fetchAddresses(opts.walletId, function (err, addresses) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (addresses.length == 0) return cb('The wallet has no addresses'); |
|
|
|
|
|
|
|
var addresses = _.pluck(addresses, 'address'); |
|
|
|
|
|
|
|
var bc = self._getBlockExplorer('insight', opts.network); |
|
|
|
bc.getUnspentUtxos(addresses, function (err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
self.getPendingTxs({ walletId: opts.walletId }, function (err, txps) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var inputs = _.chain(txps) |
|
|
|
.pluck('inputs') |
|
|
|
.flatten() |
|
|
|
.map(function (utxo) { return utxo.txid + '|' + utxo.vout }); |
|
|
|
|
|
|
|
var dictionary = _.groupBy(utxos, function (utxo) { |
|
|
|
return utxo.txid + '|' + utxo.vout; |
|
|
|
}); |
|
|
|
|
|
|
|
_.each(inputs, function (input) { |
|
|
|
if (dictionary[input]) { |
|
|
|
dictionary[input].locked = true; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
return cb(null, utxos); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -282,45 +282,45 @@ CopayServer.prototype._getUtxos = function (opts, cb) { |
|
|
|
* @returns {Object} balance - Total amount & locked amount. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.getBalance = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
var self = this; |
|
|
|
|
|
|
|
Utils.checkRequired(opts, 'walletId'); |
|
|
|
Utils.checkRequired(opts, 'walletId'); |
|
|
|
|
|
|
|
self._getUtxos({ walletId: opts.walletId }, function (err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
self._getUtxos({ walletId: opts.walletId }, function (err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var balance = {}; |
|
|
|
balance.totalAmount = _.reduce(utxos, function(sum, utxo) { return sum + utxo.amount; }, 0); |
|
|
|
balance.lockedAmount = _.reduce(_.without(utxos, { locked: true }), function(sum, utxo) { return sum + utxo.amount; }, 0); |
|
|
|
var balance = {}; |
|
|
|
balance.totalAmount = _.reduce(utxos, function(sum, utxo) { return sum + utxo.amount; }, 0); |
|
|
|
balance.lockedAmount = _.reduce(_.without(utxos, { locked: true }), function(sum, utxo) { return sum + utxo.amount; }, 0); |
|
|
|
|
|
|
|
return cb(null, balance); |
|
|
|
}); |
|
|
|
return cb(null, balance); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
CopayServer.prototype._createRawTx = function (txp) { |
|
|
|
var rawTx = new Bitcore.Transaction() |
|
|
|
.from(tx.inputs) |
|
|
|
.to(txp.toAddress, txp.amount) |
|
|
|
.change(txp.changeAddress); |
|
|
|
var rawTx = new Bitcore.Transaction() |
|
|
|
.from(tx.inputs) |
|
|
|
.to(txp.toAddress, txp.amount) |
|
|
|
.change(txp.changeAddress); |
|
|
|
|
|
|
|
return rawTx; |
|
|
|
return rawTx; |
|
|
|
}; |
|
|
|
|
|
|
|
CopayServer.prototype._selectUtxos = function (txp, utxos) { |
|
|
|
var i = 0; |
|
|
|
var total = 0; |
|
|
|
var selected = []; |
|
|
|
var inputs = _.sortBy(utxos, 'amount'); |
|
|
|
while (i < inputs.length) { |
|
|
|
selected.push(inputs[i]); |
|
|
|
total += inputs[i].amount; |
|
|
|
if (total >= txp.amount) { |
|
|
|
break; |
|
|
|
} |
|
|
|
i++; |
|
|
|
}; |
|
|
|
return selected; |
|
|
|
var i = 0; |
|
|
|
var total = 0; |
|
|
|
var selected = []; |
|
|
|
var inputs = _.sortBy(utxos, 'amount'); |
|
|
|
while (i < inputs.length) { |
|
|
|
selected.push(inputs[i]); |
|
|
|
total += inputs[i].amount; |
|
|
|
if (total >= txp.amount) { |
|
|
|
break; |
|
|
|
} |
|
|
|
i++; |
|
|
|
}; |
|
|
|
return selected; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -335,44 +335,44 @@ CopayServer.prototype._selectUtxos = function (txp, utxos) { |
|
|
|
* @returns {TxProposal} Transaction proposal. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.createTx = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
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); |
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
self._getUtxos({ walletId: wallet.id }, function (err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
self._getUtxos({ walletId: wallet.id }, function (err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
utxos = _.without(utxos, { locked: true }); |
|
|
|
utxos = _.without(utxos, { locked: true }); |
|
|
|
|
|
|
|
var txp = new TxProposal({ |
|
|
|
creatorId: opts.copayerId, |
|
|
|
toAddress: opts.toAddress, |
|
|
|
amount: opts.amount, |
|
|
|
inputs: self._selectUtxos(opts.amount, utxos), |
|
|
|
changeAddress: opts.changeAddress, |
|
|
|
requiredSignatures: wallet.m, |
|
|
|
maxRejections: wallet.n - wallet.m, |
|
|
|
}); |
|
|
|
var txp = new TxProposal({ |
|
|
|
creatorId: opts.copayerId, |
|
|
|
toAddress: opts.toAddress, |
|
|
|
amount: opts.amount, |
|
|
|
inputs: self._selectUtxos(opts.amount, utxos), |
|
|
|
changeAddress: opts.changeAddress, |
|
|
|
requiredSignatures: wallet.m, |
|
|
|
maxRejections: wallet.n - wallet.m, |
|
|
|
}); |
|
|
|
|
|
|
|
txp.rawTx = self._createRawTx(txp); |
|
|
|
txp.rawTx = self._createRawTx(txp); |
|
|
|
|
|
|
|
self.storage.storeTx(wallet.id, txp, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
self.storage.storeTx(wallet.id, txp, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
return cb(null, txp); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
return cb(null, txp); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
CopayServer.prototype._broadcastTx = function (rawTx, cb) { |
|
|
|
// TODO: this should attempt to broadcast _all_ accepted and not-yet broadcasted (status=='accepted') txps?
|
|
|
|
cb = cb || function () {}; |
|
|
|
// TODO: this should attempt to broadcast _all_ accepted and not-yet broadcasted (status=='accepted') txps?
|
|
|
|
cb = cb || function () {}; |
|
|
|
|
|
|
|
throw 'not implemented'; |
|
|
|
throw 'not implemented'; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -384,35 +384,35 @@ CopayServer.prototype._broadcastTx = function (rawTx, cb) { |
|
|
|
* @param {string} opts.signature - The signature of the tx for this copayer. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.signTx = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
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); |
|
|
|
if (!txp) return cb('Transaction proposal not found'); |
|
|
|
var action = _.find(txp.actions, { copayerId: opts.copayerId }); |
|
|
|
if (action) return cb('Copayer already voted on this transaction proposal'); |
|
|
|
if (txp.status != 'pending') return cb('The transaction proposal is not pending'); |
|
|
|
self.fetchTx(opts.walletId, opts.txProposalId, function (err, txp) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!txp) return cb('Transaction proposal not found'); |
|
|
|
var action = _.find(txp.actions, { copayerId: opts.copayerId }); |
|
|
|
if (action) return cb('Copayer already voted on this transaction proposal'); |
|
|
|
if (txp.status != 'pending') return cb('The transaction proposal is not pending'); |
|
|
|
|
|
|
|
txp.sign(opts.copayerId, opts.signature); |
|
|
|
txp.sign(opts.copayerId, opts.signature); |
|
|
|
|
|
|
|
self.storage.storeTx(opts.walletId, txp, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
self.storage.storeTx(opts.walletId, txp, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
if (txp.status == 'accepted'); |
|
|
|
self._broadcastTx(txp.rawTx, function (err, txid) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (txp.status == 'accepted'); |
|
|
|
self._broadcastTx(txp.rawTx, function (err, txid) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
tx.setBroadcasted(txid); |
|
|
|
self.storage.storeTx(opts.walletId, txp, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
tx.setBroadcasted(txid); |
|
|
|
self.storage.storeTx(opts.walletId, txp, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -424,25 +424,25 @@ CopayServer.prototype.signTx = function (opts, cb) { |
|
|
|
* @param {string} [opts.reason] - A message to other copayers explaining the rejection. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.rejectTx = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
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); |
|
|
|
if (!txp) return cb('Transaction proposal not found'); |
|
|
|
var action = _.find(txp.actions, { copayerId: opts.copayerId }); |
|
|
|
if (action) return cb('Copayer already voted on this transaction proposal'); |
|
|
|
if (txp.status != 'pending') return cb('The transaction proposal is not pending'); |
|
|
|
self.fetchTx(opts.walletId, opts.txProposalId, function (err, txp) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!txp) return cb('Transaction proposal not found'); |
|
|
|
var action = _.find(txp.actions, { copayerId: opts.copayerId }); |
|
|
|
if (action) return cb('Copayer already voted on this transaction proposal'); |
|
|
|
if (txp.status != 'pending') return cb('The transaction proposal is not pending'); |
|
|
|
|
|
|
|
txp.reject(opts.copayerId); |
|
|
|
txp.reject(opts.copayerId); |
|
|
|
|
|
|
|
self.storage.storeTx(opts.walletId, txp, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
self.storage.storeTx(opts.walletId, txp, function (err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -452,17 +452,17 @@ CopayServer.prototype.rejectTx = function (opts, cb) { |
|
|
|
* @returns {TxProposal[]} Transaction proposal. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.getPendingTxs = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
var self = this; |
|
|
|
|
|
|
|
Utils.checkRequired(opts, 'walletId'); |
|
|
|
Utils.checkRequired(opts, 'walletId'); |
|
|
|
|
|
|
|
self.storage.fetchTxs(opts.walletId, function (err, txps) { |
|
|
|
if (err) return cb(err); |
|
|
|
self.storage.fetchTxs(opts.walletId, function (err, txps) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var pending = _.filter(txps, { status: 'pending' }); |
|
|
|
var pending = _.filter(txps, { status: 'pending' }); |
|
|
|
|
|
|
|
return cb(null, pending); |
|
|
|
}); |
|
|
|
return cb(null, pending); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|