nip46: client to support decrypting with nip44 together with nip04.

This commit is contained in:
fiatjaf 2024-10-26 19:37:36 -03:00
parent d4399892ee
commit 6445b3bfe6

View File

@ -12,6 +12,7 @@ import (
"github.com/mailru/easyjson" "github.com/mailru/easyjson"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip04" "github.com/nbd-wtf/go-nostr/nip04"
"github.com/nbd-wtf/go-nostr/nip44"
"github.com/puzpuzpuz/xsync/v3" "github.com/puzpuzpuz/xsync/v3"
) )
@ -21,7 +22,8 @@ type BunkerClient struct {
pool *nostr.SimplePool pool *nostr.SimplePool
target string target string
relays []string relays []string
sharedSecret []byte sharedSecret []byte // nip04
conversationKey [32]byte // nip44
listeners *xsync.MapOf[string, chan Response] listeners *xsync.MapOf[string, chan Response]
expectingAuth *xsync.MapOf[string, struct{}] expectingAuth *xsync.MapOf[string, struct{}]
idPrefix string idPrefix string
@ -94,6 +96,7 @@ func NewBunker(
clientPublicKey, _ := nostr.GetPublicKey(clientSecretKey) clientPublicKey, _ := nostr.GetPublicKey(clientSecretKey)
sharedSecret, _ := nip04.ComputeSharedSecret(targetPublicKey, clientSecretKey) sharedSecret, _ := nip04.ComputeSharedSecret(targetPublicKey, clientSecretKey)
conversationKey, _ := nip44.GenerateConversationKey(targetPublicKey, clientSecretKey)
bunker := &BunkerClient{ bunker := &BunkerClient{
pool: pool, pool: pool,
@ -101,6 +104,7 @@ func NewBunker(
target: targetPublicKey, target: targetPublicKey,
relays: relays, relays: relays,
sharedSecret: sharedSecret, sharedSecret: sharedSecret,
conversationKey: conversationKey,
listeners: xsync.NewMapOf[string, chan Response](), listeners: xsync.NewMapOf[string, chan Response](),
expectingAuth: xsync.NewMapOf[string, struct{}](), expectingAuth: xsync.NewMapOf[string, struct{}](),
onAuth: onAuth, onAuth: onAuth,
@ -123,9 +127,12 @@ func NewBunker(
} }
var resp Response var resp Response
plain, err := nip04.Decrypt(ie.Content, sharedSecret) plain, err := nip44.Decrypt(ie.Content, conversationKey)
if err != nil { if err != nil {
continue plain, err = nip04.Decrypt(ie.Content, sharedSecret)
if err != nil {
continue
}
} }
err = json.Unmarshal([]byte(plain), &resp) err = json.Unmarshal([]byte(plain), &resp)