From 2865cccc460cd1909d2fb053b1dab5e66ef51600 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 4 Mar 2025 18:03:02 -0300 Subject: [PATCH] keyer: add ReadOnlySigner. --- keyer/plain.go | 5 ++--- keyer/readonly.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 keyer/readonly.go diff --git a/keyer/plain.go b/keyer/plain.go index 987b7af..1ca9db9 100644 --- a/keyer/plain.go +++ b/keyer/plain.go @@ -8,8 +8,7 @@ import ( "github.com/puzpuzpuz/xsync/v3" ) -// KeySigner is a signer that holds the private key in memory and can perform -// all operations instantly and easily. +// KeySigner is a signer that holds the private key in memory type KeySigner struct { sk string pk string @@ -32,7 +31,7 @@ func NewPlainKeySigner(sec string) (KeySigner, error) { 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 } +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. diff --git a/keyer/readonly.go b/keyer/readonly.go new file mode 100644 index 0000000..3b0898b --- /dev/null +++ b/keyer/readonly.go @@ -0,0 +1,27 @@ +package keyer + +import ( + "context" + "fmt" + + "github.com/nbd-wtf/go-nostr" +) + +// ReadOnlySigner is a Signer that holds a public key in memory and cannot sign anything +type ReadOnlySigner struct { + pk string +} + +func NewReadOnlySigner(pk string) ReadOnlySigner { + return ReadOnlySigner{pk} +} + +// SignEvent returns an error. +func (ros ReadOnlySigner) SignEvent(context.Context, *nostr.Event) error { + return fmt.Errorf("read-only, we don't have the secret key, cannot sign") +} + +// GetPublicKey returns the public key associated with this signer. +func (ros ReadOnlySigner) GetPublicKey(context.Context) (string, error) { + return ros.pk, nil +}