Browse Source

tests: add script.*.*.decode tests

hk-custom-address
Daniel Cousens 8 years ago
committed by Daniel Cousens
parent
commit
9db9763221
  1. 4
      src/templates/multisig/input.js
  2. 10
      src/templates/scripthash/input.js
  3. 104
      test/templates.js

4
src/templates/multisig/input.js

@ -43,9 +43,7 @@ function decode (buffer, allowIncomplete) {
var chunks = bscript.decompile(buffer) var chunks = bscript.decompile(buffer)
typeforce(check, chunks, allowIncomplete) typeforce(check, chunks, allowIncomplete)
return { return chunks.slice(1)
signatures: chunks.slice(1)
}
} }
module.exports = { module.exports = {

10
src/templates/scripthash/input.js

@ -22,9 +22,9 @@ function check (script, allowIncomplete) {
} }
check.toJSON = function () { return 'scriptHash input' } check.toJSON = function () { return 'scriptHash input' }
function encode (scriptSignature, scriptPubKey) { function encode (redeemScriptSig, redeemScript) {
var scriptSigChunks = bscript.decompile(scriptSignature) var scriptSigChunks = bscript.decompile(redeemScriptSig)
var serializedScriptPubKey = bscript.compile(scriptPubKey) var serializedScriptPubKey = bscript.compile(redeemScript)
return bscript.compile([].concat( return bscript.compile([].concat(
scriptSigChunks, scriptSigChunks,
@ -37,8 +37,8 @@ function decode (buffer) {
typeforce(check, chunks) typeforce(check, chunks)
return { return {
scriptSignature: chunks.slice(0, -1), redeemScriptSig: bscript.compile(chunks.slice(0, -1)),
scriptPubKey: chunks[chunks.length - 1] redeemScript: chunks[chunks.length - 1]
} }
} }

104
test/templates.js

@ -131,58 +131,76 @@ describe('script-templates', function () {
}) })
}) })
describe('pubKey.input.encode', function () { describe('pubKey.input', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'pubkey') return if (f.type !== 'pubkey') return
it('returns ' + f.scriptSig, function () {
var signature = new Buffer(f.signature, 'hex') var signature = new Buffer(f.signature, 'hex')
var scriptSig = bscript.pubKey.input.encode(signature) var scriptSig = bscript.pubKey.input.encode(signature)
it('encodes to ' + f.scriptSig, function () {
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
}) })
it('decodes to ' + f.signature, function () {
assert.deepEqual(bscript.pubKey.input.decode(scriptSig), signature)
})
}) })
}) })
describe('pubKey.output.encode', function () { describe('pubKey.output', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'pubkey') return if (f.type !== 'pubkey') return
it('returns ' + f.scriptPubKey, function () {
var pubKey = new Buffer(f.pubKey, 'hex') var pubKey = new Buffer(f.pubKey, 'hex')
var scriptPubKey = bscript.pubKey.output.encode(pubKey) var scriptPubKey = bscript.pubKey.output.encode(pubKey)
it('encodes to ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
}) })
it('decodes to ' + f.pubKey, function () {
assert.deepEqual(bscript.pubKey.output.decode(scriptPubKey), pubKey)
})
}) })
}) })
describe('pubKeyHash.input.encode', function () { describe('pubKeyHash.input', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'pubkeyhash') return if (f.type !== 'pubkeyhash') return
var pubKey = new Buffer(f.pubKey, 'hex') var pubKey = new Buffer(f.pubKey, 'hex')
it('returns ' + f.scriptSig, function () {
var signature = new Buffer(f.signature, 'hex') var signature = new Buffer(f.signature, 'hex')
var scriptSig = bscript.pubKeyHash.input.encode(signature, pubKey) var scriptSig = bscript.pubKeyHash.input.encode(signature, pubKey)
it('encodes to ' + f.scriptSig, function () {
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
}) })
it('decodes to original arguments', function () {
assert.deepEqual(bscript.pubKeyHash.input.decode(scriptSig), {
signature: signature,
pubKey: pubKey
})
})
}) })
}) })
describe('pubKeyHash.output.encode', function () { describe('pubKeyHash.output', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'pubkeyhash') return if (f.type !== 'pubkeyhash') return
var pubKey = new Buffer(f.pubKey, 'hex') var pubKey = new Buffer(f.pubKey, 'hex')
var pubKeyHash = bcrypto.hash160(pubKey) var pubKeyHash = bcrypto.hash160(pubKey)
it('returns ' + f.scriptPubKey, function () {
var scriptPubKey = bscript.pubKeyHash.output.encode(pubKeyHash) var scriptPubKey = bscript.pubKeyHash.output.encode(pubKeyHash)
it('encodes to ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
}) })
it('decodes to ' + pubKeyHash.toString('hex'), function () {
assert.deepEqual(bscript.pubKeyHash.output.decode(scriptPubKey), pubKeyHash)
})
}) })
fixtures.invalid.pubKeyHash.outputs.forEach(function (f) { fixtures.invalid.pubKeyHash.outputs.forEach(function (f) {
@ -197,18 +215,24 @@ describe('script-templates', function () {
}) })
}) })
describe('multisig.input.encode', function () { describe('multisig.input', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'multisig') return if (f.type !== 'multisig' && f.typeIncomplete !== 'multisig') return
var allowIncomplete = f.typeIncomplete !== undefined
it('returns ' + f.scriptSig, function () {
var signatures = f.signatures.map(function (signature) { var signatures = f.signatures.map(function (signature) {
return signature ? new Buffer(signature, 'hex') : ops.OP_0 return signature ? new Buffer(signature, 'hex') : ops.OP_0
}) })
var scriptSig = bscript.multisig.input.encode(signatures) var scriptSig = bscript.multisig.input.encode(signatures)
it('encodes to ' + f.scriptSig, function () {
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
}) })
it('decodes to ' + signatures.map(function (x) { return x === ops.OP_0 ? 'OP_0' : x.toString('hex') }), function () {
assert.deepEqual(bscript.multisig.input.decode(scriptSig, allowIncomplete), signatures)
})
}) })
fixtures.invalid.multisig.inputs.forEach(function (f) { fixtures.invalid.multisig.inputs.forEach(function (f) {
@ -227,16 +251,25 @@ describe('script-templates', function () {
}) })
}) })
describe('multisig.output.encode', function () { describe('multisig.output', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'multisig') return if (f.type !== 'multisig') return
var pubKeys = f.pubKeys.map(function (p) { return new Buffer(p, 'hex') }) var pubKeys = f.pubKeys.map(function (p) { return new Buffer(p, 'hex') })
var scriptPubKey = bscript.multisig.output.encode(pubKeys.length, pubKeys) var m = pubKeys.length
var scriptPubKey = bscript.multisig.output.encode(m, pubKeys)
it('returns ' + f.scriptPubKey, function () { it('encodes ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
}) })
it('decodes to original arguments', function () {
assert.deepEqual(bscript.multisig.output.decode(scriptPubKey), {
m: m,
pubKeys: pubKeys
})
})
}) })
fixtures.invalid.multisig.outputs.forEach(function (f) { fixtures.invalid.multisig.outputs.forEach(function (f) {
@ -253,36 +286,47 @@ describe('script-templates', function () {
}) })
}) })
describe('scriptHash.input.encode', function () { describe('scriptHash.input', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'scripthash') return if (f.type !== 'scripthash') return
var redeemScript = bscript.fromASM(f.redeemScript) var redeemScript = bscript.fromASM(f.redeemScript)
var redeemScriptSig = bscript.fromASM(f.redeemScriptSig) var redeemScriptSig = bscript.fromASM(f.redeemScriptSig)
it('returns ' + f.scriptSig, function () {
var scriptSig = bscript.scriptHash.input.encode(redeemScriptSig, redeemScript) var scriptSig = bscript.scriptHash.input.encode(redeemScriptSig, redeemScript)
it('encodes to ' + f.scriptPubKey, function () {
if (f.scriptSig) { if (f.scriptSig) {
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
} else { } else {
assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex) assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex)
} }
}) })
it('decodes to original arguments', function () {
assert.deepEqual(bscript.scriptHash.input.decode(scriptSig), {
redeemScriptSig: redeemScriptSig,
redeemScript: redeemScript
})
})
}) })
}) })
describe('scriptHash.output.encode', function () { describe('scriptHash.output', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'scripthash') return if (f.type !== 'scripthash') return
if (!f.scriptPubKey) return if (!f.scriptPubKey) return
it('returns ' + f.scriptPubKey, function () {
var redeemScript = bscript.fromASM(f.redeemScript) var redeemScript = bscript.fromASM(f.redeemScript)
var scriptPubKey = bscript.scriptHash.output.encode(bcrypto.hash160(redeemScript)) var scriptHash = bcrypto.hash160(redeemScript)
var scriptPubKey = bscript.scriptHash.output.encode(scriptHash)
it('encodes to ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
}) })
it('decodes to ' + scriptHash.toString('hex'), function () {
assert.deepEqual(bscript.scriptHash.output.decode(scriptPubKey), scriptHash)
})
}) })
fixtures.invalid.scriptHash.outputs.forEach(function (f) { fixtures.invalid.scriptHash.outputs.forEach(function (f) {
@ -297,18 +341,22 @@ describe('script-templates', function () {
}) })
}) })
describe('witnessPubKeyHash.output.encode', function () { describe('witnessPubKeyHash.output', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'witnesspubkeyhash') return if (f.type !== 'witnesspubkeyhash') return
if (!f.scriptPubKey) return if (!f.scriptPubKey) return
var pubKey = new Buffer(f.pubKey, 'hex') var pubKey = new Buffer(f.pubKey, 'hex')
var pubKeyHash = bcrypto.hash160(pubKey) var pubKeyHash = bcrypto.hash160(pubKey)
it('returns ' + f.scriptPubKey, function () {
var scriptPubKey = bscript.witnessPubKeyHash.output.encode(pubKeyHash) var scriptPubKey = bscript.witnessPubKeyHash.output.encode(pubKeyHash)
it('encodes to ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
}) })
it('decodes to ' + pubKeyHash.toString('hex'), function () {
assert.deepEqual(bscript.witnessPubKeyHash.output.decode(scriptPubKey), pubKeyHash)
})
}) })
fixtures.invalid.witnessPubKeyHash.outputs.forEach(function (f) { fixtures.invalid.witnessPubKeyHash.outputs.forEach(function (f) {
@ -337,7 +385,7 @@ describe('script-templates', function () {
}) })
it('decodes to ' + scriptHash.toString('hex'), function () { it('decodes to ' + scriptHash.toString('hex'), function () {
assert.deepEqual(bscript.witnessScriptHash.output.decode(scriptHash), witnessScriptPubKey) assert.deepEqual(bscript.witnessScriptHash.output.decode(scriptPubKey), scriptHash)
}) })
}) })

Loading…
Cancel
Save