ncrypt
ncrypt provides an implementation of the NIP-04 and NIP-44 v2/v3 encryption specifications.
[!CAUTION]
ncrypt is in v0 and will likely have breaking changes.
Installation
Add it to your project with go get:
go get pkg.nostr.land/go/ncrypt
View the documentation on pkg.go.dev.
Usage
Secret and public keys are represented as ncrypt.SecretKey and ncrypt.PublicKey, which are of type *[32]byte.
The NIP-04 and NIP-44 v2 packages expose the following APIs:
Encrypt(seckeyA ncrypt.SecretKey, pubkeyB ncrypt.PublicKey, data []byte) ([]byte, error)
Decrypt(seckeyA ncrypt.SecretKey, pubkeyB ncrypt.PublicKey, data []byte) ([]byte, error)
Even though a byte slice is returned, the encrypted data is returned as base64 encoded.
The NIP-44 v3 package also requires an additional Context:
Encrypt(seckeyA ncrypt.SecretKey, pubkeyB ncrypt.PublicKey, context nip44v3.Context, data []byte) ([]byte, error)
Decrypt(seckeyA ncrypt.SecretKey, pubkeyB ncrypt.PublicKey, context nip44v3.Context, data []byte) ([]byte, error)
The Context struct contains the kind of the event, and an additional application-customizable scope. Trying to decrypt a message with a different Context will fail.
On how to set kind/scope, read the NIP-44 v3 implementation guide for more information.