Nostr.Bech32 (Nostr Lib v0.2.0)
View SourceBech32 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 keysnsec- private keysnote- 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
Functions
Decodes a bech32 string to hex, discarding the human-readable prefix.
Examples
iex> Nostr.Bech32.decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6")
{:ok, "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}
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}
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.