nostr 0.45.0-alpha.1

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
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
//! NIP-60: Cashu Wallets
//!
//! <https://github.com/nostr-protocol/nips/blob/master/60.md>

use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;
use core::str::FromStr;

use serde::{Deserialize, Serialize};

use super::nip44;
use crate::event::{self, Event, EventId, tag};
#[cfg(all(feature = "std", feature = "os-rng"))]
use crate::event::{EventBuilder, Kind, Tag};
use crate::key::{PublicKey, SecretKey};
use crate::types::time::Timestamp;
use crate::types::url::{ParseError, Url};

const E_TAG_STR: &str = "e";
const PRIVKEY: &str = "privkey";
const MINT: &str = "mint";
const DIRECTION: &str = "direction";
const AMOUNT: &str = "amount";
const EVENT_MARKER_CREATED: &str = "created";
const EVENT_MARKER_DESTROYED: &str = "destroyed";
const EVENT_MARKER_REDEEMED: &str = "redeemed";

/// NIP60 error
#[derive(Debug)]
pub enum Error {
    /// NIP44 error
    Nip44(nip44::Error),
    /// JSON error
    Json(serde_json::Error),
    /// Event error
    Event(event::Error),
    /// Tag error
    Tag(tag::Error),
    /// URL error
    Url(ParseError),
    /// Invalid direction
    InvalidDirection,
    /// Found multiple private keys
    FoundMultiplePrivKeys,
    /// Missing required field
    MissingField(String),
    /// Invalid amount
    InvalidAmount,
    /// Missing mint tag
    MissingMintTag,
    /// Invalid mint URL
    InvalidMintUrl,
}

impl core::error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Nip44(e) => e.fmt(f),
            Self::Json(e) => e.fmt(f),
            Self::Event(e) => e.fmt(f),
            Self::Tag(e) => e.fmt(f),
            Self::Url(e) => e.fmt(f),
            Self::InvalidDirection => f.write_str("Invalid direction"),
            Self::FoundMultiplePrivKeys => f.write_str("Found multiple private keys"),
            Self::MissingField(field) => write!(f, "Missing required field: {field}"),
            Self::InvalidAmount => f.write_str("Invalid amount"),
            Self::MissingMintTag => f.write_str("Missing mint tag"),
            Self::InvalidMintUrl => f.write_str("Invalid mint URL"),
        }
    }
}

impl From<nip44::Error> for Error {
    fn from(e: nip44::Error) -> Self {
        Self::Nip44(e)
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Self {
        Self::Json(e)
    }
}

impl From<event::Error> for Error {
    fn from(e: event::Error) -> Self {
        Self::Event(e)
    }
}

impl From<tag::Error> for Error {
    fn from(e: tag::Error) -> Self {
        Self::Tag(e)
    }
}

impl From<ParseError> for Error {
    fn from(e: ParseError) -> Self {
        Self::Url(e)
    }
}

/// Cashu proof
///
/// <https://github.com/nostr-protocol/nips/blob/master/60.md>
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct CashuProof {
    /// Proof ID
    pub id: String,
    /// Amount in sats
    pub amount: u64,
    /// Secret
    pub secret: String,
    /// C value
    pub c: String,
}

/// Wallet event
///
/// <https://github.com/nostr-protocol/nips/blob/master/60.md>
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WalletEvent {
    /// Private key used to unlock P2PK ecash
    pub privkey: String,
    /// Mint URLs this wallet uses
    pub mints: Vec<Url>,
}

impl WalletEvent {
    /// Create new wallet event
    pub fn new<S>(privkey: S, mints: Vec<Url>) -> Self
    where
        S: Into<String>,
    {
        Self {
            privkey: privkey.into(),
            mints,
        }
    }

    /// Parse from wallet event
    pub fn from_wallet_event(
        secret_key: &SecretKey,
        public_key: &PublicKey,
        event: &Event,
    ) -> Result<Self, Error> {
        let decrypted: String = nip44::decrypt(secret_key, public_key, &event.content)?;
        let wallet_data: Vec<Vec<String>> = serde_json::from_str(&decrypted)?;

        let mut privkey: String = String::new();
        let mut mints: Vec<Url> = Vec::new();

        for item in wallet_data.into_iter() {
            let mut iter = item.into_iter();

            if let (Some(key), Some(value)) = (iter.next(), iter.next()) {
                match key.as_str() {
                    PRIVKEY => {
                        if privkey.is_empty() {
                            privkey = value
                        } else {
                            return Err(Error::FoundMultiplePrivKeys);
                        }
                    }
                    MINT => {
                        if let Ok(mint_url) = Url::parse(&value) {
                            mints.push(mint_url);
                        }
                    }
                    _ => {}
                }
            }
        }

        if privkey.is_empty() {
            return Err(Error::MissingField(PRIVKEY.to_string()));
        }

        if mints.is_empty() {
            return Err(Error::MissingField(MINT.to_string()));
        }

        Ok(Self { privkey, mints })
    }

