bitcore-wallet-service/lib/model/txproposal.js

228 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-01-27 11:40:27 -08:00
'use strict';
2015-01-28 12:06:29 -08:00
var _ = require('lodash');
2015-02-07 07:48:57 -08:00
var Uuid = require('uuid');
2015-03-12 07:49:01 -07:00
var WalletUtils = require('bitcore-wallet-utils');
var Bitcore = WalletUtils.Bitcore;
2015-02-06 10:15:54 -08:00
var Address = Bitcore.Address;
2015-01-28 12:06:29 -08:00
2015-01-28 11:40:07 -08:00
var TxProposalAction = require('./txproposalaction');
2015-02-17 16:20:08 -08:00
function TxProposal() {
this.version = '1.0.0';
};
2015-02-02 10:29:14 -08:00
2015-02-17 16:20:08 -08:00
TxProposal.create = function(opts) {
2015-02-02 12:07:18 -08:00
opts = opts || {};
2015-01-27 11:40:27 -08:00
2015-02-17 16:20:08 -08:00
var x = new TxProposal();
2015-02-12 05:26:13 -08:00
var now = Date.now();
2015-02-17 16:20:08 -08:00
x.createdOn = Math.floor(now / 1000);
2015-02-21 20:35:25 -08:00
x.id = _.padLeft(now, 14, '0') + Uuid.v4();
2015-03-30 07:29:19 -07:00
x.walletId = opts.walletId;
2015-02-17 16:20:08 -08:00
x.creatorId = opts.creatorId;
x.toAddress = opts.toAddress;
x.amount = opts.amount;
x.message = opts.message;
2015-03-26 13:52:59 -07:00
x.payProUrl = opts.payProUrl;
2015-02-17 16:20:08 -08:00
x.proposalSignature = opts.proposalSignature;
x.changeAddress = opts.changeAddress;
x.inputs = [];
x.inputPaths = [];
x.requiredSignatures = opts.requiredSignatures;
x.requiredRejections = opts.requiredRejections;
x.status = 'pending';
2015-02-20 07:33:46 -08:00
x.actions = [];
2015-03-12 08:01:44 -07:00
x.outputOrder = _.shuffle(_.range(2));
2015-03-12 11:21:24 -07:00
x.fee = null;
2015-02-17 16:20:08 -08:00
return x;
2015-01-27 11:40:27 -08:00
};
2015-02-05 10:50:18 -08:00
TxProposal.fromObj = function(obj) {
2015-02-02 12:07:18 -08:00
var x = new TxProposal();
2015-01-27 11:40:27 -08:00
2015-02-02 10:29:14 -08:00
x.version = obj.version;
2015-02-02 12:07:18 -08:00
x.createdOn = obj.createdOn;
x.id = obj.id;
2015-03-30 07:29:19 -07:00
x.walletId = obj.walletId;
2015-02-02 12:07:18 -08:00
x.creatorId = obj.creatorId;
x.toAddress = obj.toAddress;
x.amount = obj.amount;
x.message = obj.message;
2015-03-26 13:52:59 -07:00
x.payProUrl = obj.payProUrl;
2015-02-10 05:22:23 -08:00
x.proposalSignature = obj.proposalSignature;
2015-02-02 12:07:18 -08:00
x.changeAddress = obj.changeAddress;
x.inputs = obj.inputs;
x.requiredSignatures = obj.requiredSignatures;
2015-02-09 07:27:50 -08:00
x.requiredRejections = obj.requiredRejections;
2015-02-02 12:07:18 -08:00
x.status = obj.status;
x.txid = obj.txid;
2015-02-24 07:27:44 -08:00
x.broadcastedOn = obj.broadcastedOn;
2015-02-04 06:43:12 -08:00
x.inputPaths = obj.inputPaths;
2015-02-20 07:33:46 -08:00
x.actions = _.map(obj.actions, function(action) {
return TxProposalAction.fromObj(action);
2015-02-02 12:07:18 -08:00
});
2015-03-12 08:01:44 -07:00
x.outputOrder = obj.outputOrder;
2015-03-12 11:21:24 -07:00
x.fee = obj.fee;
2015-02-02 12:07:18 -08:00
return x;
2015-01-27 11:40:27 -08:00
};
2015-02-04 16:38:23 -08:00
2015-02-05 10:50:18 -08:00
TxProposal.prototype._updateStatus = function() {
2015-02-02 12:07:18 -08:00
if (this.status != 'pending') return;
2015-01-28 11:40:07 -08:00
2015-02-02 12:07:18 -08:00
if (this.isRejected()) {
this.status = 'rejected';
} else if (this.isAccepted()) {
this.status = 'accepted';
}
2015-01-28 11:40:07 -08:00
};
2015-02-04 16:38:23 -08:00
2015-02-13 16:00:12 -08:00
TxProposal.prototype._getCurrentSignatures = function() {
2015-02-20 07:33:46 -08:00
var acceptedActions = _.filter(this.actions, {
type: 'accept'
2015-02-13 16:16:18 -08:00
});
return _.map(acceptedActions, function(x) {
2015-02-13 16:00:12 -08:00
return {
signatures: x.signatures,
2015-02-13 16:16:18 -08:00
xpub: x.xpub,
2015-02-13 16:00:12 -08:00
};
});
};
TxProposal.prototype.getBitcoreTx = function() {
2015-02-05 10:50:18 -08:00
var self = this;
2015-03-12 07:49:01 -07:00
var t = WalletUtils.buildTx(this);
2015-02-13 16:00:12 -08:00
var sigs = this._getCurrentSignatures();
_.each(sigs, function(x) {
2015-02-13 16:16:18 -08:00
self._addSignaturesToBitcoreTx(t, x.signatures, x.xpub);
2015-02-13 16:00:12 -08:00
});
2015-02-05 10:50:18 -08:00
return t;
2015-02-04 16:38:23 -08:00
};
2015-02-06 10:15:54 -08:00
TxProposal.prototype.getNetworkName = function() {
return Bitcore.Address(this.toAddress).toObject().network;
2015-02-06 10:15:54 -08:00
};
2015-02-04 16:38:23 -08:00
2015-02-05 12:22:38 -08:00
TxProposal.prototype.getRawTx = function() {
var t = this.getBitcoreTx();
2015-02-05 12:22:38 -08:00
2015-03-04 08:37:00 -08:00
return t.uncheckedSerialize();
2015-02-05 12:22:38 -08:00
};
2015-02-10 11:55:00 -08:00
/**
* getActors
*
* @return {String[]} copayerIds that performed actions in this proposal (accept / reject)
*/
2015-02-10 11:30:58 -08:00
TxProposal.prototype.getActors = function() {
2015-02-20 07:33:46 -08:00
return _.pluck(this.actions, 'copayerId');
2015-02-10 11:30:58 -08:00
};
2015-02-10 11:55:00 -08:00
/**
* getActionBy
*
* @param {String} copayerId
* @return {Object} type / createdOn
*/
TxProposal.prototype.getActionBy = function(copayerId) {
2015-02-20 07:33:46 -08:00
return _.find(this.actions, {
copayerId: copayerId
});
2015-02-10 11:55:00 -08:00
};
2015-02-15 10:46:29 -08:00
TxProposal.prototype.addAction = function(copayerId, type, comment, signatures, xpub) {
2015-02-17 16:20:08 -08:00
var action = TxProposalAction.create({
2015-02-02 12:07:18 -08:00
copayerId: copayerId,
type: type,
2015-02-05 10:50:18 -08:00
signatures: signatures,
2015-02-13 16:00:12 -08:00
xpub: xpub,
2015-02-15 10:46:29 -08:00
comment: comment,
2015-02-02 12:07:18 -08:00
});
2015-02-20 07:33:46 -08:00
this.actions.push(action);
2015-02-02 12:07:18 -08:00
this._updateStatus();
2015-01-28 11:40:07 -08:00
};
2015-02-13 16:00:12 -08:00
TxProposal.prototype._addSignaturesToBitcoreTx = function(t, signatures, xpub) {
2015-02-05 10:50:18 -08:00
var self = this;
2015-02-04 16:38:23 -08:00
2015-02-05 10:50:18 -08:00
if (signatures.length != this.inputs.length)
2015-02-16 09:27:01 -08:00
throw new Error('Number of signatures does not match number of inputs');
2015-02-05 10:50:18 -08:00
2015-02-23 10:54:57 -08:00
var i = 0,
2015-02-05 10:50:18 -08:00
x = new Bitcore.HDPublicKey(xpub);
_.each(signatures, function(signatureHex) {
var input = self.inputs[i];
try {
var signature = Bitcore.crypto.Signature.fromString(signatureHex);
var pub = x.derive(self.inputPaths[i]).publicKey;
var s = {
inputIndex: i,
signature: signature,
sigtype: Bitcore.crypto.Signature.SIGHASH_ALL,
publicKey: pub,
};
2015-02-24 07:27:44 -08:00
t.inputs[i].addSignature(t, s);
2015-02-05 10:50:18 -08:00
i++;
2015-02-13 16:00:12 -08:00
} catch (e) {};
2015-02-05 10:50:18 -08:00
});
2015-02-13 16:00:12 -08:00
2015-02-23 10:54:57 -08:00
if (i != t.inputs.length)
2015-02-16 09:27:01 -08:00
throw new Error('Wrong signatures');
2015-02-04 16:38:23 -08:00
};
2015-02-05 10:50:18 -08:00
2015-02-13 16:00:12 -08:00
TxProposal.prototype.sign = function(copayerId, signatures, xpub) {
try {
2015-02-16 09:27:01 -08:00
// Tests signatures are OK
var t = this.getBitcoreTx();
2015-02-13 16:00:12 -08:00
this._addSignaturesToBitcoreTx(t, signatures, xpub);
2015-02-16 09:27:01 -08:00
2015-02-15 10:46:29 -08:00
this.addAction(copayerId, 'accept', null, signatures, xpub);
2015-02-13 16:00:12 -08:00
return true;
} catch (e) {
return false;
}
2015-01-28 11:40:07 -08:00
};
2015-02-15 10:46:29 -08:00
TxProposal.prototype.reject = function(copayerId, reason) {
this.addAction(copayerId, 'reject', reason);
2015-01-28 11:40:07 -08:00
};
2015-02-06 12:04:10 -08:00
TxProposal.prototype.isPending = function() {
2015-02-15 11:15:45 -08:00
return !_.contains(['broadcasted', 'rejected'], this.status);
2015-02-06 12:04:10 -08:00
};
2015-02-05 10:50:18 -08:00
TxProposal.prototype.isAccepted = function() {
2015-02-20 07:33:46 -08:00
var votes = _.countBy(this.actions, 'type');
2015-02-02 12:07:18 -08:00
return votes['accept'] >= this.requiredSignatures;
2015-01-28 11:40:07 -08:00
};
2015-02-05 10:50:18 -08:00
TxProposal.prototype.isRejected = function() {
2015-02-20 07:33:46 -08:00
var votes = _.countBy(this.actions, 'type');
2015-02-09 07:27:50 -08:00
return votes['reject'] >= this.requiredRejections;
2015-01-28 11:40:07 -08:00
};
2015-02-15 11:15:45 -08:00
TxProposal.prototype.isBroadcasted = function() {
return this.status == 'broadcasted';
};
2015-02-05 10:50:18 -08:00
TxProposal.prototype.setBroadcasted = function(txid) {
2015-02-02 12:07:18 -08:00
this.txid = txid;
this.status = 'broadcasted';
2015-02-24 07:27:44 -08:00
this.broadcastedOn = Math.floor(Date.now() / 1000);
2015-01-28 11:40:07 -08:00
};
2015-01-27 11:40:27 -08:00
module.exports = TxProposal;