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

get_acc_id_from_pk error handling #140

Merged
merged 3 commits into from
Apr 25, 2023
Merged
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
49 changes: 43 additions & 6 deletions mpc-recovery/src/leader_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,28 @@ fn get_acc_id_from_pk(
account_lookup_url: String,
) -> Result<AccountId, anyhow::Error> {
let url = format!("{}/publicKey/{}/accounts", account_lookup_url, public_key);
tracing::info!(
url = url,
public_key = public_key.to_string(),
"fetching account id from public key"
);
let client = reqwest::blocking::Client::new();
let response = client.get(url).send()?.text()?;
let accounts: Vec<String> = serde_json::from_str(&response)?;
Ok(accounts
.first()
.cloned()
.unwrap_or_default()
.parse()
.unwrap())
tracing::info!(accounts = ?accounts, "fetched accounts");
match accounts.first() {
Some(account_id) => {
tracing::info!(account_id = account_id, "using first account id");
Ok(account_id.parse()?)
}
None => {
tracing::error!(
public_key = public_key.to_string(),
"no account found for pk"
);
Err(anyhow::anyhow!("no account found for pk: {}", public_key))
}
}
}

async fn process_add_key<T: OAuthTokenVerifier>(
Expand Down Expand Up @@ -456,6 +469,13 @@ async fn add_key<T: OAuthTokenVerifier>(
tracing::error!(err = ?e);
response::add_key_unauthorized(format!("failed to verify oidc token: {}", err_msg))
}
Err(ref e @ AddKeyError::AccountNotFound(ref err_msg)) => {
tracing::error!(err = ?e);
response::add_key_bad_request(format!(
"failed to recover account_id from pk: {}",
err_msg
))
}
Err(e) => {
tracing::error!(err = ?e);
response::add_key_internal_error(format!("failed to process new account: {}", e))
Expand Down Expand Up @@ -535,4 +555,21 @@ mod tests {
let first_account = get_acc_id_from_pk(public_key, url).unwrap();
assert_eq!(first_account.to_string(), "serhii.testnet".to_string());
}

#[test]
fn test_get_acc_id_from_unexisting_pk_testnet() {
let url = "https://testnet-api.kitwallet.app".to_string();
let public_key: PublicKey = "ed25519:2uF6ZUghFFUg3Kta9rW47iiJ3crNzRdaPD2rBPQWEwyc"
.parse()
.unwrap();
match get_acc_id_from_pk(public_key.clone(), url) {
Ok(_) => panic!("Should not be able to get account id from unexisting pk"),
Err(e) => {
assert_eq!(
e.to_string(),
format!("no account found for pk: {}", public_key)
);
}
}
}
}