Skip to content

Commit 2234e49

Browse files
committed
Transaction: simplify fromBuffer verbosity
1 parent 062540e commit 2234e49

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/transaction.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,17 @@ Transaction.prototype.toBuffer = function () {
113113
slice.copy(buffer, offset)
114114
offset += slice.length
115115
}
116+
116117
function writeUInt32(i) {
117118
buffer.writeUInt32LE(i, offset)
118119
offset += 4
119120
}
121+
120122
function writeUInt64(i) {
121123
bufferutils.writeUInt64LE(buffer, i, offset)
122124
offset += 8
123125
}
126+
124127
function writeVarInt(i) {
125128
var n = bufferutils.writeVarInt(buffer, i, offset)
126129
offset += n
@@ -247,50 +250,47 @@ Transaction.fromBuffer = function(buffer) {
247250
offset += n
248251
return buffer.slice(offset - n, offset)
249252
}
253+
250254
function readUInt32() {
251255
var i = buffer.readUInt32LE(offset)
252256
offset += 4
253257
return i
254258
}
259+
255260
function readUInt64() {
256261
var i = bufferutils.readUInt64LE(buffer, offset)
257262
offset += 8
258263
return i
259264
}
265+
260266
function readVarInt() {
261267
var vi = bufferutils.readVarInt(buffer, offset)
262268
offset += vi.size
263269
return vi.number
264270
}
265271

272+
function readScript() {
273+
return Script.fromBuffer(readSlice(readVarInt()))
274+
}
275+
266276
var tx = new Transaction()
267277
tx.version = readUInt32()
268278

269279
var vinLen = readVarInt()
270280
for (var i = 0; i < vinLen; ++i) {
271-
var hash = readSlice(32)
272-
var vout = readUInt32()
273-
var scriptLen = readVarInt()
274-
var script = readSlice(scriptLen)
275-
var sequence = readUInt32()
276-
277281
tx.ins.push({
278-
hash: hash,
279-
index: vout,
280-
script: Script.fromBuffer(script),
281-
sequence: sequence
282+
hash: readSlice(32),
283+
index: readUInt32(),
284+
script: readScript(),
285+
sequence: readUInt32()
282286
})
283287
}
284288

285289
var voutLen = readVarInt()
286290
for (i = 0; i < voutLen; ++i) {
287-
var value = readUInt64()
288-
var scriptLen = readVarInt()
289-
var script = readSlice(scriptLen)
290-
291291
tx.outs.push({
292-
value: value,
293-
script: Script.fromBuffer(script)
292+
value: readUInt64(),
293+
script: readScript(),
294294
})
295295
}
296296

0 commit comments

Comments
 (0)