Tuesday, November 5, 2024

Verifying the handle and the message utilizing the general public key, handle, and signature!

I am attempting to confirm possession of a Bitcoin handle utilizing the general public key, signature, and the handle (P2PKH, P2SH, P2WPKH, P2WSH, P2TR) as enter. Nonetheless, the code under doesn’t work for Bech32 addresses.

import BitCoinLib from 'bitcoinjs-lib';
import BitCoinMessage from 'bitcoinjs-message';

BitCoinMessage.confirm(message, handle, signature);

As one other try, I attempted utilizing bitcore-lib, however it does not assist handle verification. It solely accepts public key and signature as inputs.

import bitcore from 'bitcore-lib';

export operate verifyMessage(publicKey, sig, textual content) {
    const message = new bitcore.Message(textual content);
  
    var signature = bitcore.crypto.Signature.fromCompact(
      Buffer.from(sig, "base64")
    );
    var hash = message.magicHash();
  
    // get better the general 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.confirm(hash, signature, pubkeyInSig);
  }

What I need to obtain is just like https://www.verifybitcoinmessage.com/, however that website additionally has limitations because it can not confirm P2WPKH addresses.

Is it attainable to confirm all varieties of addresses? In that case, please present steering on learn how to do it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles