From cf9a35f59bc6346bf4c6bee9466c58683fd55f2b Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 27 Jul 2018 20:14:11 +1000 Subject: [PATCH] payments: more const --- src/payments/p2pk.js | 18 +++++++++--------- src/payments/p2pkh.js | 11 ++++++----- src/payments/p2sh.js | 3 ++- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/payments/p2pk.js b/src/payments/p2pk.js index 9cddc81..9e12749 100644 --- a/src/payments/p2pk.js +++ b/src/payments/p2pk.js @@ -1,10 +1,10 @@ -let lazy = require('./lazy') -let typef = require('typeforce') -let OPS = require('bitcoin-ops') -let ecc = require('tiny-secp256k1') +const lazy = require('./lazy') +const typef = require('typeforce') +const OPS = require('bitcoin-ops') +const ecc = require('tiny-secp256k1') -let bscript = require('../script') -let BITCOIN_NETWORK = require('../networks').bitcoin +const bscript = require('../script') +const BITCOIN_NETWORK = require('../networks').bitcoin // input: {signature} // output: {pubKey} OP_CHECKSIG @@ -27,10 +27,10 @@ function p2pk (a, opts) { input: typef.maybe(typef.Buffer) }, a) - let _chunks = lazy.value(function () { return bscript.decompile(a.input) }) + const _chunks = lazy.value(function () { return bscript.decompile(a.input) }) - let network = a.network || BITCOIN_NETWORK - let o = { network } + const network = a.network || BITCOIN_NETWORK + const o = { network } lazy.prop(o, 'output', function () { if (!a.pubkey) return diff --git a/src/payments/p2pkh.js b/src/payments/p2pkh.js index 08a4329..0ab9fa0 100644 --- a/src/payments/p2pkh.js +++ b/src/payments/p2pkh.js @@ -106,18 +106,19 @@ function p2pkh (a, opts) { a.output[23] !== OPS.OP_EQUALVERIFY || a.output[24] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid') - if (hash && !hash.equals(a.output.slice(3, 23))) throw new TypeError('Hash mismatch') - else hash = a.output.slice(3, 23) + const hash2 = a.output.slice(3, 23) + if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch') + else hash = hash2 } if (a.pubkey) { - let pkh = bcrypto.hash160(a.pubkey) + const pkh = bcrypto.hash160(a.pubkey) if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch') else hash = pkh } if (a.input) { - let chunks = _chunks() + const chunks = _chunks() if (chunks.length !== 2) throw new TypeError('Input is invalid') if (!bscript.isCanonicalScriptSignature(chunks[0])) throw new TypeError('Input has invalid signature') if (!ecc.isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey') @@ -125,7 +126,7 @@ function p2pkh (a, opts) { if (a.signature && !a.signature.equals(chunks[0])) throw new TypeError('Signature mismatch') if (a.pubkey && !a.pubkey.equals(chunks[1])) throw new TypeError('Pubkey mismatch') - let pkh = bcrypto.hash160(chunks[1]) + const pkh = bcrypto.hash160(chunks[1]) if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch') } } diff --git a/src/payments/p2sh.js b/src/payments/p2sh.js index 7b95a45..9c80b09 100644 --- a/src/payments/p2sh.js +++ b/src/payments/p2sh.js @@ -111,7 +111,7 @@ function p2sh (a, opts) { if (a.address) { if (_address().version !== network.scriptHash) throw new TypeError('Invalid version or Network mismatch') if (_address().hash.length !== 20) throw new TypeError('Invalid address') - else hash = _address().hash + hash = _address().hash } if (a.hash) { @@ -125,6 +125,7 @@ function p2sh (a, opts) { a.output[0] !== OPS.OP_HASH160 || a.output[1] !== 0x14 || a.output[22] !== OPS.OP_EQUAL) throw new TypeError('Output is invalid') + const hash2 = a.output.slice(2, 22) if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch') else hash = hash2