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

0
25


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 kind of handle with this schema(bitcoinjs-message additionally helps p2wpkh and p2sh-p2wpkh, however not p2tr), however there is not any strict normal for it.

BIP322 suggests a brand new method 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 carried out 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 should utilize 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 check vector so you should utilize it. I’d attempt to comply with growth of bitcoin-core. Watch out as BIP322 itself is in transition.

Under is tips on how to 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 wish 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 believe the web site you refer might need a problem with non-ASCII encoding, I like to recommend this web site to check conventional message signing.

LEAVE A REPLY

Please enter your comment!
Please enter your name here