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

unwraps #77

Merged
merged 2 commits into from
Apr 19, 2023
Merged
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
10 changes: 5 additions & 5 deletions mpc-recovery/src/leader_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ async fn process_new_account(
let block_height = state.client.latest_block_height().await?;

// Create a transaction to create new NEAR account
let new_user_account_pk: PublicKey = request.public_key.clone().parse().unwrap();
let new_user_account_id: AccountId = request.near_account_id.clone().parse().unwrap();
let new_user_account_pk: PublicKey = request.public_key.clone().parse()?;
let new_user_account_id: AccountId = request.near_account_id.clone().parse()?;

let delegate_action = get_create_account_delegate_action(
state.account_creator_id.clone(),
Expand All @@ -130,7 +130,7 @@ async fn process_new_account(
crate::transaction::NetworkType::Testnet,
nonce + 1,
block_height + 100,
);
)?;
let signed_delegate_action = get_signed_delegated_action(
delegate_action,
state.account_creator_id.clone(),
Expand Down Expand Up @@ -197,7 +197,7 @@ async fn process_add_key(
request: &AddKeyRequest,
internal_acc_id: InternalAccountId,
) -> anyhow::Result<(StatusCode, Json<AddKeyResponse>)> {
let user_account_id: AccountId = request.near_account_id.parse().unwrap();
let user_account_id: AccountId = request.near_account_id.parse()?;

// Get nonce and recent block hash
let nonce = state
Expand All @@ -220,7 +220,7 @@ async fn process_add_key(
new_public_key,
nonce + 1,
max_block_height,
);
)?;
let signed_delegate_action = get_signed_delegated_action(
delegate_action,
user_account_id,
Expand Down
17 changes: 8 additions & 9 deletions mpc-recovery/src/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ pub trait OAuthTokenVerifier {
public_key: &[u8],
issuer: &str,
audience: &str,
) -> Result<IdTokenClaims, String> {
) -> anyhow::Result<IdTokenClaims> {
let mut validation = Validation::new(Algorithm::RS256);
validation.set_issuer(&[issuer]);
validation.set_audience(&[audience]);

let decoding_key = DecodingKey::from_rsa_pem(public_key).unwrap();
let decoding_key = DecodingKey::from_rsa_pem(public_key)?;

match decode::<IdTokenClaims>(token, &decoding_key, &validation) {
Ok(token_data) => Ok(token_data.claims),
Err(e) => Err(format!("Failed to validate the token: {}", e)),
}
let claims =
decode::<IdTokenClaims>(token, &decoding_key, &validation).map(|t| t.claims)?;
Ok(claims)
}
}

Expand Down Expand Up @@ -215,7 +214,7 @@ mod tests {
&my_claims.aud,
) {
Ok(_) => panic!("Token validation should fail"),
Err(e) => assert_eq!(e, "Failed to validate the token: InvalidSignature"),
Err(e) => assert_eq!(e.to_string(), "InvalidSignature"),
}

// Invalid issuer
Expand All @@ -226,7 +225,7 @@ mod tests {
&my_claims.aud,
) {
Ok(_) => panic!("Token validation should fail"),
Err(e) => assert_eq!(e, "Failed to validate the token: InvalidIssuer"),
Err(e) => assert_eq!(e.to_string(), "InvalidIssuer"),
}

// Invalid audience
Expand All @@ -237,7 +236,7 @@ mod tests {
"invalid_audience",
) {
Ok(_) => panic!("Token validation should fail"),
Err(e) => assert_eq!(e, "Failed to validate the token: InvalidAudience"),
Err(e) => assert_eq!(e.to_string(), "InvalidAudience"),
}
}

Expand Down
20 changes: 12 additions & 8 deletions mpc-recovery/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn get_create_account_delegate_action(
network_type: NetworkType,
nonce: Nonce,
max_block_height: u64,
) -> DelegateAction {
) -> anyhow::Result<DelegateAction> {
let create_acc_options = CreateAccountOptions {
full_access_keys: Some(vec![new_account_user_pk, new_account_recovery_pk]),
};
Expand All @@ -46,9 +46,9 @@ pub fn get_create_account_delegate_action(
deposit: 0,
});

let delegate_create_acc_action = NonDelegateAction::try_from(create_acc_action).unwrap();
let delegate_create_acc_action = NonDelegateAction::try_from(create_acc_action)?;

DelegateAction {
let delegate_action = DelegateAction {
sender_id: signer_id,
receiver_id: match network_type {
NetworkType::_Mainnet => "near".parse().unwrap(),
Expand All @@ -58,7 +58,9 @@ pub fn get_create_account_delegate_action(
nonce,
max_block_height,
public_key: signer_pk,
}
};

Ok(delegate_action)
}

pub fn get_add_key_delegate_action(
Expand All @@ -67,7 +69,7 @@ pub fn get_add_key_delegate_action(
new_public_key: PublicKey,
nonce: Nonce,
max_block_height: u64,
) -> DelegateAction {
) -> anyhow::Result<DelegateAction> {
let add_key_action = Action::AddKey(AddKeyAction {
public_key: new_public_key,
access_key: AccessKey {
Expand All @@ -76,16 +78,18 @@ pub fn get_add_key_delegate_action(
},
});

let delegate_add_key_action = NonDelegateAction::try_from(add_key_action).unwrap();
let delegate_add_key_action = NonDelegateAction::try_from(add_key_action)?;

DelegateAction {
let delegate_action = DelegateAction {
sender_id: account_id.clone(),
receiver_id: account_id,
actions: vec![delegate_add_key_action],
nonce,
max_block_height,
public_key: signer_pk,
}
};

Ok(delegate_action)
}

pub fn get_signed_delegated_action(
Expand Down