From a9740dd17e7b91f7d07279b882795f2dcff234c7 Mon Sep 17 00:00:00 2001 From: Serhii Volovyk Date: Wed, 19 Apr 2023 14:51:03 +0300 Subject: [PATCH 1/2] unwraps --- mpc-recovery/src/leader_node/mod.rs | 10 +++++----- mpc-recovery/src/oauth.rs | 21 +++++++++++++++------ mpc-recovery/src/transaction.rs | 20 ++++++++++++-------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/mpc-recovery/src/leader_node/mod.rs b/mpc-recovery/src/leader_node/mod.rs index 3fc440bcc..304f1acda 100644 --- a/mpc-recovery/src/leader_node/mod.rs +++ b/mpc-recovery/src/leader_node/mod.rs @@ -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(), @@ -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(), @@ -197,7 +197,7 @@ async fn process_add_key( request: &AddKeyRequest, internal_acc_id: InternalAccountId, ) -> anyhow::Result<(StatusCode, Json)> { - 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 @@ -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, diff --git a/mpc-recovery/src/oauth.rs b/mpc-recovery/src/oauth.rs index bb24d2f65..6100e9613 100644 --- a/mpc-recovery/src/oauth.rs +++ b/mpc-recovery/src/oauth.rs @@ -15,16 +15,19 @@ pub trait OAuthTokenVerifier { public_key: &[u8], issuer: &str, audience: &str, - ) -> Result { + ) -> anyhow::Result { 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::(token, &decoding_key, &validation) { Ok(token_data) => Ok(token_data.claims), - Err(e) => Err(format!("Failed to validate the token: {}", e)), + Err(err) => { + tracing::error!("Failed to validate the token: {}", err); + Err(anyhow::anyhow!("Failed to validate the token: {}", err)) + } } } } @@ -215,7 +218,10 @@ 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(), + "Failed to validate the token: InvalidSignature" + ), } // Invalid issuer @@ -226,7 +232,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(), "Failed to validate the token: InvalidIssuer"), } // Invalid audience @@ -237,7 +243,10 @@ 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(), + "Failed to validate the token: InvalidAudience" + ), } } diff --git a/mpc-recovery/src/transaction.rs b/mpc-recovery/src/transaction.rs index 1a263b273..db1d83b35 100644 --- a/mpc-recovery/src/transaction.rs +++ b/mpc-recovery/src/transaction.rs @@ -30,7 +30,7 @@ pub fn get_create_account_delegate_action( network_type: NetworkType, nonce: Nonce, max_block_height: u64, -) -> DelegateAction { +) -> anyhow::Result { let create_acc_options = CreateAccountOptions { full_access_keys: Some(vec![new_account_user_pk, new_account_recovery_pk]), }; @@ -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(), @@ -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( @@ -67,7 +69,7 @@ pub fn get_add_key_delegate_action( new_public_key: PublicKey, nonce: Nonce, max_block_height: u64, -) -> DelegateAction { +) -> anyhow::Result { let add_key_action = Action::AddKey(AddKeyAction { public_key: new_public_key, access_key: AccessKey { @@ -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( From 95c46bd78723f6ce4d84246477e82a1e9eb2aaea Mon Sep 17 00:00:00 2001 From: Serhii Volovyk Date: Wed, 19 Apr 2023 15:50:54 +0300 Subject: [PATCH 2/2] unwraps update --- mpc-recovery/src/oauth.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/mpc-recovery/src/oauth.rs b/mpc-recovery/src/oauth.rs index 6100e9613..ded5ecf98 100644 --- a/mpc-recovery/src/oauth.rs +++ b/mpc-recovery/src/oauth.rs @@ -22,13 +22,9 @@ pub trait OAuthTokenVerifier { let decoding_key = DecodingKey::from_rsa_pem(public_key)?; - match decode::(token, &decoding_key, &validation) { - Ok(token_data) => Ok(token_data.claims), - Err(err) => { - tracing::error!("Failed to validate the token: {}", err); - Err(anyhow::anyhow!("Failed to validate the token: {}", err)) - } - } + let claims = + decode::(token, &decoding_key, &validation).map(|t| t.claims)?; + Ok(claims) } } @@ -218,10 +214,7 @@ mod tests { &my_claims.aud, ) { Ok(_) => panic!("Token validation should fail"), - Err(e) => assert_eq!( - e.to_string(), - "Failed to validate the token: InvalidSignature" - ), + Err(e) => assert_eq!(e.to_string(), "InvalidSignature"), } // Invalid issuer @@ -232,7 +225,7 @@ mod tests { &my_claims.aud, ) { Ok(_) => panic!("Token validation should fail"), - Err(e) => assert_eq!(e.to_string(), "Failed to validate the token: InvalidIssuer"), + Err(e) => assert_eq!(e.to_string(), "InvalidIssuer"), } // Invalid audience @@ -243,10 +236,7 @@ mod tests { "invalid_audience", ) { Ok(_) => panic!("Token validation should fail"), - Err(e) => assert_eq!( - e.to_string(), - "Failed to validate the token: InvalidAudience" - ), + Err(e) => assert_eq!(e.to_string(), "InvalidAudience"), } }