Message Signing

Complete examples for signing and verifying messages with Bitcoin private keys.

Overview

This code feature demonstrates how to sign arbitrary messages with Bitcoin private keys and verify those signatures. Message signing is essential for proving ownership of a private key without revealing it, enabling authentication, proving identity, and verifying data integrity.

Related SDK Components:

Basic Message Signing

import { PrivateKey, PublicKey, Signature, Hash } from '@bsv/sdk'

/**
 * Basic Message Signing
 *
 * Sign and verify messages using Bitcoin keys
 */
class BasicMessageSigning {
  /**
   * Sign a message with a private key
   */
  signMessage(privateKey: PrivateKey, message: string): {
    signature: string
    publicKey: string
    message: string
  } {
    // Hash the message
    const messageHash = Hash.sha256(Buffer.from(message, 'utf8'))

    // Sign the hash
    const signature = privateKey.sign(messageHash)

    return {
      signature: signature.toDER().toString('hex'),
      publicKey: privateKey.toPublicKey().toString(),
      message: message
    }
  }

  /**
   * Verify a message signature
   */
  verifyMessage(
    publicKey: string,
    message: string,
    signatureHex: string
  ): boolean {
    try {
      // Reconstruct the message hash
      const messageHash = Hash.sha256(Buffer.from(message, 'utf8'))

      // Parse signature and public key
      const signature = Signature.fromDER(Buffer.from(signatureHex, 'hex'))
      const pubKey = PublicKey.fromString(publicKey)

      // Verify signature
      return pubKey.verify(messageHash, signature)
    } catch (error) {
      console.error('Verification failed:', error)
      return false
    }
  }

  /**
   * Sign message and return compact format
   */
  signMessageCompact(privateKey: PrivateKey, message: string): string {
    const messageHash = Hash.sha256(Buffer.from(message, 'utf8'))
    const signature = privateKey.sign(messageHash)

    return signature.toCompact().toString('base64')
  }

  /**
   * Verify compact signature
   */
  verifyMessageCompact(
    publicKey: string,
    message: string,
    signatureBase64: string
  ): boolean {
    try {
      const messageHash = Hash.sha256(Buffer.from(message, 'utf8'))
      const signature = Signature.fromCompact(Buffer.from(signatureBase64, 'base64'))
      const pubKey = PublicKey.fromString(publicKey)

      return pubKey.verify(messageHash, signature)
    } catch (error) {
      console.error('Verification failed:', error)
      return false
    }
  }
}

/**
 * Usage Example
 */
async function example() {
  const signer = new BasicMessageSigning()
  const privateKey = PrivateKey.fromRandom()

  // Sign a message
  const signed = signer.signMessage(
    privateKey,
    'Hello, Bitcoin!'
  )

  console.log('Signature:', signed.signature)
  console.log('Public Key:', signed.publicKey)

  // Verify the message
  const isValid = signer.verifyMessage(
    signed.publicKey,
    signed.message,
    signed.signature
  )

  console.log('Signature valid:', isValid)
}

Bitcoin Signed Message Format

Advanced Message Signing Patterns

Authentication and Proof of Ownership

See Also

SDK Components:

Learning Paths:

Last updated