Skip to content

Commit

Permalink
Simplify DIDComm unpack(), use Key instead of String (#1310)
Browse files Browse the repository at this point in the history
* feat: adjusted encryption envelope to use key references instead of base58 strings

Signed-off-by: James Ebert <jamesebert.k@gmail.com>

* feat: refactor didcomm unpack() to be streamlined and to use keys instead of strings

Signed-off-by: James Ebert <jamesebert.k@gmail.com>

* chore: adjust iterator

Signed-off-by: James Ebert <jamesebert.k@gmail.com>

* revert iter() change

Signed-off-by: James Ebert <jamesebert.k@gmail.com>

* chore: clippy

Signed-off-by: James Ebert <jamesebert.k@gmail.com>

* chore: improve borrowing

Signed-off-by: James Ebert <jamesebert.k@gmail.com>

* chore: clippy tests

Signed-off-by: James Ebert <jamesebert.k@gmail.com>

---------

Signed-off-by: James Ebert <jamesebert.k@gmail.com>
  • Loading branch information
JamesKEbert authored Dec 11, 2024
1 parent 62ce000 commit 78dce27
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 177 deletions.
23 changes: 17 additions & 6 deletions aries/agents/aath-backchannel/src/controllers/didcomm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ impl HarnessAgent {
}

pub async fn receive_message(&self, payload: Vec<u8>) -> HarnessResult<HttpResponse> {
let (message, sender_vk, recipient_vk) = EncryptionEnvelope::anon_unpack_aries_msg(
let (message, sender_vk, recipient_vk) = EncryptionEnvelope::unpack_aries_msg(
self.aries_agent.wallet().as_ref(),
payload.clone(),
&payload,
&None,
)
.await?;
let sender_vk = sender_vk.ok_or_else(|| {
Expand Down Expand Up @@ -245,22 +246,32 @@ impl HarnessAgent {
}
}
AriesMessage::TrustPing(TrustPing::Ping(msg)) => {
let connection_id = self.aries_agent.connections().get_by_sender_vk(sender_vk)?;
let connection_id = self
.aries_agent
.connections()
.get_by_sender_vk(sender_vk.base58())?;
self.aries_agent
.connections()
.process_trust_ping(msg, &connection_id)
.await?
}
AriesMessage::Connection(msg) => self.handle_connection_msg(msg).await?,
AriesMessage::CredentialIssuance(msg) => {
let connection_id = self.aries_agent.connections().get_by_sender_vk(sender_vk)?;
let connection_id = self
.aries_agent
.connections()
.get_by_sender_vk(sender_vk.base58())?;
self.handle_issuance_msg(msg, &connection_id).await?
}
AriesMessage::DidExchange(msg) => {
self.handle_did_exchange_msg(msg, recipient_vk).await?
self.handle_did_exchange_msg(msg, recipient_vk.base58())
.await?
}
AriesMessage::PresentProof(msg) => {
let connection_id = self.aries_agent.connections().get_by_sender_vk(sender_vk)?;
let connection_id = self
.aries_agent
.connections()
.get_by_sender_vk(sender_vk.base58())?;
self.handle_presentation_msg(msg, &connection_id).await?
}
m => {
Expand Down
44 changes: 22 additions & 22 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<String> {
pub fn resolve_ed25519_key_agreement(did_document: &DidDocument) -> VcxResult<Key> {
let vm_types = [
VerificationMethodType::Ed25519VerificationKey2018,
VerificationMethodType::Ed25519VerificationKey2020,
Expand All @@ -50,58 +50,58 @@ pub fn resolve_ed25519_base58_key_agreement(did_document: &DidDocument) -> VcxRe
let vm = did_document.get_key_agreement_of_type(&vm_types)?;
let key = vm.public_key()?;

match key.key_type() {
KeyType::Ed25519 => {}
_ => {
return Err(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidVerkey,
format!("Cannot resolve key agreement as an Ed25519 key: {vm:?}"),
))
}
}

Ok(vm.public_key()?.base58())
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<String>> {
) -> VcxResult<Vec<Key>> {
let service = their_did_doc.get_service_by_id(service_id)?;
let Ok(routing_keys) = service.extra_field_routing_keys() else {
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.base58());
ed25519_routing_keys.push(pub_key);
} else {
warn!(
"Unexpected key with type {} in routing keys list",
pub_key.key_type()
);
}
}

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<String>> {
) -> VcxResult<Vec<Key>> {
let service = their_did_doc.get_service_by_id(service_id)?;
let Ok(recipient_keys) = service.extra_field_recipient_keys() else {
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.base58());
ed25519_recipient_keys.push(pub_key);
} else {
warn!(
"Unexpected key with type {} in recipient keys list",
pub_key.key_type()
);
}
}

Ok(naked_recipient_keys)
Ok(ed25519_recipient_keys)
}
Loading

0 comments on commit 78dce27

Please sign in to comment.