    #[cfg(all(feature = "std", feature = "os-rng"))]
    fn to_encrypted_content(
        &self,
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<String, Error> {
        let mut wallet_data: Vec<Vec<&str>> = vec![vec![PRIVKEY, &self.privkey]];

        // Add each mint URL as a separate entry
        for mint in self.mints.iter() {
            wallet_data.push(vec![MINT, mint.as_str()]);
        }

        let json: String = serde_json::to_string(&wallet_data)?;

        Ok(nip44::encrypt(
            secret_key,
            public_key,
            json,
            nip44::Version::V2,
        )?)
    }

    /// Convert to [`EventBuilder`].
    #[cfg(all(feature = "std", feature = "os-rng"))]
    pub fn to_event_builder(
        &self,
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<EventBuilder, Error> {
        // Build event content
        let content: String = self.to_encrypted_content(secret_key, public_key)?;

        // Construct event builder
        Ok(EventBuilder::new(Kind::CashuWallet, content))
    }
}

/// Token event
///
/// <https://github.com/nostr-protocol/nips/blob/master/60.md>
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct TokenEvent {
    /// Mint URL
    pub mint: Url,
    /// Unspent proofs
    pub proofs: Vec<CashuProof>,
    /// Token event IDs that were destroyed
    pub del: Vec<String>,
}

impl TokenEvent {
    /// Create new token event data
    pub fn new(mint: Url, proofs: Vec<CashuProof>) -> Self {
        Self {
            mint,
            proofs,
            del: Vec::new(),
        }
    }

    /// Parse from token event
    pub fn from_token_event(
        secret_key: &SecretKey,
        public_key: &PublicKey,
        event: &Event,
    ) -> Result<Self, Error> {
        let decrypted: String = nip44::decrypt(secret_key, public_key, &event.content)?;
        Ok(serde_json::from_str(&decrypted)?)
    }

    /// Add destroyed token event ID
    pub fn destroyed(mut self, event_id: String) -> Self {
        self.del.push(event_id);
        self
    }

    #[cfg(all(feature = "std", feature = "os-rng"))]
    fn to_encrypted_content(
        &self,
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<String, Error> {
        let json: String = serde_json::to_string(self)?;
        Ok(nip44::encrypt(
            secret_key,
            public_key,
            json,
            nip44::Version::V2,
        )?)
    }

    /// Convert to [`EventBuilder`].
    #[cfg(all(feature = "std", feature = "os-rng"))]
    pub fn to_event_builder(
        &self,
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<EventBuilder, Error> {
        // Build event content
        let content: String = self.to_encrypted_content(secret_key, public_key)?;

        // Construct event builder
        Ok(EventBuilder::new(Kind::CashuWalletUnspentProof, content))
    }
}

/// Transaction direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TransactionDirection {
    /// Received funds
    In,
    /// Sent funds
    Out,
}

impl fmt::Display for TransactionDirection {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl TransactionDirection {
    /// Get as `&str`.
    pub fn as_str(&self) -> &str {
        match self {
            Self::In => "in",
            Self::Out => "out",
        }
    }
}

impl FromStr for TransactionDirection {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "in" => Ok(Self::In),
            "out" => Ok(Self::Out),
            _ => Err(Error::InvalidDirection),
        }
    }
}

/// Spending history event data (kind: 7376)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpendingHistory {
    /// Transaction direction
    pub direction: TransactionDirection,
    /// Amount in sats
    pub amount: u64,
    /// Created event IDs
    pub created: Vec<EventId>,
    /// Destroyed event IDs
    pub destroyed: Vec<EventId>,
    /// Redeemed event IDs
    pub redeemed: Vec<EventId>,
}

impl SpendingHistory {
    /// Create new spending history data
    pub fn new(direction: TransactionDirection, amount: u64) -> Self {
        Self {
            direction,
            amount,
            created: Vec::new(),
            destroyed: Vec::new(),
            redeemed: Vec::new(),
        }
    }

