chaincraft 0.3.2

A high-performance Rust-based platform for blockchain education and prototyping
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
//! Chatroom Protocol - A dapp example for Chaincraft
//!
//! This demonstrates how to build a decentralized application on top of Chaincraft.
//! The chatroom protocol allows:
//! - Creating chatrooms with admin control
//! - Requesting to join chatrooms
//! - Admin approval of new members
//! - Posting messages to chatrooms
//! - Message validation and signature verification

use crate::{
    crypto::{
        ecdsa::{ECDSASignature, ECDSAVerifier},
        KeyType, PrivateKey, PublicKey, Signature,
    },
    error::{ChaincraftError, Result},
    network::PeerId,
    shared::{MessageType, SharedMessage, SharedObjectId},
    shared_object::ApplicationObject,
    storage::MemoryStorage,
    ChaincraftNode,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

/// Chatroom message types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "message_type")]
pub enum ChatroomMessageType {
    #[serde(rename = "CREATE_CHATROOM")]
    CreateChatroom {
        chatroom_name: String,
        public_key_pem: String,
        #[serde(default)]
        timestamp: f64,
        #[serde(default)]
        signature: String,
    },
    #[serde(rename = "REQUEST_JOIN")]
    RequestJoin {
        chatroom_name: String,
        public_key_pem: String,
        #[serde(default)]
        timestamp: f64,
        #[serde(default)]
        signature: String,
    },
    #[serde(rename = "ACCEPT_MEMBER")]
    AcceptMember {
        chatroom_name: String,
        public_key_pem: String,
        requester_key_pem: String,
        #[serde(default)]
        timestamp: f64,
        #[serde(default)]
        signature: String,
    },
    #[serde(rename = "POST_MESSAGE")]
    PostMessage {
        chatroom_name: String,
        public_key_pem: String,
        text: String,
        #[serde(default)]
        timestamp: f64,
        #[serde(default)]
        signature: String,
    },
}

/// A chatroom structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Chatroom {
    pub name: String,
    pub admin: String,              // Admin's public key
    pub members: Vec<String>,       // Member public keys
    pub messages: Vec<ChatMessage>, // All messages including metadata
}

/// A chat message with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
    pub message_type: String,
    pub chatroom_name: String,
    pub public_key_pem: String,
    pub text: Option<String>,
    pub requester_key_pem: Option<String>,
    pub timestamp: f64,
    pub signature: String,
}

/// Chatroom application object
#[derive(Debug, Clone)]
pub struct ChatroomObject {
    id: SharedObjectId,
    chatrooms: HashMap<String, Chatroom>,
    users: HashMap<String, String>,
    verifier: ECDSAVerifier,
}

impl ChatroomObject {
    pub fn new() -> Self {
        Self {
            id: SharedObjectId::new(),
            chatrooms: HashMap::new(),
            users: HashMap::new(),
            verifier: ECDSAVerifier::new(),
        }
    }

    /// Get all chatrooms
    pub fn get_chatrooms(&self) -> &HashMap<String, Chatroom> {
        &self.chatrooms
    }

    /// Get a specific chatroom
    pub fn get_chatroom(&self, name: &str) -> Option<&Chatroom> {
        self.chatrooms.get(name)
    }

    /// Validate message signature
    fn validate_signature(
        &self,
        msg_data: &Value,
        signature: &str,
        public_key_pem: &str,
    ) -> Result<bool> {
        // Extract message data without signature for verification
        let mut msg_for_verification = msg_data.clone();
        if let Some(obj) = msg_for_verification.as_object_mut() {
            obj.remove("signature");
        }

        let payload = serde_json::to_string(&msg_for_verification).map_err(|e| {
            ChaincraftError::Serialization(crate::error::SerializationError::Json(e))
        })?;

        // Decode signature from hex
        let signature_bytes = hex::decode(signature)
            .map_err(|_| ChaincraftError::validation("Invalid signature hex"))?;

        // Verify signature
        let ecdsa_sig = ECDSASignature::from_bytes(&signature_bytes)
            .map_err(|_| ChaincraftError::validation("Invalid signature format"))?;

        self.verifier
            .verify(payload.as_bytes(), &ecdsa_sig, public_key_pem)
    }

