script: always compile in a minimaldata compliant way

This commit is contained in:
Daniel Cousens 2016-09-29 13:48:17 +10:00
parent 377d8ecc68
commit c1570f8056
3 changed files with 43 additions and 4 deletions

View file

@ -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)

View file

@ -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",

View file

@ -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'))
})
}