payments: more const

This commit is contained in:
Daniel Cousens 2018-07-27 20:14:11 +10:00
parent d8b66641b3
commit cf9a35f59b
3 changed files with 17 additions and 15 deletions

View file

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

View file

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

View file

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