diff --git a/wasm/src/main.rs b/wasm/src/main.rs index 2b1848b..b1d4d68 100644 --- a/wasm/src/main.rs +++ b/wasm/src/main.rs @@ -3,6 +3,7 @@ use fluence::WasmLoggerBuilder; mod dto; mod results; mod service_api; +mod service_impl; mod storage_impl; pub fn main() { diff --git a/wasm/src/results.rs b/wasm/src/results.rs index dce4cfd..9be3faa 100644 --- a/wasm/src/results.rs +++ b/wasm/src/results.rs @@ -1,5 +1,5 @@ use crate::dto::Certificate; -use crate::service_api::ServiceError; +use crate::service_impl::ServiceError; use fluence::fce; #[fce] diff --git a/wasm/src/service_api.rs b/wasm/src/service_api.rs index 916d45c..c34bce1 100644 --- a/wasm/src/service_api.rs +++ b/wasm/src/service_api.rs @@ -1,47 +1,9 @@ -use crate::dto::{Certificate, DtoConversionError}; +use crate::dto::Certificate; use crate::results::{AllCertsResult, InsertResult, WeightResult}; -use crate::storage_impl::get_data; +use crate::service_impl::{ + get_all_certs_impl, get_weight_impl, insert_cert_impl, insert_cert_impl_raw, +}; use fluence::fce; -use fluence_identity::public_key::PKError; -use fluence_identity::{KeyPair, PublicKey}; -use std::convert::{Into, TryInto}; -use std::str::FromStr; -use std::time::Duration; -use thiserror::Error as ThisError; -use trust_graph::{CertificateError, TrustGraphError}; - -#[derive(ThisError, Debug)] -pub enum ServiceError { - #[error("{0}")] - PublicKeyDecodeError(#[from] PKError), - #[error("{0}")] - TGError(#[from] TrustGraphError), - #[error("{0}")] - CertError(#[from] CertificateError), - #[error("{0}")] - DtoError(#[from] DtoConversionError), -} - -fn add_cert(certificate: trust_graph::Certificate, duration: u64) -> Result<(), ServiceError> { - let duration = Duration::from_millis(duration); - let mut tg = get_data().lock(); - tg.add(certificate, duration)?; - Ok(()) -} - -fn insert_cert_impl_raw(certificate: String, duration: u64) -> Result<(), ServiceError> { - let certificate = trust_graph::Certificate::from_str(&certificate)?; - - add_cert(certificate, duration)?; - Ok(()) -} - -fn insert_cert_impl(certificate: Certificate, duration: u64) -> Result<(), ServiceError> { - let certificate: trust_graph::Certificate = certificate.try_into()?; - - add_cert(certificate, duration)?; - Ok(()) -} #[fce] /// add a certificate in string representation to trust graph if it is valid @@ -58,64 +20,41 @@ fn insert_cert(certificate: Certificate, current_time: u64) -> InsertResult { insert_cert_impl(certificate, current_time).into() } -fn get_weight_impl(public_key: String) -> Result, ServiceError> { - let tg = get_data().lock(); - - let public_key = string_to_public_key(public_key)?; - - let weight = tg.weight(public_key)?; - - Ok(weight) -} - #[fce] fn get_weight(public_key: String) -> WeightResult { get_weight_impl(public_key).into() } -fn string_to_public_key(public_key: String) -> Result { - let public_key = PublicKey::from_base58(&public_key)?; - - Ok(public_key) -} - #[fce] fn get_all_certs(issued_for: String) -> AllCertsResult { get_all_certs_impl(issued_for).into() } -fn get_all_certs_impl(issued_for: String) -> Result, ServiceError> { - let tg = get_data().lock(); - - let public_key = string_to_public_key(issued_for)?; - let certs = tg.get_all_certs(public_key, &[])?; - Ok(certs.into_iter().map(|c| c.into()).collect()) -} - -#[fce] -fn test() -> String { - let mut tg = get_data().lock(); - - let root_kp = KeyPair::generate(); - let root_kp2 = KeyPair::generate(); - let second_kp = KeyPair::generate(); - - let expires_at = Duration::new(15, 15); - let issued_at = Duration::new(5, 5); - - let cert = trust_graph::Certificate::issue_root( - &root_kp, - second_kp.public_key(), - expires_at, - issued_at, - ); - tg.add_root_weight(root_kp.public().into(), 0).unwrap(); - tg.add_root_weight(root_kp2.public().into(), 1).unwrap(); - tg.add(cert, Duration::new(10, 10)).unwrap(); - - let a = tg.get(second_kp.public_key()).unwrap(); - let str = format!("{:?}", a); - log::info!("{}", &str); - - str -} +// TODO rewrite test after #[fce_test] will be implemented +// #[fce] +// fn test() -> String { +// let mut tg = get_data().lock(); +// +// let root_kp = KeyPair::generate(); +// let root_kp2 = KeyPair::generate(); +// let second_kp = KeyPair::generate(); +// +// let expires_at = Duration::new(15, 15); +// let issued_at = Duration::new(5, 5); +// +// let cert = trust_graph::Certificate::issue_root( +// &root_kp, +// second_kp.public_key(), +// expires_at, +// issued_at, +// ); +// tg.add_root_weight(root_kp.public().into(), 0).unwrap(); +// tg.add_root_weight(root_kp2.public().into(), 1).unwrap(); +// tg.add(cert, Duration::new(10, 10)).unwrap(); +// +// let a = tg.get(second_kp.public_key()).unwrap(); +// let str = format!("{:?}", a); +// log::info!("{}", &str); +// +// str +// } diff --git a/wasm/src/service_impl.rs b/wasm/src/service_impl.rs new file mode 100644 index 0000000..4d2007a --- /dev/null +++ b/wasm/src/service_impl.rs @@ -0,0 +1,63 @@ +use crate::dto::{Certificate, DtoConversionError}; +use crate::storage_impl::get_data; +use fluence_identity::public_key::PKError; +use fluence_identity::PublicKey; +use std::convert::{Into, TryInto}; +use std::str::FromStr; +use std::time::Duration; +use thiserror::Error as ThisError; +use trust_graph::{CertificateError, TrustGraphError}; + +#[derive(ThisError, Debug)] +pub enum ServiceError { + #[error("{0}")] + PublicKeyDecodeError(#[from] PKError), + #[error("{0}")] + TGError(#[from] TrustGraphError), + #[error("{0}")] + CertError(#[from] CertificateError), + #[error("{0}")] + DtoError(#[from] DtoConversionError), +} + +pub fn get_weight_impl(public_key: String) -> Result, ServiceError> { + let tg = get_data().lock(); + let public_key = string_to_public_key(public_key)?; + let weight = tg.weight(public_key)?; + Ok(weight) +} + +fn add_cert(certificate: trust_graph::Certificate, duration: u64) -> Result<(), ServiceError> { + let duration = Duration::from_millis(duration); + let mut tg = get_data().lock(); + tg.add(certificate, duration)?; + Ok(()) +} + +pub fn insert_cert_impl_raw(certificate: String, duration: u64) -> Result<(), ServiceError> { + let certificate = trust_graph::Certificate::from_str(&certificate)?; + + add_cert(certificate, duration)?; + Ok(()) +} + +fn string_to_public_key(public_key: String) -> Result { + let public_key = PublicKey::from_base58(&public_key)?; + + Ok(public_key) +} + +pub fn get_all_certs_impl(issued_for: String) -> Result, ServiceError> { + let tg = get_data().lock(); + + let public_key = string_to_public_key(issued_for)?; + let certs = tg.get_all_certs(public_key, &[])?; + Ok(certs.into_iter().map(|c| c.into()).collect()) +} + +pub fn insert_cert_impl(certificate: Certificate, duration: u64) -> Result<(), ServiceError> { + let certificate: trust_graph::Certificate = certificate.try_into()?; + + add_cert(certificate, duration)?; + Ok(()) +}