neco-secp
Minimal secp256k1 and Nostr signing core, for building Nostr clients and tools in pure Rust.
This crate provides a small pure Rust core for secret keys, x-only public keys, Schnorr signatures, and Nostr event signing. Storage, browser integration, and key isolation policy are handled by adjacent higher-level crates.
Features
nip19: enable NIP-19 bare entities (npub,nsec,note) and TLV entities (nprofile,nevent,naddr,nrelay)nip04: enable NIP-04 encryption/decryption helpersnip44: enable NIP-44 v2 conversation key, encryption, and decryption helpersserde: derive serialization for event structsnostr: enable Nostr event helpers and signed-event JSON supportbatch: enable batch key generation and mining helpers
Usage
use ;
let secret = generate.unwrap;
let event = UnsignedEvent ;
let signed = finalize_event.unwrap;
verify_event.unwrap;
KeyBundle bundles a secret key and its x-only public key together.
use KeyBundle;
let bundle = generate.unwrap;
let secret = bundle.secret;
let xonly = bundle.xonly_public_key;
# let _ = ;
With nip19 feature:
use ;
let bundle = generate.unwrap;
let npub = bundle.npub.unwrap;
let nsec = bundle.nsec.unwrap;
batch feature enables generating multiple key pairs at once, and mining helpers.
use KeyBundle;
// batch key generation
let bundles = generate_batch.unwrap;
// PoW mining: find a key with N leading zero hex nibbles in the xonly pubkey
let bundle = mine_pow.unwrap;
Vanity mining requires both batch and nip19 features.
use mine_vanity_npub;
// find a key whose npub starts with the given prefix
let bundle = mine_vanity_npub.unwrap;
NIP-19 entities are available behind the nip19 feature.
use ;
let secret = generate.unwrap;
let nsec = encode_nsec.unwrap;
let decoded = decode.unwrap;
assert!;
let profile = NProfile ;
let nprofile = encode_nprofile.unwrap;
let decoded = decode.unwrap;
assert!;
NIP-44 helpers are available behind the nip44 feature.
use ;
let secret = from_bytes.unwrap;
let peer = from_bytes.unwrap;
let peer_pubkey = peer.xonly_public_key.unwrap;
let conversation_key = get_conversation_key.unwrap;
let payload = encrypt.unwrap;
let plaintext = decrypt.unwrap;
assert_eq!;
NIP-04 helpers are available behind the nip04 feature.
use ;
let secret = from_bytes.unwrap;
let peer = from_bytes.unwrap;
let peer_pubkey = peer.xonly_public_key.unwrap;
let payload = encrypt.unwrap;
let plaintext = decrypt.unwrap;
assert_eq!;
Testing
Normal cargo test runs don't include expensive mining checks.
- Vanity mining stress tests run only when
AINE_RUN_VANITY_TESTS=1is set. - PoW mining stress tests run only when
AINE_RUN_POW_TESTS=1is set.
Examples:
# run ordinary tests
# include expensive PoW mining checks
AINE_RUN_POW_TESTS=1
# include expensive vanity mining checks
AINE_RUN_VANITY_TESTS=1
API
| Item | Description |
|---|---|
SecretKey |
Validated 32-byte secp256k1 secret key |
XOnlyPublicKey |
Validated 32-byte x-only public key |
SchnorrSignature |
64-byte Schnorr signature |
KeyBundle |
Bundled secret key and x-only public key pair |
KeyBundle::generate() |
Generates a random key pair |
KeyBundle::secret() / xonly_public_key() |
Borrow the validated secret key and x-only public key |
KeyBundle::npub() |
Returns the npub string (nip19) |
KeyBundle::nsec() |
Returns the nsec string (nip19) |
KeyBundle::generate_batch(n) |
Generates n key pairs (batch) |
mine_pow(d, n) |
Finds a key with d leading zero hex nibbles (batch) |
mine_vanity_npub(p, n) |
Finds a key whose npub starts with p (batch + nip19) |
UnsignedEvent |
Unsigned Nostr event payload |
SignedEvent |
Signed Nostr event |
nostr |
Event serialization, signing, and verification helpers |
nip19 |
Bare/TLV NIP-19 encode/decode helpers |
nip04 |
NIP-04 payload helpers |
nip44 |
NIP-44 v2 conversation key and payload helpers |
License
MIT