nostr-sdk-base 0.1.2

Rust implementation of the Nostr protocol.
Documentation
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
// Copyright (c) 2021 Paul Miller
// Copyright (c) 2022 Yuki Kishimoto
// Distributed under the MIT software license

use std::fmt;
use std::str::FromStr;
use std::time::Instant;

use anyhow::{anyhow, Result};
use bitcoin_hashes::hex::FromHex;
use bitcoin_hashes::{sha256, Hash};
use chrono::serde::ts_seconds;
use chrono::DateTime;
use chrono::{TimeZone, Utc};
use secp256k1::{schnorr, KeyPair, Secp256k1, XOnlyPublicKey};
use serde::{Deserialize, Deserializer};
use serde_json::{json, Value};
use serde_repr::{Deserialize_repr, Serialize_repr};
use url::Url;

use crate::util::{nip04, nip13};
use crate::Keys;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub struct Event {
    pub id: sha256::Hash, // hash of serialized event with id 0
    pub pubkey: XOnlyPublicKey,
    #[serde(with = "ts_seconds")]
    pub created_at: DateTime<Utc>, // unix timestamp seconds
    pub kind: Kind,
    pub tags: Vec<Tag>,
    pub content: String,
    #[serde(deserialize_with = "sig_string")] // Serde derive is being weird
    pub sig: schnorr::Signature,
}

fn sig_string<'de, D>(deserializer: D) -> Result<schnorr::Signature, D::Error>
where
    D: Deserializer<'de>,
{
    let s: String = Deserialize::deserialize(deserializer)?;
    let sig = schnorr::Signature::from_str(&s);
    sig.map_err(serde::de::Error::custom)
}

impl Event {
    fn gen_id(
        pubkey: &XOnlyPublicKey,
        created_at: &DateTime<Utc>,
        kind: &Kind,
        tags: &[Tag],
        content: &str,
    ) -> sha256::Hash {
        let json: Value = json!([0, pubkey, created_at.timestamp(), kind, tags, content]);
        let event_str: String = json.to_string();
        sha256::Hash::hash(event_str.as_bytes())
    }

    /// Create a generic type of event
    pub fn new_generic(keys: &Keys, kind: Kind, content: &str, tags: &[Tag]) -> Result<Self> {
        let secp = Secp256k1::new();
        let keypair: &KeyPair = &keys.key_pair()?;
        let pubkey: XOnlyPublicKey = keys.public_key();
        let created_at: DateTime<Utc> = Utc.timestamp(Utc::now().timestamp(), 0);

        let id: sha256::Hash = Self::gen_id(&pubkey, &created_at, &kind, tags, content);
        let message = secp256k1::Message::from_slice(&id)?;

        Ok(Event {
            id,
            pubkey,
            created_at,
            kind,
            tags: tags.to_vec(),
            content: content.to_string(),
            sig: secp.sign_schnorr(&message, keypair),
        })
    }

    pub fn set_metadata(
        keys: &Keys,
        username: Option<&str>,
        display_name: Option<&str>,
        about: Option<&str>,
        picture: Option<&str>,
    ) -> Result<Self> {
        let metadata: Value = json!({
            "name": username.unwrap_or(""),
            "display_name": display_name.unwrap_or(""),
            "about": about.unwrap_or(""),
            "picture": picture.unwrap_or(""),
        });

        Self::new_generic(
            keys,
            Kind::Base(KindBase::Metadata),
            &metadata.to_string(),
            &Vec::new(),
        )
    }

    pub fn add_recommended_relay(keys: &Keys, url: &Url) -> Result<Self> {
        Self::new_generic(
            keys,
            Kind::Base(KindBase::RecommendRelay),
            url.as_ref(),
            &[],
        )
    }

    /// Create a new TextNote Event
    pub fn new_text_note(keys: &Keys, content: &str, tags: &[Tag]) -> Result<Self> {
        Self::new_generic(keys, Kind::Base(KindBase::TextNote), content, tags)
    }

    pub fn new_pow_text_note(
        keys: &Keys,
        content: &str,
        tags: &[Tag],
        difficulty: u8,
    ) -> Result<Self> {
        let capacity: usize = tags.len() + 1;
        let pow_tag_index: usize = capacity - 1;
        let mut nonce: u128 = 0;
        #[allow(unused_assignments)]
        let mut new_tags: Vec<Tag> = Vec::with_capacity(capacity);

        new_tags = vec![
            tags.to_owned(),
            vec![Tag::new(TagData::POW { nonce, difficulty })],
        ]
        .concat();

        let now = Instant::now();

        loop {
            nonce += 1;

            if let Some(elem) = new_tags.get_mut(pow_tag_index) {
                *elem = Tag::new(TagData::POW { nonce, difficulty });
            } else {
                return Err(anyhow!("Invalid pow tag index"));
            }

            let event = Self::new_text_note(keys, content, &new_tags)?;

            let leading_zeroes = nip13::get_leading_zero_bits(event.id);
            if leading_zeroes >= difficulty {
                log::debug!(
                    "{} iterations in {} seconds. Avg rate {} hashes/second",
                    nonce,
                    now.elapsed().as_secs(),
                    nonce * 1000 / std::cmp::max(1, now.elapsed().as_millis())
                );

                return Ok(event);
            }
        }
    }

    pub fn set_contact_list(keys: &Keys, list: Vec<Contact>) -> Result<Self> {
        let tags: Vec<Tag> = list
            .iter()
            .map(|contact| {
                Tag::new(TagData::ContactList {
                    pk: contact.pk,
                    relay_url: contact.relay_url.clone(),
                    alias: contact.alias.clone(),
                })
            })
            .collect();

        Self::new_generic(keys, Kind::Base(KindBase::ContactList), "", &tags)
    }

    /// Create encrypted direct msg event
    pub fn new_encrypted_direct_msg(
        sender_keys: &Keys,
        receiver_keys: &Keys,
        content: &str,
    ) -> Result<Self> {
        Self::new_generic(
            sender_keys,
            Kind::Base(KindBase::EncryptedDirectMessage),
            &nip04::encrypt(
                &sender_keys.secret_key()?,
                &receiver_keys.public_key(),
                content,
            )?,
            &[Tag::new(TagData::PubKey(receiver_keys.public_key()))],
        )
    }

    /// Create delete event
    pub fn delete(keys: &Keys, ids: Vec<sha256::Hash>, content: Option<&str>) -> Result<Self> {
        let tags: Vec<Tag> = ids
            .iter()
            .map(|id| Tag::new(TagData::EventId(*id)))
            .collect();

        Self::new_generic(
            keys,
            Kind::Base(KindBase::EventDeletion),
            content.unwrap_or(""),
            &tags,
        )
    }

    /// Add reaction (like/upvote, dislike/downvote) to an event
    pub fn new_reaction(keys: &Keys, event_id: sha256::Hash, positive: bool) -> Result<Self> {
        let tags: &[Tag] = &[
            Tag::new(TagData::EventId(event_id)),
            Tag::new(TagData::PubKey(keys.public_key())),
        ];

        let content: &str = match positive {
            true => "+",
            false => "-",
        };

        Self::new_generic(keys, Kind::Base(KindBase::Reaction), content, tags)
    }

    pub fn verify(&self) -> Result<(), secp256k1::Error> {
        let secp = Secp256k1::new();
        let id = Self::gen_id(
            &self.pubkey,
            &self.created_at,
            &self.kind,
            &self.tags,
            &self.content,
        );
        let message = secp256k1::Message::from_slice(&id)?;
        secp.verify_schnorr(&self.sig, &message, &self.pubkey)
    }

    pub fn new_from_json(json: String) -> Result<Self> {
        Ok(serde_json::from_str(&json)?)
    }

    pub fn as_json(&self) -> Result<String> {
        Ok(serde_json::to_string(&self)?)
    }
}

impl Event {
    /// This is just for serde sanity checking
    #[allow(dead_code)]
    pub(crate) fn new_dummy(
        id: &str,
        pubkey: &str,
        created_at: i64,
        kind: u8,
        tags: Vec<Tag>,
        content: &str,
        sig: &str,
    ) -> Result<Self> {
        let id = sha256::Hash::from_hex(id)?;
        let pubkey = XOnlyPublicKey::from_str(pubkey)?;
        let created_at = Utc.timestamp(created_at, 0);
        let kind = serde_json::from_str(&kind.to_string())?;
        let sig = schnorr::Signature::from_str(sig)?;

        let event = Event {
            id,
            pubkey,
            created_at,
            kind,
            tags,
            content: content.to_string(),
            sig,
        };

        if event.verify().is_ok() {
            Ok(event)
        } else {
            Err(anyhow!("Didn't verify"))
        }
    }
}

