diff --git a/src/script.js b/src/script.js
index 1592a39..4fcce8a 100644
--- a/src/script.js
+++ b/src/script.js
@@ -24,6 +24,11 @@ function compile (chunks) {
var bufferSize = chunks.reduce(function (accum, chunk) {
// data chunk
if (Buffer.isBuffer(chunk)) {
+ // adhere to BIP62.3, minimal push policy
+ if (chunk.length === 1 && chunk[0] >= 1 && chunk[0] <= 16) {
+ return accum + 1
+ }
+
return accum + bufferutils.pushDataSize(chunk.length) + chunk.length
}
@@ -37,6 +42,14 @@ function compile (chunks) {
chunks.forEach(function (chunk) {
// data chunk
if (Buffer.isBuffer(chunk)) {
+ // adhere to BIP62.3, minimal push policy
+ if (chunk.length === 1 && chunk[0] >= 1 && chunk[0] <= 16) {
+ var opcode = OP_INT_BASE + chunk[0]
+ buffer.writeUInt8(opcode, offset)
+ offset += 1
+ return
+ }
+
offset += bufferutils.writePushDataInt(buffer, chunk.length, offset)
chunk.copy(buffer, offset)
diff --git a/test/fixtures/script.json b/test/fixtures/script.json
index 788d807..699dca1 100644
--- a/test/fixtures/script.json
+++ b/test/fixtures/script.json
@@ -150,6 +150,15 @@
"scriptSig": "OP_0 00",
"scriptSigHex": "000100"
},
+ {
+ "type": "nonstandard",
+ "scriptSig": "OP_6",
+ "scriptSigHex": "56",
+ "nonstandard": {
+ "scriptSig": "06",
+ "scriptSigHex": "0106"
+ }
+ },
{
"type": "nonstandard",
"scriptSig": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
diff --git a/test/script.js b/test/script.js
index e3e7f3f..0a23de6 100644
--- a/test/script.js
+++ b/test/script.js
@@ -33,13 +33,19 @@ describe('script', function () {
})
})
- describe('compile', function () {
+ describe('compile (via fromASM)', function () {
fixtures.valid.forEach(function (f) {
if (f.scriptSig) {
it('(' + f.type + ') compiles ' + f.scriptSig, function () {
var scriptSig = bscript.fromASM(f.scriptSig)
- assert.strictEqual(bscript.compile(scriptSig).toString('hex'), f.scriptSigHex)
+ assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex)
+
+ if (f.nonstandard) {
+ var scriptSigNS = bscript.fromASM(f.nonstandard.scriptSig)
+
+ assert.strictEqual(scriptSigNS.toString('hex'), f.scriptSigHex)
+ }
})
}
@@ -47,7 +53,7 @@ describe('script', function () {
it('(' + f.type + ') compiles ' + f.scriptPubKey, function () {
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
- assert.strictEqual(bscript.compile(scriptPubKey).toString('hex'), f.scriptPubKeyHex)
+ assert.strictEqual(scriptPubKey.toString('hex'), f.scriptPubKeyHex)
})
}
})
@@ -59,7 +65,17 @@ describe('script', function () {
it('decompiles ' + f.scriptSig, function () {
var chunks = bscript.decompile(new Buffer(f.scriptSigHex, 'hex'))
+ assert.strictEqual(bscript.compile(chunks).toString('hex'), f.scriptSigHex)
assert.strictEqual(bscript.toASM(chunks), f.scriptSig)
+
+ if (f.nonstandard) {
+ var chunksNS = bscript.decompile(new Buffer(f.nonstandard.scriptSigHex, 'hex'))
+
+ assert.strictEqual(bscript.compile(chunksNS).toString('hex'), f.scriptSigHex)
+
+ // toASM converts verbatim, only `compile` transforms the script to a minimalpush compliant script
+ assert.strictEqual(bscript.toASM(chunksNS), f.nonstandard.scriptSig)
+ }
})
}
@@ -67,6 +83,7 @@ describe('script', function () {
it('decompiles ' + f.scriptPubKey, function () {
var chunks = bscript.decompile(new Buffer(f.scriptPubKeyHex, 'hex'))
+ assert.strictEqual(bscript.compile(chunks).toString('hex'), f.scriptPubKeyHex)
assert.strictEqual(bscript.toASM(chunks), f.scriptPubKey)
})
}
@@ -466,7 +483,7 @@ describe('script', function () {
var buffer = new Buffer(i)
var script = bscript.compile([buffer])
- assert(minimalData(script), 'Failed for ' + i + ' length script')
+ assert(minimalData(script), 'Failed for ' + i + ' length script: ' + script.toString('hex'))
})
}