From 00c31c4ff044e5d4e6fe24aa37dce8bd9b4afd9c Mon Sep 17 00:00:00 2001 From: James Ebert Date: Tue, 10 Dec 2024 22:48:28 -0800 Subject: [PATCH] chore: improve borrowing Signed-off-by: James Ebert --- .../src/controllers/didcomm.rs | 4 +- aries/aries_vcx/src/utils/didcomm_utils.rs | 18 ++++---- .../src/utils/encryption_envelope.rs | 43 ++++++++++--------- aries/aries_vcx/tests/test_did_exchange.rs | 11 +++-- rust-toolchain.toml | 2 + 5 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/aries/agents/aath-backchannel/src/controllers/didcomm.rs b/aries/agents/aath-backchannel/src/controllers/didcomm.rs index f822657aca..5596076678 100644 --- a/aries/agents/aath-backchannel/src/controllers/didcomm.rs +++ b/aries/agents/aath-backchannel/src/controllers/didcomm.rs @@ -217,8 +217,8 @@ impl HarnessAgent { pub async fn receive_message(&self, payload: Vec) -> HarnessResult { let (message, sender_vk, recipient_vk) = EncryptionEnvelope::unpack_aries_msg( self.aries_agent.wallet().as_ref(), - payload.clone(), - None, + &payload, + &None, ) .await?; let sender_vk = sender_vk.ok_or_else(|| { diff --git a/aries/aries_vcx/src/utils/didcomm_utils.rs b/aries/aries_vcx/src/utils/didcomm_utils.rs index 8618953fb2..1845613a85 100644 --- a/aries/aries_vcx/src/utils/didcomm_utils.rs +++ b/aries/aries_vcx/src/utils/didcomm_utils.rs @@ -36,7 +36,7 @@ pub(crate) fn resolve_service_key_to_typed_key( /// Resolves the first ed25519 base58 public key (a.k.a. verkey) within the DIDDocuments key /// agreement keys. Useful for resolving keys that can be used for packing DIDCommV1 messages. -pub fn resolve_ed25519_base58_key_agreement(did_document: &DidDocument) -> VcxResult { +pub fn resolve_ed25519_key_agreement(did_document: &DidDocument) -> VcxResult { let vm_types = [ VerificationMethodType::Ed25519VerificationKey2018, VerificationMethodType::Ed25519VerificationKey2020, @@ -53,7 +53,7 @@ pub fn resolve_ed25519_base58_key_agreement(did_document: &DidDocument) -> VcxRe Ok(key.validate_key_type(KeyType::Ed25519)?.to_owned()) } -pub fn get_ed25519_base58_routing_keys( +pub fn get_ed25519_routing_keys( their_did_doc: &DidDocument, service_id: &Uri, ) -> VcxResult> { @@ -62,13 +62,13 @@ pub fn get_ed25519_base58_routing_keys( return Ok(vec![]); }; - let mut naked_routing_keys = Vec::new(); + let mut ed25519_routing_keys = Vec::new(); for key in routing_keys.iter() { let pub_key = resolve_service_key_to_typed_key(key, their_did_doc)?; if pub_key.key_type() == &KeyType::Ed25519 { - naked_routing_keys.push(pub_key); + ed25519_routing_keys.push(pub_key); } else { warn!( "Unexpected key with type {} in routing keys list", @@ -77,10 +77,10 @@ pub fn get_ed25519_base58_routing_keys( } } - Ok(naked_routing_keys) + Ok(ed25519_routing_keys) } -pub fn get_ed25519_base58_recipient_keys( +pub fn get_ed25519_recipient_keys( their_did_doc: &DidDocument, service_id: &Uri, ) -> VcxResult> { @@ -89,12 +89,12 @@ pub fn get_ed25519_base58_recipient_keys( return Ok(vec![]); }; - let mut naked_recipient_keys = Vec::new(); + let mut ed25519_recipient_keys = Vec::new(); for key in recipient_keys.iter() { let pub_key = resolve_service_key_to_typed_key(key, their_did_doc)?; if pub_key.key_type() == &KeyType::Ed25519 { - naked_recipient_keys.push(pub_key); + ed25519_recipient_keys.push(pub_key); } else { warn!( "Unexpected key with type {} in recipient keys list", @@ -103,5 +103,5 @@ pub fn get_ed25519_base58_recipient_keys( } } - Ok(naked_recipient_keys) + Ok(ed25519_recipient_keys) } diff --git a/aries/aries_vcx/src/utils/encryption_envelope.rs b/aries/aries_vcx/src/utils/encryption_envelope.rs index 8a9ce30314..0fed891fe6 100644 --- a/aries/aries_vcx/src/utils/encryption_envelope.rs +++ b/aries/aries_vcx/src/utils/encryption_envelope.rs @@ -8,10 +8,11 @@ use messages::{ use public_key::{Key, KeyType}; use uuid::Uuid; -use super::didcomm_utils::get_ed25519_base58_recipient_keys; use crate::{ errors::error::prelude::*, - utils::didcomm_utils::{get_ed25519_base58_routing_keys, resolve_ed25519_base58_key_agreement}, + utils::didcomm_utils::{ + get_ed25519_recipient_keys, get_ed25519_routing_keys, resolve_ed25519_key_agreement, + }, }; #[derive(Debug)] @@ -72,17 +73,17 @@ impl EncryptionEnvelope { their_did_doc: &DidDocument, their_service_id: &Uri, ) -> VcxResult { - let sender_vk = resolve_ed25519_base58_key_agreement(our_did_doc)?; + let sender_vk = resolve_ed25519_key_agreement(our_did_doc)?; let recipient_key = { - let service_keys = get_ed25519_base58_recipient_keys(their_did_doc, their_service_id)?; + let service_keys = get_ed25519_recipient_keys(their_did_doc, their_service_id)?; match service_keys.into_iter().next() { Some(key) => key, // as a backup, use the first key agreement key, or none - None => resolve_ed25519_base58_key_agreement(their_did_doc)?, + None => resolve_ed25519_key_agreement(their_did_doc)?, } }; - let routing_keys = get_ed25519_base58_routing_keys(their_did_doc, their_service_id)?; + let routing_keys = get_ed25519_routing_keys(their_did_doc, their_service_id)?; EncryptionEnvelope::create_from_keys( wallet, @@ -157,7 +158,7 @@ impl EncryptionEnvelope { data = EncryptionEnvelope::wrap_into_forward( wallet, data, - forward_to_key.clone(), + &forward_to_key, routing_key.clone(), ) .await?; @@ -169,7 +170,7 @@ impl EncryptionEnvelope { async fn wrap_into_forward( wallet: &impl BaseWallet, data: Vec, - forward_to_key: Key, + forward_to_key: &Key, routing_key: Key, ) -> VcxResult> { let content = ForwardContent::builder() @@ -195,13 +196,13 @@ impl EncryptionEnvelope { // Will unpack a message as either anoncrypt or authcrypt. async fn unpack_a2a_message( wallet: &impl BaseWallet, - encrypted_data: Vec, + encrypted_data: &[u8], ) -> VcxResult<(String, Option, Key)> { trace!( "EncryptionEnvelope::unpack_a2a_message >>> processing payload of {} bytes", encrypted_data.len() ); - let unpacked_msg = wallet.unpack_message(&encrypted_data).await?; + let unpacked_msg = wallet.unpack_message(encrypted_data).await?; let sender_key = unpacked_msg .sender_verkey .map(|key| Key::from_base58(&key, KeyType::Ed25519)) @@ -216,8 +217,8 @@ impl EncryptionEnvelope { /// Unpacks an authcrypt or anoncrypt message returning the message, which is deserialized into an Aries message, as well as the sender key (if any -- anoncrypt does not return this) and the recipient key. Optionally takes expected_sender_vk, which does a comparison to ensure the sender key is the expected key. pub async fn unpack_aries_msg( wallet: &impl BaseWallet, - encrypted_data: Vec, - expected_sender_vk: Option, + encrypted_data: &[u8], + expected_sender_vk: &Option, ) -> VcxResult<(AriesMessage, Option, Key)> { let (message, sender_vk, recipient_vk) = Self::unpack(wallet, encrypted_data, expected_sender_vk).await?; @@ -233,8 +234,8 @@ impl EncryptionEnvelope { /// Unpacks an authcrypt or anoncrypt message returning the message, the sender key (if any -- anoncrypt does not return this), and the recipient key. Optionally takes expected_sender_vk, which does a comparison to ensure the sender key is the expected key. pub async fn unpack( wallet: &impl BaseWallet, - encrypted_data: Vec, - expected_sender_vk: Option, + encrypted_data: &[u8], + expected_sender_vk: &Option, ) -> VcxResult<(String, Option, Key)> { trace!( "EncryptionEnvelope::anon_unpack >>> processing payload of {} bytes", @@ -245,7 +246,7 @@ impl EncryptionEnvelope { // If expected_sender_vk was provided and a sender_verkey exists, verify that they match if let Some(expected_key) = expected_sender_vk { - match sender_vk.clone() { + match &sender_vk { Some(sender_vk) => { if sender_vk != expected_key { error!( @@ -305,7 +306,7 @@ pub mod unit_tests { .unwrap(); let (data_unpacked, sender_verkey, _) = - EncryptionEnvelope::unpack(&setup.wallet, envelope.0, None) + EncryptionEnvelope::unpack(&setup.wallet, &envelope.0, &None) .await .unwrap(); @@ -343,7 +344,7 @@ pub mod unit_tests { .unwrap(); let (data_unpacked, _sender_vk_unpacked, _recipient_vk_unpacked) = - EncryptionEnvelope::unpack(&setup.wallet, envelope.0, Some(sender_vk)) + EncryptionEnvelope::unpack(&setup.wallet, &envelope.0, &Some(sender_vk)) .await .unwrap(); @@ -381,7 +382,7 @@ pub mod unit_tests { .await .unwrap(); - let (fwd_msg, _, _) = EncryptionEnvelope::unpack(&setup.wallet, envelope.0, None) + let (fwd_msg, _, _) = EncryptionEnvelope::unpack(&setup.wallet, &envelope.0, &None) .await .unwrap(); let fwd_payload = serde_json::from_str::(&fwd_msg) @@ -390,7 +391,7 @@ pub mod unit_tests { .unwrap() .to_string(); let (core_payload, _, _) = - EncryptionEnvelope::unpack(&setup.wallet, fwd_payload.into(), None) + EncryptionEnvelope::unpack(&setup.wallet, fwd_payload.as_bytes(), &None) .await .unwrap(); @@ -430,8 +431,8 @@ pub mod unit_tests { let err = EncryptionEnvelope::unpack( &setup.wallet, - envelope.0, - Some(alice_data.verkey().clone()), + &envelope.0, + &Some(alice_data.verkey().clone()), ) .await; assert!(err.is_err()); diff --git a/aries/aries_vcx/tests/test_did_exchange.rs b/aries/aries_vcx/tests/test_did_exchange.rs index 281b1155de..8d83da96d0 100644 --- a/aries/aries_vcx/tests/test_did_exchange.rs +++ b/aries/aries_vcx/tests/test_did_exchange.rs @@ -16,8 +16,7 @@ use aries_vcx::{ transition::transition_result::TransitionResult, }, utils::{ - didcomm_utils::resolve_ed25519_base58_key_agreement, - encryption_envelope::EncryptionEnvelope, + didcomm_utils::resolve_ed25519_key_agreement, encryption_envelope::EncryptionEnvelope, }, }; use aries_vcx_anoncreds::anoncreds::base_anoncreds::BaseAnonCreds; @@ -53,8 +52,8 @@ pub mod utils; fn assert_key_agreement(a: DidDocument, b: DidDocument) { log::warn!("comparing did doc a: {}, b: {}", a, b); - let a_key = resolve_ed25519_base58_key_agreement(&a).unwrap(); - let b_key = resolve_ed25519_base58_key_agreement(&b).unwrap(); + let a_key = resolve_ed25519_key_agreement(&a).unwrap(); + let b_key = resolve_ed25519_key_agreement(&b).unwrap(); assert_eq!(a_key, b_key); } @@ -191,9 +190,9 @@ async fn did_exchange_test( info!("Encrypted message: {:?}", m); let requesters_peer_did = requesters_peer_did.resolve_did_doc()?; - let expected_sender_vk = resolve_ed25519_base58_key_agreement(&requesters_peer_did)?; + let expected_sender_vk = resolve_ed25519_key_agreement(&requesters_peer_did)?; let unpacked = - EncryptionEnvelope::unpack(&agent_inviter.wallet, m.0, Some(expected_sender_vk)).await?; + EncryptionEnvelope::unpack(&agent_inviter.wallet, &m.0, &Some(expected_sender_vk)).await?; info!("Unpacked message: {:?}", unpacked); diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000000..c6e4d7d503 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.79"