2018-12-29 14:49:35 +01:00
|
|
|
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
2019-03-03 16:10:12 +01:00
|
|
|
import { bitcoin as BITCOIN_NETWORK } from '../networks' // eslint-disable-line
|
2018-12-28 16:00:52 +01:00
|
|
|
import * as bscript from '../script'
|
|
|
|
import * as bcrypto from '../crypto'
|
|
|
|
import * as lazy from './lazy'
|
2018-07-03 14:06:44 +02:00
|
|
|
const typef = require('typeforce')
|
2018-12-29 14:49:35 +01:00
|
|
|
const OPS = bscript.OPS
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-07-03 14:06:44 +02:00
|
|
|
const bech32 = require('bech32')
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-07-03 14:06:44 +02:00
|
|
|
const EMPTY_BUFFER = Buffer.alloc(0)
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-12-28 16:00:52 +01:00
|
|
|
function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
2018-06-05 09:24:47 +02:00
|
|
|
if (a.length !== b.length) return false
|
|
|
|
|
|
|
|
return a.every(function (x, i) {
|
|
|
|
return x.equals(b[i])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// input: <>
|
|
|
|
// witness: [redeemScriptSig ...] {redeemScript}
|
|
|
|
// output: OP_0 {sha256(redeemScript)}
|
2018-12-29 08:10:36 +01:00
|
|
|
export function p2wsh (a: Payment, opts?: PaymentOpts): Payment {
|
2018-06-05 09:24:47 +02:00
|
|
|
if (
|
|
|
|
!a.address &&
|
|
|
|
!a.hash &&
|
|
|
|
!a.output &&
|
|
|
|
!a.redeem &&
|
|
|
|
!a.witness
|
|
|
|
) throw new TypeError('Not enough data')
|
2018-08-28 07:21:18 +02:00
|
|
|
opts = Object.assign({ validate: true }, opts || {})
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
typef({
|
|
|
|
network: typef.maybe(typef.Object),
|
|
|
|
|
|
|
|
address: typef.maybe(typef.String),
|
|
|
|
hash: typef.maybe(typef.BufferN(32)),
|
|
|
|
output: typef.maybe(typef.BufferN(34)),
|
|
|
|
|
|
|
|
redeem: typef.maybe({
|
|
|
|
input: typef.maybe(typef.Buffer),
|
|
|
|
network: typef.maybe(typef.Object),
|
2018-06-27 03:22:35 +02:00
|
|
|
output: typef.maybe(typef.Buffer),
|
2018-06-05 09:24:47 +02:00
|
|
|
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
|
|
|
}),
|
|
|
|
input: typef.maybe(typef.BufferN(0)),
|
|
|
|
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
|
|
|
}, a)
|
|
|
|
|
2018-07-03 14:06:44 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2019-01-15 09:47:30 +01:00
|
|
|
const _rchunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(a.redeem!.input!) })
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-09-26 08:02:52 +02:00
|
|
|
let network = a.network
|
|
|
|
if (!network) {
|
|
|
|
network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK
|
|
|
|
}
|
|
|
|
|
2018-12-28 16:00:52 +01:00
|
|
|
const o: Payment = { network }
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
lazy.prop(o, 'address', function () {
|
|
|
|
if (!o.hash) return
|
2018-07-03 14:06:44 +02:00
|
|
|
const words = bech32.toWords(o.hash)
|
|
|
|
words.unshift(0x00)
|
2019-01-15 09:47:30 +01:00
|
|
|
return bech32.encode(network!.bech32, words)
|
2018-06-05 09:24:47 +02:00
|
|
|
})
|
|
|
|
lazy.prop(o, 'hash', function () {
|
|
|
|
if (a.output) return a.output.slice(2)
|
2018-07-03 14:06:44 +02:00
|
|
|
if (a.address) return _address().data
|
2018-06-05 09:24:47 +02:00
|
|
|
if (o.redeem && o.redeem.output) return bcrypto.sha256(o.redeem.output)
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'output', function () {
|
|
|
|
if (!o.hash) return
|
|
|
|
return bscript.compile([
|
|
|
|
OPS.OP_0,
|
|
|
|
o.hash
|
|
|
|
])
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'redeem', function () {
|
|
|
|
if (!a.witness) return
|
|
|
|
return {
|
|
|
|
output: a.witness[a.witness.length - 1],
|
|
|
|
input: EMPTY_BUFFER,
|
|
|
|
witness: a.witness.slice(0, -1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'input', function () {
|
|
|
|
if (!o.witness) return
|
|
|
|
return EMPTY_BUFFER
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'witness', function () {
|
|
|
|
// transform redeem input to witness stack?
|
2018-06-27 03:22:35 +02:00
|
|
|
if (
|
|
|
|
a.redeem &&
|
|
|
|
a.redeem.input &&
|
|
|
|
a.redeem.input.length > 0 &&
|
|
|
|
a.redeem.output &&
|
|
|
|
a.redeem.output.length > 0
|
|
|
|
) {
|
|
|
|
const stack = bscript.toStack(_rchunks())
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
// assign, and blank the existing input
|
|
|
|
o.redeem = Object.assign({ witness: stack }, a.redeem)
|
|
|
|
o.redeem.input = EMPTY_BUFFER
|
2018-12-28 17:55:07 +01:00
|
|
|
return (<Array<Buffer>>[]).concat(stack, a.redeem.output)
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!a.redeem) return
|
2018-06-27 03:22:35 +02:00
|
|
|
if (!a.redeem.output) return
|
2018-06-05 09:24:47 +02:00
|
|
|
if (!a.redeem.witness) return
|
2018-12-28 17:55:07 +01:00
|
|
|
return (<Array<Buffer>>[]).concat(a.redeem.witness, a.redeem.output)
|
2018-06-05 09:24:47 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// extended validation
|
|
|
|
if (opts.validate) {
|
2018-12-28 17:55:07 +01:00
|
|
|
let hash: Buffer = Buffer.from([])
|
2018-06-05 09:24:47 +02:00
|
|
|
if (a.address) {
|
2018-07-17 07:22:03 +02:00
|
|
|
if (_address().prefix !== network.bech32) throw new TypeError('Invalid prefix or Network mismatch')
|
|
|
|
if (_address().version !== 0x00) throw new TypeError('Invalid address version')
|
|
|
|
if (_address().data.length !== 32) throw new TypeError('Invalid address data')
|
2018-07-27 12:22:11 +02:00
|
|
|
hash = _address().data
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.hash) {
|
2018-12-28 17:55:07 +01:00
|
|
|
if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
else hash = a.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.output) {
|
|
|
|
if (
|
|
|
|
a.output.length !== 34 ||
|
|
|
|
a.output[0] !== OPS.OP_0 ||
|
|
|
|
a.output[1] !== 0x20) throw new TypeError('Output is invalid')
|
2018-06-27 03:22:35 +02:00
|
|
|
const hash2 = a.output.slice(2)
|
2018-12-28 17:55:07 +01:00
|
|
|
if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
else hash = hash2
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.redeem) {
|
|
|
|
if (a.redeem.network && a.redeem.network !== network) throw new TypeError('Network mismatch')
|
|
|
|
|
|
|
|
// is there two redeem sources?
|
|
|
|
if (
|
|
|
|
a.redeem.input &&
|
|
|
|
a.redeem.input.length > 0 &&
|
2018-06-27 03:22:35 +02:00
|
|
|
a.redeem.witness &&
|
|
|
|
a.redeem.witness.length > 0
|
|
|
|
) throw new TypeError('Ambiguous witness source')
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
// is the redeem output non-empty?
|
2018-06-27 03:22:35 +02:00
|
|
|
if (a.redeem.output) {
|
2019-01-15 09:47:30 +01:00
|
|
|
if (bscript.decompile(a.redeem.output)!.length === 0) throw new TypeError('Redeem.output is invalid')
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-06-27 03:22:35 +02:00
|
|
|
// match hash against other sources
|
2018-07-03 14:06:44 +02:00
|
|
|
const hash2 = bcrypto.sha256(a.redeem.output)
|
2018-12-28 17:55:07 +01:00
|
|
|
if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
2018-06-27 03:22:35 +02:00
|
|
|
else hash = hash2
|
|
|
|
}
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
if (a.redeem.input && !bscript.isPushOnly(_rchunks())) throw new TypeError('Non push-only scriptSig')
|
|
|
|
if (a.witness && a.redeem.witness && !stacksEqual(a.witness, a.redeem.witness)) throw new TypeError('Witness and redeem.witness mismatch')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.witness) {
|
2018-06-27 03:22:35 +02:00
|
|
|
if (a.redeem && a.redeem.output && !a.redeem.output.equals(a.witness[a.witness.length - 1])) throw new TypeError('Witness and redeem.output mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign(o, a)
|
|
|
|
}
|