use std::sync::Arc;
use crate::{agent::EnvelopeContent, export::Principal};
pub(crate) mod anonymous;
pub(crate) mod basic;
pub(crate) mod delegated;
pub(crate) mod error;
pub(crate) mod prime256v1;
pub(crate) mod secp256k1;
#[doc(inline)]
pub use anonymous::AnonymousIdentity;
#[doc(inline)]
pub use basic::BasicIdentity;
#[doc(inline)]
pub use delegated::DelegatedIdentity;
#[doc(inline)]
pub use error::DelegationError;
#[doc(inline)]
pub use ic_transport_types::{Delegation, SignedDelegation};
#[doc(inline)]
pub use prime256v1::Prime256v1Identity;
#[doc(inline)]
pub use secp256k1::Secp256k1Identity;
#[cfg(feature = "pem")]
#[doc(inline)]
pub use error::PemError;
#[derive(Clone, Debug)]
pub struct Signature {
pub public_key: Option<Vec<u8>>,
pub signature: Option<Vec<u8>>,
pub delegations: Option<Vec<SignedDelegation>>,
}
pub trait Identity: Send + Sync {
fn sender(&self) -> Result<Principal, String>;
fn public_key(&self) -> Option<Vec<u8>>;
fn sign(&self, content: &EnvelopeContent) -> Result<Signature, String>;
fn sign_delegation(&self, content: &Delegation) -> Result<Signature, String> {
let _ = content; Err(String::from("unsupported"))
}
fn sign_arbitrary(&self, content: &[u8]) -> Result<Signature, String> {
let _ = content; Err(String::from("unsupported"))
}
fn delegation_chain(&self) -> Vec<SignedDelegation> {
vec![]
}
}
macro_rules! delegating_impl {
($implementor:ty, $name:ident => $self_expr:expr) => {
impl Identity for $implementor {
fn sender(&$name) -> Result<Principal, String> {
$self_expr.sender()
}
fn public_key(&$name) -> Option<Vec<u8>> {
$self_expr.public_key()
}
fn sign(&$name, content: &EnvelopeContent) -> Result<Signature, String> {
$self_expr.sign(content)
}
fn sign_delegation(&$name, content: &Delegation) -> Result<Signature, String> {
$self_expr.sign_delegation(content)
}
fn sign_arbitrary(&$name, content: &[u8]) -> Result<Signature, String> {
$self_expr.sign_arbitrary(content)
}
fn delegation_chain(&$name) -> Vec<SignedDelegation> {
$self_expr.delegation_chain()
}
}
};
}
delegating_impl!(Box<dyn Identity>, self => **self);
delegating_impl!(Arc<dyn Identity>, self => **self);
delegating_impl!(&dyn Identity, self => *self);