summaryrefslogtreecommitdiffstats
path: root/nip04_test.go
blob: ff061517b3a1d82b37cce776626c83a9ce7d5844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package nostr

import (
	"testing"
)

func TestNIP04RoundTrip(t *testing.T) {
	alice, err := GenerateKey()
	if err != nil {
		t.Fatal(err)
	}
	bob, err := GenerateKey()
	if err != nil {
		t.Fatal(err)
	}

	plaintext := "hello, private world!"

	ct, err := alice.NIP04Encrypt(bob.Public(), plaintext)
	if err != nil {
		t.Fatalf("encrypt: %v", err)
	}

	got, err := bob.NIP04Decrypt(alice.Public(), ct)
	if err != nil {
		t.Fatalf("decrypt: %v", err)
	}

	if got != plaintext {
		t.Fatalf("expected %q, got %q", plaintext, got)
	}
}

func TestNIP04DifferentKeys(t *testing.T) {
	alice, _ := GenerateKey()
	bob, _ := GenerateKey()
	eve, _ := GenerateKey()

	ct, err := alice.NIP04Encrypt(bob.Public(), "secret")
	if err != nil {
		t.Fatal(err)
	}

	// Eve should not be able to decrypt
	_, err = eve.NIP04Decrypt(alice.Public(), ct)
	if err == nil {
		t.Fatal("expected error decrypting with wrong key, got nil")
	}
}