Skip to content

Commit ab058a1

Browse files
tomproCopilot
andauthored
Public chain handling (#534)
* Refactor handlers and subscriptions * Structured subscriptions * Docs * Chain key resolver * Update crates/bcr-ebill-transport/src/chain_keys.rs Co-authored-by: Copilot <[email protected]> * Review fixes --------- Co-authored-by: Copilot <[email protected]>
1 parent 8af80b8 commit ab058a1

File tree

13 files changed

+525
-223
lines changed

13 files changed

+525
-223
lines changed

crates/bcr-ebill-api/src/service/contact_service.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ pub trait ContactServiceApi: ServiceTraitBounds {
9999
) -> Result<Contact>;
100100

101101
/// Returns whether a given npub (as hex) is in our contact list.
102-
#[allow(dead_code)]
103102
async fn is_known_npub(&self, npub: &PublicKey) -> Result<bool>;
104103

104+
/// Returns the Npubs we want to subscribe to on Nostr.
105+
async fn get_nostr_npubs(&self) -> Result<Vec<PublicKey>>;
106+
105107
/// opens and decrypts the attached file from the given contact
106108
async fn open_and_decrypt_file(
107109
&self,
@@ -533,6 +535,14 @@ impl ContactServiceApi for ContactService {
533535
.unwrap_or(false))
534536
}
535537

538+
/// Returns the Npubs we want to subscribe to on Nostr.
539+
async fn get_nostr_npubs(&self) -> Result<Vec<PublicKey>> {
540+
Ok(self
541+
.nostr_contact_store
542+
.get_npubs(vec![TrustLevel::Trusted, TrustLevel::Participant])
543+
.await?)
544+
}
545+
536546
async fn open_and_decrypt_file(
537547
&self,
538548
id: &str,

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,10 @@ mod tests {
662662
};
663663
use super::*;
664664
use crate::tests::tests::{
665-
MockBillChainStoreApiMock, MockBillStoreApiMock, MockNostrChainEventStore,
666-
MockNostrContactStore, MockNostrEventOffsetStoreApiMock, MockNostrQueuedMessageStore,
667-
MockNotificationStoreApiMock, TEST_BILL_ID, TEST_PRIVATE_KEY_SECP, TEST_PUB_KEY_SECP,
665+
MockBillChainStoreApiMock, MockBillStoreApiMock, MockChainKeyService,
666+
MockNostrChainEventStore, MockNostrContactStore, MockNostrEventOffsetStoreApiMock,
667+
MockNostrQueuedMessageStore, MockNotificationStoreApiMock, TEST_BILL_ID,
668+
TEST_PRIVATE_KEY_SECP, TEST_PUB_KEY_SECP,
668669
};
669670

670671
fn check_chain_payload(event: &EventEnvelope, bill_event_type: BillEventType) -> bool {
@@ -1893,6 +1894,7 @@ mod tests {
18931894
let bill_store = Arc::new(MockBillStoreApiMock::new());
18941895
let bill_blockchain_store = Arc::new(MockBillChainStoreApiMock::new());
18951896
let nostr_contact_store = Arc::new(MockNostrContactStore::new());
1897+
let chain_key_store = Arc::new(MockChainKeyService::new());
18961898
let _ = create_nostr_consumer(
18971899
clients,
18981900
contact_service,
@@ -1902,6 +1904,7 @@ mod tests {
19021904
bill_blockchain_store,
19031905
bill_store,
19041906
nostr_contact_store,
1907+
chain_key_store,
19051908
)
19061909
.await;
19071910
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use bcr_ebill_persistence::company::CompanyStoreApi;
1010
use bcr_ebill_persistence::nostr::{
1111
NostrChainEventStoreApi, NostrContactStoreApi, NostrQueuedMessageStoreApi,
1212
};
13+
use bcr_ebill_transport::chain_keys::ChainKeyServiceApi;
1314
use bcr_ebill_transport::handler::{
1415
BillChainEventHandler, LoggingEventHandler, NotificationHandlerApi,
1516
};
@@ -50,6 +51,7 @@ pub async fn create_nostr_clients(
5051
keys,
5152
config.nostr_config.relays.clone(),
5253
nostr_name,
54+
true,
5355
)];
5456

5557
// optionally collect all company accounts
@@ -67,6 +69,7 @@ pub async fn create_nostr_clients(
6769
keys,
6870
config.nostr_config.relays.clone(),
6971
company.name.clone(),
72+
false,
7073
));
7174
}
7275
}
@@ -119,6 +122,7 @@ pub async fn create_nostr_consumer(
119122
bill_blockchain_store: Arc<dyn BillChainStoreApi>,
120123
bill_store: Arc<dyn BillStoreApi>,
121124
nostr_contact_store: Arc<dyn NostrContactStoreApi>,
125+
chain_key_service: Arc<dyn ChainKeyServiceApi>,
122126
) -> Result<NostrConsumer> {
123127
// we need one nostr client for nostr interactions
124128
let transport = match clients.first() {
@@ -141,6 +145,12 @@ pub async fn create_nostr_consumer(
141145
)),
142146
];
143147
debug!("initializing nostr consumer for {} clients", clients.len());
144-
let consumer = NostrConsumer::new(clients, contact_service, handlers, nostr_event_offset_store);
148+
let consumer = NostrConsumer::new(
149+
clients,
150+
contact_service,
151+
handlers,
152+
nostr_event_offset_store,
153+
chain_key_service,
154+
);
145155
Ok(consumer)
146156
}

0 commit comments

Comments
 (0)