Skip to content

Commit

Permalink
feat: add debug command
Browse files Browse the repository at this point in the history
  • Loading branch information
edytapawlak committed Jan 29, 2025
1 parent 36a7823 commit c332eca
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 117 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
keri-core = { version = "0.15.1", features = ["oobi", "mailbox"]}
keri-controller = { version = "0.15.1"}
keri-core = { version = "0.16.0", features = ["oobi", "mailbox"]}
keri-controller = { version = "0.16.0"}
said = { version = "0.4.0", features = ["macros"]}
cesrox = { version = "0.1.6", features = ["cesr-proof"] }
anyhow = { version = "1.0" }
Expand All @@ -30,9 +30,10 @@ serde_yaml = "0.9.30"
base64 = "0.22.1"
tabled = "0.16.0"
futures = "0.3.31"
teliox = {version ="0.16.0"}
tempfile = "3.10.0"

[dev-dependencies]
tempfile = "3.10.0"

[[bin]]
name = "dkms"
Expand Down
61 changes: 0 additions & 61 deletions src/debug.rs

This file was deleted.

17 changes: 12 additions & 5 deletions src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use keri_core::event::sections::seal::EventSeal;
use keri_core::oobi::Role;
use serde::{Deserialize, Serialize};

use crate::debug::generate_temporary_identifier;
use crate::temporary_identifier::generate_temporary_identifier;
use crate::tel::save_registry;
use crate::utils::{save_identifier, save_next_seed, save_seed};
use crate::{
Expand Down Expand Up @@ -104,12 +104,19 @@ pub async fn handle_import(
.await
.unwrap();
// Find KEL
let kel = tmp_id.pull_kel(&imported.last_event_seal, witness.clone()).await.unwrap();

for msg in kel {
let id = imported.last_event_seal.prefix.clone();
let sn = imported.last_event_seal.sn;
let kel = tmp_id.pull_kel(id.clone(), 0, sn, witness.clone()).await.unwrap();
if let Some(kel) = kel {
for msg in kel {
controller.known_events.process(&msg).unwrap();
}
}
} else {
println!("Identifier {} KEL not found", &id);

};


// Find TEL
if let Some(registry_id) = &imported.registry_id {
let tel_resp = tmp_id.pull_tel(registry_id, None, witness).await;
Expand Down
4 changes: 2 additions & 2 deletions src/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
error::Error,
fmt::Display,
fs::{self, File},
fs::{self},
io::Write,
path::{Path, PathBuf},
sync::Arc,
Expand Down Expand Up @@ -261,7 +261,7 @@ next: AFmIICAHyx5VfLZR2hrpSlTYKFPE58updFl-U96YBhda";
let dir = tempfile::tempdir().unwrap();

let file_path = dir.path().join("temporary_keys.yaml");
let mut file = File::create(file_path.clone()).unwrap();
let mut file = std::fs::File::create(file_path.clone()).unwrap();
writeln!(file, "{}", &keys_yaml).unwrap();

let conf: Result<KeysConfig, _> = Figment::new().merge(Yaml::file(file_path)).extract();
Expand Down
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
use error::CliError;
use subcommands::{
data::{process_data_command, DataCommand},
identifier::{process_identifier_command, IdentifierCommand},
key::{process_key_command, KeyCommands},
log::{process_log_command, LogCommand},
mesagkesto::{process_mesagkesto_command, MesagkestoCommands},
said::{process_said_command, SaidCommands},
data::{process_data_command, DataCommand}, debug::{process_debug_command, DebugCommand}, identifier::{process_identifier_command, IdentifierCommand}, key::{process_key_command, KeyCommands}, log::{process_log_command, LogCommand}, mesagkesto::{process_mesagkesto_command, MesagkestoCommands}, said::{process_said_command, SaidCommands}
};
use utils::working_directory;

Expand All @@ -24,11 +19,10 @@ mod seed;
mod sign;
mod subcommands;
mod tel;
mod temporary_id;
mod utils;
mod verification_status;
mod verify;
mod debug;
mod temporary_identifier;

#[derive(Parser)]
#[command(author, version, about, long_about = None, help_template = help::HELP_TEMPLATE)]
Expand Down Expand Up @@ -73,6 +67,11 @@ enum Commands {

/// Shows information about working environment
Info,
///
Debug {
#[command(subcommand)]
command: DebugCommand,
}
}

#[tokio::main]
Expand Down Expand Up @@ -123,7 +122,8 @@ async fn process_command(command: Commands) -> Result<(), CliError> {
Commands::Info => {
let working_directory = working_directory()?;
println!("Working directory: {}", working_directory.to_str().unwrap());
}
},
Commands::Debug {command} => process_debug_command(command).await,
};
Ok(())
}
73 changes: 73 additions & 0 deletions src/subcommands/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use clap::Subcommand;
use keri_controller::{IdentifierPrefix, LocationScheme};

use crate::temporary_identifier::generate_temporary_identifier;

#[derive(Subcommand)]
pub enum DebugCommand {
Ksn {
/// Source OOBI in json
#[arg(short, long)]
source: String,
#[arg(short, long)]
id: IdentifierPrefix,
},
Tel {
/// Source OOBI in json
#[arg(short, long)]
source: String,
#[arg(short, long)]
registry_id: IdentifierPrefix,
#[arg(short, long)]
vc_id: Option<IdentifierPrefix>,
},
Kel {
/// Source OOBI in json
#[arg(short, long)]
source: String,
#[arg(short, long)]
identifier: IdentifierPrefix,
#[arg(long)]
sn: u64,
#[arg(short, long)]
limit: Option<u64>,
}
}

pub async fn process_debug_command(cmd: DebugCommand) {
match cmd {
DebugCommand::Ksn { source, id } => {
let tmp_id = generate_temporary_identifier().unwrap();
let source: LocationScheme = serde_json::from_str(&source).unwrap();

let ksn = tmp_id.pull_ksn(id, source).await.unwrap();

println!("{}", &ksn);

},
DebugCommand::Tel { source, vc_id, registry_id: id } => {
let tmp_id = generate_temporary_identifier().unwrap();
let source: LocationScheme = serde_json::from_str(&source).unwrap();

let tel = tmp_id.pull_tel(&id, vc_id, source).await;

println!("{}", &tel);
},
DebugCommand::Kel { source, identifier, sn, limit } => {
let tmp_id = generate_temporary_identifier().unwrap();
let source: LocationScheme = serde_json::from_str(&source).unwrap();

let limit = limit.unwrap_or(1);
let kel = tmp_id.pull_kel(identifier.clone(), sn, limit, source).await.unwrap();
match kel {
Some(kel) => {
let kel_str = kel.into_iter().map(|event| String::from_utf8(event.to_cesr().unwrap()).unwrap()).collect::<Vec<_>>().join("\n");
println!("{}", kel_str);

},
None => println!("Identifier {} not found", identifier.to_string()),
}

},
}
}
1 change: 1 addition & 0 deletions src/subcommands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod key;
pub mod log;
pub mod mesagkesto;
pub mod said;
pub mod debug;
37 changes: 0 additions & 37 deletions src/temporary_id.rs

This file was deleted.

82 changes: 82 additions & 0 deletions src/temporary_identifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::sync::Arc;

use keri_controller::{communication::{Communication, SendingError}, error::ControllerError, identifier::nontransferable::NontransferableIdentifier, known_events::KnownEvents, BasicPrefix, IdentifierPrefix, LocationScheme};
use keri_core::{actor::{error::ActorError, prelude::Message, simple_controller::PossibleResponse, QueryError, SignedQueryError}, processor::escrow::EscrowConfig, signer::Signer, transport::default::DefaultTransport};
use teliox::transport::TelTransport;
use tempfile::Builder;

use crate::export::ExportError;

pub struct TemporaryIdentifier {
signer: Arc<Signer>,
id: NontransferableIdentifier
}

pub fn generate_temporary_identifier() -> Result<TemporaryIdentifier, ExportError> {
// create temporary identifier to pull KEL from witnesses
let signer = Arc::new(Signer::new());
let bp = BasicPrefix::Ed25519NT(signer.public_key());
let transport = Box::new(DefaultTransport::new());
let tel_transport = Box::new(TelTransport);
let tmp_dir = Builder::new().prefix("tmp-dir").tempdir().map_err(|_e| ExportError::TemporaryId("Temporary file creation error".to_string()))?;

let tmp_events =
Arc::new(KnownEvents::new(tmp_dir.path().to_path_buf(), EscrowConfig::default()).map_err(|_e| ExportError::TemporaryId("Temporary identifier creation error".to_string()))?);
let comm = Arc::new(Communication {
events: tmp_events.clone(),
transport,
tel_transport,
});
let tmp_id = NontransferableIdentifier::new(bp, comm.clone());
Ok(TemporaryIdentifier { signer, id: tmp_id })
}

impl TemporaryIdentifier {
pub async fn pull_kel(&self, id: IdentifierPrefix, sn: u64, limit: u64, witness_location: LocationScheme) -> Result<Option<Vec<Message>>, ControllerError> {
let witness_id = match &witness_location.eid {
IdentifierPrefix::Basic(basic_prefix) => basic_prefix.clone(),
_ => unreachable!("Witness identifier must be basic prefix"),
};

let qry = self.id.query_log(id, sn, limit, witness_id);
let signature = self.id.sign(self.signer.sign(qry.encode()?).unwrap());
let resp = self.id
.finalize_query(witness_location, qry, signature)
.await;
match resp {
Ok(PossibleResponse::Kel(vec)) => Ok(Some(vec)),
Ok(_) => unreachable!("Unexpected response from witness"),
Err(ControllerError::SendingError(SendingError::ActorInternalError(ActorError::QueryError(SignedQueryError::QueryError(QueryError::UnknownId { id: _ }))))) => Ok(None),
Err(e) => Err(e)
}
}


pub async fn pull_ksn(&self, id: IdentifierPrefix, witness_location: LocationScheme) -> Result<String, ControllerError> {
let witness_id = match &witness_location.eid {
IdentifierPrefix::Basic(basic_prefix) => basic_prefix.clone(),
_ => unreachable!("Witness identifier must be basic prefix"),
};

let qry = self.id.query_ksn(&id, witness_id);
let signature = self.id.sign(self.signer.sign(qry.encode()?).unwrap());
let resp = self.id
.finalize_query(witness_location, qry, signature)
.await?;
match resp {
PossibleResponse::Ksn(ksn) => {
let ksn = serde_json::to_string_pretty(&ksn.reply.data).unwrap();
Ok(ksn)
},
_ => unreachable!("Unexpected response from witness"),
}
}


pub async fn pull_tel(&self, registry_id: &IdentifierPrefix, vc_id: Option<IdentifierPrefix>, witness: LocationScheme) -> String {
let tel_qry = self.id.query_tel(registry_id.clone(), vc_id).unwrap();
let signature = self.signer.sign(tel_qry.encode().unwrap()).unwrap();
let sig = self.id.sign(signature);
self.id.finalize_query_tel(witness, tel_qry, sig).await.unwrap()
}
}

0 comments on commit c332eca

Please sign in to comment.