Skip to content

Commit

Permalink
error aware Keyer.GetPublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
1l0 authored and fiatjaf committed Sep 19, 2024
1 parent e1cdb71 commit 0b2b695
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
9 changes: 6 additions & 3 deletions keyer/bunker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions keyer/encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion keyer/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions keyer/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
11 changes: 7 additions & 4 deletions keyer/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions nip17/nip17.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
}) {
Expand Down

0 comments on commit 0b2b695

Please sign in to comment.