Skip to content

Commit

Permalink
Helper methods to extract data.
Browse files Browse the repository at this point in the history
  • Loading branch information
abizjak committed Aug 2, 2023
1 parent 288b075 commit 575e3e5
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions rust-src/concordium_base/src/web3id/did.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Definition of Concordium DIDs and their parser.

use crate::{base::CredentialRegistrationID, common::base16_decode_string, id::types::IpIdentity};
use crate::{
base::CredentialRegistrationID,
common::{base16_decode_string, Deserial},
id::types::IpIdentity,
};
use concordium_contracts_common::{
AccountAddress, ContractAddress, OwnedEntrypointName, OwnedParameter,
AccountAddress, ContractAddress, EntrypointName, OwnedEntrypointName, OwnedParameter,
};
use nom::{
branch::alt,
Expand Down Expand Up @@ -92,6 +96,35 @@ pub enum IdentifierType {
Idp { idp_identity: IpIdentity },
}

impl IdentifierType {
pub fn extract_contract<D: Deserial>(
&self,
ep: EntrypointName,
) -> Option<(ContractAddress, D)> {
let IdentifierType::ContractData {
address,
entrypoint,
parameter,
} = self else {
return None
};
if entrypoint.as_entrypoint_name() != ep {
return None;
}
let d = crate::common::from_bytes(&mut std::io::Cursor::new(parameter.as_ref())).ok()?;
Some((*address, d))
}

pub fn extract_public_key(&self) -> Option<ed25519_dalek::PublicKey> {
let IdentifierType::PublicKey {
key,
} = self else {
return None
};
Some(*key)
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
#[serde(try_from = "String", into = "String")]
/// A DID method.
Expand Down

0 comments on commit 575e3e5

Please sign in to comment.