    /// Parse from spending history event
    pub fn from_spending_history_event(
        secret_key: &SecretKey,
        public_key: &PublicKey,
        event: &Event,
    ) -> Result<Self, Error> {
        let decrypted: String = nip44::decrypt(secret_key, public_key, &event.content)?;
        let data: Vec<Vec<String>> = serde_json::from_str(&decrypted)?;

        let mut direction = None;
        let mut amount = None;
        let mut created: Vec<EventId> = Vec::new();
        let mut destroyed: Vec<EventId> = Vec::new();
        let mut redeemed: Vec<EventId> = Vec::new();

        // Parse encrypted content for created/destroyed events
        for item in data {
            if item.len() >= 2 {
                match item[0].as_str() {
                    DIRECTION => {
                        direction = Some(TransactionDirection::from_str(&item[1])?);
                    }
                    AMOUNT => {
                        amount = Some(item[1].parse().map_err(|_| Error::InvalidAmount)?);
                    }
                    E_TAG_STR if item.len() >= 4 => {
                        let event_id: EventId = EventId::from_hex(&item[1])?;
                        let marker: &str = &item[3];
                        match marker {
                            EVENT_MARKER_CREATED => created.push(event_id),
                            EVENT_MARKER_DESTROYED => destroyed.push(event_id),
                            _ => {}
                        }
                    }
                    _ => {}
                }
            }
        }

        // Check unencrypted e tags for redeemed events
        for tag in event.tags.iter() {
            let slice = tag.as_slice();
            if slice.len() >= 4 && slice[0] == E_TAG_STR && slice[3] == EVENT_MARKER_REDEEMED {
                redeemed.push(EventId::from_hex(&slice[1])?);
            }
        }

        let direction: TransactionDirection =
            direction.ok_or_else(|| Error::MissingField(DIRECTION.to_string()))?;
        let amount: u64 = amount.ok_or_else(|| Error::MissingField(AMOUNT.to_string()))?;

        Ok(Self {
            direction,
            amount,
            created,
            destroyed,
            redeemed,
        })
    }

    /// Add created event ID
    pub fn add_created(mut self, id: EventId) -> Self {
        self.created.push(id);
        self
    }

    /// Add destroyed event ID
    pub fn add_destroyed(mut self, id: EventId) -> Self {
        self.destroyed.push(id);
        self
    }

    /// Add redeemed event ID
    pub fn add_redeemed(mut self, id: EventId) -> Self {
        self.redeemed.push(id);
        self
    }

