Skip to content
 
 

Latest commit

 

History

35 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nostr NIP-44 encryption code

Can be used as a library in your project. Run cargo doc to document public interfaces.

To test and benchmark:

cargo test --release -- --nocapture

NIP-44 v3

This crate also implements the draft NIP-44 v3 scheme alongside v2. The v3 API is:

  • get_conversation_key_v3(seckey, xonly_pubkey) derives the v3 conversation key (the raw ECDH shared secret).
  • encrypt_v3(conversation_key, plaintext, kind, scope) encrypts binary plaintext bound to a context, the event kind and a UTF-8 scope, and returns a base64 payload.
  • decrypt_v3(conversation_key, payload, expected_kind, expected_scope) verifies the context and the MAC, then returns the plaintext. Determine the expected context from the outer event; never trust context parsed out of the payload.
  • decrypt_v3_unverified_context(conversation_key, payload) returns (plaintext, kind, scope) without context verification. Only for tools that inspect encrypted data.
  • payload_version(payload) reports 2 or 3 so callers can dispatch between versions. Note that v2 and v3 use different conversation keys.

v3 differs from v2 in that the version byte is 0x03, message keys are derived per message with HKDF salt "nip44-v3\x00" || nonce over the raw ECDH secret, ChaCha20 runs with an all-zeroes 96-bit nonce, the plaintext carries a 4-byte big-endian length prefix (allowing up to 2^31 - 1 bytes), and the kind and scope context is carried in the payload and authenticated by the MAC. MAC and padding checks are constant time.

The v3 implementation passes all official test vectors (src/nip44v3.vectors.json, copied from the specification repository).

Built with AI pair-programming assistance (Claude)

About

nip44v3 for rust

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages