diff --git a/src/clienthandler.rs b/src/clienthandler.rs index dd9bcb0..43256e1 100644 --- a/src/clienthandler.rs +++ b/src/clienthandler.rs @@ -200,6 +200,7 @@ impl ClientHandler { //We receive a result of a credential validation request or service registration from the credential gateway. let mut receive_credential_events_handler_lock = self.receive_credential_events_handler.lock(); if let Ok(event) = receive_credential_events_handler_lock.await.try_recv() { + //TODO: if ServiceDeliveranceResult, flush out the internal buffer to store in our NoteProcessor client_events.push(event); } } @@ -392,6 +393,7 @@ impl ClientHandler { self.filter_events(*msg).await; //TODO: we should link our filtering policy to our db storing, //otherwise this is a severe DoS vector + //TODO: move is_ephemeral check when receive result from CredentialGateway is in NoteProcessor if !is_ephemeral(&msg_2) { let db_request = DbRequest::WriteEvent(*msg_2); write_db.push(db_request); diff --git a/src/credentialgateway.rs b/src/credentialgateway.rs index 223e8dc..5c14198 100644 --- a/src/credentialgateway.rs +++ b/src/credentialgateway.rs @@ -22,7 +22,9 @@ use nostr::{Event, Kind, Tag, TagKind}; use staking_credentials::common::msgs::{AssetProofFeatures, CredentialsFeatures, CredentialPolicy, ServicePolicy}; use staking_credentials::common::utils::Proof; + use staking_credentials::issuance::issuerstate::IssuerState; +use staking_credentials::redemption::redemption::RedemptionEngine; use staking_credentials::common::msgs::{CredentialAuthenticationResult, CredentialAuthenticationPayload, Decodable, ServiceDeliveranceResult, FromHex}; use staking_credentials::common::utils::Credentials; @@ -122,18 +124,24 @@ impl IssuanceManager { } -struct RedemptionManager { } +struct RedemptionManager { + redemption_engine: RedemptionEngine, +} impl RedemptionManager { - fn validate_service_deliverance(&mut self, client_id: u64, ev: Event) -> Result { + fn validate_service_deliverance(&mut self, client_id: u64, ev: Event) -> ServiceDeliveranceResult { let service_id = 0; let ret = false; let reason = vec![]; + //TODO: take a PubklicKey and a Credential and a Signature. Outcome a boolean. + //let valid = self.redemption_engine.verify_credentials(); + let mut service_deliverance_result = ServiceDeliveranceResult::new(service_id, ret, reason); - Ok(service_deliverance_result) + // We always return a valid ServiceDeliveranceResult, all errors should be treated as invalid. + service_deliverance_result } } @@ -189,8 +197,10 @@ impl CredentialGateway { issuance_engine: issuer_state, }; - let redemption_manager = RedemptionManager { + let redemption_engine = RedemptionEngine::new(); + let redemption_manager = RedemptionManager { + redemption_engine, }; let hosted_services = HashMap::new(); @@ -237,25 +247,36 @@ impl CredentialGateway { } let mut proofs_to_verify = Vec::new(); + let mut redemption_result = Vec::new(); //TODO: change serialization of credential message from bytes payload to encompass ServiceDelivereRequest. //let mut deliverance_result_queue = Vec::new(); for event in credential_queue { match event { ClientEvents::Credential { client_id, event } => { - match event.tags[0].kind() { + // let credential_msg = event.decode_from_nostr(); + let credential_type = 0; // credential_msg.get_credential_type(); + match credential_type { //TODO: decode and check the exact credential requested from client - TagKind::Credential => { + 0 => { match self.issuance_manager.register_authentication_request(client_id, event) { Ok(proof) => { println!("[CIVKITD] - CREDENTIAL: adding a merkle block proof to verify"); proofs_to_verify.push(proof); }, Err(error) => { - println!("[CIVKITD] - CREDENTIAL: authentication request error {:?}", error); + println!("[CIVKITD] - CREDENTIAL: authentication request error {:?}", error); } } }, - _ => { println!("[CIVKITD] - CREDENTIAL: credential event error: unknown kind"); } + 1 => { println!("[CIVKITD] - CREDENTIAL event error: gateway should not receive CredentialAuthenticationResult"); }, + 3 => { + let result = self.redemption_manager.validate_service_deliverance(client_id, event); + println!("[CIVKITD] - CREDENTIAL: service deliverance validation result {}", result.ret); + //TODO: build back Nostr event here ? + redemption_result.push(result); + }, + 4 => { println!("[CIVKITD] - CREDENTIAL event error: gateway should not receive ServiceDeliveranceResult"); }, + _ => { println!("[CIVKITD] - CREDENTIAL: credential event error: unknown type"); } } }, _ => {}, @@ -268,7 +289,6 @@ impl CredentialGateway { send_bitcoind_request_lock.await.send(BitcoindRequest::CheckMerkleProof { request_id, proof }); } - let mut validated_requests = Vec::new(); { let mut receive_bitcoind_result_handler_lock = self.receive_bitcoind_result_handler.lock(); @@ -297,6 +317,13 @@ impl CredentialGateway { } } + { + for result in redemption_result { + let mut send_credential_lock = self.send_credential_events_gateway.lock(); + //TODO: send back event + } + } + let mut service_registration_request = Vec::new(); { let mut receive_events_gateway_lock = self.receive_events_gateway.lock(); diff --git a/src/kindprocessor.rs b/src/kindprocessor.rs index c4be9e6..285ef61 100644 --- a/src/kindprocessor.rs +++ b/src/kindprocessor.rs @@ -38,6 +38,8 @@ pub struct NoteProcessor { receive_db_requests_manager: TokioMutex>, + //TODO: add buffer of pending write DB for each client. + config: Config, } @@ -78,6 +80,8 @@ impl NoteProcessor { loop { sleep(Duration::from_millis(1000)).await; + //TODO: wait for the ServiceDeliveranceResult of the CredentialGateway before to trigger write_new_event_db. + let mut replay_request = Vec::new(); let mut ok_events = Vec::new(); {