85 lines
2.3 KiB
85 lines
2.3 KiB
// <scriptSig> {serialized scriptPubKey script}
|
|
|
|
const Buffer = require('safe-buffer').Buffer
|
|
const bscript = require('../../script')
|
|
const typeforce = require('typeforce')
|
|
|
|
const p2ms = require('../multisig/')
|
|
const p2pk = require('../pubkey/')
|
|
const p2pkh = require('../pubkeyhash/')
|
|
const p2wpkho = require('../witnesspubkeyhash/output')
|
|
const p2wsho = require('../witnessscripthash/output')
|
|
|
|
function check (script, allowIncomplete) {
|
|
var chunks = bscript.decompile(script)
|
|
if (chunks.length < 1) return false
|
|
|
|
var lastChunk = chunks[chunks.length - 1]
|
|
if (!Buffer.isBuffer(lastChunk)) return false
|
|
|
|
var scriptSigChunks = bscript.decompile(bscript.compile(chunks.slice(0, -1)))
|
|
var redeemScriptChunks = bscript.decompile(lastChunk)
|
|
|
|
// is redeemScript a valid script?
|
|
if (!redeemScriptChunks) return false
|
|
|
|
// is redeemScriptSig push only?
|
|
if (!bscript.isPushOnly(scriptSigChunks)) return false
|
|
|
|
// is witness?
|
|
if (chunks.length === 1) {
|
|
return p2wsho.check(redeemScriptChunks) ||
|
|
p2wpkho.check(redeemScriptChunks)
|
|
}
|
|
|
|
// match types
|
|
if (p2pkh.input.check(scriptSigChunks) &&
|
|
p2pkh.output.check(redeemScriptChunks)) return true
|
|
|
|
if (p2ms.input.check(scriptSigChunks, allowIncomplete) &&
|
|
p2ms.output.check(redeemScriptChunks)) return true
|
|
|
|
if (p2pk.input.check(scriptSigChunks) &&
|
|
p2pk.output.check(redeemScriptChunks)) return true
|
|
|
|
return false
|
|
}
|
|
check.toJSON = function () { return 'scriptHash input' }
|
|
|
|
function encodeStack (redeemScriptStack, redeemScript) {
|
|
var serializedScriptPubKey = bscript.compile(redeemScript)
|
|
|
|
return [].concat(redeemScriptStack, serializedScriptPubKey)
|
|
}
|
|
|
|
function encode (redeemScriptSig, redeemScript) {
|
|
var redeemScriptStack = bscript.decompile(redeemScriptSig)
|
|
|
|
return bscript.compile(encodeStack(redeemScriptStack, redeemScript))
|
|
}
|
|
|
|
function decodeStack (stack) {
|
|
typeforce(typeforce.Array, stack)
|
|
typeforce(check, stack)
|
|
|
|
return {
|
|
redeemScriptStack: stack.slice(0, -1),
|
|
redeemScript: stack[stack.length - 1]
|
|
}
|
|
}
|
|
|
|
function decode (buffer) {
|
|
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,
|
|
decodeStack: decodeStack,
|
|
encode: encode,
|
|
encodeStack: encodeStack
|
|
}
|
|
|