rewrite from scratch tests for TxProposalSet

This commit is contained in:
Matias Alejo Garcia 2014-07-31 23:32:24 -03:00
commit 42c73f9a01
6 changed files with 92 additions and 722 deletions

View file

@ -96,6 +96,7 @@ TxProposal._verifySignatures = function(inKeys, scriptSig, txSigHash) {
if (scriptSig.chunks[0] !== 0)
throw new Error('Invalid scriptSig');
var keys = TxProposal._formatKeys(inKeys);
var ret = [];
for (var i = 1; i <= scriptSig.countSignatures(); i++) {
@ -116,13 +117,14 @@ TxProposal._infoFromRedeemScript = function(s) {
var redeemScript = new Script(s.chunks[s.chunks.length - 1]);
if (!redeemScript)
throw new Error('Bad scriptSig');
var pubkeys = redeemScript.capture();
if (!pubkeys || !pubkeys.length)
throw new Error('Bad scriptSig');
return {
keys: pubkeys,
scriptBuf: redeemScript.getBuffer()
script: redeemScript,
};
};
@ -134,7 +136,7 @@ TxProposal.prototype._updateSignedBy = function() {
var scriptSig = new Script(tx.ins[i].s);
var signatureCount = scriptSig.countSignatures();
var info = TxProposal._infoFromRedeemScript(scriptSig);
var txSigHash = tx.hashForSignature(info.scriptBuf, i, Transaction.SIGHASH_ALL);
var txSigHash = tx.hashForSignature(info.script, parseInt(i), Transaction.SIGHASH_ALL);
var signatureIndexes = TxProposal._verifySignatures(info.keys, scriptSig, txSigHash);
if (signatureIndexes.length !== signatureCount)
throw new Error('Invalid signature');
@ -154,8 +156,8 @@ TxProposal.prototype._check = function() {
if (!tx.ins.length)
throw new Error('Invalid tx proposal: no ins');
var scriptSigs = this.builder.vanilla.scriptSigs;
if (!scriptSigs || !scriptSigs.length) {
var scriptSig = this.builder.vanilla.scriptSig;
if (!scriptSig || !scriptSig.length) {
throw new Error('Invalid tx proposal: no signatures');
}

View file

@ -5,6 +5,7 @@ var bitcore = require('bitcore');
var util = bitcore.util;
var Transaction = bitcore.Transaction;
var BuilderMockV0 = require('./BuilderMockV0');;
var TxProposal = require('./TxProposal');;
var Script = bitcore.Script;
var Key = bitcore.Key;
var buffertools = bitcore.buffertools;
@ -28,7 +29,7 @@ TxProposalsSet.fromObj = function(o, forceOpts) {
o.txps.forEach(function(o2) {
var t = TxProposal.fromObj(o2, forceOpts);
if (t.builder) {
var id = t.getID();
var id = t.getId();
ret.txps[id] = t;
}
});
@ -39,14 +40,9 @@ TxProposalsSet.prototype.getNtxids = function() {
return Object.keys(this.txps);
};
TxProposalsSet.prototype.toObj = function(onlyThisNtxid) {
if (onlyThisNtxid) throw new Error();
TxProposalsSet.prototype.toObj = function() {
var ret = [];
for (var id in this.txps) {
if (onlyThisNtxid && id != onlyThisNtxid)
continue;
var t = this.txps[id];
if (!t.sent)
ret.push(t.toObj());
@ -70,7 +66,7 @@ TxProposalsSet.prototype.mergeFromObj = function(txProposalObj, allowedPubKeys,
TxProposalsSet.prototype.merge = function(inTxp, allowedPubKeys) {
var myTxps = this.txps;
var ntxid = inTxp.getID();
var ntxid = inTxp.getId();
var ret = {};
ret.events = [];
ret.events.hasChanged = false;
@ -94,7 +90,7 @@ TxProposalsSet.prototype.merge = function(inTxp, allowedPubKeys) {
// Add a LOCALLY CREATED (trusted) tx proposal
TxProposalsSet.prototype.add = function(data) {
var txp = new TxProposal(data);
var ntxid = txp.getID();
var ntxid = txp.getId();
this.txps[ntxid] = txp;
return ntxid;
};
@ -158,3 +154,4 @@ TxProposalsSet.prototype.getUsedUnspent = function(maxRejectCount) {
return ret;
};
module.exports = TxProposalsSet;

View file

@ -147,7 +147,7 @@ Wallet.prototype._handleTxProposal = function(senderId, data) {
var added = this.addSeenToTxProposals();
if (added) {
this.log('### BROADCASTING txProposals with my seenBy updated.');
this.sendTxProposal(mergeInfo.inTxp.getID());
this.sendTxProposal(mergeInfo.inTxp.getId());
}
this.emit('txProposalsUpdated');