BIP-322 JavaScript Library

3.0.0 · active · verified Tue Apr 21

bip322-js is a TypeScript/JavaScript library providing utility functions for the BIP-322 signature scheme, enabling generic message signing and verification across various Bitcoin address types. It supports P2PKH, P2SH-P2WPKH, P2WPKH, and single-key-spend P2TR addresses on mainnet, testnet, and regtest. The current stable version is 3.0.0, released in April 2025. A key differentiator is its 'Loose BIP-137 Verification' by default, which allows backward compatibility with legacy BIP-137 signatures, even if they have non-standard header flags, by assuming ownership of the private key for all derivable addresses. It also provides functionalities to generate raw `toSpend` and `toSign` BIP-322 transactions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to sign a message using a private key for a P2WPKH address and then verify the generated signature, including an example of toggling strict BIP-137 verification.

import { Signer, Verifier } from 'bip322-js';

const privateKey = 'L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k'; // Example private key (do not use in production)
const address = 'bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l'; // P2WPKH address
const message = 'Hello World';

async function runBip322Example() {
  try {
    // Sign the message
    const signature = Signer.sign(privateKey, address, message);
    console.log('Generated Signature:', signature);

    // Verify the signature
    const isValid = Verifier.verifySignature(address, message, signature);
    console.log('Is signature valid (strict verification disabled by default)?', isValid);

    // Verify with strict BIP-137 verification (if applicable for BIP-137 signatures)
    const isStrictlyValid = Verifier.verifySignature(address, message, signature, true);
    console.log('Is signature valid (strict verification enabled)?', isStrictlyValid);

  } catch (error) {
    console.error('An error occurred:', error);
  }
}

runBip322Example();

view raw JSON →