payments: rm dependence on address export
This commit is contained in:
parent
eb01d35aa4
commit
0d0f1d0847
4 changed files with 87 additions and 46 deletions
src/payments
|
@ -1,14 +1,14 @@
|
|||
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 baddress = require('../address')
|
||||
let bcrypto = require('../crypto')
|
||||
let bscript = require('../script')
|
||||
let BITCOIN_NETWORK = require('../networks').bitcoin
|
||||
const bcrypto = require('../crypto')
|
||||
const bech32 = require('bech32')
|
||||
const bscript = require('../script')
|
||||
const BITCOIN_NETWORK = require('../networks').bitcoin
|
||||
|
||||
let EMPTY_BUFFER = Buffer.alloc(0)
|
||||
const EMPTY_BUFFER = Buffer.alloc(0)
|
||||
|
||||
// witness: {signature} {pubKey}
|
||||
// input: <>
|
||||
|
@ -34,14 +34,26 @@ function p2wpkh (a, opts) {
|
|||
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
||||
}, a)
|
||||
|
||||
let _address = lazy.value(function () { return baddress.fromBech32(a.address) })
|
||||
const _address = lazy.value(function () {
|
||||
const result = bech32.decode(a.address)
|
||||
const version = result.words.shift()
|
||||
const data = bech32.fromWords(result.words)
|
||||
return {
|
||||
version,
|
||||
prefix: result.prefix,
|
||||
data: Buffer.from(data)
|
||||
}
|
||||
})
|
||||
|
||||
let network = a.network || BITCOIN_NETWORK
|
||||
let o = { network }
|
||||
const network = a.network || BITCOIN_NETWORK
|
||||
const o = { network }
|
||||
|
||||
lazy.prop(o, 'address', function () {
|
||||
if (!o.hash) return
|
||||
return baddress.toBech32(o.hash, 0x00, network.bech32)
|
||||
|
||||
const words = bech32.toWords(o.hash)
|
||||
words.unshift(0x00)
|
||||
return bech32.encode(network.bech32, words)
|
||||
})
|
||||
lazy.prop(o, 'hash', function () {
|
||||
if (a.output) return a.output.slice(2, 22)
|
||||
|
@ -86,7 +98,7 @@ function p2wpkh (a, opts) {
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -113,7 +125,7 @@ function p2wpkh (a, opts) {
|
|||
if (a.signature && !a.signature.equals(a.witness[0])) throw new TypeError('Signature mismatch')
|
||||
if (a.pubkey && !a.pubkey.equals(a.witness[1])) throw new TypeError('Pubkey mismatch')
|
||||
|
||||
let pkh = bcrypto.hash160(a.witness[1])
|
||||
const pkh = bcrypto.hash160(a.witness[1])
|
||||
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue