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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
use super::{
EventDelegation, EventKind, Id, Metadata, PrivateKey, PublicKey, PublicKeyHex, RelayUrl,
Signature, Tag, Unixtime,
};
use crate::Error;
use base64::Engine;
use k256::sha2::{Digest, Sha256};
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::thread;
use std::thread::JoinHandle;
/// The main event type
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Event {
/// The Id of the event, generated as a SHA256 of the inner event data
pub id: Id,
/// The public key of the actor who created the event
pub pubkey: PublicKey,
/// The (unverified) time at which the event was created
pub created_at: Unixtime,
/// The kind of event
pub kind: EventKind,
/// A set of tags that apply to the event
pub tags: Vec<Tag>,
/// The content of the event
pub content: String,
/// An optional verified time for the event (using OpenTimestamp)
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub ots: Option<String>,
/// The signature of the event, which cryptographically verifies that the holder of
/// the PrivateKey matching the event's PublicKey generated (or authorized) this event.
/// The signature is taken over the id field only, but the id field is taken over
/// the rest of the event data.
pub sig: Signature,
}
macro_rules! serialize_inner_event {
($pubkey:expr, $created_at:expr, $kind:expr, $tags:expr,
$content:expr) => {{
format!(
"[0,{},{},{},{},{}]",
serde_json::to_string($pubkey)?,
serde_json::to_string($created_at)?,
serde_json::to_string($kind)?,
serde_json::to_string($tags)?,
serde_json::to_string($content)?
)
}};
}
/// Data used to construct an event
#[derive(Clone, Debug)]
pub struct PreEvent {
/// The public key of the actor who is creating the event
pub pubkey: PublicKey,
/// The time at which the event was created
pub created_at: Unixtime,
/// The kind of event
pub kind: EventKind,
/// A set of tags that apply to the event
pub tags: Vec<Tag>,
/// The content of the event
pub content: String,
/// An optional verified time for the event (using OpenTimestamp)
pub ots: Option<String>,
}
impl PreEvent {
/// Create a NIP-04 EncryptedDirectMessage PreEvent.
///
/// Note that this creates the 'p' tag, but does not add a recommended_relay_url to it,
/// so the caller should handle that.
pub fn new_nip04(
private_key: &PrivateKey,
recipient_public_key: PublicKey,
message: &str,
) -> Result<PreEvent, Error> {
let input: &[u8] = message.as_bytes();
let (iv, ciphertext) = private_key.nip04_encrypt(&recipient_public_key, input)?;
let content = format!(
"{}?iv={}",
base64::engine::general_purpose::STANDARD.encode(ciphertext),
base64::engine::general_purpose::STANDARD.encode(iv)
);
Ok(PreEvent {
pubkey: private_key.public_key(),
created_at: Unixtime::now().unwrap(),
kind: EventKind::EncryptedDirectMessage,
tags: vec![Tag::Pubkey {
pubkey: recipient_public_key.into(),
recommended_relay_url: None, // FIXME,
petname: None,
}],
content,
ots: None,
})
}
}
impl Event {
fn hash(input: &PreEvent) -> Result<Id, Error> {
let serialized: String = serialize_inner_event!(
&input.pubkey,
&input.created_at,
&input.kind,
&input.tags,
&input.content
);
// Hash
let mut hasher = Sha256::new();
hasher.update(serialized.as_bytes());
let id = hasher.finalize();
let id: [u8; 32] = id.into();
Ok(Id(id))
}
/// Create a new event
pub fn new(input: PreEvent, privkey: &PrivateKey) -> Result<Event, Error> {
// Generate Id
let id = Self::hash(&input)?;
// Generate Signature
let signature = privkey.sign_id(id)?;
Ok(Event {
id,
pubkey: input.pubkey,
created_at: input.created_at,
kind: input.kind,
tags: input.tags,
content: input.content,
ots: input.ots,
sig: signature,
})
}
/// Create a new event with proof of work.
///
/// This can take a long time, and is only cancellable by killing the thread.
pub fn new_with_pow(
mut input: PreEvent,
privkey: &PrivateKey,
zero_bits: u8,
) -> Result<Event, Error> {
let target = Some(format!("{}", zero_bits));
// Strip any pre-existing nonce tags
input.tags.retain(|t| !matches!(t, Tag::Nonce { .. }));
// Add nonce tag to the end
input.tags.push(Tag::Nonce {
nonce: "0".to_string(),
target: target.clone(),
});
let index = input.tags.len() - 1;
let cores = num_cpus::get();
let quitting = Arc::new(AtomicBool::new(false));
let nonce = Arc::new(AtomicU64::new(0)); // will store the nonce that works
let mut join_handles: Vec<JoinHandle<_>> = Vec::with_capacity(cores);
for core in 0..cores {
let mut attempt: u64 = core as u64 * (u64::MAX / cores as u64);
let mut input = input.clone();
let target = target.clone();
let index = index;
let quitting = quitting.clone();
let nonce = nonce.clone();
let zero_bits = zero_bits;
let join_handle = thread::spawn(move || {
loop {
if quitting.load(Ordering::Relaxed) {
break;
}
input.tags[index] = Tag::Nonce {
nonce: format!("{}", attempt),
target: target.clone(),
};
let id = Self::hash(&input).unwrap();
if get_leading_zero_bits(&id.0) >= zero_bits {
nonce.store(attempt, Ordering::Relaxed);
quitting.store(true, Ordering::Relaxed);
break;
}
attempt += 1;
// We don't update created_at, which is a bit tricky to synchronize.
}
});
join_handles.push(join_handle);
}
for joinhandle in join_handles {
let _ = joinhandle.join();
}
// We found the nonce. Do it for reals
input.tags[index] = Tag::Nonce {
nonce: format!("{}", nonce.load(Ordering::Relaxed)),
target,
};
let id = Self::hash(&input).unwrap();
// Signature
let signature = privkey.sign_id(id)?;
Ok(Event {
id,
pubkey: input.pubkey,
created_at: input.created_at,
kind: input.kind,
tags: input.tags,
content: input.content,
ots: input.ots,
sig: signature,
})
}
/// Check the validity of an event. This is useful if you deserialize an event
/// from the network. If you create an event using new() it should already be
/// trustworthy.
pub fn verify(&self, maxtime: Option<Unixtime>) -> Result<(), Error> {
use k256::schnorr::signature::Verifier;
let serialized: String = serialize_inner_event!(
&self.pubkey,
&self.created_at,
&self.kind,
&self.tags,
&self.content
);
// Verify the signature
self.pubkey.0.verify(serialized.as_bytes(), &self.sig.0)?;
// Also verify the ID is the SHA256
// (the above verify function also does it internally,
// so there is room for improvement here)
let mut hasher = Sha256::new();
hasher.update(serialized.as_bytes());
let id = hasher.finalize();
// Optional verify that the message was in the past
if let Some(mt) = maxtime {
if self.created_at > mt {
return Err(Error::EventInFuture);
}
}
if *id != self.id.0 {
Err(Error::HashMismatch)
} else {
Ok(())
}
}
// Mock data for testing
#[allow(dead_code)]
pub(crate) fn mock() -> Event {
let private_key = PrivateKey::mock();
let public_key = private_key.public_key();
let pre = PreEvent {
pubkey: public_key,
created_at: Unixtime::mock(),
kind: EventKind::mock(),
tags: vec![Tag::mock(), Tag::mock()],
content: "This is a test".to_string(),
ots: None,
};
Event::new(pre, &private_key).unwrap()
}
/// Create an event that sets Metadata
pub fn new_set_metadata(
mut input: PreEvent,
privkey: &PrivateKey,
metadata: Metadata,
) -> Result<Event, Error> {
input.kind = EventKind::Metadata;
input.content = serde_json::to_string(&metadata)?;
Event::new(input, privkey)
}
/// Create a ZapRequest event
/// These events are not published to nostr, they are sent to a lnurl.
pub fn new_zap_request(
privkey: &PrivateKey,
recipient_pubkey: PublicKeyHex,
zapped_event: Option<Id>,
millisatoshis: u64,
relays: Vec<String>,
content: String,
) -> Result<Event, Error> {
let mut pre_event = PreEvent {
pubkey: privkey.public_key(),
created_at: Unixtime::now().unwrap(),
kind: EventKind::ZapRequest,
tags: vec![
Tag::Pubkey {
pubkey: recipient_pubkey,
recommended_relay_url: None,
petname: None,
},
Tag::Other {
tag: "relays".to_owned(),
data: relays,
},
Tag::Other {
tag: "amount".to_owned(),
data: vec![format!("{}", millisatoshis)],
},
],
content,
ots: None,
};
if let Some(ze) = zapped_event {
pre_event.tags.push(Tag::Event {
id: ze,
recommended_relay_url: None,
marker: None,
});
}
Event::new(pre_event, privkey)
}
/// If an event is an EncryptedDirectMessage, decrypt it's contents
pub fn decrypted_contents(&self, private_key: &PrivateKey) -> Result<String, Error> {
if self.kind != EventKind::EncryptedDirectMessage {
return Err(Error::WrongEventKind);
}
let parts: Vec<&str> = self.content.split("?iv=").collect();
if parts.len() != 2 {
return Err(Error::BadEncryptedMessage);
}
let ciphertext: Vec<u8> = base64::engine::general_purpose::STANDARD.decode(parts[0])?;
let iv_vec: Vec<u8> = base64::engine::general_purpose::STANDARD.decode(parts[1])?;
let iv: [u8; 16] = iv_vec.try_into().unwrap();
let decrypted_bytes = private_key.nip04_decrypt(&self.pubkey, &ciphertext, iv)?;
let s: String = String::from_utf8_lossy(&decrypted_bytes).into();
Ok(s)
}
/// If the event refers to people, get all the PublicKeys it refers to
/// along with recommended relay URL and petname for each
pub fn people(&self) -> Vec<(PublicKeyHex, Option<RelayUrl>, Option<String>)> {
let mut output: Vec<(PublicKeyHex, Option<RelayUrl>, Option<String>)> = Vec::new();
// All 'p' tags
for tag in self.tags.iter() {
if let Tag::Pubkey {
pubkey,
recommended_relay_url,
petname,
} = tag
{
output.push((
pubkey.to_owned(),
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
petname.to_owned(),
));
}
}
output
}
/// If the event refers to people, get all the PublicKeys it refers to
/// along with recommended relay URL and petname for each, but only if they
/// are referenced within the note.
pub fn referenced_people(&self) -> Vec<(PublicKeyHex, Option<RelayUrl>, Option<String>)> {
let mut output: Vec<(PublicKeyHex, Option<RelayUrl>, Option<String>)> = Vec::new();
for (n, tag) in self.tags.iter().enumerate() {
if let Tag::Pubkey {
pubkey,
recommended_relay_url,
petname,
} = tag
{
if self.content.contains(&format!("#[{}]", n)) {
output.push((
pubkey.to_owned(),
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
petname.to_owned(),
));
}
}
}
output
}
/// Is the event a reply?
#[deprecated(since = "0.2.0", note = "please use `replies_to` instead")]
pub fn is_reply(&self) -> bool {
if self.kind != EventKind::TextNote {
return false;
}
for tag in self.tags.iter() {
if let Tag::Event { .. } = tag {
return true;
}
}
false
}
/// If this event replies to another, get that other event's Id along with
/// an optional recommended_relay_url
pub fn replies_to(&self) -> Option<(Id, Option<RelayUrl>)> {
// must be a text note
if self.kind != EventKind::TextNote {
return None;
}
// If there are no 'e' tags, then none
let num_e_tags = self
.tags
.iter()
.filter(|e| matches!(e, Tag::Event { .. }))
.count();
if num_e_tags == 0 {
return None;
}
// look for an 'e' tag with marker 'reply'
for tag in self.tags.iter() {
if let Tag::Event {
id,
recommended_relay_url,
marker,
} = tag
{
if marker.is_some() && marker.as_deref().unwrap() == "reply" {
return Some((
*id,
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
}
}
// look for an 'e' tag with marker 'root'
for tag in self.tags.iter() {
if let Tag::Event {
id,
recommended_relay_url,
marker,
} = tag
{
if marker.is_some() && marker.as_deref().unwrap() == "root" {
return Some((
*id,
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
}
}
// Use the last 'e' tag if unmarked
if let Some(Tag::Event {
id,
recommended_relay_url,
marker,
}) = self
.tags
.iter()
.rev()
.find(|t| matches!(t, Tag::Event { .. }))
{
if marker.is_none() {
return Some((
*id,
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
}
// Otherwise there are 'e' tags but they have unrecognized markings
// so we will not consider them as replies.
None
}
/// If this event replies to a thread, get that threads root event Id if
/// available, along with an optional recommended_relay_url
pub fn replies_to_root(&self) -> Option<(Id, Option<RelayUrl>)> {
if self.kind != EventKind::TextNote {
return None;
}
// look for an 'e' tag with marker 'root'
for tag in self.tags.iter() {
if let Tag::Event {
id,
recommended_relay_url,
marker,
} = tag
{
if marker.is_some() && marker.as_deref().unwrap() == "root" {
return Some((
*id,
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
}
}
let num_e_tags = self
.tags
.iter()
.filter(|e| matches!(e, Tag::Event { .. }))
.count();
if num_e_tags < 2 {
return None;
}
// otherwise use the first 'e' tag if unmarked
if let Some(Tag::Event {
id,
recommended_relay_url,
marker,
}) = self.tags.iter().find(|t| matches!(t, Tag::Event { .. }))
{
if marker.is_none() {
return Some((
*id,
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
}
None
}
/// If this event replies to a thread, get all ancestors in that thread.
/// This also gets all mentioned events.
pub fn replies_to_ancestors(&self) -> Vec<(Id, Option<RelayUrl>)> {
// must be a text note
if self.kind != EventKind::TextNote {
return vec![];
}
let mut output: Vec<(Id, Option<RelayUrl>)> = Vec::new();
for tag in self.tags.iter() {
if let Tag::Event {
id,
recommended_relay_url,
marker: _,
} = tag
{
output.push((
*id,
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
}
output
}
/// If this event mentions others, get those other event Ids
/// and optional recommended relay Urls
pub fn mentions(&self) -> Vec<(Id, Option<RelayUrl>)> {
// must be a text note
if self.kind != EventKind::TextNote {
return vec![];
}
let mut output: Vec<(Id, Option<RelayUrl>)> = Vec::new();
// Collect every 'e' tag marked as 'mention'
for tag in self.tags.iter() {
if let Tag::Event {
id,
recommended_relay_url,
marker,
} = tag
{
if marker.is_some() && marker.as_deref().unwrap() == "mention" {
output.push((
*id,
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
}
}
// Collect every unmarked 'e' tag that is not the first or last
let e_tags: Vec<&Tag> = self
.tags
.iter()
.filter(|e| matches!(e, Tag::Event { .. }))
.collect();
if e_tags.len() > 2 {
// mentions are everything other than first and last
for tag in &e_tags[1..e_tags.len() - 1] {
if let Tag::Event {
id,
recommended_relay_url,
marker,
} = tag
{
if marker.is_none() {
output.push((
*id,
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
}
}
}
output
}
/// If this event reacts to another, get that other event's Id,
/// the reaction content, and an optional Recommended relay Url
pub fn reacts_to(&self) -> Option<(Id, String, Option<RelayUrl>)> {
if self.kind != EventKind::Reaction {
return None;
}
// The last 'e' tag is it
if let Some(Tag::Event {
id,
recommended_relay_url,
marker: _,
}) = self
.tags
.iter()
.rev()
.find(|t| matches!(t, Tag::Event { .. }))
{
return Some((
*id,
self.content.clone(),
recommended_relay_url
.as_ref()
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok()),
));
}
None
}
/// If this event deletes others, get all the Ids of the events that it deletes
/// along with the reason for the deletion
pub fn deletes(&self) -> Option<(Vec<Id>, String)> {
if self.kind != EventKind::EventDeletion {
return None;
}
let mut ids: Vec<Id> = Vec::new();
// All 'e' tags are deleted
for tag in self.tags.iter() {
if let Tag::Event {
id,
recommended_relay_url: _,
marker: _,
} = tag
{
ids.push(*id);
}
}
if ids.is_empty() {
None
} else {
Some((ids, self.content.clone()))
}
}
/// If this event specifies the client that created it, return that client string
pub fn client(&self) -> Option<String> {
for tag in self.tags.iter() {
if let Tag::Other { tag, data } = tag {
if tag == "client" && !data.is_empty() {
return Some(data[0].clone());
}
}
}
None
}
/// If this event specifies a subject, return that subject string
pub fn subject(&self) -> Option<String> {
for tag in self.tags.iter() {
if let Tag::Subject(sub) = tag {
return Some(sub.clone());
}
}
None
}
/// If this event specifies a content warning, return that subject string
pub fn content_warning(&self) -> Option<String> {
for tag in self.tags.iter() {
if let Tag::ContentWarning(warn) = tag {
return Some(warn.clone());
}
}
None
}
/// Return all the hashtags this event refers to
pub fn hashtags(&self) -> Vec<String> {
if self.kind != EventKind::TextNote {
return vec![];
}
let mut output: Vec<String> = Vec::new();
for tag in self.tags.iter() {
if let Tag::Hashtag(hash) = tag {
output.push(hash.clone());
}
}
output
}
/// Return all the URLs this event refers to
pub fn urls(&self) -> Vec<RelayUrl> {
if self.kind != EventKind::TextNote {
return vec![];
}
let mut output: Vec<RelayUrl> = Vec::new();
for tag in self.tags.iter() {
if let Tag::Reference { url, .. } = tag {
if let Ok(relay_url) = RelayUrl::try_from_unchecked_url(url) {
output.push(relay_url);
}
}
}
output
}
/// Get the proof-of-work count of leading bits
pub fn pow(&self) -> u8 {
// Count leading bits in the Id field
let zeroes: u8 = get_leading_zero_bits(&self.id.0);
// Check that they meant it
let mut target_zeroes: u8 = 0;
for tag in self.tags.iter() {
if let Tag::Nonce { nonce: _, target } = tag {
if let Some(t) = target {
target_zeroes = t.parse::<u8>().unwrap_or(0);
}
break;
}
}
zeroes.min(target_zeroes)
}
/// Was this event delegated, was that valid, and if so what is the pubkey of
/// the delegator?
pub fn delegation(&self) -> EventDelegation {
for tag in self.tags.iter() {
if let Tag::Delegation {
pubkey,
conditions,
sig,
} = tag
{
// Convert hex strings into functional types
let signature = match Signature::try_from_hex_string(sig) {
Ok(sig) => sig,
Err(e) => return EventDelegation::InvalidDelegation(format!("{}", e)),
};
let delegator_pubkey = match PublicKey::try_from_hex_string(pubkey) {
Ok(pk) => pk,
Err(e) => return EventDelegation::InvalidDelegation(format!("{}", e)),
};
// Verify the delegation tag
match conditions.verify_signature(&delegator_pubkey, &self.pubkey, signature) {
Ok(_) => {
// Check conditions
if let Some(kind) = conditions.kind {
if self.kind != kind {
return EventDelegation::InvalidDelegation(
"Event Kind not delegated".to_owned(),
);
}
}
if let Some(created_after) = conditions.created_after {
if self.created_at < created_after {
return EventDelegation::InvalidDelegation(
"Event created before delegation started".to_owned(),
);
}
}
if let Some(created_before) = conditions.created_before {
if self.created_at > created_before {
return EventDelegation::InvalidDelegation(
"Event created after delegation ended".to_owned(),
);
}
}
return EventDelegation::DelegatedBy(delegator_pubkey);
}
Err(e) => {
return EventDelegation::InvalidDelegation(format!("{}", e));
}
}
}
}
EventDelegation::NotDelegated
}
}
#[inline]
fn get_leading_zero_bits(bytes: &[u8]) -> u8 {
let mut res = 0_u8;
for b in bytes {
if *b == 0 {
res += 8;
} else {
res += b.leading_zeros() as u8;
return res;
}
}
res
}
#[cfg(test)]
mod test {
use crate::types::*;
test_serde! {Event, test_event_serde}
#[test]
fn test_event_new_and_verify() {
let privkey = PrivateKey::mock();
let pubkey = privkey.public_key();
let preevent = PreEvent {
pubkey: pubkey.clone(),
created_at: Unixtime::mock(),
kind: EventKind::TextNote,
tags: vec![Tag::Event {
id: Id::mock(),
recommended_relay_url: Some(UncheckedUrl::mock()),
marker: None,
}],
content: "Hello World!".to_string(),
ots: None,
};
let mut event = Event::new(preevent, &privkey).unwrap();
assert!(event.verify(None).is_ok());
// Now make sure it fails when the message has been modified
event.content = "I'm changing this message".to_string();
let result = event.verify(None);
assert!(result.is_err());
// Change it back
event.content = "Hello World!".to_string();
let result = event.verify(None);
assert!(result.is_ok());
// Tweak the id only
event.id = Id([
0, 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,
]);
let result = event.verify(None);
assert!(result.is_err());
}
// helper
fn create_event_with_delegation(delegator_privkey: PrivateKey, created_at: Unixtime) -> Event {
let privkey = PrivateKey::mock();
let pubkey = privkey.public_key();
let delegator_pubkey = delegator_privkey.public_key();
let conditions = DelegationConditions::try_from_str(
"kind=1&created_at>1680000000&created_at<1680050000",
)
.unwrap();
let sig = conditions
.generate_signature(
PublicKeyHex::try_from_str(&pubkey.as_hex_string()).unwrap(),
delegator_privkey,
)
.unwrap();
let preevent = PreEvent {
pubkey: pubkey.clone(),
created_at,
kind: EventKind::TextNote,
tags: vec![
Tag::Event {
id: Id::mock(),
recommended_relay_url: Some(UncheckedUrl::mock()),
marker: None,
},
Tag::Delegation {
pubkey: PublicKeyHex::try_from_string(delegator_pubkey.as_hex_string())
.unwrap(),
conditions,
sig,
},
],
content: "Hello World!".to_string(),
ots: None,
};
Event::new(preevent, &privkey).unwrap()
}
#[test]
fn test_event_with_delegation_ok() {
let delegator_privkey = PrivateKey::mock();
let delegator_pubkey = delegator_privkey.public_key();
let event = create_event_with_delegation(delegator_privkey, Unixtime(1680000012));
assert!(event.verify(None).is_ok());
// check delegation
if let EventDelegation::DelegatedBy(pk) = event.delegation() {
// expected type, check returned delegator key
assert_eq!(pk, delegator_pubkey);
} else {
panic!("Expected DelegatedBy result, got {:?}", event.delegation());
}
}
#[test]
fn test_event_with_delegation_invalid_created_after() {
let delegator_privkey = PrivateKey::mock();
let event = create_event_with_delegation(delegator_privkey, Unixtime(1690000000));
assert!(event.verify(None).is_ok());
// check delegation
if let EventDelegation::InvalidDelegation(reason) = event.delegation() {
// expected type, check returned delegator key
assert_eq!(reason, "Event created after delegation ended");
} else {
panic!(
"Expected InvalidDelegation result, got {:?}",
event.delegation()
);
}
}
#[test]
fn test_event_with_delegation_invalid_created_before() {
let delegator_privkey = PrivateKey::mock();
let event = create_event_with_delegation(delegator_privkey, Unixtime(1610000000));
assert!(event.verify(None).is_ok());
// check delegation
if let EventDelegation::InvalidDelegation(reason) = event.delegation() {
// expected type, check returned delegator key
assert_eq!(reason, "Event created before delegation started");
} else {
panic!(
"Expected InvalidDelegation result, got {:?}",
event.delegation()
);
}
}
}