Nostr.Bech32 (Nostr Lib v0.2.0)

View Source

Bech32 encoded entities for bare keys and IDs.

Defined in NIP-19: https://github.com/nostr-protocol/nips/blob/master/19.md

This module handles simple 32-byte entities:

  • npub - public keys
  • nsec - private keys
  • note - event IDs

For shareable identifiers with TLV metadata (nprofile, nevent, naddr), use Nostr.NIP19.

Examples

iex> Nostr.Bech32.encode("npub", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d")
{:ok, "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6"}

iex> Nostr.Bech32.npub_to_hex("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6")
{:ok, "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}

Summary

Types

Bech32-encoded string

Hex-encoded data (lowercase)

Human-readable part of bech32 string (nsec, npub, note, nprofile, etc.)

Functions

Decodes a bech32 string to hex, discarding the human-readable prefix.

Encodes hex data with a bech32 human-readable prefix.

Encodes a hex event ID as a bech32 note string.

Encodes a hex public key as a bech32 npub string.

Encodes a hex secret key as a bech32 nsec string.

Decodes a bech32 note string to hex.

Decodes a bech32 npub string to hex.

Decodes a bech32 nsec string to hex.

Types

bech32()

@type bech32() :: String.t()

Bech32-encoded string

hex()

@type hex() :: String.t()

Hex-encoded data (lowercase)

hrp()

@type hrp() :: String.t()

Human-readable part of bech32 string (nsec, npub, note, nprofile, etc.)

Functions

decode(data)

@spec decode(bech32()) :: {:ok, hex()} | {:error, term()}

Decodes a bech32 string to hex, discarding the human-readable prefix.

Examples

iex> Nostr.Bech32.decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6")
{:ok, "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}

encode(hrp, data)

@spec encode(hrp(), hex()) :: {:ok, bech32()} | {:error, :invalid_hex}

Encodes hex data with a bech32 human-readable prefix.

Examples

iex> Nostr.Bech32.encode("npub", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d")
{:ok, "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6"}

iex> Nostr.Bech32.encode("npub", "invalid")
{:error, :invalid_hex}

hex_to_note(event_id)

@spec hex_to_note(hex()) :: {:ok, bech32()} | {:error, :invalid_hex}

Encodes a hex event ID as a bech32 note string.

hex_to_npub(pubkey)

@spec hex_to_npub(hex()) :: {:ok, bech32()} | {:error, :invalid_hex}

Encodes a hex public key as a bech32 npub string.

hex_to_nsec(seckey)

@spec hex_to_nsec(hex()) :: {:ok, bech32()} | {:error, :invalid_hex}

Encodes a hex secret key as a bech32 nsec string.

note_to_hex(data)

@spec note_to_hex(bech32()) :: {:ok, hex()} | {:error, term()}

Decodes a bech32 note string to hex.

npub_to_hex(data)

@spec npub_to_hex(bech32()) :: {:ok, hex()} | {:error, term()}

Decodes a bech32 npub string to hex.

nsec_to_hex(data)

@spec nsec_to_hex(bech32()) :: {:ok, hex()} | {:error, term()}

Decodes a bech32 nsec string to hex.