use super::error::Error;
mod permission;
pub use permission::{PermissionOptions, ChannelPermissionOptions};
mod protocol;
pub use protocol::{Protocol, ChannelProtocol};
mod structs;
pub use structs::{Record, RecordPath};
mod traits;
pub use traits::Response;
pub mod compiler;
pub mod scripts;
#[cfg(not(feature = "advanced"))]
mod commands;
#[cfg(feature = "advanced")]
pub mod commands;
#[cfg(feature = "advanced")]
pub mod custom_commands {
pub use super::traits::Command;
pub use super::structs::Header;
pub use uuid::Uuid;
pub use super::compiler::{CompilerMemory, CompilerCache};
}
use protocol::{SystemProtocols};
use compiler::{Compiler, CompilerCache};
use structs::PathedKey;
use traits::Command;
use crate::ed25519::SecretKey as EdSecretKey;
use crate::dwn::traits::Client;
use crate::dwn::router::Router;
use crate::dids::DidResolver;
use crate::dids::{
DidKeyPurpose,
DhtDocument,
DidKeyPair,
DidMethod,
DidKey,
Did
};
use std::collections::BTreeMap;
use simple_crypto::SecretKey;
use serde::{Serialize, Deserialize};
use uuid::Uuid;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Identity {
did_key: EdSecretKey,
sig_key: DidKeyPair,
enc_key: PathedKey,
com_key: PathedKey,
}
impl Identity {
pub async fn publish_doc(&self, document: &DhtDocument) -> Result<(), Error> {
document.publish(&self.did_key).await
}
pub fn new(service_endpoints: Vec<String>) -> Result<(Self, DhtDocument), Error> {
let did_key = EdSecretKey::new();
let did_pub = did_key.public_key();
let sig = SecretKey::new();
let sig_pub = sig.public_key();
let sig_key = DidKeyPair::new(sig, DidKey::new(
Some("sig".to_string()),
Did::new(DidMethod::DHT, did_key.public_key().thumbprint()),
sig_pub.clone(),
vec![DidKeyPurpose::Auth, DidKeyPurpose::Asm, DidKeyPurpose::Agm],
None
)).unwrap();
let com_key = SecretKey::new();
let com_pub = com_key.public_key();
Ok((
Identity{
did_key,
sig_key,
enc_key: PathedKey::new_root(SecretKey::new()),
com_key: PathedKey::new_root(com_key),
},
DhtDocument::default(did_pub, sig_pub, com_pub, service_endpoints)?
))
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct AgentKey {
sig_key: DidKeyPair,
enc_key: PathedKey,
com_key: PathedKey,
}
pub struct Wallet {
identity: Identity
}
impl Wallet {
pub fn new(
identity: Identity,
) -> Self {
Wallet{
identity
}
}
pub fn root(&self) -> AgentKey {
AgentKey{sig_key: self.identity.sig_key.clone(), enc_key: self.identity.enc_key.clone(), com_key: self.identity.com_key.clone()}
}
}
#[derive(Clone)]
pub struct Agent {
agent_key: AgentKey,
did_resolver: Box<dyn DidResolver>,
protocols: BTreeMap<Uuid, Protocol>,
router: Router,
}
impl Agent {
pub fn new(
agent_key: AgentKey,
protocols: Vec<Protocol>,
did_resolver: Box<dyn DidResolver>,
client: Box<dyn Client>
) -> Self {
let protocols = [SystemProtocols::all(), protocols].concat();
let protocols = BTreeMap::from_iter(protocols.into_iter().map(|p| (p.uuid(), p)));
let router = Router::new(did_resolver.clone(), client);
Agent{agent_key, did_resolver, protocols, router}
}
pub fn tenant(&self) -> &Did {&self.agent_key.sig_key.public.did}
#[cfg(feature = "advanced")]
pub fn new_compiler<'a>(&'a self, cache: &'a mut CompilerCache) -> Compiler<'a> {
self.internal_new_compiler(cache)
}
fn internal_new_compiler<'a>(&'a self, cache: &'a mut CompilerCache) -> Compiler<'a> {
Compiler::<'a>::new(
cache,
&*self.did_resolver,
&self.protocols,
&self.agent_key.sig_key,
&self.agent_key.enc_key,
&self.router,
self.tenant().clone()
)
}
pub async fn process_commands<'a>(&'a self, cache: &'a mut CompilerCache, commands: Vec<Box<impl Command<'a> + Clone + 'a>>) -> Result<Vec<Box<dyn Response>>, Error> {
let mut comp = self.internal_new_compiler(cache);
for command in commands.into_iter() {
comp.add_command(*command, None).await?;
}
Ok(comp.compile().await.remove(0))
}
}