diff options
Diffstat (limited to 'nip04_test.go')
| -rw-r--r-- | nip04_test.go | 49 |
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 @@ | |||
| 1 | package nostr | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "testing" | ||
| 5 | ) | ||
| 6 | |||
| 7 | func 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 | |||
| 34 | func 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 | } | ||
