payments: rm dependence on address export
This commit is contained in:
parent
eb01d35aa4
commit
0d0f1d0847
@ -1,12 +1,12 @@
|
|||||||
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 baddress = require('../address')
|
const bcrypto = require('../crypto')
|
||||||
let bcrypto = require('../crypto')
|
const bscript = require('../script')
|
||||||
let bscript = require('../script')
|
const BITCOIN_NETWORK = require('../networks').bitcoin
|
||||||
let BITCOIN_NETWORK = require('../networks').bitcoin
|
const bs58check = require('bs58check')
|
||||||
|
|
||||||
// input: {signature} {pubkey}
|
// input: {signature} {pubkey}
|
||||||
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
|
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
|
||||||
@ -31,15 +31,24 @@ function p2pkh (a, opts) {
|
|||||||
input: typef.maybe(typef.Buffer)
|
input: typef.maybe(typef.Buffer)
|
||||||
}, a)
|
}, a)
|
||||||
|
|
||||||
let _address = lazy.value(function () { return baddress.fromBase58Check(a.address) })
|
const _address = lazy.value(function () {
|
||||||
let _chunks = lazy.value(function () { return bscript.decompile(a.input) })
|
const payload = bs58check.decode(a.address)
|
||||||
|
const version = payload.readUInt8(0)
|
||||||
|
const hash = payload.slice(1)
|
||||||
|
return { version, hash }
|
||||||
|
})
|
||||||
|
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, 'address', function () {
|
lazy.prop(o, 'address', function () {
|
||||||
if (!o.hash) return
|
if (!o.hash) return
|
||||||
return baddress.toBase58Check(o.hash, network.pubKeyHash)
|
|
||||||
|
const payload = Buffer.allocUnsafe(21)
|
||||||
|
payload.writeUInt8(network.pubKeyHash, 0)
|
||||||
|
o.hash.copy(payload, 1)
|
||||||
|
return bs58check.encode(payload)
|
||||||
})
|
})
|
||||||
lazy.prop(o, 'hash', function () {
|
lazy.prop(o, 'hash', function () {
|
||||||
if (a.output) return a.output.slice(3, 23)
|
if (a.output) return a.output.slice(3, 23)
|
||||||
|
|||||||
@ -2,10 +2,10 @@ const lazy = require('./lazy')
|
|||||||
const typef = require('typeforce')
|
const typef = require('typeforce')
|
||||||
const OPS = require('bitcoin-ops')
|
const OPS = require('bitcoin-ops')
|
||||||
|
|
||||||
const baddress = require('../address')
|
|
||||||
const bcrypto = require('../crypto')
|
const bcrypto = require('../crypto')
|
||||||
const bscript = require('../script')
|
const bscript = require('../script')
|
||||||
const BITCOIN_NETWORK = require('../networks').bitcoin
|
const BITCOIN_NETWORK = require('../networks').bitcoin
|
||||||
|
const bs58check = require('bs58check')
|
||||||
|
|
||||||
function stacksEqual (a, b) {
|
function stacksEqual (a, b) {
|
||||||
if (a.length !== b.length) return false
|
if (a.length !== b.length) return false
|
||||||
@ -48,7 +48,12 @@ function p2sh (a, opts) {
|
|||||||
const network = a.network || BITCOIN_NETWORK
|
const network = a.network || BITCOIN_NETWORK
|
||||||
const o = { network }
|
const o = { network }
|
||||||
|
|
||||||
const _address = lazy.value(function () { return baddress.fromBase58Check(a.address) })
|
const _address = lazy.value(function () {
|
||||||
|
const payload = bs58check.decode(a.address)
|
||||||
|
const version = payload.readUInt8(0)
|
||||||
|
const hash = payload.slice(1)
|
||||||
|
return { version, hash }
|
||||||
|
})
|
||||||
const _chunks = lazy.value(function () { return bscript.decompile(a.input) })
|
const _chunks = lazy.value(function () { return bscript.decompile(a.input) })
|
||||||
const _redeem = lazy.value(function () {
|
const _redeem = lazy.value(function () {
|
||||||
const chunks = _chunks()
|
const chunks = _chunks()
|
||||||
@ -63,7 +68,11 @@ function p2sh (a, opts) {
|
|||||||
// output dependents
|
// output dependents
|
||||||
lazy.prop(o, 'address', function () {
|
lazy.prop(o, 'address', function () {
|
||||||
if (!o.hash) return
|
if (!o.hash) return
|
||||||
return baddress.toBase58Check(o.hash, network.scriptHash)
|
|
||||||
|
const payload = Buffer.allocUnsafe(21)
|
||||||
|
payload.writeUInt8(network.scriptHash, 0)
|
||||||
|
o.hash.copy(payload, 1)
|
||||||
|
return bs58check.encode(payload)
|
||||||
})
|
})
|
||||||
lazy.prop(o, 'hash', function () {
|
lazy.prop(o, 'hash', function () {
|
||||||
// in order of least effort
|
// in order of least effort
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
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 baddress = require('../address')
|
const bcrypto = require('../crypto')
|
||||||
let bcrypto = require('../crypto')
|
const bech32 = require('bech32')
|
||||||
let bscript = require('../script')
|
const bscript = require('../script')
|
||||||
let BITCOIN_NETWORK = require('../networks').bitcoin
|
const BITCOIN_NETWORK = require('../networks').bitcoin
|
||||||
|
|
||||||
let EMPTY_BUFFER = Buffer.alloc(0)
|
const EMPTY_BUFFER = Buffer.alloc(0)
|
||||||
|
|
||||||
// witness: {signature} {pubKey}
|
// witness: {signature} {pubKey}
|
||||||
// input: <>
|
// input: <>
|
||||||
@ -34,14 +34,26 @@ function p2wpkh (a, opts) {
|
|||||||
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
||||||
}, a)
|
}, 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
|
const network = a.network || BITCOIN_NETWORK
|
||||||
let o = { network }
|
const o = { network }
|
||||||
|
|
||||||
lazy.prop(o, 'address', function () {
|
lazy.prop(o, 'address', function () {
|
||||||
if (!o.hash) return
|
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 () {
|
lazy.prop(o, 'hash', function () {
|
||||||
if (a.output) return a.output.slice(2, 22)
|
if (a.output) return a.output.slice(2, 22)
|
||||||
@ -86,7 +98,7 @@ function p2wpkh (a, opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
@ -113,7 +125,7 @@ function p2wpkh (a, opts) {
|
|||||||
if (a.signature && !a.signature.equals(a.witness[0])) throw new TypeError('Signature mismatch')
|
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')
|
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')
|
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
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 baddress = require('../address')
|
const bech32 = require('bech32')
|
||||||
let bcrypto = require('../crypto')
|
const bcrypto = require('../crypto')
|
||||||
let bscript = require('../script')
|
const bscript = require('../script')
|
||||||
let BITCOIN_NETWORK = require('../networks').bitcoin
|
const BITCOIN_NETWORK = require('../networks').bitcoin
|
||||||
|
|
||||||
let EMPTY_BUFFER = Buffer.alloc(0)
|
const EMPTY_BUFFER = Buffer.alloc(0)
|
||||||
|
|
||||||
function stacksEqual (a, b) {
|
function stacksEqual (a, b) {
|
||||||
if (a.length !== b.length) return false
|
if (a.length !== b.length) return false
|
||||||
@ -47,19 +47,30 @@ function p2wsh (a, opts) {
|
|||||||
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
||||||
}, a)
|
}, a)
|
||||||
|
|
||||||
let _address = lazy.value(function () { return baddress.fromBech32(a.address) })
|
const _address = lazy.value(function () {
|
||||||
let _rchunks = lazy.value(function () { return bscript.decompile(a.redeem.input) })
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const _rchunks = lazy.value(function () { return bscript.decompile(a.redeem.input) })
|
||||||
|
|
||||||
let network = a.network || BITCOIN_NETWORK
|
const network = a.network || BITCOIN_NETWORK
|
||||||
let o = { network }
|
const o = { network }
|
||||||
|
|
||||||
lazy.prop(o, 'address', function () {
|
lazy.prop(o, 'address', function () {
|
||||||
if (!o.hash) return
|
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 () {
|
lazy.prop(o, 'hash', function () {
|
||||||
if (a.output) return a.output.slice(2)
|
if (a.output) return a.output.slice(2)
|
||||||
if (a.address) return baddress.fromBech32(a.address).data
|
if (a.address) return _address().data
|
||||||
if (o.redeem && o.redeem.output) return bcrypto.sha256(o.redeem.output)
|
if (o.redeem && o.redeem.output) return bcrypto.sha256(o.redeem.output)
|
||||||
})
|
})
|
||||||
lazy.prop(o, 'output', function () {
|
lazy.prop(o, 'output', function () {
|
||||||
@ -145,7 +156,7 @@ function p2wsh (a, opts) {
|
|||||||
if (bscript.decompile(a.redeem.output).length === 0) throw new TypeError('Redeem.output is invalid')
|
if (bscript.decompile(a.redeem.output).length === 0) throw new TypeError('Redeem.output is invalid')
|
||||||
|
|
||||||
// match hash against other sources
|
// match hash against other sources
|
||||||
let hash2 = bcrypto.sha256(a.redeem.output)
|
const hash2 = bcrypto.sha256(a.redeem.output)
|
||||||
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 New Issue
Block a user