@@ -23,9 +23,11 @@ function vectorSize (someVector) {
2323
2424function Transaction ( ) {
2525 this . version = 1
26+ this . type = 0
2627 this . locktime = 0
2728 this . ins = [ ]
2829 this . outs = [ ]
30+ this . extrapayload = [ ]
2931}
3032
3133Transaction . DEFAULT_SEQUENCE = 0xffffffff
@@ -59,9 +61,9 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
5961 return i
6062 }
6163
62- function readInt32 ( ) {
63- const i = buffer . readInt32LE ( offset )
64- offset += 4
64+ function readInt16 ( ) {
65+ const i = buffer . readInt16LE ( offset )
66+ offset += 2
6567 return i
6668 }
6769
@@ -89,7 +91,8 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
8991 }
9092
9193 const tx = new Transaction ( )
92- tx . version = readInt32 ( )
94+ tx . version = readInt16 ( )
95+ tx . type = readInt16 ( )
9396
9497 const marker = buffer . readUInt8 ( offset )
9598 const flag = buffer . readUInt8 ( offset + 1 )
@@ -131,6 +134,10 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
131134
132135 tx . locktime = readUInt32 ( )
133136
137+ if ( tx . version === 3 ) {
138+ tx . extrapayload = readVarSlice ( )
139+ }
140+
134141 if ( __noStrict ) return tx
135142 if ( offset !== buffer . length ) throw new Error ( 'Transaction has unexpected data' )
136143
@@ -214,13 +221,15 @@ Transaction.prototype.__byteLength = function (__allowWitness) {
214221 varuint . encodingLength ( this . outs . length ) +
215222 this . ins . reduce ( function ( sum , input ) { return sum + 40 + varSliceSize ( input . script ) } , 0 ) +
216223 this . outs . reduce ( function ( sum , output ) { return sum + 8 + varSliceSize ( output . script ) } , 0 ) +
217- ( hasWitnesses ? this . ins . reduce ( function ( sum , input ) { return sum + vectorSize ( input . witness ) } , 0 ) : 0 )
224+ ( hasWitnesses ? this . ins . reduce ( function ( sum , input ) { return sum + vectorSize ( input . witness ) } , 0 ) : 0 ) +
225+ ( this . version === 3 ? varSliceSize ( this . extrapayload ) : 0 )
218226 )
219227}
220228
221229Transaction . prototype . clone = function ( ) {
222230 const newTx = new Transaction ( )
223231 newTx . version = this . version
232+ newTx . type = this . type
224233 newTx . locktime = this . locktime
225234
226235 newTx . ins = this . ins . map ( function ( txIn ) {
@@ -240,6 +249,8 @@ Transaction.prototype.clone = function () {
240249 }
241250 } )
242251
252+ newTx . extrapayload = this . extrapayload
253+
243254 return newTx
244255}
245256
@@ -422,7 +433,7 @@ Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitne
422433 function writeSlice ( slice ) { offset += slice . copy ( buffer , offset ) }
423434 function writeUInt8 ( i ) { offset = buffer . writeUInt8 ( i , offset ) }
424435 function writeUInt32 ( i ) { offset = buffer . writeUInt32LE ( i , offset ) }
425- function writeInt32 ( i ) { offset = buffer . writeInt32LE ( i , offset ) }
436+ function writeInt16 ( i ) { offset = buffer . writeInt16LE ( i , offset ) }
426437 function writeUInt64 ( i ) { offset = bufferutils . writeUInt64LE ( buffer , i , offset ) }
427438 function writeVarInt ( i ) {
428439 varuint . encode ( i , buffer , offset )
@@ -431,7 +442,8 @@ Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitne
431442 function writeVarSlice ( slice ) { writeVarInt ( slice . length ) ; writeSlice ( slice ) }
432443 function writeVector ( vector ) { writeVarInt ( vector . length ) ; vector . forEach ( writeVarSlice ) }
433444
434- writeInt32 ( this . version )
445+ writeInt16 ( this . version )
446+ writeInt16 ( this . type )
435447
436448 const hasWitnesses = __allowWitness && this . hasWitnesses ( )
437449
@@ -468,6 +480,10 @@ Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitne
468480
469481 writeUInt32 ( this . locktime )
470482
483+ if ( this . version === 3 ) {
484+ writeVarSlice ( this . extrapayload )
485+ }
486+
471487 // avoid slicing unless necessary
472488 if ( initialOffset !== undefined ) return buffer . slice ( initialOffset , offset )
473489 return buffer
0 commit comments