    #[cfg(all(feature = "std", feature = "os-rng"))]
    fn to_encrypted_content(
        &self,
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<String, Error> {
        let mut data: Vec<Vec<String>> = vec![
            vec![DIRECTION.to_string(), self.direction.to_string()],
            vec![AMOUNT.to_string(), self.amount.to_string()],
        ];

        // Add created event references (encrypted)
        for event_id in &self.created {
            let tag: Tag = Tag::custom(
                "e",
                [
                    event_id.to_hex(),
                    String::new(),
                    EVENT_MARKER_CREATED.to_string(),
                ],
            );
            data.push(tag.to_vec());
        }

        // Add destroyed event references (encrypted)
        for event_id in &self.destroyed {
            let tag: Tag = Tag::custom(
                "e",
                [
                    event_id.to_hex(),
                    String::new(),
                    EVENT_MARKER_DESTROYED.to_string(),
                ],
            );
            data.push(tag.to_vec());
        }

        let json: String = serde_json::to_string(&data)?;

        Ok(nip44::encrypt(
            secret_key,
            public_key,
            json,
            nip44::Version::V2,
        )?)
    }

    /// Convert to event builder
    #[cfg(all(feature = "std", feature = "os-rng"))]
    pub fn to_event_builder(
        &self,
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<EventBuilder, Error> {
        let content: String = self.to_encrypted_content(secret_key, public_key)?;

        let mut tags: Vec<Tag> = Vec::with_capacity(self.redeemed.len());

        // Add redeemed event tags (unencrypted)
        for event_id in self.redeemed.iter() {
            tags.push(Tag::parse([
                "e",
                &event_id.to_hex(),
                "",
                EVENT_MARKER_REDEEMED,
            ])?);
        }

        Ok(EventBuilder::new(Kind::CashuWalletSpendingHistory, content).tags(tags))
    }
}

/// Quote event
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct QuoteEvent {
    /// Quote ID
    pub quote_id: String,
    /// Mint URL
    pub mint: Url,
    /// Expiration timestamp
    pub expiration: Option<Timestamp>,
}

impl QuoteEvent {
    /// Create new quote event data
    pub fn new<S>(quote_id: S, mint: Url) -> Self
    where
        S: Into<String>,
    {
        Self {
            quote_id: quote_id.into(),
            mint,
            expiration: None,
        }
    }

    /// Parse from quote event
    pub fn from_quote_event(
        secret_key: &SecretKey,
        public_key: &PublicKey,
        event: &Event,
    ) -> Result<Self, Error> {
        let quote_id: String = nip44::decrypt(secret_key, public_key, &event.content)?;

        // Extract mint URL from tags
        let mint: Url = event
            .tags
            .iter()
            .find(|t| t.kind() == MINT)
            .and_then(|tag| tag.content())
            .ok_or(Error::MissingMintTag)?
            .parse()
            .map_err(|_| Error::InvalidMintUrl)?;

        // Extract NIP-40 expiration from tags if present
        let expiration: Option<Timestamp> = event.tags.expiration();

        Ok(Self {
            quote_id,
            mint,
            expiration,
        })
    }

    /// Set expiration timestamp
    pub fn expiration(mut self, expiration: Timestamp) -> Self {
        self.expiration = Some(expiration);
        self
    }

    #[cfg(all(feature = "std", feature = "os-rng"))]
    fn to_encrypted_content(
        &self,
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<String, Error> {
        Ok(nip44::encrypt(
            secret_key,
            public_key,
            &self.quote_id,
            nip44::Version::V2,
        )?)
    }

    /// Convert to event builder
    #[cfg(all(feature = "std", feature = "os-rng"))]
    pub fn to_event_builder(
        &self,
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<EventBuilder, Error> {
        let content: String = self.to_encrypted_content(secret_key, public_key)?;

        let mut tags: Vec<Tag> = Vec::with_capacity(2);

        // Add mint tag
        tags.push(Tag::custom(MINT, [self.mint.as_str()]));

        // Add NIP-40 expiration tag (current time + 2 weeks)
        let expiration: Timestamp = Timestamp::now() + 14 * 24 * 60 * 60; // 2 weeks in seconds
        tags.push(Tag::expiration(expiration));

        Ok(EventBuilder::new(Kind::CashuWalletQuote, content).tags(tags))
    }
}

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

    #[test]
    fn test_transaction_direction() {
        assert_eq!(TransactionDirection::In.to_string(), "in");
        assert_eq!(TransactionDirection::Out.to_string(), "out");

        assert_eq!(
            TransactionDirection::from_str("in").unwrap(),
            TransactionDirection::In
        );
        assert_eq!(
            TransactionDirection::from_str("out").unwrap(),
            TransactionDirection::Out
        );
        assert!(TransactionDirection::from_str("invalid").is_err());
    }

    #[test]
    fn test_wallet_event() {
        let mint_url = Url::parse("https://example.com").unwrap();
        let wallet = WalletEvent::new("test_privkey", vec![mint_url.clone()]);

        assert_eq!(wallet.privkey, "test_privkey");
        assert_eq!(wallet.mints.len(), 1);
        assert_eq!(wallet.mints[0], mint_url);
    }

    #[test]
    fn test_token_event_data() {
        let mint_url = Url::parse("https://example.com").unwrap();
        let proof = CashuProof {
            id: "test_id".to_string(),
            amount: 100,
            secret: "test_secret".to_string(),
            c: "test_c".to_string(),
        };

        let token_data = TokenEvent::new(mint_url.clone(), vec![proof.clone()]);

        assert_eq!(token_data.mint, mint_url);
        assert_eq!(token_data.proofs.len(), 1);
        assert_eq!(token_data.proofs[0], proof);
        assert!(token_data.del.is_empty());
    }

    #[test]
    fn test_spending_history_data() {
        let history = SpendingHistory::new(TransactionDirection::In, 50);

        assert_eq!(history.direction, TransactionDirection::In);
        assert_eq!(history.amount, 50);
        assert!(history.created.is_empty());
        assert!(history.destroyed.is_empty());
        assert!(history.redeemed.is_empty());
    }

    #[test]
    fn test_spending_history_with_references() {
        let id1 = EventId::all_zeros();
        let id2 = EventId::from_slice(&[
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 1,
        ])
        .unwrap();
        let id3 = EventId::from_slice(&[
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 2,
        ])
        .unwrap();

        let history = SpendingHistory::new(TransactionDirection::In, 50)
            .add_created(id1)
            .add_destroyed(id2)
            .add_redeemed(id3);

        assert_eq!(history.direction, TransactionDirection::In);
        assert_eq!(history.amount, 50);
        assert_eq!(history.created.len(), 1);
        assert_eq!(history.destroyed.len(), 1);
        assert_eq!(history.redeemed.len(), 1);
        assert_eq!(history.created[0], id1);
        assert_eq!(history.destroyed[0], id2);
        assert_eq!(history.redeemed[0], id3);
    }

    #[test]
    fn test_quote_event_data() {
        let mint_url = Url::parse("https://example.com").unwrap();
        let quote = QuoteEvent::new("test_quote_id", mint_url.clone());

        assert_eq!(quote.quote_id, "test_quote_id");
        assert_eq!(quote.mint, mint_url);
        assert!(quote.expiration.is_none());
    }
}