add payments p2ms, p2pk, p2pkh, p2sh, p2wpkh, p2wsh

This commit is contained in:
Daniel Cousens 2018-06-05 17:24:47 +10:00
parent 7756c5dd76
commit f9a739e1db
18 changed files with 2696 additions and 2 deletions

19
src/payments/index.js Normal file
View file

@ -0,0 +1,19 @@
const p2ms = require('./p2ms')
const p2pk = require('./p2pk')
const p2pkh = require('./p2pkh')
const p2sh = require('./p2sh')
const p2wpkh = require('./p2wpkh')
const p2wsh = require('./p2wsh')
module.exports = {
p2ms: p2ms,
p2pk: p2pk,
p2pkh: p2pkh,
p2sh: p2sh,
p2wpkh: p2wpkh,
p2wsh: p2wsh
}
// TODO
// OP_RETURN
// witness commitment

30
src/payments/lazy.js Normal file
View file

@ -0,0 +1,30 @@
function prop (object, name, f) {
Object.defineProperty(object, name, {
configurable: true,
enumerable: true,
get: function () {
let value = f.call(this)
this[name] = value
return value
},
set: function (value) {
Object.defineProperty(this, name, {
configurable: true,
enumerable: true,
value: value,
writable: true
})
}
})
}
function value (f) {
let value
return function () {
if (value !== undefined) return value
value = f()
return value
}
}
module.exports = { prop, value }

140
src/payments/p2ms.js Normal file
View file

@ -0,0 +1,140 @@
let lazy = require('./lazy')
let typef = require('typeforce')
let OPS = require('bitcoin-ops')
let ecc = require('tiny-secp256k1')
let bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin
let OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
function stacksEqual (a, b) {
if (a.length !== b.length) return false
return a.every(function (x, i) {
return x.equals(b[i])
})
}
// input: OP_0 [signatures ...]
// output: m [pubKeys ...] n OP_CHECKMULTISIG
function p2ms (a, opts) {
if (
!a.output &&
!(a.pubkeys && a.m !== undefined)
) throw new TypeError('Not enough data')
opts = opts || { validate: true }
function isAcceptableSignature (x) {
return bscript.isCanonicalScriptSignature(x) || (opts.allowIncomplete && (x === OPS.OP_0))
}
typef({
network: typef.maybe(typef.Object),
m: typef.maybe(typef.Number),
n: typef.maybe(typef.Number),
output: typef.maybe(typef.Buffer),
pubkeys: typef.maybe(typef.arrayOf(ecc.isPoint)),
signatures: typef.maybe(typef.arrayOf(isAcceptableSignature)),
input: typef.maybe(typef.Buffer)
}, a)
let network = a.network || BITCOIN_NETWORK
let o = { network }
let chunks
let decoded = false
function decode (output) {
if (decoded) return
decoded = true
chunks = bscript.decompile(output)
let om = chunks[0] - OP_INT_BASE
let on = chunks[chunks.length - 2] - OP_INT_BASE
o.m = om
o.n = on
o.pubkeys = chunks.slice(1, -2)
}
lazy.prop(o, 'output', function () {
if (!a.m) return
if (!o.n) return
if (!a.pubkeys) return
return bscript.compile([].concat(
OP_INT_BASE + a.m,
a.pubkeys,
OP_INT_BASE + o.n,
OPS.OP_CHECKMULTISIG
))
})
lazy.prop(o, 'm', function () {
if (!o.output) return
decode(o.output)
return o.m
})
lazy.prop(o, 'n', function () {
if (!o.pubkeys) return
return o.pubkeys.length
})
lazy.prop(o, 'pubkeys', function () {
if (!a.output) return
decode(a.output)
return o.pubkeys
})
lazy.prop(o, 'signatures', function () {
if (!a.input) return
return bscript.decompile(a.input).slice(1)
})
lazy.prop(o, 'input', function () {
if (!a.signatures) return
return bscript.compile([OPS.OP_0].concat(a.signatures))
})
lazy.prop(o, 'witness', function () {
if (!o.input) return
return []
})
// extended validation
if (opts.validate) {
if (a.output) {
decode(a.output)
if (!typef.Number(chunks[0])) throw new TypeError('Output is invalid')
if (!typef.Number(chunks[chunks.length - 2])) throw new TypeError('Output is invalid')
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) throw new TypeError('Output is invalid')
if (
o.m <= 0 ||
o.n > 16 ||
o.m > o.n ||
o.n !== chunks.length - 3) throw new TypeError('Output is invalid')
if (!o.pubkeys.every(x => ecc.isPoint(x))) throw new TypeError('Output is invalid')
if (a.m !== undefined && a.m !== o.m) throw new TypeError('m mismatch')
if (a.n !== undefined && a.n !== o.n) throw new TypeError('n mismatch')
if (a.pubkeys && !stacksEqual(a.pubkeys, o.pubkeys)) throw new TypeError('Pubkeys mismatch')
}
if (a.pubkeys) {
if (a.n !== undefined && a.n !== a.pubkeys.length) throw new TypeError('Pubkey count mismatch')
o.n = a.pubkeys.length
if (o.n < o.m) throw new TypeError('Pubkey count cannot be less than m')
}
if (a.signatures) {
if (a.signatures.length < o.m) throw new TypeError('Not enough signatures provided')
if (a.signatures.length > o.m) throw new TypeError('Too many signatures provided')
}
if (a.input) {
if (a.input[0] !== OPS.OP_0) throw new TypeError('Input is invalid')
if (o.signatures.length === 0 || !o.signatures.every(isAcceptableSignature)) throw new TypeError('Input has invalid signature(s)')
if (a.signatures && !stacksEqual(a.signatures.equals(o.signatures))) throw new TypeError('Signature mismatch')
if (a.m !== undefined && a.m !== a.signatures.length) throw new TypeError('Signature count mismatch')
}
}
return Object.assign(o, a)
}
module.exports = p2ms

