summaryrefslogtreecommitdiffstats
path: root/nip04_test.go
diff options
context:
space:
mode:
authorClawd <ai@clawd.bot>2026-03-08 16:31:05 -0700
committerClawd <ai@clawd.bot>2026-03-08 16:31:45 -0700
commit0dcf191e7f63f35efe85afb60bcb57f4934a0acb (patch)
treeff9eac0c07389c0744401d07f033528082bab700 /nip04_test.go
parent97e6a3cfb67d18273fa88ce086b3db514c7e3083 (diff)
feat: add NIP-04 encrypt/decrypt methods on Key
Diffstat (limited to 'nip04_test.go')
-rw-r--r--nip04_test.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/nip04_test.go b/nip04_test.go
new file mode 100644
index 0000000..ff06151
--- /dev/null
+++ b/nip04_test.go
@@ -0,0 +1,49 @@
1package nostr
2
3import (
4 "testing"
5)
6
7func TestNIP04RoundTrip(t *testing.T) {
8 alice, err := GenerateKey()
9 if err != nil {
10 t.Fatal(err)
11 }
12 bob, err := GenerateKey()
13 if err != nil {
14 t.Fatal(err)
15 }
16
17 plaintext := "hello, private world!"
18
19 ct, err := alice.NIP04Encrypt(bob.Public(), plaintext)
20 if err != nil {
21 t.Fatalf("encrypt: %v", err)
22 }
23
24 got, err := bob.NIP04Decrypt(alice.Public(), ct)
25 if err != nil {
26 t.Fatalf("decrypt: %v", err)
27 }
28
29 if got != plaintext {
30 t.Fatalf("expected %q, got %q", plaintext, got)
31 }
32}
33
34func TestNIP04DifferentKeys(t *testing.T) {
35 alice, _ := GenerateKey()
36 bob, _ := GenerateKey()
37 eve, _ := GenerateKey()
38
39 ct, err := alice.NIP04Encrypt(bob.Public(), "secret")
40 if err != nil {
41 t.Fatal(err)
42 }
43
44 // Eve should not be able to decrypt
45 _, err = eve.NIP04Decrypt(alice.Public(), ct)
46 if err == nil {
47 t.Fatal("expected error decrypting with wrong key, got nil")
48 }
49}