mirror of
https://github.com/fluencelabs/trust-graph
synced 2025-04-25 15:52:12 +00:00
refactoring
This commit is contained in:
parent
816b3754a4
commit
a9d1e72653
@ -3,6 +3,7 @@ use fluence::WasmLoggerBuilder;
|
|||||||
mod dto;
|
mod dto;
|
||||||
mod results;
|
mod results;
|
||||||
mod service_api;
|
mod service_api;
|
||||||
|
mod service_impl;
|
||||||
mod storage_impl;
|
mod storage_impl;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::dto::Certificate;
|
use crate::dto::Certificate;
|
||||||
use crate::service_api::ServiceError;
|
use crate::service_impl::ServiceError;
|
||||||
use fluence::fce;
|
use fluence::fce;
|
||||||
|
|
||||||
#[fce]
|
#[fce]
|
||||||
|
@ -1,47 +1,9 @@
|
|||||||
use crate::dto::{Certificate, DtoConversionError};
|
use crate::dto::Certificate;
|
||||||
use crate::results::{AllCertsResult, InsertResult, WeightResult};
|
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::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]
|
#[fce]
|
||||||
/// add a certificate in string representation to trust graph if it is valid
|
/// 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()
|
insert_cert_impl(certificate, current_time).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_weight_impl(public_key: String) -> Result<Option<u32>, ServiceError> {
|
|
||||||
let tg = get_data().lock();
|
|
||||||
|
|
||||||
let public_key = string_to_public_key(public_key)?;
|
|
||||||
|
|
||||||
let weight = tg.weight(public_key)?;
|
|
||||||
|
|
||||||
Ok(weight)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fce]
|
#[fce]
|
||||||
fn get_weight(public_key: String) -> WeightResult {
|
fn get_weight(public_key: String) -> WeightResult {
|
||||||
get_weight_impl(public_key).into()
|
get_weight_impl(public_key).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn string_to_public_key(public_key: String) -> Result<PublicKey, ServiceError> {
|
|
||||||
let public_key = PublicKey::from_base58(&public_key)?;
|
|
||||||
|
|
||||||
Ok(public_key)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fce]
|
#[fce]
|
||||||
fn get_all_certs(issued_for: String) -> AllCertsResult {
|
fn get_all_certs(issued_for: String) -> AllCertsResult {
|
||||||
get_all_certs_impl(issued_for).into()
|
get_all_certs_impl(issued_for).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, ServiceError> {
|
// TODO rewrite test after #[fce_test] will be implemented
|
||||||
let tg = get_data().lock();
|
// #[fce]
|
||||||
|
// fn test() -> String {
|
||||||
let public_key = string_to_public_key(issued_for)?;
|
// let mut tg = get_data().lock();
|
||||||
let certs = tg.get_all_certs(public_key, &[])?;
|
//
|
||||||
Ok(certs.into_iter().map(|c| c.into()).collect())
|
// let root_kp = KeyPair::generate();
|
||||||
}
|
// let root_kp2 = KeyPair::generate();
|
||||||
|
// let second_kp = KeyPair::generate();
|
||||||
#[fce]
|
//
|
||||||
fn test() -> String {
|
// let expires_at = Duration::new(15, 15);
|
||||||
let mut tg = get_data().lock();
|
// let issued_at = Duration::new(5, 5);
|
||||||
|
//
|
||||||
let root_kp = KeyPair::generate();
|
// let cert = trust_graph::Certificate::issue_root(
|
||||||
let root_kp2 = KeyPair::generate();
|
// &root_kp,
|
||||||
let second_kp = KeyPair::generate();
|
// second_kp.public_key(),
|
||||||
|
// expires_at,
|
||||||
let expires_at = Duration::new(15, 15);
|
// issued_at,
|
||||||
let issued_at = Duration::new(5, 5);
|
// );
|
||||||
|
// tg.add_root_weight(root_kp.public().into(), 0).unwrap();
|
||||||
let cert = trust_graph::Certificate::issue_root(
|
// tg.add_root_weight(root_kp2.public().into(), 1).unwrap();
|
||||||
&root_kp,
|
// tg.add(cert, Duration::new(10, 10)).unwrap();
|
||||||
second_kp.public_key(),
|
//
|
||||||
expires_at,
|
// let a = tg.get(second_kp.public_key()).unwrap();
|
||||||
issued_at,
|
// let str = format!("{:?}", a);
|
||||||
);
|
// log::info!("{}", &str);
|
||||||
tg.add_root_weight(root_kp.public().into(), 0).unwrap();
|
//
|
||||||
tg.add_root_weight(root_kp2.public().into(), 1).unwrap();
|
// str
|
||||||
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
|
|
||||||
}
|
|
||||||
|
63
wasm/src/service_impl.rs
Normal file
63
wasm/src/service_impl.rs
Normal file
@ -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<Option<u32>, 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<PublicKey, ServiceError> {
|
||||||
|
let public_key = PublicKey::from_base58(&public_key)?;
|
||||||
|
|
||||||
|
Ok(public_key)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, 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(())
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user