use crate::{
DidCommPrivateContext, DidCommPublicContext, Identity, IdentityBox, IdentityResolver, IdentityResolverError,
PrivateIdentity, PrivateIdentityBox, PrivateIdentityResolver,
};
use async_trait::async_trait;
use co_primitives::Network;
use std::collections::BTreeSet;
#[derive(Debug, Clone)]
pub struct LocalIdentity {
did: String,
}
impl LocalIdentity {
pub fn device() -> Self {
LocalIdentity { did: "did:local:device".to_owned() }
}
pub fn new(name: &str) -> Self {
LocalIdentity { did: format!("did:local:{name}") }
}
}
impl Identity for LocalIdentity {
fn identity(&self) -> &str {
self.did.as_str()
}
fn public_key(&self) -> Option<Vec<u8>> {
None
}
fn verify(&self, _signature: &[u8], _data: &[u8], _public_key: Option<&[u8]>) -> bool {
true
}
fn didcomm_public(&self) -> Option<DidCommPublicContext> {
None
}
fn networks(&self) -> BTreeSet<Network> {
Default::default()
}
}
impl PrivateIdentity for LocalIdentity {
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, crate::SignError> {
Ok(data.to_vec())
}
fn didcomm_private(&self) -> Option<DidCommPrivateContext> {
None
}
}
#[derive(Debug, Clone, Default)]
pub struct LocalIdentityResolver {}
impl LocalIdentityResolver {
pub fn new() -> Self {
Self {}
}
fn into_local_identity(identity: &str) -> Result<LocalIdentity, IdentityResolverError> {
if identity.starts_with("did:local:") {
return Ok(LocalIdentity { did: identity.to_owned() });
}
Err(IdentityResolverError::NotFound)
}
pub fn private_identity(&self, identity: &str) -> Result<LocalIdentity, IdentityResolverError> {
Self::into_local_identity(identity)
}
}
#[async_trait]
impl IdentityResolver for LocalIdentityResolver {
async fn resolve(&self, identity: &str) -> Result<IdentityBox, IdentityResolverError> {
Ok(IdentityBox::new(Self::into_local_identity(identity)?))
}
}
#[async_trait]
impl PrivateIdentityResolver for LocalIdentityResolver {
async fn resolve_private(&self, identity: &str) -> Result<PrivateIdentityBox, IdentityResolverError> {
Ok(PrivateIdentityBox::new(Self::into_local_identity(identity)?))
}
}