80
src/payments/p2pk.js Normal file
View file

@ -0,0 +1,80 @@
let lazy = require('./lazy')
let typef = require('typeforce')
let OPS = require('bitcoin-ops')
let ecc = require('tiny-secp256k1')
let bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin
// input: {signature}
// output: {pubKey} OP_CHECKSIG
function p2pk (a, opts) {
if (
!a.output &&
!a.pubkey
) throw new TypeError('Not enough data')
opts = opts || { validate: true }
typef({
network: typef.maybe(typef.Object),
output: typef.maybe(typef.Buffer),
pubkey: typef.maybe(ecc.isPoint),
signature: typef.maybe(bscript.isCanonicalScriptSignature),
input: typef.maybe(typef.Buffer)
}, a)
let _chunks = lazy.value(function () { return bscript.decompile(a.input) })
let network = a.network || BITCOIN_NETWORK
let o = { network }
lazy.prop(o, 'output', function () {
if (!a.pubkey) return
return bscript.compile([
a.pubkey,
OPS.OP_CHECKSIG
])
})
lazy.prop(o, 'pubkey', function () {
if (!a.output) return
return a.output.slice(1, -1)
})
lazy.prop(o, 'signature', function () {
if (!a.input) return
return _chunks()[0]
})
lazy.prop(o, 'input', function () {
if (!a.signature) return
return bscript.compile([a.signature])
})
lazy.prop(o, 'witness', function () {
if (!o.input) return
return []
})
// extended validation
if (opts.validate) {
if (a.pubkey && a.output) {
if (!a.pubkey.equals(o.pubkey)) throw new TypeError('Pubkey mismatch')
}
if (a.output) {
if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid')
if (!ecc.isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid')
}
if (a.signature) {
if (a.input && !a.input.equals(o.input)) throw new TypeError('Input mismatch')
}
if (a.input) {
if (_chunks().length !== 1) throw new TypeError('Input is invalid')
if (!bscript.isCanonicalScriptSignature(_chunks()[0])) throw new TypeError('Input has invalid signature')
}
}
return Object.assign(o, a)
}
module.exports = p2pk

127
src/payments/p2pkh.js Normal file
View file

@ -0,0 +1,127 @@
let lazy = require('./lazy')
let typef = require('typeforce')
let OPS = require('bitcoin-ops')
let ecc = require('tiny-secp256k1')
let baddress = require('../address')
let bcrypto = require('../crypto')
let bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin
// input: {signature} {pubkey}
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
function p2pkh (a, opts) {
if (
!a.address &&
!a.hash &&
!a.output &&
!a.pubkey &&
!a.input
) throw new TypeError('Not enough data')
opts = opts || { validate: true }
typef({
network: typef.maybe(typef.Object),
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(20)),
output: typef.maybe(typef.BufferN(25)),
pubkey: typef.maybe(ecc.isPoint),
signature: typef.maybe(bscript.isCanonicalScriptSignature),
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) })
let network = a.network || BITCOIN_NETWORK
let o = { network }
lazy.prop(o, 'address', function () {
if (!o.hash) return
return baddress.toBase58Check(o.hash, network.pubKeyHash)
})
lazy.prop(o, 'hash', function () {
if (a.output) return a.output.slice(3, 23)
if (a.address) return _address().hash
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey)
})
lazy.prop(o, 'output', function () {
if (!o.hash) return
return bscript.compile([
OPS.OP_DUP,
OPS.OP_HASH160,
o.hash,
OPS.OP_EQUALVERIFY,
OPS.OP_CHECKSIG
])
})
lazy.prop(o, 'pubkey', function () {
if (!a.input) return
return _chunks()[1]
})
lazy.prop(o, 'signature', function () {
if (!a.input) return
return _chunks()[0]
})
lazy.prop(o, 'input', function () {
if (!a.pubkey) return
if (!a.signature) return
return bscript.compile([a.signature, a.pubkey])
})
lazy.prop(o, 'witness', function () {
if (!o.input) return
return []
})
// extended validation
if (opts.validate) {
let hash
if (a.address) {
if (_address().version !== network.pubKeyHash) throw new TypeError('Network mismatch')
if (_address().hash.length !== 20) throw new TypeError('Invalid address')
else hash = _address().hash
}
if (a.hash) {
if (hash && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
else hash = a.hash
}
if (a.output) {
if (
a.output.length !== 25 ||
a.output[0] !== OPS.OP_DUP ||
a.output[1] !== OPS.OP_HASH160 ||
a.output[2] !== 0x14 ||
a.output[23] !== OPS.OP_EQUALVERIFY ||
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')
else hash = a.output.slice(3, 23)
}
if (a.pubkey) {
let pkh = bcrypto.hash160(a.pubkey)
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
else hash = pkh
}
if (a.input) {
let chunks = _chunks()
if (chunks.length !== 2) throw new TypeError('Input is invalid')
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 (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')
let pkh = bcrypto.hash160(chunks[1])
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
}
}
return Object.assign(o, a)
}
module.exports = p2pkh

176
src/payments/p2sh.js Normal file
View file

@ -0,0 +1,176 @@
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
function stacksEqual (a, b) {
if (a.length !== b.length) return false
return a.every(function (x, i) {
return x.equals(b[i])
})
}
// input: [redeemScriptSig ...] {redeemScript}
// witness: <?>
// output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
function p2sh (a, opts) {
if (
!a.address &&
!a.hash &&
!a.output &&
!a.redeem &&
!a.input
) throw new TypeError('Not enough data')
opts = opts || { validate: true }
typef({
network: typef.maybe(typef.Object),
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(20)),
output: typef.maybe(typef.BufferN(23)),
redeem: typef.maybe({
network: typef.maybe(typef.Object),
output: typef.Buffer,
input: typef.maybe(typef.Buffer),
witness: typef.maybe(typef.arrayOf(typef.Buffer))
}),
input: typef.maybe(typef.Buffer),
witness: typef.maybe(typef.arrayOf(typef.Buffer))
}, a)
const network = a.network || BITCOIN_NETWORK
const o = { network }
const _address = lazy.value(function () { return baddress.fromBase58Check(a.address) })
const _chunks = lazy.value(function () { return bscript.decompile(a.input) })
const _redeem = lazy.value(function () {
const chunks = _chunks()
return {
network: network,
output: chunks[chunks.length - 1],
input: bscript.compile(chunks.slice(0, -1)),
witness: a.witness || []
}
})
// output dependents
lazy.prop(o, 'address', function () {
if (!o.hash) return
return baddress.toBase58Check(o.hash, network.scriptHash)
})
lazy.prop(o, 'hash', function () {
// in order of least effort
if (a.output) return a.output.slice(2, 22)
if (a.address) return _address().hash
if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output)
})
lazy.prop(o, 'output', function () {
if (!o.hash) return
return bscript.compile([
OPS.OP_HASH160,
o.hash,
OPS.OP_EQUAL
])
})
// input dependents
lazy.prop(o, 'redeem', function () {
if (!a.input) return
return _redeem()
})
lazy.prop(o, 'input', function () {
if (!a.redeem || !a.redeem.input) return
return bscript.compile([].concat(
bscript.decompile(a.redeem.input),
a.redeem.output
))
})
lazy.prop(o, 'witness', function () {
if (o.redeem && o.redeem.witness) return o.redeem.witness
if (o.input) return []
})
if (opts.validate) {
let hash
if (a.address) {
if (_address().version !== network.scriptHash) throw new TypeError('Network mismatch')
if (_address().hash.length !== 20) throw new TypeError('Invalid address')
else hash = _address().hash
}
if (a.hash) {
if (hash && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
else hash = a.hash
}
if (a.output) {
if (
a.output.length !== 23 ||
a.output[0] !== OPS.OP_HASH160 ||
a.output[1] !== 0x14 ||
a.output[22] !== OPS.OP_EQUAL) throw new TypeError('Output is invalid')
const hash2 = a.output.slice(2, 22)
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
else hash = hash2
}
// inlined to prevent 'no-inner-declarations' failing
const checkRedeem = function (redeem) {
// is the redeem output empty/invalid?
const decompile = bscript.decompile(redeem.output)
if (!decompile || decompile.length < 1) throw new TypeError('Redeem.output too short')
// match hash against other sources
const hash2 = bcrypto.hash160(redeem.output)
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
else hash = hash2
if (redeem.input) {
const hasInput = redeem.input.length > 0
const hasWitness = redeem.witness && redeem.witness.length > 0
if (!hasInput && !hasWitness) throw new TypeError('Empty input')
if (hasInput && hasWitness) throw new TypeError('Input and witness provided')
if (hasInput) {
const richunks = bscript.decompile(redeem.input)
if (!bscript.isPushOnly(richunks)) throw new TypeError('Non push-only scriptSig')
}
}
}
if (a.input) {
const chunks = _chunks()
if (!chunks || chunks.length < 1) throw new TypeError('Input too short')
if (!Buffer.isBuffer(_redeem().output)) throw new TypeError('Input is invalid')
checkRedeem(_redeem())
}
if (a.redeem) {
if (a.redeem.network && a.redeem.network !== network) throw new TypeError('Network mismatch')
if (o.redeem) {
if (a.redeem.output && !a.redeem.output.equals(o.redeem.output)) throw new TypeError('Redeem.output mismatch')
if (a.redeem.input && !a.redeem.input.equals(o.redeem.input)) throw new TypeError('Redeem.input mismatch')
}
checkRedeem(a.redeem)
}
if (a.witness) {
if (
a.redeem &&
a.redeem.witness &&
!stacksEqual(a.redeem.witness, a.witness)) throw new TypeError('Witness and redeem.witness mismatch')
}
}
return Object.assign(o, a)
}
module.exports = p2sh

124
src/payments/p2wpkh.js Normal file
View file

@ -0,0 +1,124 @@
let lazy = require('./lazy')
let typef = require('typeforce')
let OPS = require('bitcoin-ops')
let ecc = require('tiny-secp256k1')
let baddress = require('../address')
let bcrypto = require('../crypto')
let bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin
let EMPTY_BUFFER = Buffer.alloc(0)
// witness: {signature} {pubKey}
// input: <>
// output: OP_0 {pubKeyHash}
function p2wpkh (a, opts) {
if (
!a.address &&
!a.hash &&
!a.output &&
!a.pubkey &&
!a.witness
) throw new TypeError('Not enough data')
opts = opts || { validate: true }
typef({
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(20)),
input: typef.maybe(typef.BufferN(0)),
network: typef.maybe(typef.Object),
output: typef.maybe(typef.BufferN(22)),
pubkey: typef.maybe(ecc.isPoint),
signature: typef.maybe(bscript.isCanonicalScriptSignature),
witness: typef.maybe(typef.arrayOf(typef.Buffer))
}, a)
let _address = lazy.value(function () { return baddress.fromBech32(a.address) })
let network = a.network || BITCOIN_NETWORK
let o = { network }
lazy.prop(o, 'address', function () {
if (!o.hash) return
return baddress.toBech32(o.hash, 0x00, network.bech32)
})
lazy.prop(o, 'hash', function () {
if (a.output) return a.output.slice(2, 22)
if (a.address) return _address().data
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey)
})
lazy.prop(o, 'output', function () {
if (!o.hash) return
return bscript.compile([
OPS.OP_0,
o.hash
])
})
lazy.prop(o, 'pubkey', function () {
if (a.pubkey) return a.pubkey
if (!a.witness) return
return a.witness[1]
})
lazy.prop(o, 'signature', function () {
if (!a.witness) return
return a.witness[0]
})
lazy.prop(o, 'input', function () {
if (!o.witness) return
return EMPTY_BUFFER
})
lazy.prop(o, 'witness', function () {
if (!a.pubkey) return
if (!a.signature) return
return [a.signature, a.pubkey]
})
// extended validation
if (opts.validate) {
let hash
if (a.address) {
if (network && network.bech32 !== _address().prefix) throw new TypeError('Network mismatch')
if (_address().version !== 0x00) throw new TypeError('Invalid version')
if (_address().data.length !== 20) throw new TypeError('Invalid data')
if (hash && !hash.equals(_address().data)) throw new TypeError('Hash mismatch')
else hash = _address().data
}
if (a.pubkey) {
let pkh = bcrypto.hash160(a.pubkey)
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
else hash = pkh
}
if (a.hash) {
if (hash && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
else hash = a.hash
}
if (a.output) {
if (
a.output.length !== 22 ||
a.output[0] !== OPS.OP_0 ||
a.output[1] !== 0x14) throw new TypeError('Output is invalid')
if (hash && !hash.equals(a.output.slice(2))) throw new TypeError('Hash mismatch')
else hash = a.output.slice(2)
}
if (a.witness) {
if (a.witness.length !== 2) throw new TypeError('Input is invalid')
if (!bscript.isCanonicalScriptSignature(a.witness[0])) throw new TypeError('Input has invalid signature')
if (!ecc.isPoint(a.witness[1])) throw new TypeError('Input has invalid pubkey')
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])
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
}
}
return Object.assign(o, a)
}
module.exports = p2wpkh

