A C library for Nostr Bech32 encoding and decoding. This library is based on Pieter Wuille's reference implementation of Bech32 for SegWit.
- Encode and decode Nostr-specific Bech32 formats:
npub
: public keysnsec
: private keysnote
: note IDs- Simple C API
- No external dependencies
- MIT licensed
git clone https://github.com/NaoX/nbech32.git
cd nbech32
make
To use nbech32 in your project, simply include the header and source files:
#include "nbech32.h"
uint8_t pubkey[32] = {...}; // Your 32-byte public key
char output[65]; // Buffer for the encoded string
if (nostr_encode_pubkey(output, pubkey)) {
printf("Encoded pubkey: %s\n", output);
}
const char *npub = "npub1..."; // Bech32 encoded public key
uint8_t pubkey[32];
if (nostr_decode_pubkey(pubkey, npub)) {
// Successfully decoded
}
Similar functions exist for private keys (nostr_encode_privkey
, nostr_decode_privkey
) and note IDs (nostr_encode_note
, nostr_decode_note
).
See nbech32.h for the complete API documentation.
Run the test suite:
make test
Contributions are welcome! Please feel free to submit a Pull Request.
- Original Bech32 implementation by Pieter Wuille
- Nostr adaptation by MutinyWallet
This project is licensed under the MIT License - see the LICENSE file for details.