Skip to content

Commit

Permalink
Merge pull request #5 from MatthewDarnell/refactor-api-loop
Browse files Browse the repository at this point in the history
Refactor api loop
  • Loading branch information
MatthewDarnell authored Mar 9, 2024
2 parents 958e179 + 6ccc975 commit 12215ee
Show file tree
Hide file tree
Showing 13 changed files with 463 additions and 545 deletions.
4 changes: 2 additions & 2 deletions api/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ impl TransferTransaction {
info!("Setting Expiration Tick For Transaction To {}", tick + TICK_OFFSET);
let digest: Vec<u8> = k12_bytes(&t.as_bytes_without_signature());
//let mut sub_seed: [u8; 32] = [0; 32];
let mut sub_seed: Vec<u8> = get_subseed(source_identity.seed.as_str()).expect("Failed To Get SubSeed!");
let sub_seed: Vec<u8> = get_subseed(source_identity.seed.as_str()).expect("Failed To Get SubSeed!");
#[allow(unused_assignments)]
let mut sig: [u8; 64] = [0; 64];

sig = sign_raw(&sub_seed, &pub_key_src, digest.as_slice().try_into().unwrap());
t._signature = sig.to_vec();
t
Expand Down
2 changes: 1 addition & 1 deletion crypto/src/fourq/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub fn fp2div1271(a: &mut F2elmT) {
let mut mask: u64;
let mut temp = [0u64; 2];

mask = 0u64.wrapping_sub((1 & a[0][0]));
mask = 0u64.wrapping_sub(1 & a[0][0]);
addcarry_u64(addcarry_u64(0, a[0][0], mask, &mut temp[0]), a[0][1], mask >> 1, &mut temp[1]);
a[0][0] = __shiftright128(temp[0], temp[1], 1);
a[0][1] = temp[1] >> 1;
Expand Down
2 changes: 1 addition & 1 deletion crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod hash {


pub mod qubic_identities {
use core::{ptr::copy_nonoverlapping, fmt::{Debug, Display}, str::FromStr};
use core::ptr::copy_nonoverlapping;
use tiny_keccak::{Hasher, IntoXof, KangarooTwelve, Xof};
use crate::{A_LOWERCASE_ASCII, hash};
use hash::k12_bytes;
Expand Down
90 changes: 19 additions & 71 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rocket::fairing::{Fairing, Info, Kind};

#[rocket::main]
async fn main() {

let qubic_ascii_art_logo: &str = "
....................
..... .....
Expand Down Expand Up @@ -238,63 +239,6 @@ async fn main() {
tx.send(response).unwrap();
continue;
}
else if method == &"add_identity".to_string() {
let seed = map.get(&"seed".to_string()).unwrap();
let mut id: identity::Identity = identity::Identity::new(seed.as_str());
let message_id = map.get(&"message_id".to_string()).unwrap();
let mut response: HashMap<String, String> = HashMap::new();
if let Some(pass) = map.get(&"password".to_string()) {
match crud::master_password::get_master_password(get_db_path().as_str()) {
Ok(master_password) => {
match crypto::passwords::verify_password(pass.as_str(), master_password[1].as_str()) {
Ok(verified) => {
if !verified {
response.insert("message_id".to_string(), message_id.to_string());
response.insert("status".to_string(), "Invalid Password!".to_string());
} else {
id = id.encrypt_identity(pass.as_str()).unwrap();
match crud::insert_new_identity(get_db_path().as_str(), &id) {
Ok(_) => {
error!("Failed To Insert Identity");
response.insert("message_id".to_string(), message_id.to_string());
response.insert("status".to_string(), "200".to_string());
},
Err(err) => {
response.insert("message_id".to_string(), message_id.to_string());
response.insert("status".to_string(), err.to_string());
error!("Failed To Insert! {:?}", err);
}
}
}
},
Err(err) => {
response.insert("message_id".to_string(), message_id.to_string());
response.insert("status".to_string(), err.to_string());
error!("Failed To Verify Master Password!");
}
}
},
Err(_) => {
response.insert("message_id".to_string(), message_id.to_string());
response.insert("status".to_string(), "No Master Password Set!".to_string());
}
}
} else {
match crud::insert_new_identity(get_db_path().as_str(), &id) {
Ok(_) => {
info!("Inserted New Identity {}", id.identity.as_str());
response.insert("message_id".to_string(), message_id.to_string());
response.insert("status".to_string(), "200".to_string());
},
Err(err) => {
response.insert("message_id".to_string(), message_id.to_string());
response.insert("status".to_string(), err.to_string());
error!("Failed To Insert! {:?}", err);
}
}
}
tx.send(response).unwrap();
}
}
},
Err(_) => {
Expand Down Expand Up @@ -385,21 +329,25 @@ async fn main() {
.merge(("address", host.as_str()));
rocket::custom(figment)
.mount("/", routes![
routes::info::latest_tick,
routes::info::is_wallet_encrypted,
routes::info::encrypt_wallet,
routes::info::set_master_password,
routes::identity::balance,
routes::identity::add_identity,
routes::identity::create_random_identity,
routes::identity::add_identity_with_password,
routes::identity::get_identities,
routes::identity::get_identity_from_seed,

routes::info::info,
routes::info::peers,
routes::info::download_wallet,
routes::info::balance,
routes::info::add_peer,
routes::info::add_identity,
routes::info::create_random_identity,
routes::info::add_identity_with_password,
routes::info::get_identities,
routes::info::get_identity_from_seed,
routes::info::transfer
routes::info::latest_tick,

routes::peer::peers,
routes::peer::add_peer,

routes::transaction::transfer,

routes::wallet::is_wallet_encrypted,
routes::wallet::encrypt_wallet,
routes::wallet::set_master_password,
routes::wallet::download_wallet
])
.manage(std::sync::Mutex::new(tx))
.manage(std::sync::Mutex::new(rx_server_route_responses_from_thread))
Expand Down
6 changes: 5 additions & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
pub mod info;
pub mod info;
pub mod peer;
pub mod identity;
pub mod wallet;
pub mod transaction;
124 changes: 124 additions & 0 deletions src/routes/identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use rocket::get;
use store::get_db_path;
use store::sqlite::crud::insert_new_identity;
use store::sqlite::crud::master_password::get_master_password;
use crypto::passwords::verify_password;

#[get("/balance/<address>")]
pub fn balance(address: &str) -> String {
match store::sqlite::crud::fetch_balance_by_identity(store::get_db_path().as_str(), address) {
Ok(value) => { format!("{:?}", value) },
Err(error) => format!("{}", error)
}
}

#[get("/identities")]
pub fn get_identities() -> String {
match store::sqlite::crud::fetch_all_identities_full(store::get_db_path().as_str()) {
Ok(v) => {
let mut response: Vec<String> = vec![];
for identity in &v {
let encrypted: String = match identity.encrypted {
true => "true".to_string(),
_ => "false".to_string()
};
response.push(identity.identity.clone());
response.push(encrypted);
}
format!("{:?}", response)
},
Err(err) => format!("{}", err)
}
}

#[get("/identity/from_seed/<seed>")]
pub fn get_identity_from_seed(seed: &str) -> String {
let i: identity::Identity = identity::Identity::new(seed);
format!("{}", i.identity.as_str())
}


#[get("/identity/new/<password>")]
pub fn create_random_identity(password: &str) -> String {
let mut seed_string: String = String::from("");
while seed_string.len() < 55 {
let temp_seed: Vec<u8> = crypto::random::random_bytes(32);
for val in temp_seed {
if val >= 97 && val <= 122 {
seed_string += char::from(val).to_string().as_str();
if seed_string.len() >= 55 {
break;
}
}
}
}
let mut id: identity::Identity = identity::Identity::new(seed_string.as_str());
if password.len() > 4 { //Minimum length
let master_password = get_master_password(get_db_path().as_str())
.expect("Failed To Fetch Master Password!");
match verify_password(password, master_password[1].as_str()) {
Ok(verified) => {
if !verified {
return format!("Invalid Password!");
}
id = id.encrypt_identity(password).expect("Failed To Encrypt Identity!");
},
Err(error) => { return format!("Failed to Verify Master Password!: <{}>", error); }
}
}

let response = match insert_new_identity(get_db_path().as_str(), &id) {
Ok(_) => "200",
Err(_) => "Failed To Insert Identity!"
};
return format!("{}", response);
}

#[get("/identity/add/<seed>")]
pub fn add_identity(seed: &str) -> String {
if seed.len() != 55 {
return format!("Invalid Seed! Must be Exactly 55 characters in length!");
}
for i in seed.as_bytes() {
if *i < b'a' || *i > b'z' {
return format!("Invalid Seed! Must be a-z lowercase!");
}
}
let id: identity::Identity = identity::Identity::new(seed);
let response = match insert_new_identity(get_db_path().as_str(), &id) {
Ok(_) => "200",
Err(_) => "Failed To Insert Identity!"
};
return format!("{}", response);
}

#[get("/identity/add/<seed>/<password>")]
pub fn add_identity_with_password(seed: &str, password: &str) -> String {
if seed.len() != 55 {
return format!("Invalid Seed! Must be Exactly 55 characters in length!");
}
for i in seed.as_bytes() {
if *i < b'a' || *i > b'z' {
return format!("Invalid Seed! Must be a-z lowercase!");
}
}
let mut id: identity::Identity = identity::Identity::new(seed);
if password.len() > 4 { //Minimum length
let master_password = get_master_password(get_db_path().as_str())
.expect("Failed To Fetch Master Password!");
match verify_password(password, master_password[1].as_str()) {
Ok(verified) => {
if !verified {
return format!("Invalid Password!");
}
id = id.encrypt_identity(password).expect("Failed To Encrypt Identity!");
},
Err(error) => { return format!("Failed to Verify Master Password!: <{}>", error); }
}
}
let response = match insert_new_identity(get_db_path().as_str(), &id) {
Ok(_) => "200",
Err(_) => "Failed To Insert Identity!"
};
return format!("{}", response);
}
Loading

0 comments on commit 12215ee

Please sign in to comment.