BRC-42 Derivation

Complete examples for using BRC-42 hierarchical deterministic key derivation.

Overview

BRC-42 defines a standard for deriving BSV keys hierarchically, enabling protocol isolation and secure key management. This code feature demonstrates practical BRC-42 key derivation patterns.

Related SDK Components:

Basic BRC-42 Derivation

import { PrivateKey, KeyDeriver } from '@bsv/sdk'

/**
 * Basic BRC-42 Key Derivation
 *
 * Derive protocol-specific keys from a master key
 */
class BRC42BasicDerivation {
  /**
   * Derive a key for a specific protocol
   */
  deriveProtocolKey(
    masterPrivateKey: PrivateKey,
    protocolID: [number, string],
    keyID: string
  ): PrivateKey {
    const deriver = new KeyDeriver(masterPrivateKey)

    // Derive key for specific protocol and key ID
    const derivedKey = deriver.derivePrivateKey(
      protocolID,
      keyID
    )

    return derivedKey
  }

  /**
   * Derive a public key for a specific protocol
   */
  deriveProtocolPublicKey(
    masterPrivateKey: PrivateKey,
    protocolID: [number, string],
    keyID: string
  ): PublicKey {
    const deriver = new KeyDeriver(masterPrivateKey)

    const derivedPublicKey = deriver.derivePublicKey(
      protocolID,
      keyID
    )

    return derivedPublicKey
  }
}

/**
 * Usage Example
 */
async function example() {
  const derivation = new BRC42BasicDerivation()

  // Master key (from seed phrase or secure storage)
  const masterKey = PrivateKey.fromRandom()

  // Protocol ID: [security level, protocol name]
  // Security level 0 = no security, 1 = low, 2 = medium, etc.
  const protocolID: [number, string] = [2, 'my-app']

  // Derive key for specific purpose
  const invoiceKey = derivation.deriveProtocolKey(
    masterKey,
    protocolID,
    'invoices-2024-001'
  )

  console.log('Derived invoice key address:', invoiceKey.toAddress())
}

BRC-43 Security Levels

Protocol Isolation

Advanced Key Derivation Patterns

See Also

SDK Components:

Learning Paths:

Last updated