btc-transaction
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -0,1 +1,5 @@ | ||
0.1.3 / 2014-02-19 | ||
------------------ | ||
* When deserializing a transaction, cache the original bytes to speed up action such as getHash() and getSize(); This is because serialize() can take a very long time (sometimes in minutes) to serialize transactions with thousands of inputs | ||
0.1.2 / 2014-02-18 | ||
@@ -2,0 +6,0 @@ ----------------- |
@@ -86,2 +86,5 @@ var Address = require('btc-address'); | ||
if (doc.block) this.block = doc.block; | ||
// if we already have the original bytes; let's keep a copy | ||
// see comments at deserialize() | ||
if (doc.serialized) this.serialized = doc.serialized | ||
} | ||
@@ -264,3 +267,8 @@ }; | ||
Transaction.prototype.getHash = function() { | ||
var buffer = this.serialize(); | ||
// todo: migrate to buffer | ||
if (Array.isArray(this.serialized) && this.serialized.length > 0) { | ||
var buffer = this.serialized; | ||
} else { | ||
var buffer = this.serialize(); | ||
} | ||
@@ -273,2 +281,14 @@ //todo: change to buffer | ||
Transaction.prototype.getSize = function() { | ||
// todo: migrate to buffer | ||
if (Array.isArray(this.serialized) && this.serialized.length > 0) { | ||
var buffer = this.serialized; | ||
} else { | ||
var buffer = this.serialize(); | ||
} | ||
//todo: change to buffer | ||
return buffer.length; | ||
} | ||
/** | ||
@@ -419,2 +439,24 @@ * Create a copy of this transaction object. | ||
// TODO: Make pull request in cypto-binary | ||
Parser.prototype.readVarRaw = function readVarRaw() { | ||
if (this.hasFailed || this.pointerCheck() === false) return false; | ||
var flagRaw = this.raw(1); | ||
if (flagRaw) { | ||
var flag = flagRaw.readUInt8(0) | ||
} else { | ||
return false | ||
} | ||
if (flag < 0xfd) { | ||
return flagRaw; | ||
} else | ||
if (flag == 0xfd) { | ||
return Buffer.concat([flagRaw, this.raw(2)]); | ||
} else if (flag == 0xfe) { | ||
return Buffer.concat([flagRaw, this.raw(4)]); | ||
} else { | ||
return Buffer.concat([flagRaw, this.raw(8)]); | ||
} | ||
}; | ||
// Second argument txCount is optional; It indicates | ||
@@ -439,8 +481,17 @@ // the number of the transactions that you expect to | ||
var doc = {}; | ||
doc.version = s.readUInt32LE() | ||
// note: serializing a large transaction (1000+ inputs) such as testnet tx ('95ea61f319ed0d2b28e94cb0164396b4024bc6ad624fcb492c5c87a088592e81') can take up to 2 minutes. So we have to cache the original bytes when we deserialize it so that getHash() will be fast | ||
// this will be concatenated into a buffer in the end | ||
doc.serialized = [] | ||
var verB = s.raw(4) | ||
doc.serialized.push(verB) | ||
doc.version = new Parser(verB).readUInt32LE() | ||
doc.ins = [] | ||
doc.outs = [] | ||
var inputCount = s.readVarInt() | ||
var inB = s.readVarRaw() | ||
doc.serialized.push(inB) | ||
var inputCount = new Parser(inB).readVarInt() | ||
for (var i = 0; i < inputCount; i++) { | ||
@@ -454,2 +505,3 @@ var data = {} | ||
} | ||
doc.serialized.push(txHash) | ||
@@ -464,12 +516,25 @@ // TODO: change this to buffer in the transition to buffer | ||
isCoinbase = (data.outpoint.hash === '0000000000000000000000000000000000000000000000000000000000000000') | ||
data.outpoint.index = s.readUInt32LE() | ||
var scriptLength = s.readVarInt() | ||
data.script = new Script(s.raw(scriptLength).toString('hex'), isCoinbase) | ||
data.sequence = s.readUInt32LE() | ||
var indexB = s.raw(4) | ||
doc.serialized.push(indexB) | ||
data.outpoint.index = new Parser(indexB).readUInt32LE() | ||
var sLenB = s.readVarRaw() | ||
doc.serialized.push(sLenB) | ||
var scriptLength = new Parser(sLenB).readVarInt() | ||
var sB = s.raw(scriptLength) | ||
doc.serialized.push(sB) | ||
data.script = new Script(sB.toString('hex'), isCoinbase) | ||
var seqB = s.raw(4) | ||
doc.serialized.push(seqB) | ||
data.sequence = new Parser(seqB).readUInt32LE() | ||
doc.ins.push(data) | ||
} | ||
var outputCount = s.readVarInt() | ||
var outB = s.readVarRaw() | ||
doc.serialized.push(outB) | ||
var outputCount = new Parser(outB).readVarInt() | ||
for (var i = 0; i < outputCount; i++) { | ||
@@ -481,2 +546,3 @@ var data = {} | ||
} | ||
doc.serialized.push(value) | ||
@@ -487,9 +553,22 @@ data.value = binConv(value, { | ||
var scriptLength = s.readVarInt() | ||
data.script = new Script(s.raw(scriptLength).toString('hex')) | ||
var sLenB = s.readVarRaw() | ||
doc.serialized.push(sLenB) | ||
var scriptLength = new Parser(sLenB).readVarInt() | ||
var sB = s.raw(scriptLength) | ||
doc.serialized.push(sB) | ||
data.script = new Script(sB.toString('hex')) | ||
doc.outs.push(data) | ||
} | ||
doc.lock_time = s.readUInt32LE() | ||
var lockB = s.raw(4) | ||
doc.serialized.push(lockB) | ||
doc.lock_time = new Parser(lockB).readUInt32LE() | ||
// now we turn serialized into a buffer | ||
doc.serialized = binConv(Buffer.concat(doc.serialized), { in : 'buffer', | ||
out: 'bytes' | ||
}) | ||
if (s.hasFailed) { | ||
@@ -496,0 +575,0 @@ return false |
{ | ||
"name": "btc-transaction", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Create, parse, serialize Bitcoin transactions.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
20541
565