    /// Check if timestamp is recent (within 15 seconds)
    fn is_timestamp_recent(&self, timestamp: f64) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs_f64();

        let diff = (now - timestamp).abs();
        diff <= 15.0
    }

    /// Process a create chatroom message
    async fn process_create_chatroom(
        &mut self,
        msg: ChatroomMessageType,
        msg_data: &Value,
    ) -> Result<bool> {
        if let ChatroomMessageType::CreateChatroom {
            chatroom_name,
            public_key_pem,
            timestamp,
            signature,
        } = msg
        {
            // Validate signature
            if !self.validate_signature(msg_data, &signature, &public_key_pem)? {
                return Ok(false);
            }

            // Check timestamp
            if !self.is_timestamp_recent(timestamp) {
                return Ok(false);
            }

            // Check if chatroom already exists
            if self.chatrooms.contains_key(&chatroom_name) {
                return Ok(false);
            }

            // Create new chatroom
            let chatroom = Chatroom {
                name: chatroom_name.clone(),
                admin: public_key_pem.clone(),
                members: vec![public_key_pem.clone()], // Admin is automatically a member
                messages: Vec::new(),
            };

            self.chatrooms.insert(chatroom_name, chatroom);
            tracing::info!("Created chatroom with admin: {}", public_key_pem);

            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Process a request join message
    async fn process_request_join(
        &mut self,
        msg: ChatroomMessageType,
        msg_data: &Value,
    ) -> Result<bool> {
        if let ChatroomMessageType::RequestJoin {
            chatroom_name,
            public_key_pem,
            timestamp,
            signature,
        } = msg
        {
            // Validate signature
            if !self.validate_signature(msg_data, &signature, &public_key_pem)? {
                return Ok(false);
            }

            // Check timestamp
            if !self.is_timestamp_recent(timestamp) {
                return Ok(false);
            }

            // Check if chatroom exists
            if !self.chatrooms.contains_key(&chatroom_name) {
                return Ok(false);
            }

            // Add to pending requests (for now, just log)
            tracing::info!(
                "Join request for chatroom '{}' from: {}",
                chatroom_name,
                public_key_pem
            );

            // Store the request message
            if let Some(chatroom) = self.chatrooms.get_mut(&chatroom_name) {
                let chat_msg = ChatMessage {
                    message_type: "REQUEST_JOIN".to_string(),
                    chatroom_name: chatroom_name.clone(),
                    public_key_pem: public_key_pem.clone(),
                    text: None,
                    requester_key_pem: None,
                    timestamp,
                    signature,
                };
                chatroom.messages.push(chat_msg);
            }

            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Process an accept member message
    async fn process_accept_member(
        &mut self,
        msg: ChatroomMessageType,
        msg_data: &Value,
    ) -> Result<bool> {
        if let ChatroomMessageType::AcceptMember {
            chatroom_name,
            public_key_pem,
            requester_key_pem,
            timestamp,
            signature,
        } = msg
        {
            // Validate signature
            if !self.validate_signature(msg_data, &signature, &public_key_pem)? {
                return Ok(false);
            }

            // Check timestamp
            if !self.is_timestamp_recent(timestamp) {
                return Ok(false);
            }

            // Check if chatroom exists and sender is admin
            if let Some(chatroom) = self.chatrooms.get_mut(&chatroom_name) {
                if chatroom.admin != public_key_pem {
                    return Ok(false); // Only admin can accept members
                }

                // Add member if not already present
                if !chatroom.members.contains(&requester_key_pem) {
                    chatroom.members.push(requester_key_pem.clone());
                    tracing::info!(
                        "Added member {} to chatroom '{}'",
                        requester_key_pem,
                        chatroom_name
                    );
                }

                // Store the accept message
                let chat_msg = ChatMessage {
                    message_type: "ACCEPT_MEMBER".to_string(),
                    chatroom_name: chatroom_name.clone(),
                    public_key_pem: public_key_pem.clone(),
                    text: None,
                    requester_key_pem: Some(requester_key_pem),
                    timestamp,
                    signature,
                };
                chatroom.messages.push(chat_msg);

                Ok(true)
            } else {
                Ok(false)
            }
        } else {
            Ok(false)
        }
    }

    /// Process a post message
    async fn process_post_message(
        &mut self,
        msg: ChatroomMessageType,
        msg_data: &Value,
    ) -> Result<bool> {
        if let ChatroomMessageType::PostMessage {
            chatroom_name,
            public_key_pem,
            text,
            timestamp,
            signature,
        } = msg
        {
            // Validate signature
            if !self.validate_signature(msg_data, &signature, &public_key_pem)? {
                return Ok(false);
            }

            // Check timestamp
            if !self.is_timestamp_recent(timestamp) {
                return Ok(false);
            }

            // Check if chatroom exists and sender is a member
            if let Some(chatroom) = self.chatrooms.get_mut(&chatroom_name) {
                if !chatroom.members.contains(&public_key_pem) {
                    return Ok(false); // Only members can post messages
                }

                // Add the message
                let chat_msg = ChatMessage {
                    message_type: "POST_MESSAGE".to_string(),
                    chatroom_name: chatroom_name.clone(),
                    public_key_pem: public_key_pem.clone(),
                    text: Some(text),
                    requester_key_pem: None,
                    timestamp,
                    signature,
                };
                chatroom.messages.push(chat_msg);

                tracing::info!("Message posted to '{}' by: {}", chatroom_name, public_key_pem);

                Ok(true)
            } else {
                Ok(false)
            }
        } else {
            Ok(false)
        }
    }
}

impl Default for ChatroomObject {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl ApplicationObject for ChatroomObject {
    fn id(&self) -> &SharedObjectId {
        &self.id
    }

    fn type_name(&self) -> &'static str {
        "ChatroomObject"
    }

    async fn is_valid(&self, message: &SharedMessage) -> Result<bool> {
        // Check if this is a chatroom message
        let msg_result: std::result::Result<ChatroomMessageType, _> =
            serde_json::from_value(message.data.clone());
        Ok(msg_result.is_ok())
    }

    async fn add_message(&mut self, message: SharedMessage) -> Result<()> {
        let msg: ChatroomMessageType =
            serde_json::from_value(message.data.clone()).map_err(|e| {
                ChaincraftError::Serialization(crate::error::SerializationError::Json(e))
            })?;

        let processed = match &msg {
            ChatroomMessageType::CreateChatroom { .. } => {
                self.process_create_chatroom(msg.clone(), &message.data)
                    .await?
            },
            ChatroomMessageType::RequestJoin { .. } => {
                self.process_request_join(msg.clone(), &message.data)
                    .await?
            },
            ChatroomMessageType::AcceptMember { .. } => {
                self.process_accept_member(msg.clone(), &message.data)
                    .await?
            },
            ChatroomMessageType::PostMessage { .. } => {
                self.process_post_message(msg.clone(), &message.data)
                    .await?
            },
        };

        if processed {
            tracing::debug!("Successfully processed chatroom message: {:?}", msg);
        } else {
            tracing::warn!("Failed to process chatroom message: {:?}", msg);
        }

        Ok(())
    }

    fn is_merkleized(&self) -> bool {
        false
    }

    async fn get_latest_digest(&self) -> Result<String> {
        let mut hasher = Sha256::new();

        for (room_name, chatroom) in &self.chatrooms {
            hasher.update(room_name.as_bytes());
            hasher.update(chatroom.messages.len().to_le_bytes());
        }

        Ok(hex::encode(hasher.finalize()))
    }

    async fn has_digest(&self, _digest: &str) -> Result<bool> {
        Ok(false) // Simplified implementation
    }

    async fn is_valid_digest(&self, _digest: &str) -> Result<bool> {
        Ok(true)
    }

    async fn add_digest(&mut self, _digest: String) -> Result<bool> {
        Ok(true)
    }

    async fn gossip_messages(&self, _digest: Option<&str>) -> Result<Vec<SharedMessage>> {
        Ok(Vec::new()) // Simplified implementation
    }

    async fn get_messages_since_digest(&self, _digest: &str) -> Result<Vec<SharedMessage>> {
        Ok(Vec::new()) // Simplified implementation
    }

    async fn get_state(&self) -> Result<Value> {
        let state = serde_json::json!({
            "chatroom_count": self.chatrooms.len(),
            "chatrooms": self.chatrooms.keys().collect::<Vec<_>>(),
            "total_messages": self.chatrooms.values().map(|c| c.messages.len()).sum::<usize>()
        });
        Ok(state)
    }

    async fn reset(&mut self) -> Result<()> {
        self.chatrooms.clear();
        Ok(())
    }

    fn clone_box(&self) -> Box<dyn ApplicationObject> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Typed node wrapper for Chatroom examples.
pub struct ChatroomNode {
    node: ChaincraftNode,
    object_id: SharedObjectId,
}

impl ChatroomNode {
    pub async fn new(port: u16) -> Result<Self> {
        let mut node = ChaincraftNode::new(PeerId::new(), Arc::new(MemoryStorage::new()));
        node.set_port(port);
        let object_id = node
            .add_shared_object(Box::new(ChatroomObject::new()))
            .await?;
        Ok(Self { node, object_id })
    }

    pub async fn start(&mut self) -> Result<()> {
        self.node.start().await
    }

    pub async fn close(&mut self) -> Result<()> {
        self.node.close().await
    }

    pub async fn connect_to_peer(&mut self, addr: &str) -> Result<()> {
        self.node.connect_to_peer(addr).await
    }

    pub fn host(&self) -> &str {
        self.node.host()
    }

    pub fn port(&self) -> u16 {
        self.node.port()
    }

    pub fn id(&self) -> &crate::network::PeerId {
        self.node.id()
    }

    pub async fn publish(&mut self, data: Value) -> Result<String> {
        self.node.create_shared_message_with_data(data).await
    }

    pub async fn chatroom_message_count(&self, room: &str) -> Result<usize> {
        let registry = self.node.app_objects.read().await;
        let Some(obj) = registry.get(&self.object_id) else {
            return Err(ChaincraftError::validation("ChatroomObject not found"));
        };
        let Some(chatroom) = obj.as_any().downcast_ref::<ChatroomObject>() else {
            return Err(ChaincraftError::validation("Object type mismatch for ChatroomObject"));
        };
        Ok(chatroom
            .get_chatroom(room)
            .map(|r| r.messages.len())
            .unwrap_or(0))
    }

    pub async fn chatroom_names(&self) -> Result<Vec<String>> {
        let registry = self.node.app_objects.read().await;
        let Some(obj) = registry.get(&self.object_id) else {
            return Err(ChaincraftError::validation("ChatroomObject not found"));
        };
        let Some(chatroom) = obj.as_any().downcast_ref::<ChatroomObject>() else {
            return Err(ChaincraftError::validation("Object type mismatch for ChatroomObject"));
        };
        Ok(chatroom.get_chatrooms().keys().cloned().collect())
    }
}

/// Helper functions for creating chatroom messages
pub mod helpers {
    use super::*;
    use crate::crypto::ecdsa::ECDSASigner;

    /// Create a create chatroom message
    pub fn create_chatroom_message(chatroom_name: String, signer: &ECDSASigner) -> Result<Value> {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs_f64();

        let public_key_pem = signer.get_public_key_pem()?;

        let mut msg = serde_json::json!({
            "message_type": "CREATE_CHATROOM",
            "chatroom_name": chatroom_name,
            "public_key_pem": public_key_pem,
            "timestamp": timestamp
        });

        // Sign the message
        let payload = serde_json::to_string(&msg).map_err(|e| {
            ChaincraftError::Serialization(crate::error::SerializationError::Json(e))
        })?;
        let signature = signer.sign(payload.as_bytes())?;

        msg["signature"] = serde_json::Value::String(hex::encode(signature.to_bytes()));

        Ok(msg)
    }

    /// Create a post message
    pub fn create_post_message(
        chatroom_name: String,
        text: String,
        signer: &ECDSASigner,
    ) -> Result<Value> {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs_f64();

        let public_key_pem = signer.get_public_key_pem()?;

        let mut msg = serde_json::json!({
            "message_type": "POST_MESSAGE",
            "chatroom_name": chatroom_name,
            "public_key_pem": public_key_pem,
            "text": text,
            "timestamp": timestamp
        });

        // Sign the message
        let payload = serde_json::to_string(&msg).map_err(|e| {
            ChaincraftError::Serialization(crate::error::SerializationError::Json(e))
        })?;
        let signature = signer.sign(payload.as_bytes())?;

        msg["signature"] = serde_json::Value::String(hex::encode(signature.to_bytes()));

        Ok(msg)
    }
}