cipher_test.go raw
1 package cipher
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/getAlby/go-nostr"
8 "github.com/getAlby/hub/constants"
9
10 "github.com/stretchr/testify/assert"
11 )
12
13 func TestCipher(t *testing.T) {
14 doTestCipher(t, constants.ENCRYPTION_TYPE_NIP04)
15 doTestCipher(t, constants.ENCRYPTION_TYPE_NIP44_V2)
16 }
17
18 func doTestCipher(t *testing.T, encryption string) {
19 reqPrivateKey := nostr.GeneratePrivateKey()
20 reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
21
22 nip47Cipher, err := NewNip47Cipher(encryption, reqPubkey, reqPrivateKey)
23 assert.NoError(t, err)
24
25 payload := "test payload"
26 msg, err := nip47Cipher.Encrypt(payload)
27 assert.NoError(t, err)
28
29 decrypted, err := nip47Cipher.Decrypt(msg)
30 assert.Equal(t, payload, decrypted)
31 }
32
33 func TestCipher_UnsupportedEncrptions(t *testing.T) {
34 doTestCipher_UnsupportedEncrptions(t, "nip44")
35 doTestCipher_UnsupportedEncrptions(t, "nip44_v0")
36 doTestCipher_UnsupportedEncrptions(t, "nip44_v1")
37 doTestCipher_UnsupportedEncrptions(t, "nip44v2")
38 doTestCipher_UnsupportedEncrptions(t, "nip-44")
39 }
40
41 func doTestCipher_UnsupportedEncrptions(t *testing.T, encryption string) {
42 reqPrivateKey := nostr.GeneratePrivateKey()
43 reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
44
45 _, err = NewNip47Cipher(encryption, reqPubkey, reqPrivateKey)
46 assert.Error(t, err)
47 assert.Equal(t, fmt.Sprintf("invalid encryption: %s", encryption), err.Error())
48 }
49