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

53 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-12-14 05:15:01 +01:00
// {signature} {pubKey}
2018-06-25 08:25:12 +02:00
const bscript = require('../../script')
const typeforce = require('typeforce')
function check (script) {
var chunks = bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalScriptSignature(chunks[0]) &&
bscript.isCanonicalPubKey(chunks[1])
}
check.toJSON = function () { return 'pubKeyHash input' }
function encodeStack (signature, pubKey) {
typeforce({
signature: bscript.isCanonicalScriptSignature,
pubKey: bscript.isCanonicalPubKey
}, {
signature: signature,
pubKey: pubKey
})
return [signature, pubKey]
}
function encode (signature, pubKey) {
return bscript.compile(encodeStack(signature, pubKey))
}
function decodeStack (stack) {
typeforce(typeforce.Array, stack)
typeforce(check, stack)
return {
signature: stack[0],
pubKey: stack[1]
}
}
function decode (buffer) {
var stack = bscript.decompile(buffer)
return decodeStack(stack)
}
module.exports = {
check: check,
decode: decode,
decodeStack: decodeStack,
encode: encode,
encodeStack: encodeStack
}