I'm trying to verify ownership of a Bitcoin address using the public key, signature, and the address (P2PKH, P2SH, P2WPKH, P2WSH, P2TR) as input. However, the code below does not work for Bech32 addresses.
import BitCoinLib from 'bitcoinjs-lib';
import BitCoinMessage from 'bitcoinjs-message';
BitCoinMessage.verify(message, address, signature);
As another attempt, I tried using bitcore-lib, but it doesn't support address verification. It only accepts public key and signature as inputs.
import bitcore from 'bitcore-lib';
export function verifyMessage(publicKey, sig, text) {
const message = new bitcore.Message(text);
var signature = bitcore.crypto.Signature.fromCompact(
Buffer.from(sig, "base64")
);
var hash = message.magicHash();
// recover the public key
var ecdsa = new bitcore.crypto.ECDSA();
ecdsa.hashbuf = hash;
ecdsa.sig = signature;
const pubkeyInSig = ecdsa.toPublicKey();
const pubkeyInSigString = new bitcore.PublicKey(
Object.assign({}, pubkeyInSig.toObject(), { compressed: true })
).toString();
if (pubkeyInSigString != publicKey) {
return false;
}
return bitcore.crypto.ECDSA.verify(hash, signature, pubkeyInSig);
}
What I want to achieve is similar to https://www.verifybitcoinmessage.com/, but that site also has limitations as it cannot verify P2WPKH addresses.
Is it possible to verify all types of addresses? If so, please provide guidance on how to do it.