docstrings for many functions.

This commit is contained in:
fiatjaf
2025-03-04 11:08:31 -03:00
parent a82780e82e
commit 5bfaed2740
22 changed files with 293 additions and 66 deletions

View File

@@ -8,7 +8,8 @@ import (
"github.com/puzpuzpuz/xsync/v3"
)
// Keysigner is a signer that holds the private key in memory and can do all the operations instantly and easily.
// KeySigner is a signer that holds the private key in memory and can perform
// all operations instantly and easily.
type KeySigner struct {
sk string
pk string
@@ -16,6 +17,8 @@ type KeySigner struct {
conversationKeys *xsync.MapOf[string, [32]byte]
}
// NewPlainKeySigner creates a new KeySigner from a private key.
// Returns an error if the private key is invalid.
func NewPlainKeySigner(sec string) (KeySigner, error) {
pk, err := nostr.GetPublicKey(sec)
if err != nil {
@@ -24,9 +27,15 @@ func NewPlainKeySigner(sec string) (KeySigner, error) {
return KeySigner{sec, pk, xsync.NewMapOf[string, [32]byte]()}, nil
}
// SignEvent signs the provided event with the signer's private key.
// It sets the event's ID, PubKey, and Sig fields.
func (ks KeySigner) SignEvent(ctx context.Context, evt *nostr.Event) error { return evt.Sign(ks.sk) }
// GetPublicKey returns the public key associated with this signer.
func (ks KeySigner) GetPublicKey(ctx context.Context) (string, error) { return ks.pk, nil }
// Encrypt encrypts a plaintext message for a recipient using NIP-44.
// It caches conversation keys for efficiency in repeated operations.
func (ks KeySigner) Encrypt(ctx context.Context, plaintext string, recipient string) (string, error) {
ck, ok := ks.conversationKeys.Load(recipient)
if !ok {
@@ -40,6 +49,8 @@ func (ks KeySigner) Encrypt(ctx context.Context, plaintext string, recipient str
return nip44.Encrypt(plaintext, ck)
}
// Decrypt decrypts a base64-encoded ciphertext from a sender using NIP-44.
// It caches conversation keys for efficiency in repeated operations.
func (ks KeySigner) Decrypt(ctx context.Context, base64ciphertext string, sender string) (string, error) {
ck, ok := ks.conversationKeys.Load(sender)
if !ok {