Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
orthecreedence committed Mar 10, 2024
1 parent 5d3ba6b commit 2488dab
Show file tree
Hide file tree
Showing 17 changed files with 1,205 additions and 1,189 deletions.
12 changes: 4 additions & 8 deletions src/commands/agent.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::SyncToken;
use anyhow::{anyhow, Result};
use crate::{
SyncToken,
};
use stamp_aux::util::UIMessage;
use stamp_core::{
crypto::base::SecretKey,
};
use stamp_core::crypto::base::SecretKey;
//use stamp_net::Multiaddr;
use tokio::{task, sync::mpsc as channel};
use tracing::{warn};
use tokio::{sync::mpsc as channel, task};
use tracing::warn;

/*
pub fn run(bind: Multiaddr, sync_token: Option<SyncToken>, sync_join: Vec<Multiaddr>, agent_port: u32, agent_lock_after: u64, net: bool, net_join: Vec<Multiaddr>) -> Result<()> {
Expand Down
142 changes: 83 additions & 59 deletions src/commands/claim.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
use anyhow::{anyhow, Result};
use crate::{
commands::{dag, id, stamp},
config,
db,
util,
config, db, util,
};
use anyhow::{anyhow, Result};
use prettytable::Table;
use stamp_aux;
use stamp_core::{
crypto::{
base::{SecretKey, rng},
base::{rng, SecretKey},
private::MaybePrivate,
},
dag::{TransactionID, Transactions},
identity::{
Identity,
IdentityID,
claim::{Claim, ClaimID, ClaimSpec, RelationshipType},
stamp::{Stamp},
stamp::Stamp,
Identity, IdentityID,
},
rasn::{Encode, Decode},
util::{Date, Url, Public, BinaryVec, SerText, Timestamp},
rasn::{Decode, Encode},
util::{BinaryVec, Date, Public, SerText, Timestamp, Url},
};
use std::convert::TryFrom;
use std::ops::Deref;
Expand All @@ -39,8 +36,10 @@ pub(crate) fn claim_pre_noval(id: &str) -> Result<(SecretKey, Transactions)> {
let transactions = id::try_load_single_identity(id)?;
let identity = util::build_identity(&transactions)?;
let id_str = id_str!(identity.id())?;
let master_key = util::passphrase_prompt(format!("Your master passphrase for identity {}", IdentityID::short(&id_str)), identity.created())?;
transactions.test_master_key(&master_key)
let master_key =
util::passphrase_prompt(format!("Your master passphrase for identity {}", IdentityID::short(&id_str)), identity.created())?;
transactions
.test_master_key(&master_key)
.map_err(|e| anyhow!("Incorrect passphrase: {:?}", e))?;
Ok((master_key, transactions))
}
Expand All @@ -52,36 +51,43 @@ pub(crate) fn claim_pre(id: &str, prompt: &str) -> Result<(SecretKey, Transactio
}

fn unwrap_maybe<T, F>(maybe: &MaybePrivate<T>, masterkey_fn: F) -> Result<T>
where T: Encode + Decode + Clone,
F: FnOnce() -> Result<SecretKey>,
where
T: Encode + Decode + Clone,
F: FnOnce() -> Result<SecretKey>,
{
if maybe.has_private() {
let master_key = masterkey_fn()?;
maybe.open(&master_key)
.map_err(|e| anyhow!("Unable to open private claim: {}", e))
maybe.open(&master_key).map_err(|e| anyhow!("Unable to open private claim: {}", e))
} else {
let mut rng = rng::chacha20();
let fake_master_key = SecretKey::new_xchacha20poly1305(&mut rng)
.map_err(|e| anyhow!("Unable to generate key: {}", e))?;
maybe.open(&fake_master_key)
.map_err(|e| anyhow!("Unable to open claim: {}", e))
let fake_master_key = SecretKey::new_xchacha20poly1305(&mut rng).map_err(|e| anyhow!("Unable to generate key: {}", e))?;
maybe.open(&fake_master_key).map_err(|e| anyhow!("Unable to open claim: {}", e))
}
}

pub fn check(claim_id: &str) -> Result<()> {
let transactions = db::find_identity_by_prefix("claim", claim_id)?
.ok_or(anyhow!("Identity with claim id {} was not found", claim_id))?;
let transactions =
db::find_identity_by_prefix("claim", claim_id)?.ok_or(anyhow!("Identity with claim id {} was not found", claim_id))?;
let identity = util::build_identity(&transactions)?;
let id_str = id_str!(identity.id())?;
let claim = identity.claims().iter()
let claim = identity
.claims()
.iter()
.find(|x| id_str!(x.id()).map(|x| x.starts_with(claim_id)).ok() == Some(true))
.ok_or(anyhow!("Couldn't find the claim {} in identity {}", claim_id, IdentityID::short(&id_str)))?;
let claim_id_str = id_str!(claim.id())?;
match stamp_aux::claim::check_claim(&transactions, claim) {
Ok(url) => {
let green = dialoguer::console::Style::new().green();
println!("\nThe claim {} has been {}!\n", ClaimID::short(&claim_id_str), green.apply_to("verified"));
println!("{}", util::text_wrap(&format!("It is very likely that the identity {} owns the resource {}", IdentityID::short(&id_str), url)));
println!(
"{}",
util::text_wrap(&format!(
"It is very likely that the identity {} owns the resource {}",
IdentityID::short(&id_str),
url
))
);
Ok(())
}
Err(err) => {
Expand Down Expand Up @@ -110,8 +116,10 @@ pub fn view(id: &str, claim_id: &str, output: &str) -> Result<()> {

let id_str = id_str!(identity.id())?;
let masterkey_fn = || {
let master_key = util::passphrase_prompt(format!("Your master passphrase for identity {}", IdentityID::short(&id_str)), identity.created())?;
identity.test_master_key(&master_key)
let master_key =
util::passphrase_prompt(format!("Your master passphrase for identity {}", IdentityID::short(&id_str)), identity.created())?;
identity
.test_master_key(&master_key)
.map_err(|e| anyhow!("Incorrect passphrase: {:?}", e))?;
Ok(master_key)
};
Expand Down Expand Up @@ -164,19 +172,23 @@ pub fn list(id: &str, private: bool, verbose: bool) -> Result<()> {
let identity = util::build_identity(&transactions)?;
let master_key_maybe = if private {
let id_str = id_str!(identity.id())?;
let master_key = util::passphrase_prompt(format!("Your master passphrase for identity {}", IdentityID::short(&id_str)), identity.created())?;
identity.test_master_key(&master_key)
let master_key =
util::passphrase_prompt(format!("Your master passphrase for identity {}", IdentityID::short(&id_str)), identity.created())?;
identity
.test_master_key(&master_key)
.map_err(|e| anyhow!("Incorrect passphrase: {:?}", e))?;
Some(master_key)
} else {
None
};
let ts_fake = Timestamp::from_str("0000-01-01T00:00:00.000Z")
.map_err(|e| anyhow!("Error creating fake timestamp: {:?}", e))?;
let claim_list = identity.claims().iter()
let ts_fake = Timestamp::from_str("0000-01-01T00:00:00.000Z").map_err(|e| anyhow!("Error creating fake timestamp: {:?}", e))?;
let claim_list = identity
.claims()
.iter()
.map(|claim| {
let claim_id: TransactionID = claim.id().deref().clone();
let ts = transactions.iter()
let ts = transactions
.iter()
.find(|t| t.id() == &claim_id)
.map(|t| t.entry().created().clone())
.unwrap_or_else(|| ts_fake.clone());
Expand All @@ -191,33 +203,35 @@ pub fn stamp_list(id: &str, claim_id_or_name: &str, verbose: bool) -> Result<()>
let transactions = id::try_load_single_identity(id)?;
let identity = util::build_identity(&transactions)?;
let id_str = id_str!(identity.id())?;
let claim = identity.claims().iter()
let claim = identity
.claims()
.iter()
.find(|x| {
x.name().as_ref().map(|y| y == claim_id_or_name).unwrap_or(false) ||
id_str!(x.id()).unwrap_or("".into()).starts_with(claim_id_or_name)
x.name().as_ref().map(|y| y == claim_id_or_name).unwrap_or(false)
|| id_str!(x.id()).unwrap_or("".into()).starts_with(claim_id_or_name)
})
.ok_or_else(|| anyhow!("Could not find claim {} in identity {}.", claim_id_or_name, id_str))?;
let stamps = claim.stamps().iter()
.collect::<Vec<_>>();
let stamps = claim.stamps().iter().collect::<Vec<_>>();
stamp::print_stamps_table(&stamps, verbose, false)?;
Ok(())
}

fn find_stamp_by_id<'a>(identity: &'a Identity, stamp_id: &str) -> Option<&'a Stamp> {
identity.claims().iter()
.find_map(|c| {
c.stamps().iter()
.find(|s| id_str!(s.id()).unwrap_or("".into()).starts_with(stamp_id))
})
identity.claims().iter().find_map(|c| {
c.stamps()
.iter()
.find(|s| id_str!(s.id()).unwrap_or("".into()).starts_with(stamp_id))
})
}

pub fn stamp_view(id: &str, stamp_id: &str) -> Result<()> {
let transactions = id::try_load_single_identity(id)?;
let identity = util::build_identity(&transactions)?;
let id_str = id_str!(identity.id())?;
let stamp = find_stamp_by_id(&identity, stamp_id)
.ok_or_else(|| anyhow!("Could not find stamp {} in identity {}.", stamp_id, id_str))?;
let stamp_text = stamp.serialize_text()
let stamp =
find_stamp_by_id(&identity, stamp_id).ok_or_else(|| anyhow!("Could not find stamp {} in identity {}.", stamp_id, id_str))?;
let stamp_text = stamp
.serialize_text()
.map_err(|e| anyhow!("Problem serializing stamp transaction: {:?}", e))?;
println!("{}", stamp_text);
Ok(())
Expand All @@ -228,19 +242,24 @@ pub fn stamp_delete(id: &str, stamp_id: &str, stage: bool, sign_with: Option<&st
let transactions = id::try_load_single_identity(id)?;
let identity = util::build_identity(&transactions)?;
let id_str = id_str!(identity.id())?;
let stamp = find_stamp_by_id(&identity, stamp_id)
.ok_or_else(|| anyhow!("Could not find stamp {} in identity {}.", stamp_id, id_str))?;
let stamp_text = stamp.serialize_text()
let stamp =
find_stamp_by_id(&identity, stamp_id).ok_or_else(|| anyhow!("Could not find stamp {} in identity {}.", stamp_id, id_str))?;
let stamp_text = stamp
.serialize_text()
.map_err(|e| anyhow!("Problem serializing stamp transaction: {:?}", e))?;
println!("{}", stamp_text);
println!("----------");
if !util::yesno_prompt("Do you wish to delete the above stamp? [Y/n]", "Y")? {
println!("Aborted.");
return Ok(());
}
let trans = transactions.delete_stamp(&hash_with, Timestamp::now(), stamp.id().clone())
let trans = transactions
.delete_stamp(&hash_with, Timestamp::now(), stamp.id().clone())
.map_err(|e| anyhow!("Problem creating stamp delete transaction: {:?}", e))?;
let master_key = util::passphrase_prompt(&format!("Your current master passphrase for identity {}", IdentityID::short(&id_str)), identity.created())?;
let master_key = util::passphrase_prompt(
&format!("Your current master passphrase for identity {}", IdentityID::short(&id_str)),
identity.created(),
)?;
let signed = util::sign_helper(&identity, trans, &master_key, stage, sign_with)?;
dag::save_or_stage(transactions, signed, stage)?;
Ok(())
Expand All @@ -256,7 +275,8 @@ pub fn print_claims_table(claims: &Vec<(Claim, Timestamp)>, master_key_maybe: Op
macro_rules! extract_str {
($maybe:expr, $tostr:expr) => {
if let Some(master_key) = master_key_maybe.as_ref() {
$maybe.open(master_key)
$maybe
.open(master_key)
.map(|val| {
let strval = $tostr(val);
if $maybe.has_private() {
Expand All @@ -269,9 +289,7 @@ pub fn print_claims_table(claims: &Vec<(Claim, Timestamp)>, master_key_maybe: Op
.unwrap_or_else(|e| format!("Decryption error: {}", e))
} else {
match $maybe {
MaybePrivate::Public(val) => {
$tostr(val.clone())
}
MaybePrivate::Public(val) => $tostr(val.clone()),
MaybePrivate::Private { .. } => {
let red = dialoguer::console::Style::new().red();
format!("{}", red.apply_to("<private>"))
Expand All @@ -285,10 +303,17 @@ pub fn print_claims_table(claims: &Vec<(Claim, Timestamp)>, master_key_maybe: Op
}
let name = claim.name().as_ref().map(|x| x.clone()).unwrap_or("-".into());
let (ty, val) = match claim.spec() {
ClaimSpec::Identity(id) => ("identity", extract_str!(id, |x: IdentityID| {
let (id_full, id_short) = id_str_split!(&x);
if verbose { id_full } else { id_short }
})),
ClaimSpec::Identity(id) => (
"identity",
extract_str!(id, |x: IdentityID| {
let (id_full, id_short) = id_str_split!(&x);
if verbose {
id_full
} else {
id_short
}
}),
),
ClaimSpec::Name(name) => ("name", extract_str!(name)),
ClaimSpec::Birthday(birthday) => ("birthday", extract_str!(birthday, |x: Date| x.to_string())),
ClaimSpec::Email(email) => ("email", extract_str!(email)),
Expand Down Expand Up @@ -327,4 +352,3 @@ pub fn print_claims_table(claims: &Vec<(Claim, Timestamp)>, master_key_maybe: Op
}
table.printstd();
}

12 changes: 2 additions & 10 deletions src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
use crate::{commands, config, db, util};
use anyhow::{anyhow, Result};
use crate::{
commands,
config,
db,
util,
};
use std::convert::TryFrom;

pub fn set_default(search: &str) -> Result<()> {
let mut conf = config::load()?;
let identities = db::list_local_identities(Some(search))?;
if identities.len() > 1 {
let identities_vec = identities.iter()
.map(|x| util::build_identity(x))
.collect::<Result<Vec<_>>>()?;
let identities_vec = identities.iter().map(|x| util::build_identity(x)).collect::<Result<Vec<_>>>()?;
commands::id::print_identities_table(&identities_vec, false);
Err(anyhow!("Multiple identities matched that search"))?;
} else if identities.len() == 0 {
Expand All @@ -26,4 +19,3 @@ pub fn set_default(search: &str) -> Result<()> {
conf.default_identity = Some(id_str);
config::save(&conf)
}

Loading

0 comments on commit 2488dab

Please sign in to comment.