payments: rm dependence on address export

This commit is contained in:
Daniel Cousens 2018-07-03 22:06:44 +10:00
parent eb01d35aa4
commit 0d0f1d0847
4 changed files with 87 additions and 46 deletions

View file

@ -1,12 +1,12 @@
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 bscript = require('../script')
const BITCOIN_NETWORK = require('../networks').bitcoin
const bs58check = require('bs58check')
// input: {signature} {pubkey}
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
@ -31,15 +31,24 @@ function p2pkh (a, opts) {
input: typef.maybe(typef.Buffer)
}, a)
let _address = lazy.value(function () { return baddress.fromBase58Check(a.address) })
let _chunks = lazy.value(function () { return bscript.decompile(a.input) })
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) })
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.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 () {
if (a.output) return a.output.slice(3, 23)

View file

@ -2,10 +2,10 @@ const lazy = require('./lazy')
const typef = require('typeforce')
const OPS = require('bitcoin-ops')
const baddress = require('../address')
const bcrypto = require('../crypto')
const bscript = require('../script')
const BITCOIN_NETWORK = require('../networks').bitcoin
const bs58check = require('bs58check')
function stacksEqual (a, b) {
if (a.length !== b.length) return false
@ -48,7 +48,12 @@ function p2sh (a, opts) {
const network = a.network || BITCOIN_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 _redeem = lazy.value(function () {
const chunks = _chunks()
@ -63,7 +68,11 @@ function p2sh (a, opts) {
// output dependents
lazy.prop(o, 'address', function () {
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 () {
// in order of least effort

View file

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

View file

@ -1,13 +1,13 @@
let lazy = require('./lazy')
let typef = require('typeforce')
let OPS = require('bitcoin-ops')
const lazy = require('./lazy')
const typef = require('typeforce')
const OPS = require('bitcoin-ops')
let baddress = require('../address')
let bcrypto = require('../crypto')
let bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin
const bech32 = require('bech32')
const bcrypto = require('../crypto')
const bscript = require('../script')
const BITCOIN_NETWORK = require('../networks').bitcoin
let EMPTY_BUFFER = Buffer.alloc(0)
const EMPTY_BUFFER = Buffer.alloc(0)
function stacksEqual (a, b) {
if (a.length !== b.length) return false
@ -47,19 +47,30 @@ function p2wsh (a, opts) {
witness: typef.maybe(typef.arrayOf(typef.Buffer))
}, a)
let _address = lazy.value(function () { return baddress.fromBech32(a.address) })
let _rchunks = lazy.value(function () { return bscript.decompile(a.redeem.input) })
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)
}
})
const _rchunks = lazy.value(function () { return bscript.decompile(a.redeem.input) })
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)
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)
})
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')
// 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')
else hash = hash2
}