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

51 lines
1,023 B
JavaScript
Raw Normal View History

2016-12-14 05:15:01 +01:00
// {signature} {pubKey}
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
function check (script) {
var chunks = bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalSignature(chunks[0]) &&
bscript.isCanonicalPubKey(chunks[1])
}
check.toJSON = function () { return 'pubKeyHash input' }
function encodeStack (signature, pubKey) {
typeforce({
signature: types.Buffer, pubKey: types.Buffer
}, {
signature: signature, pubKey: 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 stack = bscript.decompile(buffer)
return decodeStack(stack)
}
module.exports = {
check: check,
decode: decode,
decodeStack: decodeStack,
encode: encode,
encodeStack: encodeStack
}