-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtest_key.py
99 lines (68 loc) · 3.09 KB
/
test_key.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from nostr.event import Event, EncryptedDirectMessage
from nostr.key import PrivateKey
def test_eq_true():
""" __eq__ should return True when PrivateKeys are equal """
pk1 = PrivateKey()
pk2 = PrivateKey(pk1.raw_secret)
assert pk1 == pk2
def test_eq_false():
""" __eq__ should return False when PrivateKeys are not equal """
pk1 = PrivateKey()
pk2 = PrivateKey()
assert pk1.raw_secret != pk2.raw_secret
assert pk1 != pk2
def test_from_nsec():
""" PrivateKey.from_nsec should yield the source's raw_secret """
pk1 = PrivateKey()
pk2 = PrivateKey.from_nsec(pk1.bech32())
assert pk1.raw_secret == pk2.raw_secret
class TestEvent:
def setup_class(self):
self.sender_pk = PrivateKey()
self.sender_pubkey = self.sender_pk.public_key.hex()
def test_sign_event_is_valid(self):
""" sign should create a signature that can be verified against Event.id """
event = Event(content="Hello, world!")
self.sender_pk.sign_event(event)
assert event.verify()
def test_sign_event_adds_pubkey(self):
""" sign should add the sender's pubkey if not already specified """
event = Event(content="Hello, world!")
# The event's public_key hasn't been specified yet
assert event.public_key is None
self.sender_pk.sign_event(event)
# PrivateKey.sign() should have populated public_key
assert event.public_key == self.sender_pubkey
class TestEncryptedDirectMessage:
def setup_class(self):
self.sender_pk = PrivateKey()
self.sender_pubkey = self.sender_pk.public_key.hex()
self.recipient_pk = PrivateKey()
self.recipient_pubkey = self.recipient_pk.public_key.hex()
def test_encrypt_dm(self):
""" Should encrypt a DM and populate its `content` field with ciphertext that either party can decrypt """
message = "My secret message!"
dm = EncryptedDirectMessage(
recipient_pubkey=self.recipient_pubkey,
cleartext_content=message,
)
# DM's content field should be initially blank
assert dm.content is None
self.sender_pk.encrypt_dm(dm)
# After encrypting, the content field should now be populated
assert dm.content is not None
# Sender should be able to decrypt
decrypted_message = self.sender_pk.decrypt_message(encoded_message=dm.content, public_key_hex=self.recipient_pubkey)
assert decrypted_message == message
# Recipient should be able to decrypt by referencing the sender's pubkey
decrypted_message = self.recipient_pk.decrypt_message(encoded_message=dm.content, public_key_hex=self.sender_pubkey)
assert decrypted_message == message
def test_sign_encrypts_dm(self):
""" `sign` should encrypt a DM that hasn't been encrypted yet """
dm = EncryptedDirectMessage(
recipient_pubkey=self.recipient_pubkey,
cleartext_content="Some DM message",
)
assert dm.content is None
self.sender_pk.sign_event(dm)
assert dm.content is not None