payments: more const
This commit is contained in:
parent
d8b66641b3
commit
cf9a35f59b
3 changed files with 17 additions and 15 deletions
|
@ -1,10 +1,10 @@
|
||||||
let lazy = require('./lazy')
|
const lazy = require('./lazy')
|
||||||
let typef = require('typeforce')
|
const typef = require('typeforce')
|
||||||
let OPS = require('bitcoin-ops')
|
const OPS = require('bitcoin-ops')
|
||||||
let ecc = require('tiny-secp256k1')
|
const ecc = require('tiny-secp256k1')
|
||||||
|
|
||||||
let bscript = require('../script')
|
const bscript = require('../script')
|
||||||
let BITCOIN_NETWORK = require('../networks').bitcoin
|
const BITCOIN_NETWORK = require('../networks').bitcoin
|
||||||
|
|
||||||
// input: {signature}
|
// input: {signature}
|
||||||
// output: {pubKey} OP_CHECKSIG
|
// output: {pubKey} OP_CHECKSIG
|
||||||
|
@ -27,10 +27,10 @@ function p2pk (a, opts) {
|
||||||
input: typef.maybe(typef.Buffer)
|
input: typef.maybe(typef.Buffer)
|
||||||
}, a)
|
}, 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
|
const network = a.network || BITCOIN_NETWORK
|
||||||
let o = { network }
|
const o = { network }
|
||||||
|
|
||||||
lazy.prop(o, 'output', function () {
|
lazy.prop(o, 'output', function () {
|
||||||
if (!a.pubkey) return
|
if (!a.pubkey) return
|
||||||
|
|
|
@ -106,18 +106,19 @@ function p2pkh (a, opts) {
|
||||||
a.output[23] !== OPS.OP_EQUALVERIFY ||
|
a.output[23] !== OPS.OP_EQUALVERIFY ||
|
||||||
a.output[24] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid')
|
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')
|
const hash2 = a.output.slice(3, 23)
|
||||||
else hash = a.output.slice(3, 23)
|
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
||||||
|
else hash = hash2
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a.pubkey) {
|
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')
|
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
||||||
else hash = pkh
|
else hash = pkh
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a.input) {
|
if (a.input) {
|
||||||
let chunks = _chunks()
|
const chunks = _chunks()
|
||||||
if (chunks.length !== 2) throw new TypeError('Input is invalid')
|
if (chunks.length !== 2) throw new TypeError('Input is invalid')
|
||||||
if (!bscript.isCanonicalScriptSignature(chunks[0])) throw new TypeError('Input has invalid signature')
|
if (!bscript.isCanonicalScriptSignature(chunks[0])) throw new TypeError('Input has invalid signature')
|
||||||
if (!ecc.isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey')
|
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.signature && !a.signature.equals(chunks[0])) throw new TypeError('Signature mismatch')
|
||||||
if (a.pubkey && !a.pubkey.equals(chunks[1])) throw new TypeError('Pubkey 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')
|
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,7 +111,7 @@ function p2sh (a, opts) {
|
||||||
if (a.address) {
|
if (a.address) {
|
||||||
if (_address().version !== network.scriptHash) throw new TypeError('Invalid version or Network mismatch')
|
if (_address().version !== network.scriptHash) throw new TypeError('Invalid version or Network mismatch')
|
||||||
if (_address().hash.length !== 20) throw new TypeError('Invalid address')
|
if (_address().hash.length !== 20) throw new TypeError('Invalid address')
|
||||||
else hash = _address().hash
|
hash = _address().hash
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a.hash) {
|
if (a.hash) {
|
||||||
|
@ -125,6 +125,7 @@ function p2sh (a, opts) {
|
||||||
a.output[0] !== OPS.OP_HASH160 ||
|
a.output[0] !== OPS.OP_HASH160 ||
|
||||||
a.output[1] !== 0x14 ||
|
a.output[1] !== 0x14 ||
|
||||||
a.output[22] !== OPS.OP_EQUAL) throw new TypeError('Output is invalid')
|
a.output[22] !== OPS.OP_EQUAL) throw new TypeError('Output is invalid')
|
||||||
|
|
||||||
const hash2 = a.output.slice(2, 22)
|
const hash2 = a.output.slice(2, 22)
|
||||||
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
||||||
else hash = hash2
|
else hash = hash2
|
||||||
|
|
Loading…
Reference in a new issue