nostr-1.3.0.0: test/Test/Nostr/Nip04Spec.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
module Test.Nostr.Nip04Spec (spec) where
import Test.Hspec
import Nostr.Nip04
import Nostr.Crypto
import qualified Data.Text as T
spec :: Spec
spec = do
describe "Nostr.Nip04" $ do
it "encrypts and decrypts a message" $ do
(sec1, pub1) <- generateKeyPair
(sec2, pub2) <- generateKeyPair
let message = "Hello, secret Nostr!"
-- User 1 encrypts message for User 2
encryptedOpt <- encryptNip04 sec1 pub2 message
encryptedOpt `shouldSatisfy` (\case Just _ -> True; _ -> False)
let Just encrypted = encryptedOpt
-- User 2 decrypts message from User 1
let decryptedOpt = decryptNip04 sec2 pub1 encrypted
decryptedOpt `shouldBe` Just message
it "fails to decrypt with wrong key" $ do
(sec1, pub1) <- generateKeyPair
(sec2, pub2) <- generateKeyPair
(sec3, pub3) <- generateKeyPair -- malicious user
let message = "Secret"
Just encrypted <- encryptNip04 sec1 pub2 message
let decryptedOpt = decryptNip04 sec3 pub1 encrypted
decryptedOpt `shouldBe` Nothing