Skip to content

Commit d12aa91

Browse files
authored
Chain invite handler (#538)
* Extract block processor, add public block handler * Crude handler implementation * Less verbose logging * Event chain builder * Nostr chain validation * Single block handler * Review fixes * Review fixes
1 parent 58bbfc7 commit d12aa91

File tree

14 files changed

+1854
-648
lines changed

14 files changed

+1854
-648
lines changed

crates/bcr-ebill-api/src/service/notification_service/default_service.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ mod tests {
642642
previous_event: Option<nostr::event::Event>,
643643
root_event: Option<nostr::event::Event>) -> bcr_ebill_transport::Result<nostr::event::Event>;
644644
async fn resolve_contact(&self, node_id: &str) -> Result<Option<bcr_ebill_transport::transport::NostrContactData>>;
645+
async fn resolve_public_chain(&self, id: &str, chain_type: BlockchainType) -> Result<Vec<nostr::event::Event>>;
645646
}
646647
}
647648

@@ -1895,6 +1896,7 @@ mod tests {
18951896
let bill_blockchain_store = Arc::new(MockBillChainStoreApiMock::new());
18961897
let nostr_contact_store = Arc::new(MockNostrContactStore::new());
18971898
let chain_key_store = Arc::new(MockChainKeyService::new());
1899+
let chain_event_store = Arc::new(MockNostrChainEventStore::new());
18981900
let _ = create_nostr_consumer(
18991901
clients,
19001902
contact_service,
@@ -1905,6 +1907,7 @@ mod tests {
19051907
bill_store,
19061908
nostr_contact_store,
19071909
chain_key_store,
1910+
chain_event_store,
19081911
)
19091912
.await;
19101913
}

crates/bcr-ebill-api/src/service/notification_service/mod.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use bcr_ebill_persistence::nostr::{
1212
};
1313
use bcr_ebill_transport::chain_keys::ChainKeyServiceApi;
1414
use bcr_ebill_transport::handler::{
15-
BillChainEventHandler, LoggingEventHandler, NotificationHandlerApi,
15+
BillChainEventHandler, BillChainEventProcessor, BillInviteEventHandler, LoggingEventHandler,
16+
NotificationHandlerApi,
1617
};
1718
use bcr_ebill_transport::{Error, EventType, Result};
1819
use bcr_ebill_transport::{NotificationServiceApi, PushApi};
@@ -123,12 +124,21 @@ pub async fn create_nostr_consumer(
123124
bill_store: Arc<dyn BillStoreApi>,
124125
nostr_contact_store: Arc<dyn NostrContactStoreApi>,
125126
chain_key_service: Arc<dyn ChainKeyServiceApi>,
127+
chain_event_store: Arc<dyn NostrChainEventStoreApi>,
126128
) -> Result<NostrConsumer> {
127129
// we need one nostr client for nostr interactions
128-
let transport = match clients.first() {
130+
let transport = match clients.iter().find(|c| c.is_primary()) {
129131
Some(client) => client.clone(),
130132
None => panic!("Cant create Nostr consumer as there is no nostr client available"),
131133
};
134+
135+
let processor = Arc::new(BillChainEventProcessor::new(
136+
bill_blockchain_store,
137+
bill_store,
138+
transport.clone(),
139+
nostr_contact_store,
140+
));
141+
132142
// register the logging event handler for all events for now. Later we will probably
133143
// setup the handlers outside and pass them to the consumer via this functions arguments.
134144
let handlers: Vec<Box<dyn NotificationHandlerApi>> = vec![
@@ -138,10 +148,12 @@ pub async fn create_nostr_consumer(
138148
Box::new(BillChainEventHandler::new(
139149
notification_store,
140150
push_service,
141-
bill_blockchain_store,
142-
bill_store,
143-
transport,
144-
nostr_contact_store,
151+
processor.clone(),
152+
)),
153+
Box::new(BillInviteEventHandler::new(
154+
transport.clone(),
155+
processor.clone(),
156+
chain_event_store.clone(),
145157
)),
146158
];
147159
debug!("initializing nostr consumer for {} clients", clients.len());

crates/bcr-ebill-api/src/service/notification_service/nostr.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bcr_ebill_transport::{
99
event::EventEnvelope,
1010
handler::NotificationHandlerApi,
1111
transport::{
12-
NostrContactData, create_nip04_event, create_public_chain_event,
12+
NostrContactData, chain_filter, create_nip04_event, create_public_chain_event,
1313
decrypt_public_chain_event, unwrap_direct_message, unwrap_public_chain_event,
1414
},
1515
};
@@ -357,12 +357,10 @@ impl NotificationJsonTransportApi for NostrClient {
357357
previous_event,
358358
root_event,
359359
)?;
360-
info!("Sending public {} chain event: {:?}", blockchain, event);
361360
let send_event = self.client.sign_event_builder(event).await.map_err(|e| {
362361
error!("Failed to sign Nostr event: {e}");
363362
Error::Crypto("Failed to sign Nostr event".to_string())
364363
})?;
365-
trace!("sending event {send_event:?}");
366364
self.client.send_event(&send_event).await.map_err(|e| {
367365
error!("Failed to send Nostr event: {e}");
368366
Error::Network("Failed to send Nostr event".to_string())
@@ -396,6 +394,16 @@ impl NotificationJsonTransportApi for NostrClient {
396394
Ok(None)
397395
}
398396
}
397+
398+
async fn resolve_public_chain(
399+
&self,
400+
id: &str,
401+
chain_type: BlockchainType,
402+
) -> Result<Vec<nostr::event::Event>> {
403+
Ok(self
404+
.fetch_events(chain_filter(id, chain_type), Some(SortOrder::Asc), None)
405+
.await?)
406+
}
399407
}
400408

401409
#[derive(Clone)]
@@ -508,7 +516,7 @@ impl NostrConsumer {
508516
{
509517
let success = match event.kind {
510518
Kind::EncryptedDirectMessage | Kind::GiftWrap => {
511-
info!("Received encrypted direct message: {event:?}");
519+
trace!("Received encrypted direct message: {event:?}");
512520
match handle_direct_message(
513521
event.clone(),
514522
&signer,
@@ -525,7 +533,7 @@ impl NostrConsumer {
525533
}
526534
}
527535
Kind::TextNote => {
528-
info!("Received text note: {event:?}");
536+
trace!("Received text note: {event:?}");
529537
match handle_public_event(
530538
event.clone(),
531539
&client_id,
@@ -629,7 +637,7 @@ async fn handle_public_event(
629637
.await
630638
{
631639
let decrypted = decrypt_public_chain_event(&encrypted_data.payload, &chain_keys)?;
632-
info!("Handling public chain event: {:?}", decrypted);
640+
trace!("Handling public chain event: {decrypted:?}");
633641
handle_event(decrypted, node_id, handlers).await?
634642
}
635643
}

crates/bcr-ebill-core/src/blockchain/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub enum Error {
7070
InvalidBlockchainType(String),
7171
}
7272

73-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
73+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
7474
pub enum BlockchainType {
7575
#[serde(rename = "bill")]
7676
Bill,

crates/bcr-ebill-core/src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ pub const ACCEPT_DEADLINE_SECONDS: u64 = 86400 * 2; // 2 days
33
pub const RECOURSE_DEADLINE_SECONDS: u64 = 86400 * 2; // 2 days
44

55
pub const VALID_CURRENCIES: [&str; 1] = ["sat"];
6+
7+
// the chain prefix we use when tagging our events on Nostr
8+
pub const BCR_NOSTR_CHAIN_PREFIX: &str = "bitcredit";

crates/bcr-ebill-transport/src/event/bill_events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl BillChainEvent {
155155
let invite = ChainInvite::bill(self.bill.id.to_owned(), self.bill_keys.clone());
156156
self.new_participants()
157157
.keys()
158-
.map(|node_id| (node_id.to_owned(), Event::new_chain(invite.clone())))
158+
.map(|node_id| (node_id.to_owned(), Event::new_invite(invite.clone())))
159159
.collect()
160160
}
161161
}

crates/bcr-ebill-transport/src/event/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ pub enum EventType {
1212
Bill,
1313
/// Public Bill chain events
1414
BillChain,
15+
/// Private Bill invites with keys
16+
BillChainInvite,
1517
}
1618

1719
impl EventType {
1820
pub fn all() -> Vec<EventType> {
19-
vec![EventType::Bill, EventType::BillChain]
21+
vec![
22+
EventType::Bill,
23+
EventType::BillChain,
24+
EventType::BillChainInvite,
25+
]
2026
}
2127
}
2228

@@ -47,6 +53,10 @@ impl<T: Serialize> Event<T> {
4753
pub fn new_chain(data: T) -> Self {
4854
Self::new(EventType::BillChain, data)
4955
}
56+
57+
pub fn new_invite(data: T) -> Self {
58+
Self::new(EventType::BillChainInvite, data)
59+
}
5060
}
5161

5262
/// The event version that is used for all events if no specific version

0 commit comments

Comments
 (0)