From 113e7dcdc3813ef341faf23a68a97a9484b22523 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Mon, 5 May 2014 04:30:39 +0400 Subject: [PATCH] tx: fix verification --- lib/bcoin/protocol/constants.js | 8 ++++---- lib/bcoin/script.js | 23 +++++++++++++++++------ lib/bcoin/tx.js | 10 ++++++++-- lib/bcoin/wallet.js | 2 +- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index 1ab04a72..a3be0e7b 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -142,8 +142,8 @@ Object.keys(exports.opcodes).forEach(function(name) { // Little-endian hash type exports.hashType = { - all: [ 1, 0, 0, 0 ], - none: [ 2, 0, 0, 0 ], - single: [ 3, 0, 0, 0 ], - anyonecaypay: [ 0x80, 0, 0, 0 ], + all: 1, + none: 2, + single: 3, + anyonecaypay: 0x80 }; diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index d02e278b..a86eb2a3 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -115,19 +115,30 @@ script.execute = function execute(s, stack, tx) { return false; var res = bcoin.utils.isEqual(stack.pop(), stack.pop()); - stack.push([ res ? 1 : 0 ]); - if (!res && o === 'eqverify') - return false; + if (o === 'eqverify') { + if (!res) + return false; + } else { + stack.push([ res ? 1 : 0 ]); + } + } else if (o === 'checksigverify' || o === 'checksig') { if (!tx || stack.length < 2) return false; var pub = stack.pop(); var sig = stack.pop(); - var res = bcoin.ecdsa.verify(tx, sig, pub); - stack.push([ res ? 1 : 0 ]); - if (!res && o ==='checksigverify') + var type = sig.pop(); + if (type !== 1) return false; + + var res = bcoin.ecdsa.verify(tx, sig, pub); + if (o === 'checksigverify') { + if (!res) + return false; + } else { + stack.push([ res ? 1 : 0 ]); + } } else { // Unknown operation return false; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 6a389639..0ff18347 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -121,7 +121,9 @@ TX.prototype.subscriptHash = function subscriptHash(index, s, type) { input.script = index === i ? s : []; }); var verifyStr = copy.render(); - verifyStr = verifyStr.concat(bcoin.protocol.constants.hashType[type]); + verifyStr = verifyStr.concat( + bcoin.protocol.constants.hashType[type], 0, 0, 0 + ); var hash = utils.dsha256(verifyStr); return hash; @@ -138,6 +140,10 @@ TX.prototype.validate = function validate() { var stack = []; bcoin.script.execute(input.script, stack); var prev = input.out.tx.outputs[input.out.index].script; - return bcoin.script.execute(prev, stack, hash); + var res = bcoin.script.execute(prev, stack, hash); + if (!res) + return false; + + return stack.length > 0 && utils.isEqual(stack.pop(), [ 1 ]); }, this); }; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 41e7fe8e..85301144 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -81,7 +81,7 @@ Wallet.prototype.sign = function sign(tx, type) { var signature = bcoin.ecdsa.sign(hash, this.key).toDER(); input.script = [ - signature, + signature.concat(bcoin.protocol.constants.hashType[type]), pub ]; }, this);