Merge pull request #1153 from bitcoinjs/paybetter

Increased coverage for payments
This commit is contained in:
Jonathan Underwood 2018-08-23 13:47:07 +09:00 committed by GitHub
commit 20551fd380
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 62 additions and 29 deletions

View file

@ -1,10 +1,10 @@
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 bscript = require('../script') const bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin const BITCOIN_NETWORK = require('../networks').bitcoin
// input: {signature} // input: {signature}
// output: {pubKey} OP_CHECKSIG // output: {pubKey} OP_CHECKSIG
@ -27,10 +27,10 @@ function p2pk (a, opts) {
input: typef.maybe(typef.Buffer) input: typef.maybe(typef.Buffer)
}, a) }, a)
let _chunks = lazy.value(function () { return bscript.decompile(a.input) }) 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, 'output', function () { lazy.prop(o, 'output', function () {
if (!a.pubkey) return if (!a.pubkey) return
@ -58,22 +58,19 @@ function p2pk (a, opts) {
// extended validation // extended validation
if (opts.validate) { 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) {
if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid') 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 (!ecc.isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid')
if (a.pubkey && !a.pubkey.equals(o.pubkey)) throw new TypeError('Pubkey mismatch')
} }
if (a.signature) { if (a.signature) {
if (a.input && !a.input.equals(o.input)) throw new TypeError('Input mismatch') if (a.input && !a.input.equals(o.input)) throw new TypeError('Signature mismatch')
} }
if (a.input) { if (a.input) {
if (_chunks().length !== 1) throw new TypeError('Input is invalid') if (_chunks().length !== 1) throw new TypeError('Input is invalid')
if (!bscript.isCanonicalScriptSignature(_chunks()[0])) throw new TypeError('Input has invalid signature') if (!bscript.isCanonicalScriptSignature(o.signature)) throw new TypeError('Input has invalid signature')
} }
} }

View file

@ -106,18 +106,19 @@ function p2pkh (a, opts) {
a.output[23] !== OPS.OP_EQUALVERIFY || a.output[23] !== OPS.OP_EQUALVERIFY ||
a.output[24] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid') 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') const hash2 = a.output.slice(3, 23)
else hash = a.output.slice(3, 23) if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
else hash = hash2
} }
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
} }
if (a.input) { if (a.input) {
let chunks = _chunks() const chunks = _chunks()
if (chunks.length !== 2) throw new TypeError('Input is invalid') if (chunks.length !== 2) throw new TypeError('Input is invalid')
if (!bscript.isCanonicalScriptSignature(chunks[0])) throw new TypeError('Input has invalid signature') 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 (!ecc.isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey')
@ -125,7 +126,7 @@ function p2pkh (a, opts) {
if (a.signature && !a.signature.equals(chunks[0])) throw new TypeError('Signature mismatch') 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') if (a.pubkey && !a.pubkey.equals(chunks[1])) throw new TypeError('Pubkey mismatch')
let pkh = bcrypto.hash160(chunks[1]) const pkh = bcrypto.hash160(chunks[1])
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch') if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
} }
} }

View file

