scripts: extract all scripts to encode/decode/check style

This commit is contained in:
Daniel Cousens 2016-11-02 12:30:37 +11:00 committed by Daniel Cousens
parent 27b48e8aa2
commit d9fa39a2cc
23 changed files with 852 additions and 729 deletions

View file

@ -28,8 +28,8 @@ function toBase58Check (hash, version) {
function fromOutputScript (scriptPubKey, network) {
network = network || networks.bitcoin
if (bscript.isPubKeyHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(3, 23), network.pubKeyHash)
if (bscript.isScriptHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(2, 22), network.scriptHash)
if (bscript.pubKeyHash.output.check(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(3, 23), network.pubKeyHash)
if (bscript.scriptHash.output.check(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(2, 22), network.scriptHash)
throw new Error(bscript.toASM(scriptPubKey) + ' has no matching Address')
}
@ -38,8 +38,8 @@ function toOutputScript (address, network) {
network = network || networks.bitcoin
var decode = fromBase58Check(address)
if (decode.version === network.pubKeyHash) return bscript.pubKeyHashOutput(decode.hash)
if (decode.version === network.scriptHash) return bscript.scriptHashOutput(decode.hash)
if (decode.version === network.pubKeyHash) return bscript.pubKeyHash.output.encode(decode.hash)
if (decode.version === network.scriptHash) return bscript.scriptHash.output.encode(decode.hash)
throw new Error(address + ' has no matching Script')
}

View file

@ -12,16 +12,6 @@ var REVERSE_OPS = (function () {
return result
})()
var LIST_DECODE_NAMES = [
decodePubKeyOutput,
decodePubKeyHashOutput,
decodeMultisigOutput,
decodeNullDataOutput,
decodeScriptHashOutput,
decodeWitnessPubKeyHashOutput,
decodeWitnessScriptHashOutput
]
var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
function compile (chunks) {
@ -169,410 +159,71 @@ function isCanonicalSignature (buffer) {
return bip66.check(buffer.slice(0, -1))
}
function isPubKeyHashInput (script) {
var chunks = decompile(script)
return chunks.length === 2 &&
isCanonicalSignature(chunks[0]) &&
isCanonicalPubKey(chunks[1])
}
function decodePubKeyHashOutput (buffer, chunks) {
if (buffer.length !== 25) {
throw new Error('pub-key-hash output script is 25 bytes')
}
if (buffer[0] !== OPS.OP_DUP) {
throw new Error('PubKeyHash script missing OP_DUP')
}
if (buffer[1] !== OPS.OP_HASH160) {
throw new Error('PubKeyHash script missing OP_HASH160')
}
if (buffer[2] !== 0x14) {
throw new Error('Incorrect opcode for pubkeyhash')
}
if (buffer[23] !== OPS.OP_EQUALVERIFY) {
throw new Error('PubKeyHash script missing OP_EQUALVERIFY')
}
if (buffer[24] !== OPS.OP_CHECKSIG) {
throw new Error('PubKeyHash script missing OP_CHECKSIG')
}
return {
type: 'pubkeyhash',
pubKeyHash: chunks[2]
}
}
function isPubKeyHashOutput (script) {
return determinesTypeOrNonstandard([decodePubKeyHashOutput], script) === 'pubkeyhash'
}
function isPubKeyInput (script) {
var chunks = decompile(script)
return chunks.length === 1 &&
isCanonicalSignature(chunks[0])
}
function decodePubKeyOutput (script, chunks) {
if (script[0] !== 0x21 && script[0] !== 0x41) {
throw new Error('Bad (or non-minimal) public key length for pub')
}
if (!isCanonicalPubKey(chunks[0])) {
throw new Error('pub-key output does not have a canonical public key')
}
if (chunks[1] !== OPS.OP_CHECKSIG) {
throw new Error('pub-key output missing OP_CHECKSIG operator')
}
if (chunks.length !== 2) {
throw new Error('pub-key output has two elements')
}
return {
type: 'pubkey',
publicKey: chunks[0]
}
}
function isPubKeyOutput (script) {
return determinesTypeOrNonstandard([decodePubKeyOutput], script) === 'pubkey'
}
function isScriptHashInput (script, allowIncomplete) {
var chunks = decompile(script)
if (chunks.length < 2) return false
var lastChunk = chunks[chunks.length - 1]
if (!Buffer.isBuffer(lastChunk)) return false
var scriptSigChunks = chunks.slice(0, -1)
var redeemScriptChunks = decompile(lastChunk)
// is redeemScript a valid script?
if (redeemScriptChunks.length === 0) return false
return classifyInput(scriptSigChunks, allowIncomplete) === classifyOutput(lastChunk)
}
function decodeScriptHashOutput (chunks) {
if (chunks[0] !== OPS.OP_HASH160) {
throw new Error()
}
if (chunks[1] !== 0x14) {
throw new Error()
}
if (chunks[22] !== OPS.OP_EQUAL) {
throw new Error()
}
return {
type: 'scripthash',
scriptHash: chunks[1]
}
}
function isScriptHashOutput (script) {
return determinesTypeOrNonstandard([decodeScriptHashOutput], script) === 'scripthash'
}
function decodeWitnessPubKeyHashOutput (script, chunks) {
if (script.length !== 22) {
throw new Error('P2WPKH script should be 22 bytes')
}
if (script[0] !== OPS.OP_0) {
throw new Error('Missing v0 prefix for witness keyhash')
}
if (script[1] !== 0x14) {
throw new Error('Witness keyhash length marker is wrong')
}
return {
type: 'witnesspubkeyhash',
witnessKeyHash: chunks[2]
}
}
function isWitnessPubKeyHashOutput (script) {
return determinesTypeOrNonstandard([decodeWitnessPubKeyHashOutput], script) === 'witnesspubkeyhash'
}
function decodeWitnessScriptHashOutput (script, chunks) {
if (script.length !== 34) {
throw new Error('P2WSH script should be 34 bytes')
}
if (script[0] !== OPS.OP_0) {
throw new Error('Missing v0 prefix for witness script hash')
}
if (script[1] !== 0x20) {
throw new Error('Witness program length marker is wrong')
}
return {
type: 'witnessscripthash',
witnessScriptHash: chunks[2]
}
}
function isWitnessScriptHashOutput (script) {
return determinesTypeOrNonstandard([decodeWitnessScriptHashOutput], script) === 'witnessscripthash'
}
// allowIncomplete is to account for combining signatures
// See https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/sign.cpp#L195-L197
function isMultisigInput (script, allowIncomplete) {
var chunks = decompile(script)
if (chunks.length < 2) return false
if (chunks[0] !== OPS.OP_0) return false
if (allowIncomplete) {
return chunks.slice(1).every(function (chunk) {
return chunk === OPS.OP_0 || isCanonicalSignature(chunk)
})
}
return chunks.slice(1).every(isCanonicalSignature)
}
function decodeMultisigOutput (scriptPubKey, chunks) {
if (chunks.length < 4) {
throw new Error('Multisig script should contain at least 4 elements')
}
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) {
throw new Error('Final opcode should be OP_CHECKMULTISIG')
}
if (!types.Number(chunks[0])) {
throw new Error('First element must be a number-push opcode')
}
if (!types.Number(chunks[chunks.length - 2])) {
throw new Error('Second-last element must be a number-push opcode')
}
var m = chunks[0] - OP_INT_BASE
var n = chunks[chunks.length - 2] - OP_INT_BASE
if (m <= 0) {
throw new Error('Number of signatures required must be greater than zero')
}
if (n > 16) {
throw new Error('Number of public keys must be less than 17')
}
if (m > n) {
throw new Error('Number of signatures cannot exceed the number of public keys')
}
if (n !== chunks.length - 3) {
throw new Error('n does not match the number of public keys')
}
var keys = chunks.slice(1, -2)
if (!keys.every(isCanonicalPubKey)) {
throw new Error('Non-canonical pubic key found')
}
return {
type: 'multisig',
nRequiredSigs: m,
nPublicKeys: n,
publicKeyBuffers: keys
}
}
function isMultisigOutput (script) {
return determinesTypeOrNonstandard([decodeMultisigOutput], script) === 'multisig'
}
function isNullDataOutput (script) {
return determinesTypeOrNonstandard([decodeNullDataOutput], script) === 'nulldata'
}
function decodeNullDataOutput (script, chunks) {
if (script[0] !== OPS.OP_RETURN) {
throw new Error('Missing OP_RETURN at start of script')
}
return {
type: 'nulldata'
}
}
function determinesTypeOrNonstandard (functions, script) {
if (!types.Array(functions)) {
throw new Error('Must provide an array of functions to determinesTypeOrNonstandard')
}
if (!types.Buffer(script)) {
throw new Error('Must provide a script to determinesTypeOrNonstandard')
}
var decoded
var decompiled = decompile(script)
var type = 'nonstandard'
for (var i = 0; i < functions.length && type === 'nonstandard'; i++) {
try {
decoded = functions[i](script, decompiled)
type = decoded.type
} catch (e) {
}
}
return type
}
function classifyOutput (script) {
return determinesTypeOrNonstandard(LIST_DECODE_NAMES, script)
}
function classifyInput (script, allowIncomplete) {
var chunks = decompile(script)
if (isPubKeyHashInput(chunks)) {
return 'pubkeyhash'
} else if (isMultisigInput(chunks, allowIncomplete)) {
return 'multisig'
} else if (isScriptHashInput(chunks, allowIncomplete)) {
return 'scripthash'
} else if (isPubKeyInput(chunks)) {
return 'pubkey'
}
return 'nonstandard'
}
// Standard Script Templates
// {pubKey} OP_CHECKSIG
function pubKeyOutput (pubKey) {
return compile([pubKey, OPS.OP_CHECKSIG])
}
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
function pubKeyHashOutput (pubKeyHash) {
typeforce(types.Hash160bit, pubKeyHash)
return compile([OPS.OP_DUP, OPS.OP_HASH160, pubKeyHash, OPS.OP_EQUALVERIFY, OPS.OP_CHECKSIG])
}
// OP_HASH160 {scriptHash} OP_EQUAL
function scriptHashOutput (scriptHash) {
typeforce(types.Hash160bit, scriptHash)
return compile([OPS.OP_HASH160, scriptHash, OPS.OP_EQUAL])
}
// m [pubKeys ...] n OP_CHECKMULTISIG
function multisigOutput (m, pubKeys) {
typeforce(types.tuple(types.Number, [types.Buffer]), arguments)
var n = pubKeys.length
if (n < m) throw new Error('Not enough pubKeys provided')
return compile([].concat(
OP_INT_BASE + m,
pubKeys,
OP_INT_BASE + n,
OPS.OP_CHECKMULTISIG
))
}
// OP_0 {pubKeyHash}
function witnessPubKeyHashOutput (pubKeyHash) {
typeforce(types.Hash160bit, pubKeyHash)
return compile([OPS.OP_0, pubKeyHash])
}
// OP_0 {scriptHash}
function witnessScriptHashOutput (scriptHash) {
typeforce(types.Hash256bit, scriptHash)
return compile([OPS.OP_0, scriptHash])
}
// {signature}
function pubKeyInput (signature) {
typeforce(types.Buffer, signature)
return compile([signature])
}
// {signature} {pubKey}
function pubKeyHashInput (signature, pubKey) {
typeforce(types.tuple(types.Buffer, types.Buffer), arguments)
return compile([signature, pubKey])
}
// <scriptSig> {serialized scriptPubKey script}
function scriptHashInput (scriptSig, scriptPubKey) {
var scriptSigChunks = decompile(scriptSig)
var serializedScriptPubKey = compile(scriptPubKey)
return compile([].concat(
scriptSigChunks,
serializedScriptPubKey
))
}
// <scriptSig> {serialized scriptPubKey script}
function witnessScriptHashInput (scriptSig, scriptPubKey) {
return scriptHashInput(scriptSig, scriptPubKey)
}
// OP_0 [signatures ...]
function multisigInput (signatures, scriptPubKey) {
if (scriptPubKey) {
var scriptData = decodeMultisigOutput(scriptPubKey, decompile(scriptPubKey))
if (signatures.length < scriptData.nRequiredSigs) throw new Error('Not enough signatures provided')
if (signatures.length > scriptData.nPublicKeys) throw new Error('Too many signatures provided')
}
return compile([].concat(OPS.OP_0, signatures))
}
function nullDataOutput (data) {
return compile([OPS.OP_RETURN, data])
}
module.exports = {
compile: compile,
decompile: decompile,
fromASM: fromASM,
toASM: toASM,
number: require('./script_number'),
isCanonicalPubKey: isCanonicalPubKey,
isCanonicalSignature: isCanonicalSignature,
isDefinedHashType: isDefinedHashType,
decodePubKeyOutput: decodePubKeyOutput,
decodePubKeyHashOutput: decodePubKeyHashOutput,
decodeMultisigOutput: decodeMultisigOutput,
decodeScriptHashOutput: decodeScriptHashOutput,
decodeNullDataOutput: decodeNullDataOutput,
decodeWitnessPubKeyHashOutput: decodeWitnessPubKeyHashOutput,
decodeWitnessScriptHashOutput: decodeWitnessScriptHashOutput,
isPubKeyOutput: isPubKeyOutput,
isPubKeyHashOutput: isPubKeyHashOutput,
isMultisigOutput: isMultisigOutput,
isScriptHashOutput: isScriptHashOutput,
isNullDataOutput: isNullDataOutput,
isWitnessPubKeyHashOutput: isWitnessPubKeyHashOutput,
isWitnessScriptHashOutput: isWitnessScriptHashOutput,
classifyOutput: classifyOutput,
isPubKeyInput: isPubKeyInput,
isPubKeyHashInput: isPubKeyHashInput,
isMultisigInput: isMultisigInput,
isScriptHashInput: isScriptHashInput,
classifyInput: classifyInput,
pubKeyOutput: pubKeyOutput,
pubKeyHashOutput: pubKeyHashOutput,
multisigOutput: multisigOutput,
scriptHashOutput: scriptHashOutput,
nullDataOutput: nullDataOutput,
witnessPubKeyHashOutput: witnessPubKeyHashOutput,
witnessScriptHashOutput: witnessScriptHashOutput,
pubKeyInput: pubKeyInput,
pubKeyHashInput: pubKeyHashInput,
multisigInput: multisigInput,
scriptHashInput: scriptHashInput,
witnessScriptHashInput: witnessScriptHashInput
isDefinedHashType: isDefinedHashType
}
//
var multisig = require('./templates/multisig')
var nullData = require('./templates/nulldata')
var pubKey = require('./templates/pubkey')
var pubKeyHash = require('./templates/pubkeyhash')
var scriptHash = require('./templates/scripthash')
var witnessPubKeyHash = require('./templates/witnesspubkeyhash')
var witnessScriptHash = require('./templates/witnessscripthash')
function classifyOutput (script) {
if (witnessPubKeyHash.output.check(script)) return 'witnesspubkeyhash'
if (witnessScriptHash.output.check(script)) return 'witnessscripthash'
if (pubKeyHash.output.check(script)) return 'pubkeyhash'
if (scriptHash.output.check(script)) return 'scripthash'
// XXX: optimization, below functions .decompile before use
var chunks = decompile(script)
if (multisig.output.check(chunks)) return 'multisig'
if (pubKey.output.check(chunks)) return 'pubkey'
if (nullData.output.check(chunks)) return 'nulldata'
return 'nonstandard'
}
function classifyInput (script, allowIncomplete) {
// XXX: optimization, below functions .decompile before use
var chunks = decompile(script)
if (pubKeyHash.input.check(chunks)) return 'pubkeyhash'
if (scriptHash.input.check(chunks, allowIncomplete)) return 'scripthash'
if (multisig.input.check(chunks, allowIncomplete)) return 'multisig'
if (pubKey.input.check(chunks)) return 'pubkey'
return 'nonstandard'
}
function classifyWitness (script, allowIncomplete) {
// XXX: optimization, below functions .decompile before use
var chunks = decompile(script)
if (pubKeyHash.input.check(chunks)) return 'witnesspubkeyhash'
if (scriptHash.input.check(chunks)) return 'witnessscripthash'
return 'nonstandard'
}
module.exports.classifyInput = classifyInput
module.exports.classifyOutput = classifyOutput
module.exports.classifyWitness = classifyWitness
module.exports.multisig = multisig
module.exports.nullData = nullData
module.exports.pubKey = pubKey
module.exports.pubKeyHash = pubKeyHash
module.exports.scriptHash = scriptHash
module.exports.witnessPubKeyHash = witnessPubKeyHash
module.exports.witnessScriptHash = witnessScriptHash

View file

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

View file

@ -0,0 +1,54 @@
// OP_0 [signatures ...]
var bscript = require('../../script')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
function partialSignature (value) {
return value === OPS.OP_0 || bscript.isCanonicalSignature(value)
}
function check (script, allowIncomplete) {
var chunks = bscript.decompile(script)
if (chunks.length < 2) return false
if (chunks[0] !== OPS.OP_0) return false
if (allowIncomplete) {
return chunks.slice(1).every(partialSignature)
}
return chunks.slice(1).every(bscript.isCanonicalSignature)
}
function encode (signatures, scriptPubKey) {
typeforce([partialSignature], signatures)
if (scriptPubKey) {
var scriptData = bscript.multisig.output.decode(scriptPubKey)
if (signatures.length < scriptData.m) {
throw new TypeError('Not enough signatures provided')
}
if (signatures.length > scriptData.pubKeys.length) {
throw new TypeError('Too many signatures provided')
}
}
return bscript.compile([].concat(OPS.OP_0, signatures))
}
function decode (buffer, allowIncomplete) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks, allowIncomplete)
return {
signatures: chunks.slice(1)
}
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

@ -0,0 +1,63 @@
// m [pubKeys ...] n OP_CHECKMULTISIG
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
function check (script, allowIncomplete) {
var chunks = bscript.decompile(script)
if (chunks.length < 4) return false
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) return false
if (!types.Number(chunks[0])) return false
if (!types.Number(chunks[chunks.length - 2])) return false
var m = chunks[0] - OP_INT_BASE
var n = chunks[chunks.length - 2] - OP_INT_BASE
if (m <= 0) return false
if (n > 16) return false
if (m > n) return false
if (n !== chunks.length - 3) return false
if (allowIncomplete) return true
var keys = chunks.slice(1, -2)
return keys.every(bscript.isCanonicalPubKey)
}
function encode (m, pubKeys) {
typeforce({
m: types.Number,
pubKeys: [bscript.isCanonicalPubKey]
}, {
m: m,
pubKeys: pubKeys
})
var n = pubKeys.length
if (n < m) throw new TypeError('Not enough pubKeys provided')
return bscript.compile([].concat(
OP_INT_BASE + m,
pubKeys,
OP_INT_BASE + n,
OPS.OP_CHECKMULTISIG
))
}
function decode (buffer, allowIncomplete) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks, allowIncomplete)
return {
m: chunks[0] - OP_INT_BASE,
pubKeys: chunks.slice(1, -2)
}
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

