Friday, September 20, 2024
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 widespread 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 no strict commonplace for it.

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

I used to be additionally searching for bip322 message signing library, and simply find yourself implementing it on my own. In case you want you need to use it. My open supply bitcoin-sdk-js has a characteristic of bip322 signing and verifying message with javascript, which assist p2pkh, p2wpkh and p2tr. It is verified with bip322 take a look at vector so you need to 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 difficulty 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