mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-17 19:13:56 +02:00
error aware Keyer.GetPublicKey
This commit is contained in:
@@ -17,11 +17,14 @@ func NewBunkerSignerFromBunkerClient(bc *nip46.BunkerClient) BunkerSigner {
|
||||
return BunkerSigner{bc}
|
||||
}
|
||||
|
||||
func (bs BunkerSigner) GetPublicKey(ctx context.Context) string {
|
||||
func (bs BunkerSigner) GetPublicKey(ctx context.Context) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
|
||||
defer cancel()
|
||||
pk, _ := bs.bunker.GetPublicKey(ctx)
|
||||
return pk
|
||||
pk, err := bs.bunker.GetPublicKey(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
func (bs BunkerSigner) SignEvent(ctx context.Context, evt *nostr.Event) error {
|
||||
|
@@ -16,18 +16,21 @@ type EncryptedKeySigner struct {
|
||||
callback func(context.Context) string
|
||||
}
|
||||
|
||||
func (es *EncryptedKeySigner) GetPublicKey(ctx context.Context) string {
|
||||
func (es *EncryptedKeySigner) GetPublicKey(ctx context.Context) (string, error) {
|
||||
if es.pk != "" {
|
||||
return es.pk
|
||||
return es.pk, nil
|
||||
}
|
||||
password := es.callback(ctx)
|
||||
key, err := nip49.Decrypt(es.ncryptsec, password)
|
||||
if err != nil {
|
||||
return ""
|
||||
return "", err
|
||||
}
|
||||
pk, err := nostr.GetPublicKey(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pk, _ := nostr.GetPublicKey(key)
|
||||
es.pk = pk
|
||||
return pk
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
func (es *EncryptedKeySigner) SignEvent(ctx context.Context, evt *nostr.Event) error {
|
||||
|
@@ -22,7 +22,7 @@ type Keyer interface {
|
||||
|
||||
// A Signer provides basic public key signing methods.
|
||||
type Signer interface {
|
||||
GetPublicKey(context.Context) string
|
||||
GetPublicKey(context.Context) (string, error)
|
||||
SignEvent(context.Context, *nostr.Event) error
|
||||
}
|
||||
|
||||
|
@@ -10,7 +10,7 @@ import (
|
||||
// It can be used when an app for some reason wants to ask the user to manually provide a signed event
|
||||
// by copy-and-paste, for example.
|
||||
type ManualSigner struct {
|
||||
ManualGetPublicKey func(context.Context) string
|
||||
ManualGetPublicKey func(context.Context) (string, error)
|
||||
ManualSignEvent func(context.Context, *nostr.Event) error
|
||||
ManualEncrypt func(ctx context.Context, plaintext string, recipientPublicKey string) (base64ciphertext string, err error)
|
||||
ManualDecrypt func(ctx context.Context, base64ciphertext string, senderPublicKey string) (plaintext string, err error)
|
||||
@@ -20,7 +20,7 @@ func (ms ManualSigner) SignEvent(ctx context.Context, evt *nostr.Event) error {
|
||||
return ms.ManualSignEvent(ctx, evt)
|
||||
}
|
||||
|
||||
func (ms ManualSigner) GetPublicKey(ctx context.Context) string {
|
||||
func (ms ManualSigner) GetPublicKey(ctx context.Context) (string, error) {
|
||||
return ms.ManualGetPublicKey(ctx)
|
||||
}
|
||||
|
||||
|
@@ -16,13 +16,16 @@ type KeySigner struct {
|
||||
conversationKeys *xsync.MapOf[string, [32]byte]
|
||||
}
|
||||
|
||||
func NewPlainKeySigner(sec string) KeySigner {
|
||||
pk, _ := nostr.GetPublicKey(sec)
|
||||
return KeySigner{sec, pk, xsync.NewMapOf[string, [32]byte]()}
|
||||
func NewPlainKeySigner(sec string) (KeySigner, error) {
|
||||
pk, err := nostr.GetPublicKey(sec)
|
||||
if err != nil {
|
||||
return KeySigner{}, err
|
||||
}
|
||||
return KeySigner{sec, pk, xsync.NewMapOf[string, [32]byte]()}, nil
|
||||
}
|
||||
|
||||
func (ks KeySigner) SignEvent(ctx context.Context, evt *nostr.Event) error { return evt.Sign(ks.sk) }
|
||||
func (ks KeySigner) GetPublicKey(ctx context.Context) string { return ks.pk }
|
||||
func (ks KeySigner) GetPublicKey(ctx context.Context) (string, error) { return ks.pk, nil }
|
||||
|
||||
func (ks KeySigner) Encrypt(ctx context.Context, plaintext string, recipient string) (string, error) {
|
||||
ck, ok := ks.conversationKeys.Load(recipient)
|
||||
|
@@ -38,7 +38,10 @@ func PrepareMessage(
|
||||
recipientPubKey string,
|
||||
modify func(*nostr.Event),
|
||||
) (toUs nostr.Event, toThem nostr.Event, err error) {
|
||||
ourPubkey := kr.GetPublicKey(ctx)
|
||||
ourPubkey, err := kr.GetPublicKey(ctx)
|
||||
if err != nil {
|
||||
return nostr.Event{}, nostr.Event{}, err
|
||||
}
|
||||
|
||||
rumor := nostr.Event{
|
||||
Kind: 14,
|
||||
@@ -87,10 +90,16 @@ func ListenForMessages(
|
||||
go func() {
|
||||
defer close(ch)
|
||||
|
||||
pk, err := kr.GetPublicKey(ctx)
|
||||
if err != nil {
|
||||
nostr.InfoLogger.Printf("[nip17] failed to get public key from Keyer: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
for ie := range pool.SubMany(ctx, ourRelays, nostr.Filters{
|
||||
{
|
||||
Kinds: []int{1059},
|
||||
Tags: nostr.TagMap{"p": []string{kr.GetPublicKey(ctx)}},
|
||||
Tags: nostr.TagMap{"p": []string{pk}},
|
||||
Since: &since,
|
||||
},
|
||||
}) {
|
||||
|
Reference in New Issue
Block a user