@ -58,7 +58,7 @@ function p2sh (a, opts) {
const _redeem = lazy.value(function () { const _redeem = lazy.value(function () {
const chunks = _chunks() const chunks = _chunks()
return { return {
network: network, network,
output: chunks[chunks.length - 1], output: chunks[chunks.length - 1],
input: bscript.compile(chunks.slice(0, -1)), input: bscript.compile(chunks.slice(0, -1)),
witness: a.witness || [] witness: a.witness || []
@ -111,7 +111,7 @@ function p2sh (a, opts) {
if (a.address) { if (a.address) {
if (_address().version !== network.scriptHash) throw new TypeError('Invalid version or Network mismatch') if (_address().version !== network.scriptHash) throw new TypeError('Invalid version or Network mismatch')
if (_address().hash.length !== 20) throw new TypeError('Invalid address') if (_address().hash.length !== 20) throw new TypeError('Invalid address')
else hash = _address().hash hash = _address().hash
} }
if (a.hash) { if (a.hash) {
@ -125,6 +125,7 @@ function p2sh (a, opts) {
a.output[0] !== OPS.OP_HASH160 || a.output[0] !== OPS.OP_HASH160 ||
a.output[1] !== 0x14 || a.output[1] !== 0x14 ||
a.output[22] !== OPS.OP_EQUAL) throw new TypeError('Output is invalid') a.output[22] !== OPS.OP_EQUAL) throw new TypeError('Output is invalid')
const hash2 = a.output.slice(2, 22) const hash2 = a.output.slice(2, 22)
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
@ -165,9 +166,10 @@ function p2sh (a, opts) {
if (a.redeem) { if (a.redeem) {
if (a.redeem.network && a.redeem.network !== network) throw new TypeError('Network mismatch') if (a.redeem.network && a.redeem.network !== network) throw new TypeError('Network mismatch')
if (o.redeem) { if (a.input) {
if (a.redeem.output && !a.redeem.output.equals(o.redeem.output)) throw new TypeError('Redeem.output mismatch') const redeem = _redeem()
if (a.redeem.input && !a.redeem.input.equals(o.redeem.input)) throw new TypeError('Redeem.input mismatch') if (a.redeem.output && !a.redeem.output.equals(redeem.output)) throw new TypeError('Redeem.output mismatch')
if (a.redeem.input && !a.redeem.input.equals(redeem.input)) throw new TypeError('Redeem.input mismatch')
} }
checkRedeem(a.redeem) checkRedeem(a.redeem)

View file

@ -93,7 +93,6 @@ function p2wpkh (a, opts) {
if (network && network.bech32 !== _address().prefix) throw new TypeError('Invalid prefix or Network mismatch') if (network && network.bech32 !== _address().prefix) throw new TypeError('Invalid prefix or Network mismatch')
if (_address().version !== 0x00) throw new TypeError('Invalid address version') if (_address().version !== 0x00) throw new TypeError('Invalid address version')
if (_address().data.length !== 20) throw new TypeError('Invalid address data') if (_address().data.length !== 20) throw new TypeError('Invalid address data')
// if (hash && !hash.equals(_address().data)) throw new TypeError('Hash mismatch')
hash = _address().data hash = _address().data
} }

View file

@ -122,7 +122,7 @@ function p2wsh (a, opts) {
if (_address().prefix !== network.bech32) throw new TypeError('Invalid prefix or Network mismatch') 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().version !== 0x00) throw new TypeError('Invalid address version')
if (_address().data.length !== 32) throw new TypeError('Invalid address data') if (_address().data.length !== 32) throw new TypeError('Invalid address data')
else hash = _address().data hash = _address().data
} }
if (a.hash) { if (a.hash) {

View file

@ -7,7 +7,7 @@
}, },
"expected": { "expected": {
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001", "pubkey": "030000000000000000000000000000000000000000000000000000000000000001",
"signatures": null, "signature": null,
"input": null, "input": null,
"witness": null "witness": null
} }
@ -19,7 +19,7 @@
}, },
"expected": { "expected": {
"output": "030000000000000000000000000000000000000000000000000000000000000001 OP_CHECKSIG", "output": "030000000000000000000000000000000000000000000000000000000000000001 OP_CHECKSIG",
"signatures": null, "signature": null,
"input": null, "input": null,
"witness": null "witness": null
} }
@ -116,6 +116,19 @@
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001", "pubkey": "030000000000000000000000000000000000000000000000000000000000000001",
"input": "ffffffffffffffff" "input": "ffffffffffffffff"
} }
},
{
"exception": "Input has invalid signature",
"arguments": {
"input": "30060201ff0201ff01"
}
},
{
"exception": "Signature mismatch",
"arguments": {
"signature": "300602010002010001",
"input": "300602010302010301"
}
} }
], ],
"dynamic": { "dynamic": {

View file

@ -204,6 +204,13 @@
"hash": "ffffffffffffffffffffffffffffffffffffffff", "hash": "ffffffffffffffffffffffffffffffffffffffff",
"input": "300602010002010001 030000000000000000000000000000000000000000000000000000000000000001" "input": "300602010002010001 030000000000000000000000000000000000000000000000000000000000000001"
} }
},
{
"exception": "Signature mismatch",
"arguments": {
"signature": "300602010002010001",
"input": "300602010302010301 030000000000000000000000000000000000000000000000000000000000000001"
}
} }
], ],
"dynamic": { "dynamic": {

View file

@ -269,6 +269,20 @@
] ]
} }
}, },
{
"exception": "Witness and redeem.witness mismatch",
"arguments": {
"redeem": {
"output": "OP_TRUE",
"witness": [
"04000000ff"
]
},
"witness": [
"04000000ee"
]
}
},
{ {
"exception": "Ambiguous witness source", "exception": "Ambiguous witness source",
"arguments": { "arguments": {