trust-graph-test/wasm/src/service_api.rs

100 lines
2.6 KiB
Rust
Raw Normal View History

2021-02-11 15:10:18 +03:00
use crate::dto::Certificate;
2021-02-10 15:26:26 +03:00
use crate::results::{AllCertsResult, InsertResult, WeightResult};
use crate::storage_impl::get_data;
2021-01-25 15:32:43 +03:00
use fluence::fce;
2021-02-11 17:57:09 +03:00
use fluence_identity::public_key::PKError;
use fluence_identity::{KeyPair, PublicKey};
2021-02-11 15:10:18 +03:00
use std::convert::Into;
use std::str::FromStr;
use std::time::Duration;
2021-02-11 17:57:09 +03:00
use thiserror::Error as ThisError;
use trust_graph::{CertificateError, TrustGraphError};
#[derive(ThisError, Debug)]
pub enum ServiceError {
#[error("{0}")]
2021-02-11 18:14:14 +03:00
PublicKeyDecodeError(#[from] PKError),
2021-02-11 17:57:09 +03:00
#[error("{0}")]
2021-02-11 18:14:14 +03:00
TGError(#[from] TrustGraphError),
2021-02-11 17:57:09 +03:00
#[error("{0}")]
2021-02-11 18:14:14 +03:00
CertError(#[from] CertificateError),
2021-02-11 17:57:09 +03:00
}
2021-02-11 17:57:09 +03:00
fn insert_cert_impl(certificate: String, duration: u64) -> Result<(), ServiceError> {
let duration = Duration::from_millis(duration);
2021-02-11 18:14:14 +03:00
let certificate = trust_graph::Certificate::from_str(&certificate)?;
let mut tg = get_data().lock();
2021-02-11 18:14:14 +03:00
tg.add(certificate, duration)?;
Ok(())
}
#[fce]
// TODO: some sort of auth?
fn insert_cert(certificate: String, duration: u64) -> InsertResult {
insert_cert_impl(certificate, duration).into()
}
2021-02-11 17:57:09 +03:00
fn get_weight_impl(public_key: String) -> Result<Option<u32>, ServiceError> {
2021-02-11 15:10:18 +03:00
let tg = get_data().lock();
let public_key = string_to_public_key(public_key)?;
2021-02-11 18:14:14 +03:00
let weight = tg.weight(public_key)?;
Ok(weight)
}
#[fce]
fn get_weight(public_key: String) -> WeightResult {
get_weight_impl(public_key).into()
}
2021-02-11 17:57:09 +03:00
fn string_to_public_key(public_key: String) -> Result<PublicKey, ServiceError> {
2021-02-11 18:14:14 +03:00
let public_key = PublicKey::from_base58(&public_key)?;
Ok(public_key)
}
2021-02-11 15:10:18 +03:00
#[fce]
fn get_all_certs(issued_for: String) -> AllCertsResult {
get_all_certs_impl(issued_for).into()
}
2021-02-11 17:57:09 +03:00
fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, ServiceError> {
2021-02-11 15:10:18 +03:00
let tg = get_data().lock();
let public_key = string_to_public_key(issued_for)?;
2021-02-11 18:14:14 +03:00
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);
2021-02-11 15:10:18 +03:00
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
}