# BRC-42

## Overview

BRC-42 defines a standardized protocol for deriving encryption and signing keys from a master private key. This enables secure, deterministic key generation for various applications and services.

## Purpose

BRC-42 solves the key management problem by:

* Providing deterministic key derivation
* Enabling per-protocol and per-counterparty keys
* Supporting both encryption and signing operations
* Maintaining privacy through key isolation

## Key Concepts

### Protocol ID

A unique identifier for each application or protocol (e.g., "hello world", "payments")

### Key ID

An identifier for specific use cases within a protocol (e.g., message encryption, data signing)

### Counterparty

The public key of the party you're interacting with (optional)

### Invoice Number

A unique number for per-interaction keys (optional)

## Basic Usage

```typescript
import { PrivateKey } from '@bsv/sdk'

const privateKey = PrivateKey.fromWif('your-wif-key')

// Derive a key for a specific protocol
const derivedKey = privateKey.deriveChild(
  PrivateKey.fromString('hello world'), // protocolID
  PrivateKey.fromString('encryption')    // keyID
)
```

## Use Cases

### Application-Specific Keys

Derive unique keys for different applications without exposing your master key.

### Encryption Keys

Generate encryption keys for secure communication with specific counterparties.

### Signing Keys

Create signing keys for authentication and message verification.

### Invoice-Specific Keys

Generate unique keys for each payment or interaction.

## Security Features

* **Key Isolation**: Each protocol/application uses separate keys
* **Deterministic**: Same inputs always produce same keys
* **Counterparty-Specific**: Keys can be unique per interaction partner
* **Master Key Protection**: Master key never needs to be exposed

## Related Components

* [Private Keys](/bsv-code-academy/sdk-components-reference/sdk-components/private-keys.md) - Key management fundamentals
* [HD Wallets](/bsv-code-academy/sdk-components-reference/sdk-components/hd-wallets.md) - Hierarchical key derivation

## Code Examples

See [Code Features - BRC-42 Key Derivation](/bsv-code-academy/code-features/code-features/brc-42-derivation.md) for complete examples.

## Common Patterns

### Derive Application Key

```typescript
const appKey = masterKey.deriveChild(
  PrivateKey.fromString('myapp'),
  PrivateKey.fromString('primary')
)
```

### Derive Counterparty-Specific Key

```typescript
const sharedKey = masterKey.deriveChild(
  protocolID,
  keyID,
  counterpartyPublicKey
)
```

### Derive Invoice Key

```typescript
const invoiceKey = masterKey.deriveChild(
  protocolID,
  keyID,
  counterpartyPublicKey,
  invoiceNumber
)
```

## Best Practices

1. Use descriptive protocol IDs
2. Document your keyID conventions
3. Never reuse invoice numbers
4. Store protocol/key IDs securely
5. Use counterparty keys when possible for added security

## Specification

For the complete BRC-42 specification, visit the official BSV documentation.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hub.bsvblockchain.org/bsv-code-academy/sdk-components-reference/sdk-components/brc-42.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
