You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
3.6 KiB

10 years ago
'use strict';
10 years ago
var _ = require('lodash');
10 years ago
var Guid = require('guid');
var Bitcore = require('bitcore');
10 years ago
10 years ago
var TxProposalAction = require('./txproposalaction');
var VERSION = '1.0.0';
10 years ago
function TxProposal(opts) {
opts = opts || {};
10 years ago
this.version = VERSION;
this.createdOn = Math.floor(Date.now() / 1000);
10 years ago
this.id = Guid.raw();
this.creatorId = opts.creatorId;
this.toAddress = opts.toAddress;
this.amount = opts.amount;
this.message = opts.message;
this.changeAddress = opts.changeAddress;
this.inputs = opts.inputs;
10 years ago
this.inputPaths = opts.inputPaths;
this.requiredSignatures = opts.requiredSignatures;
this.maxRejections = opts.maxRejections;
this.status = 'pending';
this.actions = {};
10 years ago
};
TxProposal.fromObj = function(obj) {
var x = new TxProposal();
10 years ago
x.version = obj.version;
x.createdOn = obj.createdOn;
x.id = obj.id;
x.creatorId = obj.creatorId;
x.toAddress = obj.toAddress;
x.amount = obj.amount;
x.message = obj.message;
x.changeAddress = obj.changeAddress;
x.inputs = obj.inputs;
x.requiredSignatures = obj.requiredSignatures;
x.maxRejections = obj.maxRejections;
x.status = obj.status;
x.txid = obj.txid;
10 years ago
x.inputPaths = obj.inputPaths;
x.actions = obj.actions;
_.each(x.actions, function(action, copayerId) {
x.actions[copayerId] = new TxProposalAction(action);
});
return x;
10 years ago
};
TxProposal.prototype._updateStatus = function() {
if (this.status != 'pending') return;
10 years ago
if (this.isRejected()) {
this.status = 'rejected';
} else if (this.isAccepted()) {
this.status = 'accepted';
}
10 years ago
};
TxProposal.prototype._getBitcoreTx = function(n) {
var self = this;
var t = new Bitcore.Transaction();
_.each(this.inputs, function(i) {
t.from(i, i.publicKeys, self.requiredSignatures)
});
t.to(this.toAddress, this.amount)
.change(this.changeAddress);
t._updateChangeOutput();
return t;
};
TxProposal.prototype.addAction = function(copayerId, type, signatures) {
var action = new TxProposalAction({
copayerId: copayerId,
type: type,
signatures: signatures,
});
this.actions[copayerId] = action;
this._updateStatus();
10 years ago
};
// TODO: no sure we should receive xpub or a list of pubkeys (pre derived)
TxProposal.prototype.checkSignatures = function(signatures, xpub) {
var self = this;
var t = this._getBitcoreTx();
if (signatures.length != this.inputs.length)
return false;
var oks = 0,
i = 0,
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,
};
i++;
t.applySignature(s);
oks++;
} catch (e) {
// TODO only for debug now
console.log('DEBUG ONLY:',e.message); //TODO
};
});
return oks === t.inputs.length;
};
TxProposal.prototype.sign = function(copayerId, signatures) {
this.addAction(copayerId, 'accept', signatures);
10 years ago
};
TxProposal.prototype.reject = function(copayerId) {
this.addAction(copayerId, 'reject');
10 years ago
};
TxProposal.prototype.isAccepted = function() {
var votes = _.countBy(_.values(this.actions), 'type');
return votes['accept'] >= this.requiredSignatures;
10 years ago
};
TxProposal.prototype.isRejected = function() {
var votes = _.countBy(_.values(this.actions), 'type');
return votes['reject'] > this.maxRejections;
10 years ago
};
TxProposal.prototype.setBroadcasted = function(txid) {
this.txid = txid;
this.status = 'broadcasted';
10 years ago
};
10 years ago
module.exports = TxProposal;