Browse Source

Merge pull request #1127 from bitcoinjs/pays

Payments coverage tests (>90%)
addLowRGrinding
Jonathan Underwood 7 years ago
committed by GitHub
parent
commit
9ed9ebde7c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      src/payments/p2ms.js
  2. 4
      src/payments/p2pkh.js
  3. 2
      src/payments/p2sh.js
  4. 28
      src/payments/p2wpkh.js
  5. 18
      test/fixtures/p2pkh.json
  6. 33
      test/fixtures/p2sh.json
  7. 29
      test/fixtures/p2wpkh.json

24
src/payments/p2ms.js

@ -1,11 +1,11 @@
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 bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin
let OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
const bscript = require('../script')
const BITCOIN_NETWORK = require('../networks').bitcoin
const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
function stacksEqual (a, b) {
if (a.length !== b.length) return false
@ -41,8 +41,8 @@ function p2ms (a, opts) {
input: typef.maybe(typef.Buffer)
}, a)
let network = a.network || BITCOIN_NETWORK
let o = { network }
const network = a.network || BITCOIN_NETWORK
const o = { network }
let chunks
let decoded = false
@ -50,10 +50,8 @@ function p2ms (a, opts) {
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.m = chunks[0] - OP_INT_BASE
o.n = chunks[chunks.length - 2] - OP_INT_BASE
o.pubkeys = chunks.slice(1, -2)
}

4
src/payments/p2pkh.js

@ -87,9 +87,9 @@ function p2pkh (a, opts) {
if (opts.validate) {
let hash
if (a.address) {
if (_address().version !== network.pubKeyHash) throw new TypeError('Network mismatch')
if (_address().version !== network.pubKeyHash) throw new TypeError('Invalid version or Network mismatch')
if (_address().hash.length !== 20) throw new TypeError('Invalid address')
else hash = _address().hash
hash = _address().hash
}
if (a.hash) {

2
src/payments/p2sh.js

@ -109,7 +109,7 @@ function p2sh (a, opts) {
if (opts.validate) {
let hash
if (a.address) {
if (_address().version !== network.scriptHash) throw new TypeError('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')
else hash = _address().hash
}

28
src/payments/p2wpkh.js

@ -90,17 +90,11 @@ function p2wpkh (a, opts) {
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) {
const pkh = bcrypto.hash160(a.pubkey)
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
else hash = pkh
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().data.length !== 20) throw new TypeError('Invalid address data')
// if (hash && !hash.equals(_address().data)) throw new TypeError('Hash mismatch')
hash = _address().data
}
if (a.hash) {
@ -117,10 +111,16 @@ function p2wpkh (a, opts) {
else hash = a.output.slice(2)
}
if (a.pubkey) {
const pkh = bcrypto.hash160(a.pubkey)
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
else hash = pkh
}
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.witness.length !== 2) throw new TypeError('Witness is invalid')
if (!bscript.isCanonicalScriptSignature(a.witness[0])) throw new TypeError('Witness has invalid signature')
if (!ecc.isPoint(a.witness[1])) throw new TypeError('Witness 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')

18
test/fixtures/p2pkh.json

@ -121,6 +121,24 @@
"outputHex": "76a94c14aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac"
}
},
{
"exception": "Invalid version or Network mismatch",
"arguments": {
"address": "3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr"
}
},
{
"exception": "Invalid address",
"arguments": {
"address": "111111111111111111117K4nzc"
}
},
{
"exception": "Invalid address",
"arguments": {
"address": "111111111111111111111111133izVn"
}
},
{
"exception": "Pubkey mismatch",
"arguments": {

33
test/fixtures/p2sh.json

@ -227,6 +227,24 @@
}
}
},
{
"exception": "Invalid version or Network mismatch",
"arguments": {
"address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh"
}
},
{
"exception": "Invalid address",
"arguments": {
"address": "TYPjCiGbKiwP6r12cdkmVjySbQr7mb3r"
}
},
{
"exception": "Invalid address",
"arguments": {
"address": "EDaBpuERpLssFzbCV1kgy8tKJsHrcwmzY7HDMF2"
}
},
{
"exception": "Input too short",
"arguments": {
@ -280,6 +298,21 @@
"inputHex": "021000"
}
},
{
"exception": "Witness and redeem.witness mismatch",
"arguments": {
"witness": [
"3045ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"030000000000000000000000000000000000000000000000000000000000000001"
],
"redeem": {
"witness": [
"3045dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
"030000000000000000000000000000000000000000000000000000000000000001"
]
}
}
},
{
"exception": "Hash mismatch",
"arguments": {

29
test/fixtures/p2wpkh.json

@ -116,6 +116,18 @@
]
}
},
{
"exception": "Invalid prefix or Network mismatch",
"arguments": {
"address": "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7"
}
},
{
"exception": "Invalid address version",
"arguments": {
"address": "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx"
}
},
{
"exception": "Hash mismatch",
"arguments": {
@ -137,6 +149,13 @@
"hash": "ffffffffffffffffffffffffffffffffffffffff"
}
},
{
"exception": "Hash mismatch",
"arguments": {
"hash": "ffffffffffffffffffffffffffffffffffffffff",
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001"
}
},
{
"exception": "Hash mismatch",
"arguments": {
@ -148,7 +167,13 @@
}
},
{
"exception": "Input has invalid signature",
"exception": "Witness is invalid",
"arguments": {
"witness": []
}
},
{
"exception": "Witness has invalid signature",
"arguments": {
"witness": [
"ffffffffffffffffff",
@ -157,7 +182,7 @@
}
},
{
"exception": "Input has invalid pubkey",
"exception": "Witness has invalid pubkey",
"arguments": {
"witness": [
"300602010002010001",

Loading…
Cancel
Save