a User interface that is like a Signer but can't sign.

This commit is contained in:
fiatjaf 2025-03-10 02:25:57 -03:00
parent fa17e4003f
commit 94e8b6790a
6 changed files with 37 additions and 5 deletions

View File

@ -15,15 +15,20 @@ type Keyer interface {
Cipher
}
// Signer is an interface for signing events.
// User is an entity that has a public key (although they can't sign anything).
type User interface {
// GetPublicKey returns the public key associated with this user.
GetPublicKey(ctx context.Context) (string, error)
}
// Signer is a User that can also sign events.
type Signer interface {
User
// SignEvent signs the provided event, setting its ID, PubKey, and Sig fields.
// The context can be used for operations that may require user interaction or
// network access, such as with remote signers.
SignEvent(ctx context.Context, evt *Event) error
// GetPublicKey returns the public key associated with this signer.
GetPublicKey(ctx context.Context) (string, error)
}
// Cipher is an interface for encrypting and decrypting messages with NIP-44

View File

@ -9,6 +9,8 @@ import (
"github.com/nbd-wtf/go-nostr/nip46"
)
var _ nostr.Keyer = (*BunkerSigner)(nil)
// BunkerSigner is a signer that delegates operations to a remote bunker using NIP-46.
// It communicates with the bunker for all cryptographic operations rather than
// handling the private key locally.

View File

@ -9,6 +9,8 @@ import (
"github.com/nbd-wtf/go-nostr/nip49"
)
var _ nostr.Keyer = (*EncryptedKeySigner)(nil)
// EncryptedKeySigner is a signer that must ask the user for a password before every operation.
// It stores the private key in encrypted form (NIP-49) and uses a callback to request the password
// when needed for operations.

View File

@ -6,6 +6,8 @@ import (
"github.com/nbd-wtf/go-nostr"
)
var _ nostr.Keyer = (*ManualSigner)(nil)
// ManualSigner is a signer that delegates all operations to user-provided functions.
// It can be used when an app wants to ask the user or some custom server to manually provide a
// signed event or an encrypted or decrypted payload by copy-and-paste, for example, or when the

View File

@ -8,6 +8,8 @@ import (
"github.com/puzpuzpuz/xsync/v3"
)
var _ nostr.Keyer = (*KeySigner)(nil)
// KeySigner is a signer that holds the private key in memory
type KeySigner struct {
sk string

View File

@ -7,7 +7,26 @@ import (
"github.com/nbd-wtf/go-nostr"
)
// ReadOnlySigner is a Signer that holds a public key in memory and cannot sign anything
var (
_ nostr.User = (*ReadOnlyUser)(nil)
_ nostr.Signer = (*ReadOnlySigner)(nil)
)
// ReadOnlyUser is a nostr.User that has this public key
type ReadOnlyUser struct {
pk string
}
func NewReadOnlyUser(pk string) ReadOnlyUser {
return ReadOnlyUser{pk}
}
// GetPublicKey returns the public key associated with this signer.
func (ros ReadOnlyUser) GetPublicKey(context.Context) (string, error) {
return ros.pk, nil
}
// ReadOnlySigner is like a ReadOnlyUser, but has a fake GetPublicKey method that doesn't work.
type ReadOnlySigner struct {
pk string
}