From 3ffac6cd397d79419bde7c5c959ef6744c52e1f0 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 22 Aug 2017 09:15:53 +1000 Subject: [PATCH] add witnessPubKeyHash compressed policy --- src/templates/pubkeyhash/input.js | 7 ++-- src/templates/witnesspubkeyhash/input.js | 43 +++++++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/templates/pubkeyhash/input.js b/src/templates/pubkeyhash/input.js index 82b89f9..0a1fe1e 100644 --- a/src/templates/pubkeyhash/input.js +++ b/src/templates/pubkeyhash/input.js @@ -1,7 +1,6 @@ // {signature} {pubKey} var bscript = require('../../script') -var types = require('../../types') var typeforce = require('typeforce') function check (script) { @@ -15,9 +14,11 @@ check.toJSON = function () { return 'pubKeyHash input' } function encodeStack (signature, pubKey) { typeforce({ - signature: types.Buffer, pubKey: types.Buffer + signature: bscript.isCanonicalSignature, + pubKey: bscript.isCanonicalPubKey }, { - signature: signature, pubKey: pubKey + signature: signature, + pubKey: pubKey }) return [signature, pubKey] diff --git a/src/templates/witnesspubkeyhash/input.js b/src/templates/witnesspubkeyhash/input.js index d9d678b..eacdab9 100644 --- a/src/templates/witnesspubkeyhash/input.js +++ b/src/templates/witnesspubkeyhash/input.js @@ -1,9 +1,44 @@ // {signature} {pubKey} -var pkh = require('../pubkeyhash/input') +var bscript = require('../../script') +var typeforce = require('typeforce') + +function isCompressedCanonicalPubKey (pubKey) { + return bscript.isCanonicalPubKey(pubKey) && pubKey.length === 33 +} + +function check (script) { + var chunks = bscript.decompile(script) + + return chunks.length === 2 && + bscript.isCanonicalSignature(chunks[0]) && + isCompressedCanonicalPubKey(chunks[1]) +} +check.toJSON = function () { return 'witnessPubKeyHash input' } + +function encodeStack (signature, pubKey) { + typeforce({ + signature: bscript.isCanonicalSignature, + pubKey: isCompressedCanonicalPubKey + }, { + signature: signature, + pubKey: pubKey + }) + + return [signature, pubKey] +} + +function decodeStack (stack) { + typeforce(check, stack) + + return { + signature: stack[0], + pubKey: stack[1] + } +} module.exports = { - check: pkh.check, - decodeStack: pkh.decodeStack, - encodeStack: pkh.encodeStack + check: check, + decodeStack: decodeStack, + encodeStack: encodeStack }