| 1 | //go:build !race |
| 2 | |
| 3 | package records |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/hex" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "fiatjaf.com/nostr" |
| 12 | "fiatjaf.com/nostr/nip44" |
| 13 | "fiatjaf.com/nostr/nip59" |
| 14 | "github.com/FelineStateMachine/tinyrelay/internal/event" |
| 15 | "github.com/FelineStateMachine/tinyrelay/internal/policy" |
| 16 | "github.com/FelineStateMachine/tinyrelay/internal/storage" |
| 17 | ) |
| 18 | |
| 19 | func TestNotificationIsNIP17NIP59Unwrapable(t *testing.T) { |
| 20 | ctx := context.Background() |
| 21 | s, err := storage.Open(ctx, filepath.Join(t.TempDir(), "tenant.db" )) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | defer s.Close() |
| 26 | ownerSecret := "0101010101010101010101010101010101010101010101010101010101010101" |
| 27 | owner, err := event.PublicKey(ownerSecret) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | p := policy.Defaults(owner) |
| 32 | var got event.Event |
| 33 | r, err := New(ctx, Config{Community: emptyCommunityReader{}, Store: s, Policy: func() policy.Policy { return p }, RelayURL: "wss://relay" , OnGenerated: func(_ context.Context, e event.Event) error { got = e; return nil }}) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if err := r.Notify(ctx, "test" , "hello" , "subject" , owner, 100); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if got.Kind != event.KIND_WRAP { |
| 41 | t.Fatalf("want gift wrap, got %d" , got.Kind) |
| 42 | } |
| 43 | sk, err := nostr.SecretKeyFromHex(ownerSecret) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | sigBytes, _ := hex.DecodeString(got.Sig) |
| 48 | var sig [64]byte |
| 49 | copy(sig[:], sigBytes) |
| 50 | gw := nostr.Event{ID: nostr.MustIDFromHex(got.ID), PubKey: nostr.MustPubKeyFromHex(got.PubKey), CreatedAt: nostr.Timestamp(got.CreatedAt), Kind: nostr.Kind(got.Kind), Content: got.Content, Sig: sig} |
| 51 | for _, tag := range got.Tags { |
| 52 | gw.Tags = append(gw.Tags, nostr.Tag(tag)) |
| 53 | } |
| 54 | rumor, err := nip59.GiftUnwrap(gw, func(other nostr.PubKey, ciphertext string) (string, error) { |
| 55 | key, err := nip44.GenerateConversationKey(other, sk) |
| 56 | if err != nil { |
| 57 | return "" , err |
| 58 | } |
| 59 | return nip44.Decrypt(ciphertext, key) |
| 60 | }) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | if rumor.Kind != nostr.KindDirectMessage || rumor.Content != "hello" { |
| 65 | t.Fatalf("unexpected rumor: kind=%d content=%q" , rumor.Kind, rumor.Content) |
| 66 | } |
| 67 | } |