bitcoinjs-lib/src/templates/witnesspubkeyhash/input.js

46 lines
958 B
JavaScript
Raw Normal View History

// {signature} {pubKey}
var bscript = require('../../script')
var typeforce = require('typeforce')
function isCompressedCanonicalPubKey (pubKey) {
return bscript.isCanonicalPubKey(pubKey) && pubKey.length === 33
}
function check (script) {
var chunks = bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalSignature(chunks[0]) &&
isCompressedCanonicalPubKey(chunks[1])
}
check.toJSON = function () { return 'witnessPubKeyHash input' }
function encodeStack (signature, pubKey) {
typeforce({
signature: bscript.isCanonicalSignature,
pubKey: isCompressedCanonicalPubKey
}, {
signature: signature,
pubKey: pubKey
})
return [signature, pubKey]
}
function decodeStack (stack) {
typeforce(typeforce.Array, stack)
typeforce(check, stack)
return {
signature: stack[0],
pubKey: stack[1]
}
}
module.exports = {
check: check,
decodeStack: decodeStack,
encodeStack: encodeStack
}