keyer, nip17, nip44, nip59: this time is different!

This commit is contained in:
fiatjaf
2024-09-15 11:06:42 -03:00
parent f976296e01
commit db023e12e9
5 changed files with 67 additions and 55 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/nbd-wtf/go-nostr/nip46"
"github.com/nbd-wtf/go-nostr/nip49"
"github.com/puzpuzpuz/xsync/v3"
)
type Keyer interface {
@@ -61,7 +62,7 @@ func New(ctx context.Context, pool *nostr.SimplePool, input string, opts *Signer
return nil, fmt.Errorf("failed to decrypt with given password: %w", err)
}
pk, _ := nostr.GetPublicKey(sec)
return KeySigner{sec, pk, make(map[string][32]byte)}, nil
return KeySigner{sec, pk, xsync.NewMapOf[string, [32]byte]()}, nil
} else if nip46.IsValidBunkerURL(input) || nip05.IsValidIdentifier(input) {
bcsk := nostr.GeneratePrivateKey()
oa := func(url string) { println("auth_url received but not handled") }
@@ -81,11 +82,11 @@ func New(ctx context.Context, pool *nostr.SimplePool, input string, opts *Signer
} else if prefix, parsed, err := nip19.Decode(input); err == nil && prefix == "nsec" {
sec := parsed.(string)
pk, _ := nostr.GetPublicKey(sec)
return KeySigner{sec, pk, make(map[string][32]byte)}, nil
return KeySigner{sec, pk, xsync.NewMapOf[string, [32]byte]()}, nil
} else if _, err := hex.DecodeString(input); err == nil && len(input) < 64 {
input = strings.Repeat("0", 64-len(input)) + input // if the key is like '01', fill all the left zeroes
pk, _ := nostr.GetPublicKey(input)
return KeySigner{input, pk, make(map[string][32]byte)}, nil
return KeySigner{input, pk, xsync.NewMapOf[string, [32]byte]()}, nil
}
return nil, fmt.Errorf("unsupported input '%s'", input)

View File

@@ -5,6 +5,7 @@ import (
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip44"
"github.com/puzpuzpuz/xsync/v3"
)
// Keysigner is a signer that holds the private key in memory and can do all the operations instantly and easily.
@@ -12,34 +13,34 @@ type KeySigner struct {
sk string
pk string
conversationKeys map[string][32]byte
conversationKeys *xsync.MapOf[string, [32]byte]
}
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) Encrypt(ctx context.Context, plaintext string, recipient string) (string, error) {
ck, ok := ks.conversationKeys[recipient]
ck, ok := ks.conversationKeys.Load(recipient)
if !ok {
var err error
ck, err = nip44.GenerateConversationKey(recipient, ks.sk)
if err != nil {
return "", err
}
ks.conversationKeys[recipient] = ck
ks.conversationKeys.Store(recipient, ck)
}
return nip44.Encrypt(plaintext, ck)
}
func (ks KeySigner) Decrypt(ctx context.Context, base64ciphertext string, sender string) (string, error) {
ck, ok := ks.conversationKeys[sender]
ck, ok := ks.conversationKeys.Load(sender)
if !ok {
var err error
ck, err = nip44.GenerateConversationKey(sender, ks.sk)
if err != nil {
return "", err
}
ks.conversationKeys[sender] = ck
ks.conversationKeys.Store(sender, ck)
}
return nip44.Decrypt(base64ciphertext, ck)
}