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

Fix/handle wrong args #59

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
91 changes: 71 additions & 20 deletions crates/grapevine_cli/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::utils::fs::ACCOUNT_PATH;
use babyjubjub_rs::{decompress_point, Point};
use grapevine_common::http::requests::{
CreateUserRequest, DegreeProofRequest, GetNonceRequest, PhraseRequest,
NewRelationshipRequest,
CreateUserRequest, DegreeProofRequest, GetNonceRequest, NewRelationshipRequest, PhraseRequest,
};
use grapevine_common::http::responses::{DegreeData, PhraseCreationResponse};
use grapevine_common::models::ProvingData;
Expand Down Expand Up @@ -31,8 +30,11 @@ pub async fn get_pubkey_req(username: String) -> Result<Point, GrapevineError> {
let pubkey = res.text().await.unwrap();
Ok(decompress_point(hex::decode(pubkey).unwrap().try_into().unwrap()).unwrap())
}
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
StatusCode::NOT_FOUND => Err(GrapevineError::UserNotFound(username)),
_ => Err(res.json::<GrapevineError>().await.unwrap()),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand All @@ -45,7 +47,10 @@ pub async fn get_nonce_req(body: GetNonceRequest) -> Result<u64, GrapevineError>
let nonce = res.text().await.unwrap();
Ok(nonce.parse().unwrap())
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand All @@ -72,7 +77,10 @@ pub async fn get_available_proofs_req(
let proofs = res.json::<Vec<String>>().await.unwrap();
Ok(proofs)
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand Down Expand Up @@ -100,7 +108,10 @@ pub async fn get_proof_with_params_req(
let proof = res.json::<ProvingData>().await.unwrap();
Ok(proof)
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand All @@ -117,7 +128,10 @@ pub async fn create_user_req(body: CreateUserRequest) -> Result<(), GrapevineErr
let res = client.post(&url).json(&body).send().await.unwrap();
match res.status() {
StatusCode::CREATED => return Ok(()),
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand Down Expand Up @@ -153,7 +167,10 @@ pub async fn add_relationship_req(
.unwrap();
return Ok(message);
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand Down Expand Up @@ -183,13 +200,17 @@ pub async fn phrase_req(
.unwrap();
match res.status() {
StatusCode::CREATED => {
let data: PhraseCreationResponse = serde_json::from_str(&res.text().await.unwrap()).unwrap();
let data: PhraseCreationResponse =
serde_json::from_str(&res.text().await.unwrap()).unwrap();
// increment nonce
account
.increment_nonce(Some((&**ACCOUNT_PATH).to_path_buf()))
.unwrap();
return Ok(data);
}
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => {
// Err(res.json::<GrapevineError>().await.unwrap())
Err(GrapevineError::InternalError)
Expand Down Expand Up @@ -220,7 +241,10 @@ pub async fn get_account_details_req(
let details = res.json::<(u64, u64, u64)>().await.unwrap();
Ok(details)
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand All @@ -247,7 +271,10 @@ pub async fn get_degrees_req(
let degrees = res.json::<Vec<DegreeData>>().await.unwrap();
Ok(degrees)
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand Down Expand Up @@ -283,7 +310,10 @@ pub async fn degree_proof_req(
.unwrap();
return Ok(());
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand All @@ -310,7 +340,10 @@ pub async fn get_known_req(
let proofs = res.json::<Vec<DegreeData>>().await.unwrap();
Ok(proofs)
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand Down Expand Up @@ -338,7 +371,10 @@ pub async fn get_phrase_req(
let data = res.json::<DegreeData>().await.unwrap();
Ok(data)
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

Expand Down Expand Up @@ -366,11 +402,17 @@ pub async fn show_connections_req(
let connection_data = res.json::<(u64, Vec<u64>)>().await.unwrap();
Ok(connection_data)
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mistmatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

pub async fn get_relationships_req(active: bool, account: &mut GrapevineAccount) -> Result<Vec<String>, GrapevineError> {
pub async fn get_relationships_req(
active: bool,
account: &mut GrapevineAccount,
) -> Result<Vec<String>, GrapevineError> {
let route = if active { "active" } else { "pending" };
let url = format!("{}/user/relationship/{}", &**SERVER_URL, route);
// produce signature over current nonce
Expand All @@ -392,11 +434,17 @@ pub async fn get_relationships_req(active: bool, account: &mut GrapevineAccount)
let relationships = res.json::<Vec<String>>().await.unwrap();
Ok(relationships)
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mismatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}

pub async fn reject_relationship_req(username: &String, account: &mut GrapevineAccount) -> Result<(), GrapevineError> {
pub async fn reject_relationship_req(
username: &String,
account: &mut GrapevineAccount,
) -> Result<(), GrapevineError> {
let url = format!("{}/user/relationship/reject/{}", &**SERVER_URL, username);
// produce signature over current nonce
let signature = hex::encode(account.sign_nonce().compress());
Expand All @@ -416,6 +464,9 @@ pub async fn reject_relationship_req(username: &String, account: &mut GrapevineA
.unwrap();
Ok(())
}
_ => Err(res.json::<GrapevineError>().await.unwrap()),
StatusCode::UNAUTHORIZED => Err(GrapevineError::Other(
"Nonce mismatch. Syncing, please try again".to_string(),
)),
_ => Err(GrapevineError::Other(res.text().await.unwrap())),
}
}
}
2 changes: 1 addition & 1 deletion crates/grapevine_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub async fn main() {
println!("{}", message);
}
Err(e) => {
println!("Error: {}", e);
eprintln!("\x1b[0;31merror\x1b[0m: {}", e);
}
};
}
8 changes: 5 additions & 3 deletions crates/grapevine_common/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub enum GrapevineError {
SerdeError(String),
DegreeProofExists,
DegreeProofVerificationFailed,
FsError(String)
FsError(String),
Other(String),
}

impl std::fmt::Display for GrapevineError {
Expand All @@ -46,7 +47,7 @@ impl std::fmt::Display for GrapevineError {
}
GrapevineError::UserExists(msg) => {
write!(f, "User {} already exists with the supplied pubkey", msg)
},
}
GrapevineError::PhraseTooLong => write!(f, "Phrase is too long"),
GrapevineError::PendingRelationshipExists(sender, recipient) => {
write!(
Expand Down Expand Up @@ -94,8 +95,9 @@ impl std::fmt::Display for GrapevineError {
}
GrapevineError::DegreeProofVerificationFailed => {
write!(f, "Failed to verify degree proof")
},
}
GrapevineError::FsError(msg) => write!(f, "Filesystem error: {}", msg),
GrapevineError::Other(msg) => write!(f, "{}", msg),
}
}
}
Expand Down