templates: add toJSON for clearer error message

This commit is contained in:
Daniel Cousens 2016-11-02 14:33:46 +11:00 committed by Daniel Cousens
parent ce5babde83
commit 0c67f5e585
12 changed files with 20 additions and 4 deletions

View file

@ -19,6 +19,7 @@ function check (script, allowIncomplete) {
return chunks.slice(1).every(bscript.isCanonicalSignature)
}
check.toJSON = function () { return 'multi-sig input' }
function encode (signatures, scriptPubKey) {
typeforce([partialSignature], signatures)

View file

@ -25,6 +25,7 @@ function check (script, allowIncomplete) {
var keys = chunks.slice(1, -2)
return keys.every(bscript.isCanonicalPubKey)
}
check.toJSON = function () { return 'multi-sig output' }
function encode (m, pubKeys) {
typeforce({

View file

@ -11,6 +11,7 @@ function check (script) {
return buffer.length > 1 &&
buffer[0] === OPS.OP_RETURN
}
check.toJSON = function () { return 'null data output' }
function encode (data) {
typeforce(types.Buffer, data)

View file

@ -10,6 +10,7 @@ function check (script) {
return chunks.length === 1 &&
bscript.isCanonicalSignature(chunks[0])
}
check.toJSON = function () { return 'pubKey input' }
function encode (signature) {
typeforce(types.Buffer, signature)

View file

@ -11,6 +11,7 @@ function check (script) {
bscript.isCanonicalPubKey(chunks[0]) &&
chunks[1] === OPS.OP_CHECKSIG
}
check.toJSON = function () { return 'pubKey output' }
function encode (pubKey) {
typeforce(bscript.isCanonicalPubKey, pubKey)

View file

@ -11,6 +11,7 @@ function check (script) {
bscript.isCanonicalSignature(chunks[0]) &&
bscript.isCanonicalPubKey(chunks[1])
}
check.toJSON = function () { return 'pubKeyHash input' }
function encode (signature, pubKey) {
typeforce({

View file

@ -15,6 +15,7 @@ function check (script) {
buffer[23] === OPS.OP_EQUALVERIFY &&
buffer[24] === OPS.OP_CHECKSIG
}
check.toJSON = function () { return 'pubKeyHash output' }
function encode (pubKeyHash) {
typeforce(types.Hash160bit, pubKeyHash)

View file

@ -20,6 +20,7 @@ function check (script, allowIncomplete) {
var outputType = bscript.classifyOutput(redeemScriptChunks)
return inputType === outputType
}
check.toJSON = function () { return 'scriptHash input' }
function encode (scriptSignature, scriptPubKey) {
var scriptSigChunks = bscript.decompile(scriptSignature)

View file

@ -13,6 +13,7 @@ function check (script) {
buffer[1] === 0x14 &&
buffer[22] === OPS.OP_EQUAL
}
check.toJSON = function () { return 'scriptHash output' }
function encode (scriptHash) {
typeforce(types.Hash160bit, scriptHash)

View file

@ -12,6 +12,7 @@ function check (script) {
buffer[0] === OPS.OP_0 &&
buffer[1] === 0x14
}
check.toJSON = function () { return 'Witness pubKeyHash output' }
function encode (pubKeyHash) {
typeforce(types.Hash160bit, pubKeyHash)

View file

@ -12,6 +12,7 @@ function check (script) {
buffer[0] === OPS.OP_0 &&
buffer[1] === 0x20
}
check.toJSON = function () { return 'Witness scriptHash output' }
function encode (scriptHash) {
typeforce(types.Hash256bit, scriptHash)

View file

@ -323,17 +323,22 @@ describe('script-templates', function () {
})
})
describe('witnessScriptHash.outputs.encode', function () {
describe('witnessScriptHash.output', function () {
fixtures.valid.forEach(function (f) {
if (f.type !== 'witnessscripthash') return
if (!f.scriptPubKey) return
it('returns ' + f.scriptPubKey, function () {
var witnessScriptPubKey = bscript.fromASM(f.witnessScriptPubKey)
var scriptPubKey = bscript.witnessScriptHash.output.encode(bcrypto.hash256(witnessScriptPubKey))
var scriptHash = bcrypto.hash256(witnessScriptPubKey)
var scriptPubKey = bscript.witnessScriptHash.output.encode(scriptHash)
it('encodes to ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
})
it('decodes to ' + scriptHash.toString('hex'), function () {
assert.deepEqual(bscript.witnessScriptHash.output.decode(scriptHash), witnessScriptPubKey)
})
})
fixtures.invalid.witnessScriptHash.outputs.forEach(function (f) {