bech32encoding

package
v0.2.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2025 License: Unlicense Imports: 15 Imported by: 0

Documentation

Overview

Package bech32encoding implements NIP-19 entities, which are bech32 encoded data that describes nostr data types.

These are not just identifiers of events and users, but also include things like relay hints where to find events.

Index

Constants

View Source
const (
	// MinKeyStringLen is 56 because Bech32 needs 52 characters plus 4 for the HRP,
	// any string shorter than this cannot be a nostr key.
	MinKeyStringLen = 56
	// HexKeyLen is the length of a nostr key in hexadecimal.
	HexKeyLen = 64
	// Bech32HRPLen is the length of the standard nostr keys, nsec and npub.
	Bech32HRPLen = 4
)

Variables

View Source
var (
	// SecHRP is the standard Human Readable Prefix (HRP) for a nostr secret key in bech32 encoding - nsec
	SecHRP = []byte("nsec")
	// PubHRP is the standard Human Readable Prefix (HRP) for a nostr public key in bech32 encoding - nsec
	PubHRP = []byte("npub")
)
View Source
var (
	// NoteHRP is the Human Readable Prefix (HRP) for a nostr note (kind 1)
	NoteHRP = []byte("note")

	// NsecHRP is the Human Readable Prefix (HRP) for a nostr secret key
	NsecHRP = []byte("nsec")

	// NpubHRP is the Human Readable Prefix (HRP) for a nostr public key
	NpubHRP = []byte("npub")

	// NprofileHRP is the Human Readable Prefix (HRP) for a nostr profile metadata
	// event (kind 0)
	NprofileHRP = []byte("nprofile")

	// NeventHRP is the Human Readable Prefix (HRP) for a nostr event, which may
	// include relay hints to find the event, and the author's npub.
	NeventHRP = []byte("nevent")

	// NentityHRP is the Human Readable Prefix (HRP) for a nostr is a generic nostr
	// entity, which may include relay hints to find the event, and the author's
	// npub.
	NentityHRP = []byte("naddr")
)

Functions

func BinToNpub

func BinToNpub(b []byte) (s []byte, err error)

BinToNpub converts a raw 32 byte public key to nostr bech32 encoded npub.

func BinToNsec

func BinToNsec(sk []byte) (nsec []byte, err error)

BinToNsec converts a binary secret key to a bech32 encoded nsec.

func ConvertForBech32

func ConvertForBech32(b8 []byte) (b5 []byte, err error)

ConvertForBech32 performs the bit expansion required for encoding into Bech32.

func ConvertFromBech32

func ConvertFromBech32(b5 []byte) (b8 []byte, err error)

ConvertFromBech32 collapses together the bit expanded 5 bit numbers encoded in bech32.

func Decode

func Decode(bech32string []byte) (prefix []byte, value any, err error)

Decode a nostr bech32 encoded entity, return the prefix, and the decoded value, and any error if one occurred in the process of decoding.

func EncodeEntity

func EncodeEntity(pk []byte, k *kind.T, id []byte, relays [][]byte) (
	s []byte, err error,
)

EncodeEntity encodes a pubkey, kind, event Id, and relay hints.

func EncodeEvent

func EncodeEvent(
	eventIDHex *eventid.T, relays [][]byte, author []byte,
) (s []byte, err error)

EncodeEvent encodes an event, including relay hints and author pubkey.

func EncodeNote

func EncodeNote(eventIDHex []byte) (s []byte, err error)

EncodeNote encodes a standard nostr NIP-19 note entity (mostly meaning a nostr kind 1 short text note)

func EncodeProfile

func EncodeProfile(publicKeyHex []byte, relays [][]byte) (s []byte, err error)

EncodeProfile encodes a pubkey and a set of relays into a bech32 encoded entity.

func HexToNpub

func HexToNpub(publicKeyHex []byte) (s []byte, err error)

HexToNpub converts a raw 64 character hex encoded public key (as used in standard nostr json events) to a bech32 encoded npub.

func HexToNsec

func HexToNsec(sk []byte) (nsec []byte, err error)

HexToNsec converts a hex encoded secret key to a bech32 encoded nsec.

func HexToPublicKey

func HexToPublicKey(pk string) (p *btcec.PublicKey, err error)

HexToPublicKey decodes a string that should be a 64 character long hex encoded public key into a btcec.PublicKey that can be used to verify a signature or encode to Bech32.

func HexToSecretKey

func HexToSecretKey(sk []byte) (s *btcec.SecretKey, err error)

HexToSecretKey decodes a string that should be a 64 character long hex encoded public key into a btcec.PublicKey that can be used to verify a signature or encode to Bech32.

func NpubToBytes

func NpubToBytes(encoded []byte) (pk []byte, err error)

NpubToBytes converts a bech32 encoded public key to raw bytes.

func NpubToPublicKey

func NpubToPublicKey(encoded []byte) (pk *secp256k1.PublicKey, err error)

NpubToPublicKey decodes an nostr public key (npub) and returns an secp256k1 public key.

func NsecToBytes

func NsecToBytes(encoded []byte) (sk []byte, err error)

NsecToBytes converts a nostr bech32 encoded secret key to raw bytes.

func NsecToHex

func NsecToHex(nsec []byte) (hexSec []byte, err error)

NsecToHex converts a bech32 encoded nostr secret key to a raw hexadecimal string.

func NsecToSecretKey

func NsecToSecretKey(encoded []byte) (sk *secp256k1.SecretKey, err error)

NsecToSecretKey decodes a nostr secret key (nsec) and returns the secp256k1 secret key.

func PublicKeyToNpub

func PublicKeyToNpub(pk *secp256k1.PublicKey) (encoded []byte, err error)

PublicKeyToNpub encodes a public key as a bech32 string (npub).

func SecretKeyToHex

func SecretKeyToHex(sk *btcec.SecretKey) (hexSec []byte)

SecretKeyToHex converts a secret key to the hex encoding.

func SecretKeyToNsec

func SecretKeyToNsec(sk *secp256k1.SecretKey) (encoded []byte, err error)

SecretKeyToNsec encodes an secp256k1 secret key as a Bech32 string (nsec).

Types

This section is empty.

Directories

Path Synopsis
Package pointers is a set of basic nip-19 data types for generating bech32 encoded nostr entities.
Package pointers is a set of basic nip-19 data types for generating bech32 encoded nostr entities.
Package tlv implements a simple Type Length Value encoder for nostr NIP-19 bech32 encoded entities.
Package tlv implements a simple Type Length Value encoder for nostr NIP-19 bech32 encoded entities.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL