Browse Source

Script templates: add witness commitment template

hk-custom-address
Thomas Kerin 8 years ago
committed by Daniel Cousens
parent
commit
9d9d101b5f
  1. 7
      src/templates/index.js
  2. 3
      src/templates/witnesscommitment/index.js
  3. 36
      src/templates/witnesscommitment/output.js
  4. 4
      test/fixtures/script.json
  5. 21
      test/fixtures/templates.json
  6. 46
      test/templates.js

7
src/templates/index.js

@ -6,6 +6,8 @@ var pubKeyHash = require('./pubkeyhash')
var scriptHash = require('./scripthash') var scriptHash = require('./scripthash')
var witnessPubKeyHash = require('./witnesspubkeyhash') var witnessPubKeyHash = require('./witnesspubkeyhash')
var witnessScriptHash = require('./witnessscripthash') var witnessScriptHash = require('./witnessscripthash')
var witnessCommitment = require('./witnesscommitment')
var types = { var types = {
MULTISIG: 'multisig', MULTISIG: 'multisig',
NONSTANDARD: 'nonstandard', NONSTANDARD: 'nonstandard',
@ -14,7 +16,8 @@ var types = {
P2PKH: 'pubkeyhash', P2PKH: 'pubkeyhash',
P2SH: 'scripthash', P2SH: 'scripthash',
P2WPKH: 'witnesspubkeyhash', P2WPKH: 'witnesspubkeyhash',
P2WSH: 'witnessscripthash' P2WSH: 'witnessscripthash',
WITNESS_COMMITMENT: 'witnesscommitment'
} }
function classifyOutput (script) { function classifyOutput (script) {
@ -27,6 +30,7 @@ function classifyOutput (script) {
var chunks = decompile(script) var chunks = decompile(script)
if (multisig.output.check(chunks)) return types.MULTISIG if (multisig.output.check(chunks)) return types.MULTISIG
if (pubKey.output.check(chunks)) return types.P2PK if (pubKey.output.check(chunks)) return types.P2PK
if (witnessCommitment.output.check(chunks)) return types.WITNESS_COMMITMENT
if (nullData.output.check(chunks)) return types.NULLDATA if (nullData.output.check(chunks)) return types.NULLDATA
return types.NONSTANDARD return types.NONSTANDARD
@ -65,5 +69,6 @@ module.exports = {
scriptHash: scriptHash, scriptHash: scriptHash,
witnessPubKeyHash: witnessPubKeyHash, witnessPubKeyHash: witnessPubKeyHash,
witnessScriptHash: witnessScriptHash, witnessScriptHash: witnessScriptHash,
witnessCommitment: witnessCommitment,
types: types types: types
} }

3
src/templates/witnesscommitment/index.js

@ -0,0 +1,3 @@
module.exports = {
output: require('./output')
}

36
src/templates/witnesscommitment/output.js

@ -0,0 +1,36 @@
// OP_RETURN 36bytes:[0xaa21a9ed, Hash256(witnessRoot )]
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('bitcoin-ops')
function check (script) {
var buffer = bscript.compile(script)
return buffer.length > 37 &&
buffer[0] === OPS.OP_RETURN &&
buffer[1] === 0x24 &&
buffer.slice(2, 6).toString('hex') === 'aa21a9ed'
}
check.toJSON = function () { return 'Witness commitment output' }
function encode (commitment) {
// hash256 0x21 hash160 0xed
typeforce(types.Hash256bit, commitment)
return bscript.compile([OPS.OP_RETURN, new Buffer('aa21a9ed' + commitment.toString('hex'), 'hex')])
}
function decode (buffer) {
typeforce(check, buffer)
return bscript.decompile(buffer)[1].slice(4, 36)
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

4
test/fixtures/script.json

@ -165,6 +165,10 @@
"stack": [ "stack": [
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
] ]
},
{
"asm": "OP_RETURN aa21a9ed4db4fb830efe3e804337413ffe8ad7393af301e0ec8e71b6e6f2b860a56f4dcd",
"script": "6a24aa21a9ed4db4fb830efe3e804337413ffe8ad7393af301e0ec8e71b6e6f2b860a56f4dcd"
} }
], ],
"invalid": { "invalid": {

21
test/fixtures/templates.json

@ -494,6 +494,27 @@
"hash": "ffffff" "hash": "ffffff"
} }
] ]
},
"witnessCommitment": {
"inputs": [],
"outputs": [
{
"exception": "",
"commitment": "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd"
},
{
"description": "wrong OPCODE at the start",
"scriptPubKeyHex": "6024aa21a9ed4db4fb830efe3e804337413ffe8ad7393af301e0ec8e71b6e6f2b860a56f4dcd"
},
{
"description": "wrong length marker",
"scriptPubKeyHex": "6a23aa21a9ed4db4fb830efe3e804337413ffe8ad7393af301e0ec8e71b6e6f2b860a56f4dcd"
},
{
"description": "commitment of wrong length",
"scriptPubKeyHex": "6a23aa21a9ed4db4fb830efe3e804337413ffe8ad7393af301e0ec8e71b6e6f2b860a56f4d"
}
]
} }
} }
} }

46
test/templates.js

@ -53,7 +53,8 @@ describe('script-templates', function () {
'witnessPubKeyHash', 'witnessPubKeyHash',
'witnessScriptHash', 'witnessScriptHash',
'multisig', 'multisig',
'nullData' 'nullData',
'witnessCommitment'
].forEach(function (name) { ].forEach(function (name) {
var inputType = bscript[name].input var inputType = bscript[name].input
var outputType = bscript[name].output var outputType = bscript[name].output
@ -106,6 +107,12 @@ describe('script-templates', function () {
it('returns ' + expected + ' for ' + f.output, function () { it('returns ' + expected + ' for ' + f.output, function () {
var output = bscript.fromASM(f.output) var output = bscript.fromASM(f.output)
if (name.toLowerCase() === 'nulldata' && f.type === bscript.types.WITNESS_COMMITMENT) {
return
}
if (name.toLowerCase() === 'witnesscommitment' && f.type === bscript.types.NULLDATA) {
return
}
assert.strictEqual(outputType.check(output), expected) assert.strictEqual(outputType.check(output), expected)
}) })
} }
@ -401,6 +408,43 @@ describe('script-templates', function () {
}) })
}) })
describe('witnessCommitment.output', function () {
fixtures.valid.forEach(function (f) {
if (f.type !== 'witnesscommitment') return
if (!f.scriptPubKey) return
var commitment = new Buffer(f.witnessCommitment, 'hex')
var scriptPubKey = bscript.witnessCommitment.output.encode(commitment)
it('encodes to ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
})
it('decodes to ' + commitment.toString('hex'), function () {
assert.deepEqual(bscript.witnessCommitment.output.decode(scriptPubKey), commitment)
})
})
fixtures.invalid.witnessCommitment.outputs.forEach(function (f) {
if (f.commitment) {
var hash = new Buffer(f.commitment, 'hex')
it('throws on bad encode data', function () {
assert.throws(function () {
bscript.witnessCommitment.output.encode(hash)
}, new RegExp(f.exception))
})
}
if (f.scriptPubKeyHex) {
it('.decode throws on ' + f.description, function () {
assert.throws(function () {
bscript.witnessCommitment.output.decode(new Buffer(f.scriptPubKeyHex, 'hex'))
}, new RegExp(f.exception))
})
}
})
})
describe('nullData.output', function () { describe('nullData.output', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'nulldata') return if (f.type !== 'nulldata') return

Loading…
Cancel
Save