Skip to content

Commit 6a54b4c

Browse files
committed
script: always compile in a minimaldata compliant way
1 parent 377d8ec commit 6a54b4c

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

Diff for: src/script.js

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ function compile (chunks) {
5050
var bufferSize = chunks.reduce(function (accum, chunk) {
5151
// data chunk
5252
if (Buffer.isBuffer(chunk)) {
53+
// adhere to BIP62.3, minimal push policy
54+
if (chunk.length === 1 && chunk[0] >= 1 && chunk[0] <= 16) {
55+
return accum + 1
56+
}
57+
5358
return accum + bufferutils.pushDataSize(chunk.length) + chunk.length
5459
}
5560

@@ -63,6 +68,14 @@ function compile (chunks) {
6368
chunks.forEach(function (chunk) {
6469
// data chunk
6570
if (Buffer.isBuffer(chunk)) {
71+
// adhere to BIP62.3, minimal push policy
72+
if (chunk.length === 1 && chunk[0] >= 1 && chunk[0] <= 16) {
73+
var opcode = OPS.OP_INT_BASE + chunk[0]
74+
buffer.writeUInt8(opcode, offset)
75+
offset += 1
76+
return
77+
}
78+
6679
offset += bufferutils.writePushDataInt(buffer, chunk.length, offset)
6780

6881
chunk.copy(buffer, offset)

Diff for: test/fixtures/script.json

+9
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@
150150
"scriptSig": "OP_0 00",
151151
"scriptSigHex": "000100"
152152
},
153+
{
154+
"type": "nonstandard",
155+
"scriptSig": "OP_6",
156+
"scriptSigHex": "56",
157+
"nonstandard": {
158+
"scriptSig": "06",
159+
"scriptSigHex": "0106"
160+
}
161+
},
153162
{
154163
"type": "nonstandard",
155164
"scriptSig": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",

Diff for: test/script.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,27 @@ describe('script', function () {
3333
})
3434
})
3535

36-
describe('compile', function () {
36+
describe('compile (via fromASM)', function () {
3737
fixtures.valid.forEach(function (f) {
3838
if (f.scriptSig) {
3939
it('(' + f.type + ') compiles ' + f.scriptSig, function () {
4040
var scriptSig = bscript.fromASM(f.scriptSig)
4141

42-
assert.strictEqual(bscript.compile(scriptSig).toString('hex'), f.scriptSigHex)
42+
assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex)
43+
44+
if (f.nonstandard) {
45+
var scriptSigNS = bscript.fromASM(f.nonstandard.scriptSig)
46+
47+
assert.strictEqual(scriptSigNS.toString('hex'), f.scriptSigHex)
48+
}
4349
})
4450
}
4551

4652
if (f.scriptPubKey) {
4753
it('(' + f.type + ') compiles ' + f.scriptPubKey, function () {
4854
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
4955

50-
assert.strictEqual(bscript.compile(scriptPubKey).toString('hex'), f.scriptPubKeyHex)
56+
assert.strictEqual(scriptPubKey.toString('hex'), f.scriptPubKeyHex)
5157
})
5258
}
5359
})
@@ -59,14 +65,25 @@ describe('script', function () {
5965
it('decompiles ' + f.scriptSig, function () {
6066
var chunks = bscript.decompile(new Buffer(f.scriptSigHex, 'hex'))
6167

68+
assert.strictEqual(bscript.compile(chunks).toString('hex'), f.scriptSigHex)
6269
assert.strictEqual(bscript.toASM(chunks), f.scriptSig)
70+
71+
if (f.nonstandard) {
72+
var chunksNS = bscript.decompile(new Buffer(f.nonstandard.scriptSigHex, 'hex'))
73+
74+
assert.strictEqual(bscript.compile(chunksNS).toString('hex'), f.scriptSigHex)
75+
76+
// toASM converts verbatim, only `compile` transforms the script to a minimalpush compliant script
77+
assert.strictEqual(bscript.toASM(chunksNS), f.nonstandard.scriptSig)
78+
}
6379
})
6480
}
6581

6682
if (f.scriptPubKeyHex) {
6783
it('decompiles ' + f.scriptPubKey, function () {
6884
var chunks = bscript.decompile(new Buffer(f.scriptPubKeyHex, 'hex'))
6985

86+
assert.strictEqual(bscript.compile(chunks).toString('hex'), f.scriptPubKeyHex)
7087
assert.strictEqual(bscript.toASM(chunks), f.scriptPubKey)
7188
})
7289
}
@@ -466,7 +483,7 @@ describe('script', function () {
466483
var buffer = new Buffer(i)
467484
var script = bscript.compile([buffer])
468485

469-
assert(minimalData(script), 'Failed for ' + i + ' length script')
486+
assert(minimalData(script), 'Failed for ' + i + ' length script: ' + script.toString('hex'))
470487
})
471488
}
472489

0 commit comments

Comments
 (0)