templates: add encode/decode* stack functions

This commit is contained in:
Daniel Cousens 2016-12-14 15:52:38 +11:00 committed by Daniel Cousens
parent b862a62867
commit 80762543e7
9 changed files with 98 additions and 40 deletions

View file

@ -52,8 +52,8 @@ function classifyWitness (script, allowIncomplete) {
// XXX: optimization, below functions .decompile before use
var chunks = decompile(script)
if (pubKeyHash.input.check(chunks)) return types.P2WPKH
if (scriptHash.input.check(chunks)) return types.P2WSH
if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH
if (witnessScriptHash.input.check(chunks)) return types.P2WSH
return types.NONSTANDARD
}

View file

@ -21,7 +21,7 @@ function check (script, allowIncomplete) {
}
check.toJSON = function () { return 'multisig input' }
function encode (signatures, scriptPubKey) {
function encodeStack (signatures, scriptPubKey) {
typeforce([partialSignature], signatures)
if (scriptPubKey) {
@ -36,18 +36,27 @@ function encode (signatures, scriptPubKey) {
}
}
return bscript.compile([].concat(OPS.OP_0, signatures))
return [].concat(OPS.OP_0, signatures)
}
function encode (signatures, scriptPubKey) {
return bscript.compile(encodeStack(signatures, scriptPubKey))
}
function decodeStack (stack, allowIncomplete) {
typeforce(check, stack, allowIncomplete)
return stack.slice(1)
}
function decode (buffer, allowIncomplete) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks, allowIncomplete)
return chunks.slice(1)
var stack = bscript.decompile(buffer)
return decodeStack(stack, allowIncomplete)
}
module.exports = {
check: check,
decode: decode,
encode: encode
decodeStack: decodeStack,
encode: encode,
encodeStack: encodeStack
}

View file

@ -12,21 +12,29 @@ function check (script) {
}
check.toJSON = function () { return 'pubKey input' }
function encode (signature) {
function encodeStack (signature) {
typeforce(types.Buffer, signature)
return [signature]
}
return bscript.compile([signature])
function encode (signature) {
return bscript.compile(encodeStack(signature))
}
function decodeStack (stack) {
typeforce(check, stack)
return stack[0]
}
function decode (buffer) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks)
return chunks[0]
var stack = bscript.decompile(buffer)
return decodeStack(stack)
}
module.exports = {
check: check,
decode: decode,
encode: encode
decodeStack: decodeStack,
encode: encode,
encodeStack: encodeStack
}

View file

@ -13,28 +13,38 @@ function check (script) {
}
check.toJSON = function () { return 'pubKeyHash input' }
function encode (signature, pubKey) {
function encodeStack (signature, pubKey) {
typeforce({
signature: types.Buffer, pubKey: types.Buffer
}, {
signature: signature, pubKey: pubKey
})
return bscript.compile([signature, pubKey])
return [signature, pubKey]
}
function encode (signature, pubKey) {
return bscript.compile(encodeStack(signature, pubKey))
}
function decodeStack (stack) {
typeforce(check, stack)
return {
signature: stack[0],
pubKey: stack[1]
}
}
function decode (buffer) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks)
return {
signature: chunks[0],
pubKey: chunks[1]
}
var stack = bscript.decompile(buffer)
return decodeStack(stack)
}
module.exports = {
check: check,
decode: decode,
encode: encode
decodeStack: decodeStack,
encode: encode,
encodeStack: encodeStack
}

View file

@ -25,28 +25,39 @@ function check (script, allowIncomplete) {
}
check.toJSON = function () { return 'scriptHash input' }
function encode (redeemScriptSig, redeemScript) {
var scriptSigChunks = bscript.decompile(redeemScriptSig)
function encodeStack (redeemScriptStack, redeemScript) {
var serializedScriptPubKey = bscript.compile(redeemScript)
return bscript.compile([].concat(
scriptSigChunks,
serializedScriptPubKey
))
return [].concat(redeemScriptStack, serializedScriptPubKey)
}
function encode (redeemScriptSig, redeemScript) {
var redeemScriptStack = bscript.decompile(redeemScriptSig)
return bscript.compile(encodeStack(redeemScriptStack, redeemScript))
}
function decodeStack (stack) {
typeforce(check, stack)
return {
redeemScriptStack: stack.slice(0, -1),
redeemScript: stack[stack.length - 1]
}
}
function decode (buffer) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks)
return {
redeemScriptSig: bscript.compile(chunks.slice(0, -1)),
redeemScript: chunks[chunks.length - 1]
}
var stack = bscript.decompile(buffer)
var result = decodeStack(stack)
result.redeemScriptSig = bscript.compile(result.redeemScriptStack)
delete result.redeemScriptStack
return result
}
module.exports = {
check: check,
decode: decode,
encode: encode
decodeStack: decodeStack,
encode: encode,
encodeStack: encodeStack
}

View file

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

View file

@ -0,0 +1,9 @@
// {signature} {pubKey}
var pkh = require('../pubkeyhash/input')
module.exports = {
check: pkh.check,
decodeStack: pkh.decodeStack,
encodeStack: pkh.encodeStack
}

View file

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

View file

@ -0,0 +1,9 @@
// {signature} {pubKey}
var p2sh = require('../scripthash/input')
module.exports = {
check: p2sh.check,
decodeStack: p2sh.decodeStack,
encodeStack: p2sh.encodeStack
}