Uses Array.prototype.foreach for stricter scoping

This commit is contained in:
Daniel Cousens 2014-03-24 06:03:58 +11:00
parent 87453f1828
commit a062297be2

View file

@ -24,15 +24,15 @@ var Transaction = function (doc) {
if (doc.version) this.version = doc.version; if (doc.version) this.version = doc.version;
if (doc.locktime) this.locktime = doc.locktime; if (doc.locktime) this.locktime = doc.locktime;
if (doc.ins && doc.ins.length) { if (doc.ins && doc.ins.length) {
for (var i = 0; i < doc.ins.length; i++) { doc.ins.forEach(function(input) {
this.addInput(new TransactionIn(doc.ins[i])); this.addInput(new TransactionIn(input));
} }, this);
} }
if (doc.outs && doc.outs.length) { if (doc.outs && doc.outs.length) {
for (var i = 0; i < doc.outs.length; i++) { doc.outs.forEach(function(output) {
this.addOutput(new TransactionOut(doc.outs[i])); this.addOutput(new TransactionOut(output));
} }, this);
} }
this.hash = this.hash || this.getHash(); this.hash = this.hash || this.getHash();
@ -113,9 +113,8 @@ Transaction.prototype.serialize = function () {
var buffer = []; var buffer = [];
buffer = buffer.concat(convert.numToBytes(parseInt(this.version), 4)); buffer = buffer.concat(convert.numToBytes(parseInt(this.version), 4));
buffer = buffer.concat(convert.numToVarInt(this.ins.length)); buffer = buffer.concat(convert.numToVarInt(this.ins.length));
for (var i = 0; i < this.ins.length; i++) {
var txin = this.ins[i];
this.ins.forEach(function(txin) {
// Why do blockchain.info, blockexplorer.com, sx and just about everybody // Why do blockchain.info, blockexplorer.com, sx and just about everybody
// else use little-endian hashes? No idea... // else use little-endian hashes? No idea...
buffer = buffer.concat(convert.hexToBytes(txin.outpoint.hash).reverse()); buffer = buffer.concat(convert.hexToBytes(txin.outpoint.hash).reverse());
@ -126,10 +125,11 @@ Transaction.prototype.serialize = function () {
buffer = buffer.concat(convert.numToVarInt(scriptBytes.length)); buffer = buffer.concat(convert.numToVarInt(scriptBytes.length));
buffer = buffer.concat(scriptBytes); buffer = buffer.concat(scriptBytes);
buffer = buffer.concat(txin.sequence); buffer = buffer.concat(txin.sequence);
} });
buffer = buffer.concat(convert.numToVarInt(this.outs.length)); buffer = buffer.concat(convert.numToVarInt(this.outs.length));
for (var i = 0; i < this.outs.length; i++) {
var txout = this.outs[i]; this.outs.forEach(function(txout) {
buffer = buffer.concat(convert.numToBytes(txout.value,8)); buffer = buffer.concat(convert.numToBytes(txout.value,8));
var scriptBytes = txout.script.buffer; var scriptBytes = txout.script.buffer;
@ -174,9 +174,9 @@ function (connectedScript, inIndex, hashType)
});*/ });*/
// Blank out other inputs' signatures // Blank out other inputs' signatures
for (var i = 0; i < txTmp.ins.length; i++) { txTmp.ins.forEach(function(txin) {
txTmp.ins[i].script = new Script(); txin.script = new Script();
} });
txTmp.ins[inIndex].script = connectedScript; txTmp.ins[inIndex].script = connectedScript;
@ -185,9 +185,12 @@ function (connectedScript, inIndex, hashType)
txTmp.outs = []; txTmp.outs = [];
// Let the others update at will // Let the others update at will
for (var i = 0; i < txTmp.ins.length; i++) txTmp.ins.forEach(function(txin, i) {
if (i != inIndex) if (i != inIndex) {
txTmp.ins[i].sequence = 0; txTmp.ins[i].sequence = 0;
}
});
} else if ((hashType & 0x1f) == SIGHASH_SINGLE) { } else if ((hashType & 0x1f) == SIGHASH_SINGLE) {
// TODO: Implement // TODO: Implement
} }
@ -224,14 +227,15 @@ Transaction.prototype.clone = function ()
var newTx = new Transaction(); var newTx = new Transaction();
newTx.version = this.version; newTx.version = this.version;
newTx.locktime = this.locktime; newTx.locktime = this.locktime;
for (var i = 0; i < this.ins.length; i++) {
var txin = this.ins[i].clone(); this.ins.forEach(function(txin) {
newTx.addInput(txin); newTx.addInput(txin.clone());
} });
for (var i = 0; i < this.outs.length; i++) {
var txout = this.outs[i].clone(); this.outs.forEach(function(txout) {
newTx.addOutput(txout); newTx.addOutput(txout.clone());
} });
return newTx; return newTx;
}; };
@ -327,9 +331,10 @@ Transaction.prototype.signWithKeys = function(keys, outputs, type) {
}); });
var hmap = {}; var hmap = {};
for (var o in outputs) { outputs.forEach(function(o) {
hmap[outputs[o].output] = outputs[o]; hmap[o.output] = o;
} });
for (var i = 0; i < this.ins.length; i++) { for (var i = 0; i < this.ins.length; i++) {
var outpoint = this.ins[i].outpoint.hash + ':' + this.ins[i].outpoint.index; var outpoint = this.ins[i].outpoint.hash + ':' + this.ins[i].outpoint.index;
var histItem = hmap[outpoint]; var histItem = hmap[outpoint];