bitcore-wallet-service/lib/server.js

470 lines
13 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-01-27 11:40:21 -08:00
var Explorers = require('bitcore-explorers');
2015-01-27 05:18:45 -08:00
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-01-27 07:54:17 -08:00
/**
* Creates an instance of the Copay server.
* @constructor
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-01-27 05:18:45 -08:00
function CopayServer(opts) {
2015-02-02 12:07:18 -08:00
opts = opts || {};
this.storage = opts.storage || new Storage();
2015-01-27 05:18:45 -08:00
};
2015-02-02 10:56:53 -08:00
inherits(CopayServer, events.EventEmitter);
CopayServer._emit = function (event) {
var args = Array.prototype.slice.call(arguments);
log.debug('Emitting: ', args);
this.emit.apply(this, arguments);
};
2015-01-27 05:18:45 -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-01-27 05:18:45 -08:00
CopayServer.prototype.createWallet = function (opts, cb) {
2015-02-02 12:07:18 -08:00
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']);
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');
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 12:07:18 -08:00
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);
});
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-01-27 07:54:17 -08:00
* @param {string} opts.id - The wallet id.
* @returns {Object} wallet
*/
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-02 12:07:18 -08:00
self.storage.fetchWallet(opts.id, function (err, wallet) {
if (err) return cb(err);
if (!wallet) return cb('Wallet not found');
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
*/
CopayServer.prototype._verifySignature = function (text, signature, pubKey) {
return SignUtils.verify( text, signature, pubKey);
};
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.
*/
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 12:07:18 -08:00
Utils.runLocked(opts.walletId, cb, function (cb) {
self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err) return cb(err);
2015-02-01 06:41:16 -08:00
if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) {
return cb('Bad request');
}
2015-02-02 12:07:18 -08:00
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();
});
});
});
2015-01-27 05:18:45 -08:00
};
2015-01-27 11:40:21 -08:00
CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) {
2015-02-02 12:07:18 -08:00
throw 'not implemented';
2015-01-27 05:18:45 -08:00
};
2015-01-27 11:40:21 -08:00
/**
2015-02-02 04:12:08 -08:00
*
* TODO: How this is going to be authenticated?
*
2015-01-27 11:40:21 -08:00
* Creates a new address.
* @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {truthy} opts.isChange - Indicates whether this is a regular address or a change address.
* @returns {Address} address
*/
CopayServer.prototype.createAddress = function (opts, cb) {
2015-02-02 12:07:18 -08:00
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);
});
});
});
});
2015-01-27 05:18:45 -08:00
};
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.walletId - The wallet id.
* @param {string} opts.copayerId - The wallet id.
* @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.
*/
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-02 12:07:18 -08:00
Utils.checkRequired(opts, ['walletId', 'copayerId', 'message', 'signature']);
2015-02-02 10:29:14 -08:00
2015-02-02 12:07:18 -08:00
self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err) return cb(err);
2015-01-28 09:21:09 -08:00
2015-02-02 12:07:18 -08:00
var copayer = wallet.getCopayer(opts.copayerId);
if (!copayer) return cb('Copayer not found');
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
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
};
CopayServer.prototype._getUtxos = function (opts, cb) {
2015-02-02 12:07:18 -08:00
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);
});
});
});
2015-01-27 05:18:45 -08:00
};
2015-01-30 12:37:30 -08:00
/**
* Creates a new transaction proposal.
* @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @returns {Object} balance - Total amount & locked amount.
*/
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-02 12:07:18 -08:00
Utils.checkRequired(opts, 'walletId');
2015-02-02 10:29:14 -08:00
2015-02-02 12:07:18 -08:00
self._getUtxos({ walletId: opts.walletId }, function (err, utxos) {
if (err) return cb(err);
2015-01-30 12:37:30 -08:00
2015-02-02 12:07:18 -08:00
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);
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-01-30 13:29:46 -08:00
CopayServer.prototype._createRawTx = function (txp) {
2015-02-02 12:07:18 -08:00
var rawTx = new Bitcore.Transaction()
.from(tx.inputs)
.to(txp.toAddress, txp.amount)
.change(txp.changeAddress);
2015-01-27 05:18:45 -08:00
2015-02-02 12:07:18 -08:00
return rawTx;
2015-01-27 05:18:45 -08:00
};
2015-01-30 13:29:46 -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');
while (i < inputs.length) {
selected.push(inputs[i]);
total += inputs[i].amount;
if (total >= txp.amount) {
break;
}
i++;
};
return selected;
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.walletId - The wallet id.
* @param {string} opts.copayerId - The wallet id.
* @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-01-27 11:40:21 -08:00
* @returns {TxProposal} Transaction proposal.
2015-01-27 07:54:17 -08:00
*/
2015-01-27 05:18:45 -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-02 12:07:18 -08:00
Utils.checkRequired(opts, ['walletId', 'copayerId', 'toAddress', 'amount', 'message']);
2015-02-02 10:29:14 -08:00
2015-02-02 12:07:18 -08:00
self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err) return cb(err);
2015-01-27 05:18:45 -08:00
2015-02-02 12:07:18 -08:00
self._getUtxos({ walletId: wallet.id }, function (err, utxos) {
if (err) return cb(err);
2015-01-28 12:06:29 -08:00
2015-02-02 12:07:18 -08:00
utxos = _.without(utxos, { locked: true });
2015-01-30 12:37:30 -08:00
2015-02-02 12:07:18 -08:00
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,
});
2015-01-30 12:37:30 -08:00
2015-02-02 12:07:18 -08:00
txp.rawTx = self._createRawTx(txp);
2015-01-28 07:06:34 -08:00
2015-02-02 12:07:18 -08:00
self.storage.storeTx(wallet.id, txp, function (err) {
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-01-28 07:06:34 -08:00
};
2015-01-28 11:40:07 -08:00
CopayServer.prototype._broadcastTx = function (rawTx, cb) {
2015-02-02 12:07:18 -08:00
// TODO: this should attempt to broadcast _all_ accepted and not-yet broadcasted (status=='accepted') txps?
cb = cb || function () {};
2015-01-28 08:28:18 -08:00
2015-02-02 12:07:18 -08:00
throw 'not implemented';
2015-01-28 08:28:18 -08:00
};
2015-01-28 07:06:34 -08:00
/**
* Sign a transaction proposal.
* @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {string} opts.copayerId - The wallet id.
2015-01-28 08:28:18 -08:00
* @param {string} opts.txProposalId - The identifier of the transaction.
2015-01-28 07:06:34 -08:00
* @param {string} opts.signature - The signature of the tx for this copayer.
*/
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-02 12:07:18 -08:00
Utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId', 'signature']);
2015-02-02 10:29:14 -08:00
2015-02-02 12:07:18 -08:00
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');
2015-01-27 05:18:45 -08:00
2015-02-02 12:07:18 -08:00
txp.sign(opts.copayerId, opts.signature);
2015-01-28 08:28:18 -08:00
2015-02-02 12:07:18 -08:00
self.storage.storeTx(opts.walletId, txp, function (err) {
if (err) return cb(err);
2015-01-28 11:40:07 -08:00
2015-02-02 12:07:18 -08:00
if (txp.status == 'accepted');
self._broadcastTx(txp.rawTx, function (err, txid) {
if (err) return cb(err);
2015-01-27 05:18:45 -08:00
2015-02-02 12:07:18 -08:00
tx.setBroadcasted(txid);
self.storage.storeTx(opts.walletId, txp, function (err) {
if (err) return cb(err);
2015-01-27 05:18:45 -08:00
2015-02-02 12:07:18 -08:00
return cb();
});
});
});
});
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.walletId - The wallet id.
* @param {string} opts.copayerId - The wallet id.
* @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
*/
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-02 12:07:18 -08:00
Utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId']);
2015-02-02 10:29:14 -08:00
2015-02-02 12:07:18 -08:00
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');
2015-01-29 09:57:26 -08:00
2015-02-02 12:07:18 -08:00
txp.reject(opts.copayerId);
2015-01-29 09:57:26 -08:00
2015-02-02 12:07:18 -08:00
self.storage.storeTx(opts.walletId, txp, function (err) {
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
* @param {string} opts.walletId - The wallet id.
* @returns {TxProposal[]} Transaction proposal.
*/
2015-01-27 05:18:45 -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-02 12:07:18 -08:00
Utils.checkRequired(opts, 'walletId');
2015-02-02 10:29:14 -08:00
2015-02-02 12:07:18 -08:00
self.storage.fetchTxs(opts.walletId, function (err, txps) {
if (err) return cb(err);
2015-01-28 12:40:37 -08:00
2015-02-02 12:07:18 -08:00
var pending = _.filter(txps, { status: 'pending' });
2015-01-28 12:40:37 -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;