BSV.PublicKey (bsv_sdk v1.1.0)

Copy Markdown View Source

Bitcoin public key operations: derivation, compression, address generation, verification.

Summary

Functions

Compress a public key to 33 bytes.

Decompress a public key to 65 bytes.

Derive a child public key using BRC-42 key derivation.

Compute ECDH shared secret (delegates to private key).

Create a public key from raw bytes (33 compressed or 65 uncompressed).

Derive a compressed public key from a private key.

Generate a Bitcoin address from this public key.

Verify a signature against a message hash.

Types

t()

@type t() :: %BSV.PublicKey{point: binary()}

Functions

compress(key)

@spec compress(t()) :: t()

Compress a public key to 33 bytes.

decompress(key)

@spec decompress(t()) :: {:ok, t()}

Decompress a public key to 65 bytes.

derive_child(pub_key, priv_key, invoice_number)

@spec derive_child(t(), BSV.PrivateKey.t(), String.t()) ::
  {:ok, t()} | {:error, String.t()}

Derive a child public key using BRC-42 key derivation.

Computes ECDH shared secret with the private key, then HMAC-SHA256(shared_secret_compressed, invoice_number) to derive a scalar, multiplies G by that scalar, and adds the result to this public key point.

derive_shared_secret(pub_key, priv_key)

@spec derive_shared_secret(t(), BSV.PrivateKey.t()) ::
  {:ok, t()} | {:error, String.t()}

Compute ECDH shared secret (delegates to private key).

from_bytes(bin)

@spec from_bytes(binary()) :: {:ok, t()} | {:error, String.t()}

Create a public key from raw bytes (33 compressed or 65 uncompressed).

Validates that the point lies on the secp256k1 curve (y² ≡ x³ + 7 mod p).

from_private_key(private_key)

@spec from_private_key(BSV.PrivateKey.t()) :: t()

Derive a compressed public key from a private key.

to_address(key, opts \\ [])

@spec to_address(
  t(),
  keyword()
) :: String.t()

Generate a Bitcoin address from this public key.

verify(public_key, arg, signature_der)

@spec verify(t(), binary(), binary()) :: boolean()

Verify a signature against a message hash.