bitcore-wallet-service/lib/server.js

581 lines
16 KiB
JavaScript
Raw Normal View History

2015-01-27 05:18:45 -08:00
'use strict';
var _ = require('lodash');
var $ = require('preconditions').singleton();
var async = require('async');
var log = require('npmlog');
log.debug = log.verbose;
2015-02-02 10:56:53 -08:00
var inherits = require('inherits');
var events = require('events');
2015-02-02 10:29:14 -08:00
2015-01-27 11:40:21 -08:00
var Bitcore = require('bitcore');
2015-01-31 14:56:50 -08:00
var PublicKey = Bitcore.PublicKey;
2015-02-01 11:50:58 -08:00
var HDPublicKey = Bitcore.HDPublicKey;
2015-02-06 10:15:54 -08:00
var Address = Bitcore.Address;
2015-01-27 11:40:21 -08:00
var Explorers = require('bitcore-explorers');
2015-01-27 05:18:45 -08:00
2015-02-04 08:31:02 -08:00
var ClientError = require('./clienterror');
2015-02-02 11:00:32 -08:00
var Utils = require('./utils');
2015-01-27 05:18:45 -08:00
var Storage = require('./storage');
2015-02-01 06:41:16 -08:00
var SignUtils = require('./signutils');
2015-01-27 11:40:21 -08:00
2015-01-27 05:18:45 -08:00
var Wallet = require('./model/wallet');
var Copayer = require('./model/copayer');
2015-01-27 11:40:21 -08:00
var Address = require('./model/address');
var TxProposal = require('./model/txproposal');
2015-01-27 05:18:45 -08:00
2015-02-01 11:50:58 -08:00
2015-02-06 12:56:51 -08:00
var initialized = false;
var storage;
2015-01-27 07:54:17 -08:00
/**
* Creates an instance of the Copay server.
* @constructor
2015-02-06 12:56:51 -08:00
*/
function CopayServer() {
if (!initialized) throw new Error('Server not initialized');
this.storage = storage;
};
/**
* Initializes global settings for all instances.
2015-01-28 05:52:45 -08:00
* @param {Object} opts
* @param {Storage} [opts.storage] - The storage provider.
2015-01-27 07:54:17 -08:00
*/
2015-02-06 12:56:51 -08:00
CopayServer.initialize = function (opts) {
2015-02-02 12:07:18 -08:00
opts = opts || {};
2015-02-06 12:56:51 -08:00
storage = opts.storage ||  new Storage();
initialized = true;
2015-01-27 05:18:45 -08:00
};
2015-02-06 12:56:51 -08:00
/**
* Gets an instance of the server after authenticating the copayer.
* @param {Object} opts
* @param {string} opts.copayerId - The copayer id making the request.
* @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) {
Utils.checkRequired(opts, ['copayerId', 'message', 'signature']);
var server = new CopayServer();
server.storage.fetchCopayerLookup(opts.copayerId, function (err, copayer) {
if (err) return cb(err);
if (!copayer) return cb('Copayer not found');
var isValid = server._verifySignature(opts.message, opts.signature, copayer.signingPubKey);
if (!isValid) return cb('Invalid signature');
2015-02-02 10:56:53 -08:00
2015-02-06 12:56:51 -08:00
server.copayerId = opts.copayerId;
server.walletId = copayer.walletId;
return cb(null, server);
});
2015-02-02 10:56:53 -08:00
};
2015-01-27 05:18:45 -08:00
2015-02-06 12:56:51 -08:00
2015-01-27 07:54:17 -08:00
/**
* Creates a new wallet.
2015-01-27 11:40:21 -08:00
* @param {Object} opts
2015-01-27 07:54:17 -08:00
* @param {string} opts.id - The wallet id.
* @param {string} opts.name - The wallet name.
* @param {number} opts.m - Required copayers.
* @param {number} opts.n - Total copayers.
* @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.
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.createWallet = function(opts, cb) {
var self = this,
pubKey;
2015-01-27 05:18:45 -08:00
2015-02-02 12:07:18 -08:00
Utils.checkRequired(opts, ['id', 'name', 'm', 'n', 'pubKey']);
2015-02-04 08:31:02 -08:00
if (!Wallet.verifyCopayerLimits(opts.m, opts.n)) return cb(new ClientError('Invalid combination of required copayers / total copayers'));
2015-02-02 12:07:18 -08:00
var network = opts.network || 'livenet';
2015-02-04 08:31:02 -08:00
if (network != 'livenet' && network != 'testnet') return cb(new ClientError('Invalid network'));
2015-02-02 10:29:14 -08:00
2015-01-31 14:56:50 -08:00
try {
pubKey = new PublicKey.fromString(opts.pubKey);
} catch (e) {
return cb(e.toString());
};
2015-01-30 06:58:28 -08:00
2015-02-02 15:13:13 -08:00
self.storage.fetchWallet(opts.id, function(err, wallet) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-04 08:31:02 -08:00
if (wallet) return cb(new ClientError('WEXISTS', 'Wallet already exists'));
2015-02-02 12:07:18 -08:00
var wallet = new Wallet({
id: opts.id,
name: opts.name,
m: opts.m,
n: opts.n,
2015-02-02 06:55:03 -08:00
network: opts.network || 'livenet',
2015-02-02 12:07:18 -08:00
pubKey: pubKey,
});
2015-02-02 06:55:03 -08:00
2015-02-02 12:07:18 -08:00
self.storage.storeWallet(wallet, cb);
});
2015-01-27 05:18:45 -08:00
};
2015-01-27 07:54:17 -08:00
/**
* Retrieves a wallet from storage.
2015-01-27 11:40:21 -08:00
* @param {Object} opts
2015-02-02 06:55:03 -08:00
* @returns {Object} wallet
2015-01-27 07:54:17 -08:00
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.getWallet = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-01-27 05:18:45 -08:00
2015-02-06 12:56:51 -08:00
self.storage.fetchWallet(self.walletId, function(err, wallet) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-04 08:31:02 -08:00
if (!wallet) return cb(new ClientError('Wallet not found'));
2015-02-02 12:07:18 -08:00
return cb(null, wallet);
});
2015-01-27 05:18:45 -08:00
};
2015-01-28 05:36:49 -08:00
2015-02-01 06:41:16 -08:00
/**
* Verifies a signature
* @param text
* @param signature
* @param pubKey
*/
2015-02-02 06:55:03 -08:00
CopayServer.prototype._verifySignature = function(text, signature, pubKey) {
return SignUtils.verify(text, signature, pubKey);
2015-02-01 06:41:16 -08:00
};
2015-01-27 07:54:17 -08:00
/**
* Joins a wallet in creation.
2015-01-27 11:40:21 -08:00
* @param {Object} opts
2015-01-27 07:54:17 -08:00
* @param {string} opts.walletId - The wallet id.
* @param {string} opts.id - The copayer id.
* @param {string} opts.name - The copayer name.
* @param {number} opts.xPubKey - Extended Public Key for this copayer.
* @param {number} opts.xPubKeySignature - Signature of xPubKey using the wallet pubKey.
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.joinWallet = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-01-27 05:18:45 -08:00
2015-02-02 12:07:18 -08:00
Utils.checkRequired(opts, ['walletId', 'id', 'name', 'xPubKey', 'xPubKeySignature']);
2015-02-02 10:29:14 -08:00
2015-02-02 15:13:13 -08:00
Utils.runLocked(opts.walletId, cb, function(cb) {
2015-02-06 12:56:51 -08:00
self.storage.fetchWallet(opts.walletId, function(err, wallet) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-06 12:56:51 -08:00
if (!wallet) return cb(new ClientError('Wallet not found'));
2015-02-01 06:41:16 -08:00
if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) {
2015-02-04 08:31:02 -08:00
return cb(new ClientError());
2015-02-01 06:41:16 -08:00
}
2015-02-02 06:55:03 -08:00
if (_.find(wallet.copayers, {
xPubKey: opts.xPubKey
2015-02-04 08:31:02 -08:00
})) return cb(new ClientError('CINWALLET', 'Copayer already in wallet'));
if (wallet.copayers.length == wallet.n) return cb(new ClientError('WFULL', 'Wallet full'));
2015-02-02 06:55:03 -08:00
2015-02-02 12:07:18 -08:00
var copayer = new Copayer({
id: opts.id,
name: opts.name,
xPubKey: opts.xPubKey,
xPubKeySignature: opts.xPubKeySignature,
2015-02-02 04:36:55 -08:00
copayerIndex: wallet.copayers.length,
2015-02-02 12:07:18 -08:00
});
2015-02-02 15:13:13 -08:00
2015-02-02 12:07:18 -08:00
wallet.addCopayer(copayer);
2015-02-06 05:46:41 -08:00
self.storage.storeWalletAndUpdateCopayersLookup(wallet, function(err) {
2015-02-02 06:55:03 -08:00
return cb(err);
2015-02-02 12:07:18 -08:00
});
});
});
2015-01-27 05:18:45 -08:00
};
2015-01-27 11:40:21 -08:00
/**
* Creates a new address.
* @param {Object} opts
* @param {truthy} opts.isChange - Indicates whether this is a regular address or a change address.
2015-02-02 06:55:03 -08:00
* @returns {Address} address
2015-01-27 11:40:21 -08:00
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.createAddress = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-02-02 11:32:13 -08:00
var isChange = opts.isChange || false;
2015-02-02 12:07:18 -08:00
2015-02-06 12:56:51 -08:00
Utils.checkRequired(opts, ['isChange']);
2015-02-02 12:07:18 -08:00
2015-02-06 12:56:51 -08:00
Utils.runLocked(self.walletId, cb, function(cb) {
self.getWallet({}, function(err, wallet) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-02 15:13:13 -08:00
2015-02-03 10:44:44 -08:00
var address = wallet.createAddress(opts.isChange);
2015-02-02 11:32:13 -08:00
2015-02-03 12:32:40 -08:00
self.storage.storeAddress(wallet.id, address, function(err) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-04 11:18:36 -08:00
2015-02-03 12:32:40 -08:00
self.storage.storeWallet(wallet, function(err) {
if (err) {
2015-02-04 11:18:36 -08:00
self.storage.removeAddress(wallet.id, address, function() {
2015-02-03 12:32:40 -08:00
return cb(err);
});
} else {
return cb(null, address);
}
2015-02-02 12:07:18 -08:00
});
});
});
});
2015-01-27 05:18:45 -08:00
};
2015-02-03 12:32:40 -08:00
/**
* Get all addresses.
* @param {Object} opts
* @returns {Address[]}
*/
2015-02-04 11:18:36 -08:00
CopayServer.prototype.getAddresses = function(opts, cb) {
2015-02-03 12:32:40 -08:00
var self = this;
2015-02-06 12:56:51 -08:00
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
2015-02-03 12:32:40 -08:00
if (err) return cb(err);
return cb(null, addresses);
});
};
2015-01-28 07:06:34 -08:00
/**
* Verifies that a given message was actually sent by an authorized copayer.
* @param {Object} opts
* @param {string} opts.message - The message to verify.
* @param {string} opts.signature - The signature of message to verify.
* @returns {truthy} The result of the verification.
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.verifyMessageSignature = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-01-28 07:06:34 -08:00
2015-02-06 12:56:51 -08:00
Utils.checkRequired(opts, ['message', 'signature']);
2015-02-02 10:29:14 -08:00
2015-02-06 12:56:51 -08:00
self.getWallet({}, function(err, wallet) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-01-28 09:21:09 -08:00
2015-02-06 12:56:51 -08:00
var copayer = wallet.getCopayer(self.copayerId);
2015-01-28 07:06:34 -08:00
2015-02-02 12:07:18 -08:00
var isValid = self._verifySignature(opts.message, opts.signature, copayer.signingPubKey);
return cb(null, isValid);
});
2015-01-28 07:06:34 -08:00
};
2015-01-27 05:18:45 -08:00
2015-02-02 15:13:13 -08:00
CopayServer.prototype._getBlockExplorer = function(provider, network) {
2015-02-02 12:07:18 -08:00
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;
}
2015-01-27 05:18:45 -08:00
};
2015-02-05 12:22:38 -08:00
/**
* _getUtxos
*
*/
2015-02-06 12:56:51 -08:00
CopayServer.prototype._getUtxos = function(cb) {
2015-02-02 12:07:18 -08:00
var self = this;
// Get addresses for this wallet
2015-02-06 12:56:51 -08:00
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-04 08:31:02 -08:00
if (addresses.length == 0) return cb(new ClientError('The wallet has no addresses'));
2015-02-02 12:07:18 -08:00
2015-02-04 06:43:12 -08:00
var addressStrs = _.pluck(addresses, 'address');
var addressToPath = _.indexBy(addresses, 'address'); // TODO : check performance
2015-02-06 10:15:54 -08:00
var networkName = Bitcore.Address(addressStrs[0]).toObject().networkName;
2015-02-02 12:07:18 -08:00
2015-02-06 10:15:54 -08:00
var bc = self._getBlockExplorer('insight', networkName);
2015-02-04 06:43:12 -08:00
bc.getUnspentUtxos(addressStrs, function(err, utxos) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-06 12:56:51 -08:00
self.getPendingTxs({}, function(err, txps) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
var inputs = _.chain(txps)
.pluck('inputs')
.flatten()
2015-02-02 15:13:13 -08:00
.map(function(utxo) {
return utxo.txid + '|' + utxo.vout
})
.value();
2015-02-02 12:07:18 -08:00
2015-02-04 11:18:36 -08:00
var dictionary = _.reduce(utxos, function(memo, utxo) {
memo[utxo.txid + '|' + utxo.vout] = utxo;
return memo;
}, {});
2015-02-02 12:07:18 -08:00
2015-02-02 15:13:13 -08:00
_.each(inputs, function(input) {
2015-02-02 12:07:18 -08:00
if (dictionary[input]) {
dictionary[input].locked = true;
}
});
2015-02-03 18:17:06 -08:00
2015-02-04 06:43:12 -08:00
// Needed for the clients to sign UTXOs
_.each(utxos, function(utxo) {
utxo.path = addressToPath[utxo.address].path;
utxo.publicKeys = addressToPath[utxo.address].publicKeys;
});
2015-02-03 18:17:06 -08:00
2015-02-02 12:07:18 -08:00
return cb(null, utxos);
});
});
});
2015-01-27 05:18:45 -08:00
};
2015-01-30 12:37:30 -08:00
/**
* Creates a new transaction proposal.
* @param {Object} opts
2015-02-02 06:55:03 -08:00
* @returns {Object} balance - Total amount & locked amount.
2015-01-30 12:37:30 -08:00
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.getBalance = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-01-30 12:37:30 -08:00
2015-02-06 12:56:51 -08:00
self._getUtxos(function(err, utxos) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-01-30 12:37:30 -08:00
2015-02-02 12:07:18 -08:00
var balance = {};
2015-02-03 18:17:06 -08:00
balance.totalAmount = Utils.strip(_.reduce(utxos, function(sum, utxo) {
2015-02-04 11:18:36 -08:00
return sum + self._inputSatoshis(utxo);
2015-02-03 18:17:06 -08:00
}, 0));
balance.lockedAmount = Utils.strip(_.reduce(_.filter(utxos, {
2015-02-02 15:13:13 -08:00
locked: true
}), function(sum, utxo) {
2015-02-04 11:18:36 -08:00
return sum + self._inputSatoshis(utxo);
2015-02-03 18:17:06 -08:00
}, 0));
2015-01-30 12:37:30 -08:00
2015-02-02 12:07:18 -08:00
return cb(null, balance);
});
2015-01-30 12:37:30 -08:00
};
2015-02-06 12:56:51 -08:00
// TODO: should be in Utils
2015-02-04 11:18:36 -08:00
CopayServer.prototype._inputSatoshis = function(i) {
return i.amount ? Utils.strip(i.amount * 1e8) : i.satoshis;
};
2015-02-02 06:55:03 -08:00
CopayServer.prototype._selectUtxos = function(txp, utxos) {
2015-02-02 12:07:18 -08:00
var i = 0;
var total = 0;
var selected = [];
var inputs = _.sortBy(utxos, 'amount');
2015-02-02 12:07:18 -08:00
while (i < inputs.length) {
selected.push(inputs[i]);
2015-02-04 11:18:36 -08:00
total += this._inputSatoshis(inputs[i]);
2015-02-02 12:07:18 -08:00
if (total >= txp.amount) {
2015-02-04 11:18:36 -08:00
2015-02-02 12:07:18 -08:00
break;
}
i++;
};
2015-02-04 11:18:36 -08:00
return total >= txp.amount ? selected : null;
2015-01-30 13:29:46 -08:00
};
2015-01-27 05:18:45 -08:00
2015-01-27 07:54:17 -08:00
/**
* Creates a new transaction proposal.
2015-01-27 11:40:21 -08:00
* @param {Object} opts
2015-01-27 07:54:17 -08:00
* @param {string} opts.toAddress - Destination address.
* @param {number} opts.amount - Amount to transfer in satoshi.
* @param {string} opts.message - A message to attach to this transaction.
2015-02-02 06:55:03 -08:00
* @returns {TxProposal} Transaction proposal.
2015-01-27 07:54:17 -08:00
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.createTx = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-01-27 05:18:45 -08:00
2015-02-06 12:56:51 -08:00
Utils.checkRequired(opts, ['toAddress', 'amount', 'message']);
2015-02-02 10:29:14 -08:00
2015-02-03 18:17:06 -08:00
// TODO?
// Check some parameters like:
// amount > dust
2015-02-06 12:56:51 -08:00
self.getWallet({}, function(err, wallet) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-01-27 05:18:45 -08:00
2015-02-06 12:56:51 -08:00
self._getUtxos(function(err, utxos) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-01-28 12:06:29 -08:00
2015-02-03 18:17:06 -08:00
var changeAddress = wallet.createAddress(true).address;
utxos = _.reject(utxos, {
2015-02-02 15:13:13 -08:00
locked: true
});
2015-01-30 12:37:30 -08:00
2015-02-02 12:07:18 -08:00
var txp = new TxProposal({
2015-02-06 12:56:51 -08:00
creatorId: self.copayerId,
2015-02-02 12:07:18 -08:00
toAddress: opts.toAddress,
amount: opts.amount,
2015-02-03 18:17:06 -08:00
changeAddress: changeAddress,
2015-02-02 12:07:18 -08:00
requiredSignatures: wallet.m,
maxRejections: wallet.n - wallet.m,
});
2015-01-30 12:37:30 -08:00
2015-02-03 18:17:06 -08:00
txp.inputs = self._selectUtxos(txp, utxos);
if (!txp.inputs) {
2015-02-04 08:31:02 -08:00
return cb(new ClientError('INSUFFICIENTFUNDS', 'Insufficient funds'));
}
2015-01-28 07:06:34 -08:00
2015-02-04 11:18:36 -08:00
txp.inputPaths = _.pluck(txp.inputs, 'path');
2015-02-04 06:43:12 -08:00
2015-02-03 18:17:06 -08:00
// no need to do this now: // TODO remove this comment
//self._createRawTx(txp);
2015-02-02 15:13:13 -08:00
self.storage.storeTx(wallet.id, txp, function(err) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-01-28 07:06:34 -08:00
2015-02-02 12:07:18 -08:00
return cb(null, txp);
});
});
});
2015-02-02 15:13:13 -08:00
};
2015-01-28 07:06:34 -08:00
2015-02-04 10:45:08 -08:00
/**
* Retrieves a tx from storage.
* @param {Object} opts
* @param {string} opts.id - The tx id.
* @returns {Object} txProposal
*/
CopayServer.prototype.getTx = function(opts, cb) {
var self = this;
2015-02-06 12:56:51 -08:00
self.storage.fetchTx(self.walletId, opts.id, function(err, txp) {
2015-02-04 10:45:08 -08:00
if (err) return cb(err);
if (!txp) return cb(new ClientError('Transaction proposal not found'));
return cb(null, txp);
});
};
2015-02-06 10:51:40 -08:00
CopayServer.prototype._broadcastTx = function(txp, cb) {
2015-02-05 12:22:38 -08:00
var raw = txp.getRawTx();
2015-02-06 10:51:40 -08:00
var bc = this._getBlockExplorer('insight', txp.getNetworkName());
2015-02-05 12:22:38 -08:00
bc.broadcast(raw, function(err, txid) {
return cb(err, txid);
})
2015-01-28 08:28:18 -08:00
};
2015-01-28 07:06:34 -08:00
/**
* Sign a transaction proposal.
* @param {Object} opts
2015-01-28 08:28:18 -08:00
* @param {string} opts.txProposalId - The identifier of the transaction.
2015-02-04 11:18:36 -08:00
* @param {string} opts.signatures - The signatures of the inputs of this tx for this copayer (in apperance order)
2015-01-28 07:06:34 -08:00
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.signTx = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-01-28 07:06:34 -08:00
2015-02-06 12:56:51 -08:00
Utils.checkRequired(opts, ['txProposalId', 'signatures']);
2015-02-02 10:29:14 -08:00
2015-02-06 12:56:51 -08:00
self.getWallet({}, function(err, wallet) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-01-27 05:18:45 -08:00
2015-02-05 10:50:18 -08:00
self.getTx({
id: opts.txProposalId
}, function(err, txp) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-05 10:50:18 -08:00
if (!txp) return cb(new ClientError('Transaction proposal not found'));
var action = _.find(txp.actions, {
copayerId: opts.copayerId
});
2015-02-05 12:22:38 -08:00
if (action)
2015-02-05 10:50:18 -08:00
return cb(new ClientError('CVOTED', 'Copayer already voted on this transaction proposal'));
2015-02-05 12:22:38 -08:00
if (txp.status != 'pending')
2015-02-05 10:50:18 -08:00
return cb(new ClientError('TXNOTPENDING', 'The transaction proposal is not pending'));
2015-01-28 11:40:07 -08:00
2015-02-06 12:56:51 -08:00
var copayer = wallet.getCopayer(self.copayerId);
2015-01-27 05:18:45 -08:00
2015-02-05 10:50:18 -08:00
if (!txp.checkSignatures(opts.signatures, copayer.xPubKey))
return cb(new ClientError('BADSIGNATURES', 'Bad signatures'));
2015-02-06 12:56:51 -08:00
txp.sign(self.copayerId, opts.signatures);
2015-02-05 10:50:18 -08:00
2015-02-06 12:56:51 -08:00
self.storage.storeTx(self.walletId, txp, function(err) {
2015-02-05 10:50:18 -08:00
if (err) return cb(err);
if (txp.status == 'accepted') {
2015-02-06 10:51:40 -08:00
self._broadcastTx(txp, function(err, txid) {
2015-02-06 12:56:51 -08:00
if (err) return cb(err);
2015-02-04 11:18:36 -08:00
2015-02-06 10:15:54 -08:00
txp.setBroadcasted(txid);
2015-02-06 12:56:51 -08:00
self.storage.storeTx(self.walletId, txp, function(err) {
2015-02-05 10:50:18 -08:00
if (err) return cb(err);
return cb(null, txp);
});
2015-02-04 11:18:36 -08:00
});
2015-02-05 10:50:18 -08:00
} else {
return cb(null, txp);
}
});
2015-02-02 12:07:18 -08:00
});
});
2015-02-02 06:55:03 -08:00
};
2015-01-27 05:18:45 -08:00
2015-01-29 09:57:26 -08:00
/**
* Reject a transaction proposal.
* @param {Object} opts
* @param {string} opts.txProposalId - The identifier of the transaction.
2015-02-02 10:29:14 -08:00
* @param {string} [opts.reason] - A message to other copayers explaining the rejection.
2015-01-29 09:57:26 -08:00
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.rejectTx = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-01-29 09:57:26 -08:00
2015-02-06 12:56:51 -08:00
Utils.checkRequired(opts, ['txProposalId']);
2015-02-02 10:29:14 -08:00
2015-02-04 11:27:36 -08:00
self.getTx({
id: opts.txProposalId
}, function(err, txp) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-02-04 08:31:02 -08:00
if (!txp) return cb(new ClientError('Transaction proposal not found'));
2015-02-02 15:13:13 -08:00
var action = _.find(txp.actions, {
2015-02-06 12:56:51 -08:00
copayerId: self.copayerId
2015-02-02 15:13:13 -08:00
});
2015-02-04 08:31:02 -08:00
if (action) return cb(new ClientError('CVOTED', 'Copayer already voted on this transaction proposal'));
if (txp.status != 'pending') return cb(new ClientError('TXNOTPENDING', 'The transaction proposal is not pending'));
2015-01-29 09:57:26 -08:00
2015-02-06 12:56:51 -08:00
txp.reject(self.copayerId);
2015-01-29 09:57:26 -08:00
2015-02-06 12:56:51 -08:00
self.storage.storeTx(self.walletId, txp, function(err) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-01-29 09:57:26 -08:00
2015-02-02 12:07:18 -08:00
return cb();
});
});
2015-01-29 09:57:26 -08:00
};
2015-01-28 07:06:34 -08:00
/**
* Retrieves all pending transaction proposals.
* @param {Object} opts
2015-02-02 06:55:03 -08:00
* @returns {TxProposal[]} Transaction proposal.
2015-01-28 07:06:34 -08:00
*/
2015-02-02 15:13:13 -08:00
CopayServer.prototype.getPendingTxs = function(opts, cb) {
2015-02-02 12:07:18 -08:00
var self = this;
2015-01-27 05:18:45 -08:00
2015-02-06 12:51:21 -08:00
self.storage.fetchPendingTxs(self.walletId, function(err, txps) {
2015-02-02 12:07:18 -08:00
if (err) return cb(err);
2015-01-28 12:40:37 -08:00
2015-02-06 12:04:10 -08:00
var pending = _.filter(txps, function(txp) {
return txp.isPending();
2015-02-02 15:13:13 -08:00
});
2015-02-02 12:07:18 -08:00
return cb(null, pending);
});
2015-01-27 05:18:45 -08:00
};
module.exports = CopayServer;