Wallet/js/models/core/TxProposals.js

274 lines
6.5 KiB
JavaScript
Raw Normal View History

2014-04-09 17:28:35 -03:00
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
var util = bitcore.util;
2014-04-09 17:28:35 -03:00
var Transaction = bitcore.Transaction;
var Builder = bitcore.TransactionBuilder;
var Script = bitcore.Script;
2014-04-09 17:28:35 -03:00
var buffertools = bitcore.buffertools;
2014-04-09 20:37:14 -03:00
function TxProposal(opts) {
this.creator = opts.creator;
this.createdTs = opts.createdTs;
this.seenBy = opts.seenBy || {};
2014-04-10 02:16:57 -03:00
this.signedBy = opts.signedBy || {};
2014-04-23 02:01:54 -03:00
this.rejectedBy = opts.rejectedBy || {};
this.builder = opts.builder;
2014-04-20 21:53:54 -03:00
this.sentTs = opts.sentTs || null;
2014-04-21 07:27:45 -03:00
this.sentTxid = opts.sentTxid || null;
2014-05-30 15:07:52 -03:00
this.inputChainPaths = opts.inputChainPaths || [];
this.comment = opts.comment || null;
2014-04-18 19:28:28 -03:00
}
2014-06-17 16:42:49 -03:00
TxProposal.prototype.getID = function() {
var ntxid = this.builder.build().getNormalizedHash().toString('hex');
return ntxid;
};
2014-04-18 19:28:28 -03:00
TxProposal.prototype.toObj = function() {
var o = JSON.parse(JSON.stringify(this));
delete o['builder'];
o.builderObj = this.builder.toObj();
return o;
};
2014-04-20 21:23:53 -03:00
2014-04-21 07:27:45 -03:00
TxProposal.prototype.setSent = function(sentTxid) {
2014-04-21 11:28:25 -03:00
this.sentTxid = sentTxid;
this.sentTs = Date.now();
2014-04-20 21:23:53 -03:00
};
2014-04-18 19:28:28 -03:00
TxProposal.fromObj = function(o) {
var t = new TxProposal(o);
var b = new Builder.fromObj(o.builderObj);
t.builder = b;
return t;
2014-04-09 20:37:14 -03:00
};
2014-04-18 19:28:28 -03:00
2014-04-20 20:24:24 -03:00
TxProposal.getSentTs = function() {
return this.sentTs;
};
TxProposal.prototype.merge = function(other, author) {
var ret = {};
ret.events = this.mergeMetadata(other, author);
2014-06-18 10:09:40 -03:00
ret.hasChanged = this.mergeBuilder(other);
return ret;
};
2014-06-17 15:30:38 -03:00
TxProposal.prototype.mergeBuilder = function(other) {
var b0 = this.builder;
var b1 = other.builder;
// TODO: improve this comparison
var before = JSON.stringify(b0.toObj());
b0.merge(b1);
var after = JSON.stringify(b0.toObj());
return after !== before;
2014-06-17 15:30:38 -03:00
};
TxProposal.prototype.mergeMetadata = function(v1, author) {
2014-06-17 15:30:38 -03:00
var events = [];
var v0 = this;
var ntxid = this.getID();
2014-06-17 15:30:38 -03:00
Object.keys(v1.seenBy).forEach(function(k) {
if (!v0.seenBy[k]) {
// TODO: uncomment below and change protocol to make this work
//if (k != author) throw new Error('Non authoritative seenBy change by ' + author);
2014-06-17 15:30:38 -03:00
v0.seenBy[k] = v1.seenBy[k];
events.push({
type: 'seen',
cId: k,
txId: ntxid
2014-06-17 15:30:38 -03:00
});
}
});
Object.keys(v1.signedBy).forEach(function(k) {
if (!v0.signedBy[k]) {
// TODO: uncomment below and change protocol to make this work
//if (k != author) throw new Error('Non authoritative signedBy change by ' + author);
2014-06-17 15:30:38 -03:00
v0.signedBy[k] = v1.signedBy[k];
events.push({
type: 'signed',
cId: k,
txId: ntxid
2014-06-17 15:30:38 -03:00
});
}
});
Object.keys(v1.rejectedBy).forEach(function(k) {
if (!v0.rejectedBy[k]) {
// TODO: uncomment below and change protocol to make this work
//if (k != author) throw new Error('Non authoritative rejectedBy change by ' + author);
2014-06-17 15:30:38 -03:00
v0.rejectedBy[k] = v1.rejectedBy[k];
events.push({
type: 'rejected',
cId: k,
txId: ntxid
2014-06-17 15:30:38 -03:00
});
}
});
if (!v0.sentTxid && v1.sentTxid) {
v0.sentTs = v1.sentTs;
v0.sentTxid = v1.sentTxid;
events.push({
type: 'broadcast',
txId: ntxid
2014-06-17 15:30:38 -03:00
});
}
return events;
};
2014-04-09 20:37:14 -03:00
module.exports = require('soop')(TxProposal);
2014-04-09 17:28:35 -03:00
function TxProposals(opts) {
opts = opts || {};
this.walletId = opts.walletId;
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
2014-04-18 19:28:28 -03:00
this.txps = {};
2014-04-09 17:28:35 -03:00
}
2014-04-10 02:16:57 -03:00
TxProposals.fromObj = function(o) {
var ret = new TxProposals({
networkName: o.networkName,
walletId: o.walletId,
2014-04-10 02:16:57 -03:00
});
2014-04-18 19:28:28 -03:00
o.txps.forEach(function(o2) {
var t = TxProposal.fromObj(o2);
var id = t.builder.build().getNormalizedHash().toString('hex');
ret.txps[id] = t;
2014-04-10 02:16:57 -03:00
});
return ret;
};
2014-04-09 17:28:35 -03:00
2014-05-15 17:43:41 -03:00
TxProposals.prototype.getNtxids = function() {
return Object.keys(this.txps);
};
2014-05-15 17:43:41 -03:00
TxProposals.prototype.toObj = function(onlyThisNtxid) {
2014-06-18 10:09:40 -03:00
if (onlyThisNtxid) throw new Error();
2014-04-10 02:16:57 -03:00
var ret = [];
for (var id in this.txps) {
2014-05-15 17:43:41 -03:00
if (onlyThisNtxid && id != onlyThisNtxid)
continue;
2014-04-18 19:28:28 -03:00
var t = this.txps[id];
2014-04-20 20:24:24 -03:00
if (!t.sent)
ret.push(t.toObj());
2014-04-18 19:28:28 -03:00
}
return {
txps: ret,
walletId: this.walletId,
2014-04-10 02:16:57 -03:00
networkName: this.network.name,
};
2014-04-09 17:28:35 -03:00
};
TxProposals.prototype.merge = function(inTxp, author) {
2014-06-17 15:30:38 -03:00
var myTxps = this.txps;
var ntxid = inTxp.getID();
2014-06-18 10:09:40 -03:00
var ret = {};
ret.events = [];
ret.events.hasChanged = false;
if (myTxps[ntxid]) {
var v0 = myTxps[ntxid];
var v1 = inTxp;
ret = v0.merge(v1, author);
} else {
2014-06-18 10:21:26 -03:00
this.txps[ntxid] = inTxp;
ret.hasChanged = true;
ret.events.push({
type: 'new',
cid: inTxp.creator,
tx: ntxid
});
}
return ret;
2014-04-11 01:09:42 -03:00
};
var preconditions = require('preconditions').instance();
2014-04-15 18:23:35 -03:00
TxProposals.prototype.add = function(data) {
preconditions.checkArgument(data.inputChainPaths);
preconditions.checkArgument(data.signedBy);
preconditions.checkArgument(data.creator);
preconditions.checkArgument(data.createdTs);
preconditions.checkArgument(data.builder);
2014-06-17 16:42:49 -03:00
var txp = new TxProposal(data);
var ntxid = txp.getID();
this.txps[ntxid] = txp;
2014-05-15 16:39:22 -03:00
return ntxid;
2014-04-18 19:28:28 -03:00
};
TxProposals.prototype.setSent = function(ntxid, txid) {
2014-04-20 20:24:24 -03:00
//sent TxProposals are local an not broadcasted.
2014-04-21 07:30:46 -03:00
this.txps[ntxid].setSent(txid);
2014-04-15 18:23:35 -03:00
};
2014-04-23 02:01:54 -03:00
TxProposals.prototype.getTxProposal = function(ntxid, copayers) {
2014-04-23 02:01:54 -03:00
var txp = this.txps[ntxid];
var i = JSON.parse(JSON.stringify(txp));
i.builder = txp.builder;
i.ntxid = ntxid;
i.peerActions = {};
if (copayers) {
for (var j = 0; j < copayers.length; j++) {
var p = copayers[j];
i.peerActions[p] = {};
}
}
for (var p in txp.seenBy) {
i.peerActions[p] = {
seen: txp.seenBy[p]
};
2014-04-23 02:01:54 -03:00
}
for (var p in txp.signedBy) {
i.peerActions[p] = i.peerActions[p] || {};
2014-04-23 02:01:54 -03:00
i.peerActions[p].sign = txp.signedBy[p];
}
var r = 0;
for (var p in txp.rejectedBy) {
i.peerActions[p] = i.peerActions[p] || {};
2014-04-23 02:01:54 -03:00
i.peerActions[p].rejected = txp.rejectedBy[p];
r++;
}
i.rejectCount = r;
2014-04-23 02:01:54 -03:00
var c = txp.creator;
i.peerActions[c] = i.peerActions[c] || {};
i.peerActions[c].create = txp.createdTs;
return i;
};
2014-06-05 14:55:19 -03:00
//returns the unspent txid-vout used in PENDING Txs
2014-04-23 02:01:54 -03:00
TxProposals.prototype.getUsedUnspent = function(maxRejectCount) {
2014-06-05 14:55:19 -03:00
var ret = {};
for (var i in this.txps) {
2014-04-21 12:00:14 -03:00
var u = this.txps[i].builder.getSelectedUnspent();
2014-06-05 14:55:19 -03:00
var p = this.getTxProposal(i);
if (p.rejectCount > maxRejectCount || p.sentTxid)
2014-04-23 02:01:54 -03:00
continue;
2014-06-05 14:55:19 -03:00
for (var j in u) {
ret[u[j].txid + ',' + u[j].vout] = 1;
2014-04-21 12:00:14 -03:00
}
}
return ret;
};
2014-04-18 19:28:28 -03:00
2014-06-18 09:07:32 -03:00
TxProposals.TxProposal = TxProposal;
2014-04-09 17:28:35 -03:00
module.exports = require('soop')(TxProposals);