34
src/templates/nulldata.js Normal file
View file

@ -0,0 +1,34 @@
// OP_RETURN {data}
var bscript = require('../script')
var types = require('../types')
var typeforce = require('typeforce')
var OPS = require('../opcodes.json')
function check (script) {
var buffer = bscript.compile(script)
return buffer.length > 1 &&
buffer[0] === OPS.OP_RETURN
}
function encode (data) {
typeforce(types.Buffer, data)
return bscript.compile([OPS.OP_RETURN, data])
}
function decode (buffer) {
typeforce(check, buffer)
return buffer.slice(1)
}
module.exports = {
output: {
check: check,
decode: decode,
encode: encode
}
}

View file

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

View file

@ -0,0 +1,31 @@
// {signature}
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
function check (script) {
var chunks = bscript.decompile(script)
return chunks.length === 1 &&
bscript.isCanonicalSignature(chunks[0])
}
function encode (signature) {
typeforce(types.Buffer, signature)
return bscript.compile([signature])
}
function decode (buffer) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks)
return chunks[0]
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

@ -0,0 +1,32 @@
// {pubKey} OP_CHECKSIG
var bscript = require('../../script')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
function check (script) {
var chunks = bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalPubKey(chunks[0]) &&
chunks[1] === OPS.OP_CHECKSIG
}
function encode (pubKey) {
typeforce(bscript.isCanonicalPubKey, pubKey)
return bscript.compile([pubKey, OPS.OP_CHECKSIG])
}
function decode (buffer) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks)
return chunks[0]
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

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

