Browse Source

templates: add toJSON for clearer error message

hk-custom-address
Daniel Cousens 8 years ago
committed by Daniel Cousens
parent
commit
0c67f5e585
  1. 1
      src/templates/multisig/input.js
  2. 1
      src/templates/multisig/output.js
  3. 1
      src/templates/nulldata.js
  4. 1
      src/templates/pubkey/input.js
  5. 1
      src/templates/pubkey/output.js
  6. 1
      src/templates/pubkeyhash/input.js
  7. 1
      src/templates/pubkeyhash/output.js
  8. 1
      src/templates/scripthash/input.js
  9. 1
      src/templates/scripthash/output.js
  10. 1
      src/templates/witnesspubkeyhash/output.js
  11. 1
      src/templates/witnessscripthash/output.js
  12. 11
      test/templates.js

1
src/templates/multisig/input.js

@ -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)

1
src/templates/multisig/output.js

@ -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({

1
src/templates/nulldata.js

@ -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)

1
src/templates/pubkey/input.js

@ -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)

1
src/templates/pubkey/output.js

@ -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)

1
src/templates/pubkeyhash/input.js

@ -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({

1
src/templates/pubkeyhash/output.js

@ -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)

1
src/templates/scripthash/input.js

@ -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)

1
src/templates/scripthash/output.js

@ -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)

1
src/templates/witnesspubkeyhash/output.js

@ -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)

1
src/templates/witnessscripthash/output.js

@ -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)

11
test/templates.js

@ -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) {

Loading…
Cancel
Save