Tuesday, November 5, 2024

message signing – Find out how to confirm {that a} signature was signed by a pubkey? Taproot and BIP0322

Utilizing wallets like XVerse, customers can signal a message utilizing their ordinal btc tackle, to illustrate bc1XXX, the message is hashed based mostly on BIP0322.

So I’ve:

  • Consumer pockets: bc1XXX
  • Message hash: YYY
  • Signature signed by bc1XXX: ZZZ

So this knowledge is distributed to my backend server, and I need to confirm that ZZZ was certainly signed by bc1XXX (and accommodates YYY as message).

I am utilizing this thus far:

    const msgHash = bip0322Hash(message);
    const signatureBuffer = Buffer.from(signatureStr, 'base64');

    const decodedSignature = signatureBuffer.slice(2, 66);
    const recoveryId = signatureBuffer[0];

    // Extract public key from the signature
    const recoveredPublicKeyBuffer = secp.recoverPublicKey(
        msgHash,
        decodedSignature,
        recoveryId, // Restoration ID (0 or 1)
        false
    );
    console.log(publicKeyToTaprootAddress(recoveredPublicKeyBuffer)); //no match with my unique pubkey that signed the message

However I’ve a tough time getting the proper tackle from recoveredPublicKeyBuffer which I can not match with the general public key tackle of my check set.

I am making an attempt to make use of this operate, however the output would not match my pubkey:

    operate publicKeyToTaprootAddress(publicKey: Uint8Array) {
        // Compute the SHA-256 hash of the general public key
        const hash = sha256(Buffer.from(publicKey));

        // Assemble the human-readable half and the info a part of the Bech32m string
        const hrp = 'bc';
        const knowledge = sha256(Buffer.from([0x01].concat(Array.from(hash))));
        const data2 = bech32m.toWords(Buffer.from(knowledge));

        // Encode the Bech32m string
        return bech32m.encode(hrp, data2);
    } 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles