Throw errors when p2wsh or p2wpkh contain uncompressed pubkeys.

This will enforce BIP143 compressed pubkey rules on an address generation level.
This commit is contained in:
junderw 2020-05-21 11:11:12 +09:00
parent 4eb698df50
commit 25b5806cf1
No known key found for this signature in database
GPG key ID: B256185D3A971908
7 changed files with 107 additions and 17 deletions
src/payments

View file

@ -107,12 +107,14 @@ function p2wpkh(a, opts) {
if (hash.length > 0 && !hash.equals(pkh))
throw new TypeError('Hash mismatch');
else hash = pkh;
if (!ecc.isPoint(a.pubkey) || a.pubkey.length !== 33)
throw new TypeError('Invalid pubkey for p2wpkh');
}
if (a.witness) {
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]))
if (!ecc.isPoint(a.witness[1]) || a.witness[1].length !== 33)
throw new TypeError('Witness has invalid pubkey');
if (a.signature && !a.signature.equals(a.witness[0]))
throw new TypeError('Signature mismatch');

View file

@ -6,6 +6,7 @@ const bscript = require('../script');
const lazy = require('./lazy');
const typef = require('typeforce');
const OPS = bscript.OPS;
const ecc = require('tiny-secp256k1');
const bech32 = require('bech32');
const EMPTY_BUFFER = Buffer.alloc(0);
function stacksEqual(a, b) {
@ -14,6 +15,14 @@ function stacksEqual(a, b) {
return x.equals(b[i]);
});
}
function chunkHasUncompressedPubkey(chunk) {
if (Buffer.isBuffer(chunk) && chunk.length === 65) {
if (ecc.isPoint(chunk)) return true;
else return false;
} else {
return false;
}
}
// input: <>
// witness: [redeemScriptSig ...] {redeemScript}
// output: OP_0 {sha256(redeemScript)}
@ -51,6 +60,9 @@ function p2wsh(a, opts) {
const _rchunks = lazy.value(() => {
return bscript.decompile(a.redeem.input);
});
const _rochunks = lazy.value(() => {
return bscript.decompile(a.redeem.output);
});
let network = a.network;
if (!network) {
network = (a.redeem && a.redeem.network) || networks_1.bitcoin;
@ -166,14 +178,24 @@ function p2wsh(a, opts) {
!stacksEqual(a.witness, a.redeem.witness)
)
throw new TypeError('Witness and redeem.witness mismatch');
}
if (a.witness) {
if (
a.redeem &&
a.redeem.output &&
!a.redeem.output.equals(a.witness[a.witness.length - 1])
)
(a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) ||
(a.redeem.output && _rochunks().some(chunkHasUncompressedPubkey))
) {
throw new TypeError(
'redeem.input or redeem.output contains uncompressed pubkey',
);
}
}
if (a.witness && a.witness.length > 0) {
const wScript = a.witness[a.witness.length - 1];
if (a.redeem && a.redeem.output && !a.redeem.output.equals(wScript))
throw new TypeError('Witness and redeem.output mismatch');
if (
a.witness.some(chunkHasUncompressedPubkey) ||
(bscript.decompile(wScript) || []).some(chunkHasUncompressedPubkey)
)
throw new TypeError('Witness contains uncompressed pubkey');
}
}
return Object.assign(o, a);