Bech-32 encoding is utilized for the primary purpose of transportability between users/client/applications. In addition to bech-32 encoding of the data a series of prefixes are also used to help easily differentiate between different data objects. npub/nsec for public and private keys, respectively and note for note ids.

Extra metadata may be included when communicating between applications to make cross compatibility more streamlined. These follow a type-length-value structure and have the following possible prefixes: nprofile, nevent, nrelay and naddr.

The nip19 module and associated Nip19Event and Nip19Profile structs can be used to handle construction and interpretation of these data.

bech32-encoded entities (NIP-19)




For most of these examples you will see that the to_bech32() and from_bech32() methods generally facilitate encoding or decoding objects per the NIP-19 standard.

Public and Private (or secret) keys in npub and nsec formats.

    print(f" Public key: {keys.public_key().to_bech32()}")
    print(f" Secret key: {keys.secret_key().to_bech32()}")

Simple note presented in NIP-19 format.

    event = EventBuilder.text_note("Hello from Rust Nostr Python bindings!", []).to_event(keys)
    print(f" Event     : {}")

Using the Nip19Profile class to create a shareable nprofile that includes relay data to help other applications to locate the profile data.

    # Create NIP-19 profile including relays data
    relays = ["wss://"]
    nprofile = Nip19Profile(keys.public_key(),relays)
    print(f" Profile (encoded): {nprofile.to_bech32()}")

Using the Nip19 class to decode the previously shared profile data. This class helps generalize the decoding process for all NIP-19 objects.

    # Decode NIP-19 profile
    decode_nprofile = Nip19.from_bech32(nprofile.to_bech32())
    print(f" Profile (decoded): {decode_nprofile}")

Using the Nip19Event class to create a shareable nevent that includes author and relay data. This is followed by decoding the event object.

    # Create NIP-19 event including author and relays data
    nevent = Nip19Event(, keys.public_key(), kind=None, relays=relays)
    print(f" Event (encoded): {nevent.to_bech32()}")
    # Decode NIP-19 event
    decode_nevent = Nip19.from_bech32(nevent.to_bech32())
    print(f" Event (decoded): {decode_nevent}")

Using the Coordinate class to generate the coordinates for a replaceable event (in this case Metadata). This is followed by decoding the object.

    # Create NIP-19 coordinate
    coord = Coordinate(Kind(0),keys.public_key())
    print(f" Coordinate (encoded): {coord.to_bech32()}")
    # Decode NIP-19 coordinate
    decode_coord = Nip19.from_bech32(coord.to_bech32())
    print(f" Coordinate (decoded): {decode_coord}")