154
src/payments/p2wsh.js Normal file
View file

@ -0,0 +1,154 @@
let lazy = require('./lazy')
let typef = require('typeforce')
let OPS = require('bitcoin-ops')
let baddress = require('../address')
let bcrypto = require('../crypto')
let bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin
let EMPTY_BUFFER = Buffer.alloc(0)
function stacksEqual (a, b) {
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)}
function p2wsh (a, opts) {
if (
!a.address &&
!a.hash &&
!a.output &&
!a.redeem &&
!a.witness
) throw new TypeError('Not enough data')
opts = opts || { validate: true }
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),
output: typef.Buffer,
witness: typef.maybe(typef.arrayOf(typef.Buffer))
}),
input: typef.maybe(typef.BufferN(0)),
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) })
let network = a.network || BITCOIN_NETWORK
let o = { network }
lazy.prop(o, 'address', function () {
if (!o.hash) return
return baddress.toBech32(o.hash, 0x00, network.bech32)
})
lazy.prop(o, 'hash', function () {
if (a.output) return a.output.slice(2)
if (a.address) return baddress.fromBech32(a.address).data
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?
if (a.redeem && a.redeem.input && a.redeem.input.length > 0) {
let stack = bscript.toStack(_rchunks())
// assign, and blank the existing input
o.redeem = Object.assign({ witness: stack }, a.redeem)
o.redeem.input = EMPTY_BUFFER
return [].concat(stack, a.redeem.output)
}
if (!a.redeem) return
if (!a.redeem.witness) return
return [].concat(a.redeem.witness, a.redeem.output)
})
// extended validation
if (opts.validate) {
let hash
if (a.address) {
if (_address().prefix !== network.bech32) throw new TypeError('Network mismatch')
if (_address().version !== 0x00) throw new TypeError('Invalid version')
if (_address().data.length !== 32) throw new TypeError('Invalid data')
else hash = _address().data
}
if (a.hash) {
if (hash && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
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')
let hash2 = a.output.slice(2)
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
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 &&
a.redeem.witness) throw new TypeError('Ambiguous witness source')
// is the redeem output non-empty?
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)
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
else hash = hash2
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) {
if (a.redeem && !a.redeem.output.equals(a.witness[a.witness.length - 1])) throw new TypeError('Witness and redeem.output mismatch')
}
}
return Object.assign(o, a)
}
module.exports = p2wsh

40
src/payments/package.json Normal file
View file

@ -0,0 +1,40 @@
{
"name": "bitcoinjs-playground",
"version": "1.0.0",
"description": "Go nuts!",
"main": "_testnet.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bitcoinjs/bitcoinjs-playground.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/bitcoinjs/bitcoinjs-playground/issues"
},
"homepage": "https://github.com/bitcoinjs/bitcoinjs-playground#readme",
"dependencies": {
"async": "^2.5.0",
"bech32": "^1.1.3",
"bip21": "^2.0.1",
"bip32-utils": "^0.11.1",
"bip38": "^2.0.2",
"bip39": "^2.5.0",
"bip69": "^2.1.1",
"bitcoin-ops": "^1.4.1",
"bitcoinjs-lib": "^3.3.2",
"bs58": "^4.0.1",
"bs58check": "^2.1.1",
"cb-http-client": "^0.2.3",
"coinselect": "^3.1.11",
"dhttp": "^2.4.2",
"merkle-lib": "^2.0.10",
"mocha": "^5.0.5",
"tape": "^4.9.0",
"typeforce": "^1.11.4",
"utxo": "^2.0.4"
}
}

View file

@ -98,11 +98,11 @@ function decompile (buffer) {
if ((opcode > OPS.OP_0) && (opcode <= OPS.OP_PUSHDATA4)) {
const d = pushdata.decode(buffer, i)
// did reading a pushDataInt fail? empty script
// did reading a pushDataInt fail?
if (d === null) return null
i += d.size
// attempt to read too much data? empty script
// attempt to read too much data?
if (i + d.number > buffer.length) return null
const data = buffer.slice(i, i + d.number)