copay/js/models/TxProposals.js

156 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-07-30 17:20:08 -07:00
'use strict';
2014-11-26 10:15:12 -08:00
var preconditions = require('preconditions').singleton();
2014-07-30 17:20:08 -07:00
var bitcore = require('bitcore');
var util = bitcore.util;
var Transaction = bitcore.Transaction;
var Script = bitcore.Script;
var Key = bitcore.Key;
var buffertools = bitcore.buffertools;
2014-11-26 10:15:12 -08:00
var log = require('../log');
2014-11-26 10:15:12 -08:00
var TxProposal = require('./TxProposal');;
2014-07-30 17:20:08 -07:00
2014-07-31 21:09:46 -07:00
function TxProposals(opts) {
2014-07-30 17:20:08 -07:00
opts = opts || {};
this.walletId = opts.walletId;
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
this.txps = {};
}
// fromObj => from a trusted source
2014-07-31 21:09:46 -07:00
TxProposals.fromObj = function(o, forceOpts) {
var ret = new TxProposals({
2014-07-30 17:20:08 -07:00
networkName: o.networkName,
walletId: o.walletId,
});
o.txps.forEach(function(o2) {
try {
var t = TxProposal.fromObj(o2, forceOpts);
} catch (e) {
log.info('Ignoring corrupted TxProposal:', o2, e);
}
if (t && t.builder) {
var id = t.getId();
2014-07-30 17:20:08 -07:00
ret.txps[id] = t;
}
2014-07-30 17:20:08 -07:00
});
return ret;
};
2014-08-21 10:12:55 -07:00
TxProposals.prototype.length = function() {
return Object.keys(this.txps).length;
};
2014-09-09 19:00:24 -07:00
TxProposals.prototype.getNtxidsSince = function(sinceTs) {
preconditions.checkArgument(sinceTs);
var ret = [];
2014-11-26 03:38:02 -08:00
for (var ii in this.txps) {
2014-09-09 19:00:24 -07:00
var txp = this.txps[ii];
2014-11-26 03:38:02 -08:00
if (txp.createdTs >= sinceTs)
2014-09-09 19:00:24 -07:00
ret.push(ii);
}
return ret;
};
2014-07-31 21:09:46 -07:00
TxProposals.prototype.getNtxids = function() {
2014-07-30 17:20:08 -07:00
return Object.keys(this.txps);
};
TxProposals.prototype.deleteOne = function(ntxid) {
preconditions.checkState(this.txps[ntxid], 'Unknown TXP: ' + ntxid);
delete this.txps[ntxid];
};
2014-08-21 10:12:55 -07:00
TxProposals.prototype.deleteAll = function() {
this.txps = {};
};
TxProposals.prototype.deletePending = function(maxRejectCount) {
for (var ntxid in this.txps) {
if (this.txps[ntxid].isPending(maxRejectCount))
delete this.txps[ntxid];
};
};
2014-07-31 21:09:46 -07:00
TxProposals.prototype.toObj = function() {
2014-07-30 17:20:08 -07:00
var ret = [];
for (var id in this.txps) {
var t = this.txps[id];
if (!t.sent)
ret.push(t.toObj());
}
return {
txps: ret,
walletId: this.walletId,
networkName: this.network.name,
};
};
// Add a LOCALLY CREATED (trusted) tx proposal
TxProposals.prototype.add = function(txp) {
var ntxid = txp.getId();
2014-07-30 17:20:08 -07:00
this.txps[ntxid] = txp;
return ntxid;
};
2014-08-03 19:57:23 -07:00
2014-11-27 12:45:10 -08:00
TxProposals.prototype.exist = function(ntxid) {
return this.txps[ntxid] ? true : false;
};
2014-08-05 12:25:02 -07:00
TxProposals.prototype.get = function(ntxid) {
2014-08-03 19:57:23 -07:00
var ret = this.txps[ntxid];
if (!ret)
2014-11-26 03:38:02 -08:00
throw new Error('Unknown TXP: ' + ntxid);
2014-08-03 19:57:23 -07:00
return ret;
};
2014-07-30 17:20:08 -07:00
//returns the unspent txid-vout used in PENDING Txs
2014-07-31 21:09:46 -07:00
TxProposals.prototype.getUsedUnspent = function(maxRejectCount) {
2014-07-30 17:20:08 -07:00
var ret = {};
2014-11-26 03:38:02 -08:00
var self = this;
2014-08-21 11:19:08 -07:00
2014-11-26 03:38:02 -08:00
_.each(this.txps, function(txp) {
if (!txp.isPending(maxRejectCount))
return
2014-08-21 10:12:55 -07:00
2014-11-26 03:38:02 -08:00
_.each(txp.builder.getSelectedUnspent(), function(u) {
ret[u.txid + ',' + u.vout] = 1;
});
});
2014-07-30 17:20:08 -07:00
return ret;
};
2014-11-23 10:52:39 -08:00
/**
* purge
*
* @param deleteAll
* @return {undefined}
*/
TxProposals.prototype.purge = function(deleteAll, maxRejectCount) {
var m = _.size(this.txps);
if (deleteAll) {
this.deleteAll();
} else {
this.deletePending(maxRejectCount);
}
var n = _.size(this.txps);
return m - n;
};
2014-07-31 21:09:46 -07:00
module.exports = TxProposals;