Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RedemptionEngine and flow back ServiceDeliveranceResult #93

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/clienthandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down
45 changes: 36 additions & 9 deletions src/credentialgateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ServiceDeliveranceResult, ()> {
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
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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"); }
}
},
_ => {},
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/kindprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct NoteProcessor {

receive_db_requests_manager: TokioMutex<mpsc::UnboundedReceiver<DbRequest>>,

//TODO: add buffer of pending write DB for each client.

config: Config,
}

Expand Down Expand Up @@ -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();
{
Expand Down