BSV.Wallet.KeyDeriver (bsv_sdk v1.1.0)

Copy Markdown View Source

BRC-42/43 key derivation.

Derives private, public, and symmetric keys from a root private key using the BRC-42 invoice number scheme.

Summary

Functions

Derive a private key for the given protocol, key ID, and counterparty.

Derive a public key for the given protocol, key ID, counterparty, and direction.

Derive a symmetric key for the given protocol, key ID, and counterparty.

The identity public key (root key's public key).

Hex-encoded compressed identity public key.

Create a new KeyDeriver. If no private key is given, uses the "anyone" key (scalar=1).

Reveal the counterparty shared secret. Cannot be used for 'self'.

Reveal the specific key association (HMAC of shared secret + invoice number).

Types

t()

@type t() :: %BSV.Wallet.KeyDeriver{root_key: BSV.PrivateKey.t()}

Functions

derive_private_key(kd, protocol, key_id, counterparty)

@spec derive_private_key(
  t(),
  BSV.Wallet.Types.Protocol.t(),
  String.t(),
  BSV.Wallet.Types.Counterparty.t()
) :: {:ok, BSV.PrivateKey.t()} | {:error, String.t()}

Derive a private key for the given protocol, key ID, and counterparty.

derive_public_key(kd, protocol, key_id, counterparty, for_self)

@spec derive_public_key(
  t(),
  BSV.Wallet.Types.Protocol.t(),
  String.t(),
  BSV.Wallet.Types.Counterparty.t(),
  boolean()
) :: {:ok, BSV.PublicKey.t()} | {:error, String.t()}

Derive a public key for the given protocol, key ID, counterparty, and direction.

If for_self is true, derives the key corresponding to our own private key (what the counterparty would compute for us). Otherwise derives the counterparty's key.

derive_symmetric_key(kd, protocol, key_id, counterparty, opts \\ [])

@spec derive_symmetric_key(
  t(),
  BSV.Wallet.Types.Protocol.t(),
  String.t(),
  BSV.Wallet.Types.Counterparty.t(),
  keyword()
) :: {:ok, BSV.SymmetricKey.t()} | {:error, String.t()}

Derive a symmetric key for the given protocol, key ID, and counterparty.

Derives both a public and private key, then computes their ECDH shared secret. The x-coordinate is passed through SHA-256 before use as the symmetric key.

Migration

Pass legacy: true to derive using the legacy method (raw x-coordinate). This is used internally for backward-compatible decryption fallback.

identity_key(key_deriver)

@spec identity_key(t()) :: BSV.PublicKey.t()

The identity public key (root key's public key).

identity_key_hex(kd)

@spec identity_key_hex(t()) :: String.t()

Hex-encoded compressed identity public key.

new(pk)

@spec new(BSV.PrivateKey.t() | nil) :: t()

Create a new KeyDeriver. If no private key is given, uses the "anyone" key (scalar=1).

reveal_counterparty_secret(kd, counterparty)

@spec reveal_counterparty_secret(t(), BSV.Wallet.Types.Counterparty.t()) ::
  {:ok, BSV.PublicKey.t()} | {:error, String.t()}

Reveal the counterparty shared secret. Cannot be used for 'self'.

reveal_specific_secret(kd, counterparty, protocol, key_id)

@spec reveal_specific_secret(
  t(),
  BSV.Wallet.Types.Counterparty.t(),
  BSV.Wallet.Types.Protocol.t(),
  String.t()
) :: {:ok, binary()} | {:error, String.t()}

Reveal the specific key association (HMAC of shared secret + invoice number).