#[derive(Serialize_repr, Deserialize_repr, Eq, PartialEq, Debug, Copy, Clone)]
#[repr(u8)]
pub enum KindBase {
    Metadata = 0,
    TextNote = 1,
    RecommendRelay = 2,
    ContactList = 3,
    EncryptedDirectMessage = 4,
    EventDeletion = 5,
    Boost = 6,
    Reaction = 7,
    ChannelCreation = 40,
    ChannelMetadata = 41,
    ChannelMessage = 42,
    ChannelHideMessage = 43,
    ChannelMuteUser = 44,
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Copy, Clone)]
#[serde(untagged)]
pub enum Kind {
    Base(KindBase),
    Custom(u16),
}

pub enum TagKind {
    P,
    E,
    Nonce,
}

impl fmt::Display for TagKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::P => write!(f, "p"),
            Self::E => write!(f, "e"),
            Self::Nonce => write!(f, "nonce"),
        }
    }
}

pub enum TagData {
    Generic(TagKind, Vec<String>),
    EventId(sha256::Hash),
    PubKey(XOnlyPublicKey),
    ContactList {
        pk: XOnlyPublicKey,
        relay_url: String,
        alias: String,
    },
    POW {
        nonce: u128,
        difficulty: u8,
    },
}

impl From<TagData> for Vec<String> {
    fn from(data: TagData) -> Self {
        match data {
            TagData::Generic(kind, data) => vec![vec![kind.to_string()], data].concat(),
            TagData::EventId(id) => vec![TagKind::E.to_string(), id.to_string()],
            TagData::PubKey(pk) => vec![TagKind::P.to_string(), pk.to_string()],
            TagData::ContactList {
                pk,
                relay_url,
                alias,
            } => vec![TagKind::P.to_string(), pk.to_string(), relay_url, alias],
            TagData::POW { nonce, difficulty } => vec![
                TagKind::Nonce.to_string(),
                nonce.to_string(),
                difficulty.to_string(),
            ],
        }
    }
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub struct Tag(Vec<String>);

impl Tag {
    pub fn new(data: TagData) -> Self {
        Self(data.into())
    }

    pub fn kind(&self) -> &str {
        &self.0[0]
    }

    pub fn content(&self) -> &str {
        &self.0[1]
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct Contact {
    pub alias: String,
    pub pk: XOnlyPublicKey,
    pub relay_url: String,
}

impl Contact {
    pub fn new(alias: &str, pk: XOnlyPublicKey, relay_url: &str) -> Self {
        Self {
            alias: alias.into(),
            pk,
            relay_url: relay_url.into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tags_deser_without_recommended_relay() {
        //The TAG array has dynamic length because the third element(Recommended relay url) is optional
        let sample_event = r#"{"id":"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45","pubkey":"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785","created_at":1640839235,"kind":4,"tags":[["p","13adc511de7e1cfcf1c6b7f6365fb5a03442d7bcacf565ea57fa7770912c023d"]],"content":"uRuvYr585B80L6rSJiHocw==?iv=oh6LVqdsYYol3JfFnXTbPA==","sig":"a5d9290ef9659083c490b303eb7ee41356d8778ff19f2f91776c8dc4443388a64ffcf336e61af4c25c05ac3ae952d1ced889ed655b67790891222aaa15b99fdd"}"#;
        let ev_ser = Event::new_from_json(sample_event.into()).unwrap();
        assert_eq!(ev_ser.as_json(), sample_event);
    }

    #[test]
    fn test_custom_kind() {
        let keys = Keys::generate_from_os_random();
        let e = Event::new_generic(&keys, Kind::Custom(123), "my content", &vec![]).unwrap();

        let serialized = e.as_json().unwrap();
        let deserialized = Event::new_from_json(serialized).unwrap();

        assert_eq!(e, deserialized);
        assert_eq!(Kind::Custom(123), e.kind);
        assert_eq!(Kind::Custom(123), deserialized.kind);
    }
}