Skip to content

Commit

Permalink
nip46: client to support decrypting with nip44 together with nip04.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 26, 2024
1 parent d439989 commit 6445b3b
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions nip46/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/mailru/easyjson"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip04"
"github.com/nbd-wtf/go-nostr/nip44"
"github.com/puzpuzpuz/xsync/v3"
)

Expand All @@ -21,7 +22,8 @@ type BunkerClient struct {
pool *nostr.SimplePool
target string
relays []string
sharedSecret []byte
sharedSecret []byte // nip04
conversationKey [32]byte // nip44
listeners *xsync.MapOf[string, chan Response]
expectingAuth *xsync.MapOf[string, struct{}]
idPrefix string
Expand Down Expand Up @@ -94,13 +96,15 @@ func NewBunker(

clientPublicKey, _ := nostr.GetPublicKey(clientSecretKey)
sharedSecret, _ := nip04.ComputeSharedSecret(targetPublicKey, clientSecretKey)
conversationKey, _ := nip44.GenerateConversationKey(targetPublicKey, clientSecretKey)

bunker := &BunkerClient{
pool: pool,
clientSecretKey: clientSecretKey,
target: targetPublicKey,
relays: relays,
sharedSecret: sharedSecret,
conversationKey: conversationKey,
listeners: xsync.NewMapOf[string, chan Response](),
expectingAuth: xsync.NewMapOf[string, struct{}](),
onAuth: onAuth,
Expand All @@ -123,9 +127,12 @@ func NewBunker(
}

var resp Response
plain, err := nip04.Decrypt(ie.Content, sharedSecret)
plain, err := nip44.Decrypt(ie.Content, conversationKey)
if err != nil {
continue
plain, err = nip04.Decrypt(ie.Content, sharedSecret)
if err != nil {
continue
}
}

err = json.Unmarshal([]byte(plain), &resp)
Expand Down

0 comments on commit 6445b3b

Please sign in to comment.