Skip to content

Commit 69807fd

Browse files
committed
Support DIP2 special txes
1 parent 9a8552a commit 69807fd

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/transaction.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ function vectorSize (someVector) {
2323

2424
function Transaction () {
2525
this.version = 1
26+
this.type = 0
2627
this.locktime = 0
2728
this.ins = []
2829
this.outs = []
30+
this.extrapayload = []
2931
}
3032

3133
Transaction.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

221229
Transaction.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

test/fixtures/transaction.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,25 @@
185185
"virtualSize": 126,
186186
"weight": 504
187187
},
188+
{
189+
"description": "Quorum special transaction",
190+
"id": "f96ca72497c5ad795e8a4d1c840ebc1e5804f75171f73e6df4b3dc010e21c50c",
191+
"hash": "0cc5210e01dcb3f46d3ef77151f704581ebc0e841c4d8a5e79adc59724a76cf9",
192+
"hex": "03000600000000000000fd490101007a2e0400010001e58d0b7e02ba7b56cbfe89a92b5cfd644b1be005a53884b405a5e70000000000320000000000000032000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
193+
"raw": {
194+
"version": 3,
195+
"type": 6,
196+
"ins": [
197+
],
198+
"outs": [
199+
],
200+
"locktime": 0,
201+
"extrapayload": "01007a2e0400010001e58d0b7e02ba7b56cbfe89a92b5cfd644b1be005a53884b405a5e70000000000320000000000000032000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
202+
},
203+
"coinbase": false,
204+
"virtualSize": 342,
205+
"weight": 1368
206+
},
188207
{
189208
"description": "Transaction that ignores script chunking rules - Bug #367",
190209
"id": "ebc9fa1196a59e192352d76c0f6e73167046b9d37b8302b6bb6968dfd279b767",

test/transaction.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('Transaction', function () {
88
function fromRaw (raw, noWitness) {
99
const tx = new Transaction()
1010
tx.version = raw.version
11+
tx.type = raw.type
1112
tx.locktime = raw.locktime
1213

1314
raw.ins.forEach(function (txIn, i) {
@@ -43,6 +44,10 @@ describe('Transaction', function () {
4344
tx.addOutput(script, txOut.value)
4445
})
4546

47+
if (tx.version === 3) {
48+
tx.extrapayload = Buffer.from(raw.extrapayload, 'hex')
49+
}
50+
4651
return tx
4752
}
4853

0 commit comments

Comments
 (0)