Script templates: add witness commitment template

This commit is contained in:
Thomas Kerin 2016-12-13 11:03:07 +00:00 committed by Daniel Cousens
parent 228a2c1879
commit 9d9d101b5f
6 changed files with 115 additions and 2 deletions
src/templates
index.js
witnesscommitment

View file

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

View file

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

View file

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