Repository: bitcoinjs/bitcoinjs-lib
Pull request #638 (2016-09-29 03:49:59)

script: always compile in a minimaldata compliant way
I can't decide if this is the right thing to do.
On the one hand,  I can't imagine another work flow that isn't a pain in the ass,  but at the same time it means `decompile(compile(x)) !== x` in more cases.

Existing exceptions already exist around non-standard data pushes... so I suppose that makes it more acceptable? 


Patch:
diff --git a/src/script.js b/src/script.js
index 07f85511..d729baa7 100644
--- a/src/script.js
+++ b/src/script.js
@@ -50,6 +50,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
     }
 
@@ -63,6 +68,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)


Test patch:
diff --git a/test/fixtures/script.json b/test/fixtures/script.json
index 788d8077..699dca1f 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 e3e7f3f2..0a23de6a 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'))
       })
     }