Skip to content

Commit

Permalink
chore: improve borrowing
Browse files Browse the repository at this point in the history
Signed-off-by: James Ebert <[email protected]>
  • Loading branch information
JamesKEbert committed Dec 11, 2024
1 parent 9aba204 commit 00c31c4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
4 changes: 2 additions & 2 deletions aries/agents/aath-backchannel/src/controllers/didcomm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ impl HarnessAgent {
pub async fn receive_message(&self, payload: Vec<u8>) -> HarnessResult<HttpResponse> {
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(|| {
Expand Down
18 changes: 9 additions & 9 deletions aries/aries_vcx/src/utils/didcomm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Key> {
pub fn resolve_ed25519_key_agreement(did_document: &DidDocument) -> VcxResult<Key> {
let vm_types = [
VerificationMethodType::Ed25519VerificationKey2018,
VerificationMethodType::Ed25519VerificationKey2020,
Expand All @@ -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<Vec<Key>> {
Expand All @@ -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",
Expand All @@ -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<Vec<Key>> {
Expand All @@ -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",
Expand All @@ -103,5 +103,5 @@ pub fn get_ed25519_base58_recipient_keys(
}
}

Ok(naked_recipient_keys)
Ok(ed25519_recipient_keys)
}
43 changes: 22 additions & 21 deletions aries/aries_vcx/src/utils/encryption_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -72,17 +73,17 @@ impl EncryptionEnvelope {
their_did_doc: &DidDocument,
their_service_id: &Uri,
) -> VcxResult<EncryptionEnvelope> {
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,
Expand Down Expand Up @@ -157,7 +158,7 @@ impl EncryptionEnvelope {
data = EncryptionEnvelope::wrap_into_forward(
wallet,
data,
forward_to_key.clone(),
&forward_to_key,
routing_key.clone(),
)
.await?;
Expand All @@ -169,7 +170,7 @@ impl EncryptionEnvelope {
async fn wrap_into_forward(
wallet: &impl BaseWallet,
data: Vec<u8>,
forward_to_key: Key,
forward_to_key: &Key,
routing_key: Key,
) -> VcxResult<Vec<u8>> {
let content = ForwardContent::builder()
Expand All @@ -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<u8>,
encrypted_data: &[u8],
) -> VcxResult<(String, Option<Key>, 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))
Expand All @@ -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<u8>,
expected_sender_vk: Option<Key>,
encrypted_data: &[u8],
expected_sender_vk: &Option<Key>,
) -> VcxResult<(AriesMessage, Option<Key>, Key)> {
let (message, sender_vk, recipient_vk) =
Self::unpack(wallet, encrypted_data, expected_sender_vk).await?;
Expand All @@ -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<u8>,
expected_sender_vk: Option<Key>,
encrypted_data: &[u8],
expected_sender_vk: &Option<Key>,
) -> VcxResult<(String, Option<Key>, Key)> {
trace!(
"EncryptionEnvelope::anon_unpack >>> processing payload of {} bytes",
Expand All @@ -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!(
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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::<Value>(&fwd_msg)
Expand All @@ -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();

Expand Down Expand Up @@ -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());
Expand Down
11 changes: 5 additions & 6 deletions aries/aries_vcx/tests/test_did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.79"

0 comments on commit 00c31c4

Please sign in to comment.