UNPKG

@exodus/bip322-js

Version:

A Javascript library that provides utility functions related to the BIP-322 signature scheme

28 lines (27 loc) 1.09 kB
import * as bitcoinMessage from '@exodus/bitcoinjs/message'; import { recoverPublicKey } from '@noble/secp256k1'; class BIP137 { static isBIP137Signature(signature) { return signature.byteLength === 65; } static derivePubKey(message, signature) { const messageHash = bitcoinMessage.magicHash(message); const signatureDecoded = this.decodeSignature(signature); const recoveredPublicKey = recoverPublicKey(messageHash, signatureDecoded.signature, signatureDecoded.recovery, signatureDecoded.compressed); return Buffer.from(recoveredPublicKey); } static decodeSignature(signature) { if (signature.length !== 65) throw new Error('Invalid signature length'); const flagByte = signature.readUInt8(0) - 27; if (flagByte > 19 || flagByte < 0) { throw new Error('Invalid signature parameter'); } return { compressed: !!(flagByte & 12), recovery: flagByte & 3, signature: signature.subarray(1), }; } } export default BIP137;