View file

@ -0,0 +1,39 @@
// {pubKey} OP_CHECKSIG
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])
}
function encode (signature, pubKey) {
typeforce({
signature: types.Buffer, pubKey: types.Buffer
}, {
signature: signature, pubKey: pubKey
})
return bscript.compile([signature, pubKey])
}
function decode (buffer) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks)
return {
signature: chunks[0],
pubKey: chunks[1]
}
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

@ -0,0 +1,41 @@
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
function check (script) {
var buffer = bscript.compile(script)
return buffer.length === 25 &&
buffer[0] === OPS.OP_DUP &&
buffer[1] === OPS.OP_HASH160 &&
buffer[2] === 0x14 &&
buffer[23] === OPS.OP_EQUALVERIFY &&
buffer[24] === OPS.OP_CHECKSIG
}
function encode (pubKeyHash) {
typeforce(types.Hash160bit, pubKeyHash)
return bscript.compile([
OPS.OP_DUP,
OPS.OP_HASH160,
pubKeyHash,
OPS.OP_EQUALVERIFY,
OPS.OP_CHECKSIG
])
}
function decode (buffer) {
typeforce(check, buffer)
return buffer.slice(3, 23)
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

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

View file

@ -0,0 +1,48 @@
// <scriptSig> {serialized scriptPubKey script}
var bscript = require('../../script')
var typeforce = require('typeforce')
function check (script, allowIncomplete) {
var chunks = bscript.decompile(script)
if (chunks.length < 2) return false
var lastChunk = chunks[chunks.length - 1]
if (!Buffer.isBuffer(lastChunk)) return false
var scriptSigChunks = chunks.slice(0, -1)
var redeemScriptChunks = bscript.decompile(lastChunk)
// is redeemScript a valid script?
if (redeemScriptChunks.length === 0) return false
var inputType = bscript.classifyInput(scriptSigChunks, allowIncomplete)
var outputType = bscript.classifyOutput(redeemScriptChunks)
return inputType === outputType
}
function encode (scriptSignature, scriptPubKey) {
var scriptSigChunks = bscript.decompile(scriptSignature)
var serializedScriptPubKey = bscript.compile(scriptPubKey)
return bscript.compile([].concat(
scriptSigChunks,
serializedScriptPubKey
))
}
function decode (buffer) {
var chunks = bscript.decompile(buffer)
typeforce(check, chunks)
return {
scriptSignature: chunks.slice(0, -1),
scriptPubKey: chunks[chunks.length - 1]
}
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

@ -0,0 +1,33 @@
// OP_HASH160 {scriptHash} OP_EQUAL
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
function check (script) {
var buffer = bscript.compile(script)
return buffer.length === 23 &&
buffer[0] === OPS.OP_HASH160 &&
buffer[1] === 0x14 &&
buffer[22] === OPS.OP_EQUAL
}
function encode (scriptHash) {
typeforce(types.Hash160bit, scriptHash)
return bscript.compile([OPS.OP_HASH160, scriptHash, OPS.OP_EQUAL])
}
function decode (buffer) {
typeforce(check, buffer)
return buffer.slice(2, 22)
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

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

View file

@ -0,0 +1,32 @@
// OP_0 {pubKeyHash}
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
function check (script) {
var buffer = bscript.compile(script)
return buffer.length === 22 &&
buffer[0] === OPS.OP_0 &&
buffer[1] === 0x14
}
function encode (pubKeyHash) {
typeforce(types.Hash160bit, pubKeyHash)
return bscript.compile([OPS.OP_0, pubKeyHash])
}
function decode (buffer) {
typeforce(check, buffer)
return buffer.slice(2)
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

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

View file

@ -0,0 +1,32 @@
// OP_0 {scriptHash}
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
function check (script) {
var buffer = bscript.compile(script)
return buffer.length === 34 &&
buffer[0] === OPS.OP_0 &&
buffer[1] === 0x20
}
function encode (scriptHash) {
typeforce(types.Hash256bit, scriptHash)
return bscript.compile([OPS.OP_0, scriptHash])
}
function decode (buffer) {
typeforce(check, buffer)
return buffer.slice(2)
}
module.exports = {
check: check,
decode: decode,
encode: encode
}

View file

@ -29,7 +29,7 @@ function expandInput (scriptSig, redeemScript) {
var result = expandInput(redeemScriptSig, redeemScript)
result.redeemScript = redeemScript
result.redeemScriptType = result.prevOutType
result.prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript))
result.prevOutScript = bscript.scriptHash.output.encode(bcrypto.hash160(redeemScript))
result.prevOutType = 'scripthash'
return result
@ -39,7 +39,7 @@ function expandInput (scriptSig, redeemScript) {
signatures = scriptSigChunks.slice(0, 1)
if (redeemScript) break
prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
prevOutScript = bscript.pubKeyHash.output.encode(bcrypto.hash160(pubKeys[0]))
break
case 'pubkey':
@ -161,7 +161,7 @@ function prepareInput (input, kpPubKey, redeemScript) {
input.signatures = expanded.signatures
input.redeemScript = redeemScript
input.redeemScriptType = expanded.scriptType
input.prevOutScript = input.prevOutScript || bscript.scriptHashOutput(redeemScriptHash)
input.prevOutScript = input.prevOutScript || bscript.scriptHash.output.encode(redeemScriptHash)
input.prevOutType = 'scripthash'
// maybe we have some prevOut knowledge
@ -178,7 +178,7 @@ function prepareInput (input, kpPubKey, redeemScript) {
// no prior knowledge, assume pubKeyHash
} else {
input.prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(kpPubKey))
input.prevOutScript = bscript.pubKeyHash.output.encode(bcrypto.hash160(kpPubKey))
input.prevOutType = 'pubkeyhash'
input.pubKeys = [kpPubKey]
input.signatures = [undefined]
@ -195,9 +195,9 @@ function buildInput (input, allowIncomplete) {
case 'pubkey':
if (signatures.length < 1 || !signatures[0]) throw new Error('Not enough signatures provided')
if (scriptType === 'pubkeyhash') {
scriptSig = bscript.pubKeyHashInput(signatures[0], input.pubKeys[0])
scriptSig = bscript.pubKeyHash.input.encode(signatures[0], input.pubKeys[0])
} else {
scriptSig = bscript.pubKeyInput(signatures[0])
scriptSig = bscript.pubKey.input.encode(signatures[0])
}
break
@ -213,7 +213,7 @@ function buildInput (input, allowIncomplete) {
signatures = signatures.filter(function (x) { return x !== ops.OP_0 })
}
scriptSig = bscript.multisigInput(signatures, allowIncomplete ? undefined : input.redeemScript)
scriptSig = bscript.multisig.input.encode(signatures, allowIncomplete ? undefined : input.redeemScript)
break
default: return
@ -221,7 +221,7 @@ function buildInput (input, allowIncomplete) {
// wrap as scriptHash if necessary
if (input.prevOutType === 'scripthash') {
scriptSig = bscript.scriptHashInput(scriptSig, input.redeemScript)
scriptSig = bscript.scriptHash.input.encode(scriptSig, input.redeemScript)
}
return scriptSig