2024-09-11 11:43:49 -03:00
|
|
|
package keyer
|
2024-09-10 22:37:48 -03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
)
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// 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
|
|
|
|
// app wants to implement custom signing logic.
|
2024-09-10 22:37:48 -03:00
|
|
|
type ManualSigner struct {
|
2025-03-04 11:08:31 -03:00
|
|
|
// ManualGetPublicKey is called when the public key is needed
|
2024-09-19 17:47:18 +09:00
|
|
|
ManualGetPublicKey func(context.Context) (string, error)
|
2025-03-04 11:08:31 -03:00
|
|
|
|
|
|
|
// ManualSignEvent is called when an event needs to be signed
|
|
|
|
ManualSignEvent func(context.Context, *nostr.Event) error
|
|
|
|
|
|
|
|
// ManualEncrypt is called when a message needs to be encrypted
|
|
|
|
ManualEncrypt func(ctx context.Context, plaintext string, recipientPublicKey string) (base64ciphertext string, err error)
|
|
|
|
|
|
|
|
// ManualDecrypt is called when a message needs to be decrypted
|
|
|
|
ManualDecrypt func(ctx context.Context, base64ciphertext string, senderPublicKey string) (plaintext string, err error)
|
2024-09-10 22:37:48 -03:00
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// SignEvent delegates event signing to the ManualSignEvent function.
|
2024-09-10 22:37:48 -03:00
|
|
|
func (ms ManualSigner) SignEvent(ctx context.Context, evt *nostr.Event) error {
|
|
|
|
return ms.ManualSignEvent(ctx, evt)
|
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// GetPublicKey delegates public key retrieval to the ManualGetPublicKey function.
|
2024-09-19 17:47:18 +09:00
|
|
|
func (ms ManualSigner) GetPublicKey(ctx context.Context) (string, error) {
|
2024-09-10 22:37:48 -03:00
|
|
|
return ms.ManualGetPublicKey(ctx)
|
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// Encrypt delegates encryption to the ManualEncrypt function.
|
2024-09-10 22:37:48 -03:00
|
|
|
func (ms ManualSigner) Encrypt(ctx context.Context, plaintext string, recipient string) (c64 string, err error) {
|
|
|
|
return ms.ManualEncrypt(ctx, plaintext, recipient)
|
|
|
|
}
|
|
|
|
|
2025-03-04 11:08:31 -03:00
|
|
|
// Decrypt delegates decryption to the ManualDecrypt function.
|
2024-09-10 22:37:48 -03:00
|
|
|
func (ms ManualSigner) Decrypt(ctx context.Context, base64ciphertext string, sender string) (plaintext string, err error) {
|
|
|
|
return ms.ManualDecrypt(ctx, base64ciphertext, sender)
|
|
|
|
}
|