[ad_1]
If you’re in search of working JavaScript code, I like to recommend bitcoin-sdk-js
which helps BIP322 together with taproot handle signing & verification.
Beneath is methods to implement BIP322 signing & verification for p2pkh(legacy
), p2wpkh(segwit
) and p2tr(taproot
).
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,
);
Disclaimer: I am the proprietor of the library.
[ad_2]
Supply hyperlink
Leave a Reply