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

Simplify DIDComm unpack(), use Key instead of String #1310

Merged
merged 8 commits into from
Dec 11, 2024
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
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
Loading