Monday, September 1, 2025
HomeBitcoinVerifying the handle and the message utilizing the general public key, handle,...

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

bitcoinjs-message makes use of conventional signing course of which merely signal message with prefix(if not given) x18Bitcoin Signed Message:n. This type of signing schema has been frequent however has limitation because it solely helps p2pkh. There are implementations which assist different sort of handle with this schema(bitcoinjs-message additionally helps p2wpkh and p2sh-p2wpkh, however not p2tr), however there is not any strict commonplace for it.

BIP322 suggests a brand new means of signing schema, wherein digital transaction is required to signal and confirm all kinds of addresses whereas p2pkh makes use of conventional signing course of. Nonetheless, it is nonetheless in growth and never but applied in bitcoin-core as I do know.

I used to be additionally in search of bip322 message signing library, and simply find yourself implementing it on my own. When you want you should use it. My open supply bitcoin-sdk-js has a function of bip322 signing and verifying message with javascript, which assist p2pkh, p2wpkh and p2tr. It is verified with bip322 check vector so you should use it. I might attempt to observe growth of bitcoin-core. Watch out as BIP322 itself is in transition.

Under is how you can implement.

import * as bitcoin from 'bitcoin-sdk-js'

const keyPair = await bitcoin.pockets.generateKeyPair();
const privkey = keyPair.privateKey;
const pubkey = keyPair.publicKey;
const legacyAddress = await bitcoin.handle.generateAddress(
  pubkey,
  'legacy',
);
const segwitAddress = await bitcoin.handle.generateAddress(
  pubkey,
  'segwit',
);
const tapAddress = await bitcoin.handle.generateAddress(
  (
    await bitcoin.tapscript.getTapTweakedPubkey(
      pubkey.slice(2),
      await bitcoin.tapscript.getTapTweak(pubkey.slice(2)),
    )
  ).tweakedPubKey,
  'taproot',
);
const msg = 'message you need to signal';
// When
const sigLegacy = await bitcoin.crypto.signMessage(
   msg,
   privkey,
   legacyAddress,
);
const sigSegwit = await bitcoin.crypto.signMessage(
  msg,
  privkey,
  segwitAddress,
);
const sigTap = await bitcoin.crypto.signMessage(msg, privkey, tapAddress);
// Then
assert.strictEqual(
  await bitcoin.crypto.verifyMessage(msg, sigLegacy, legacyAddress),
  true,
);
assert.strictEqual(
  await bitcoin.crypto.verifyMessage(msg, sigSegwit, segwitAddress),
  true,
);
assert.strictEqual(
  await bitcoin.crypto.verifyMessage(msg, sigTap, tapAddress),
  true,
);

p.s. I feel the web site you refer may need a problem with non-ASCII encoding, I like to recommend this web site to check conventional message signing.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments