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};
|
2021-01-20 17:21:02 +03:00
|
|
|
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;
|
2021-02-10 15:23:51 +03:00
|
|
|
use fluence_identity::{KeyPair, PublicKey};
|
2021-02-11 15:10:18 +03:00
|
|
|
use std::convert::Into;
|
2021-02-09 15:50:21 +03:00
|
|
|
use std::str::FromStr;
|
2021-01-20 17:21:02 +03:00
|
|
|
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-01-27 12:57:19 +03:00
|
|
|
|
2021-02-11 17:57:09 +03:00
|
|
|
fn insert_cert_impl(certificate: String, duration: u64) -> Result<(), ServiceError> {
|
2021-01-27 12:57:19 +03:00
|
|
|
let duration = Duration::from_millis(duration);
|
2021-02-11 18:14:14 +03:00
|
|
|
let certificate = trust_graph::Certificate::from_str(&certificate)?;
|
2021-01-27 12:57:19 +03:00
|
|
|
|
|
|
|
let mut tg = get_data().lock();
|
2021-02-11 18:14:14 +03:00
|
|
|
tg.add(certificate, duration)?;
|
2021-02-09 15:50:21 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-01-27 12:57:19 +03:00
|
|
|
|
2021-02-10 15:23:51 +03:00
|
|
|
#[fce]
|
2021-02-12 14:27:22 +03:00
|
|
|
/// add a certificate in string representation to trust graph if it is valid
|
|
|
|
/// see `Certificate` class for string encoding/decoding
|
|
|
|
// TODO change `current_time` to time service
|
|
|
|
fn insert_cert(certificate: String, current_time: u64) -> InsertResult {
|
|
|
|
insert_cert_impl(certificate, current_time).into()
|
2021-01-27 12:57:19 +03:00
|
|
|
}
|
|
|
|
|
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();
|
2021-02-10 15:23:51 +03:00
|
|
|
|
|
|
|
let public_key = string_to_public_key(public_key)?;
|
|
|
|
|
2021-02-11 18:14:14 +03:00
|
|
|
let weight = tg.weight(public_key)?;
|
2021-02-10 15:23:51 +03:00
|
|
|
|
|
|
|
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)?;
|
2021-02-10 15:23:51 +03:00
|
|
|
|
|
|
|
Ok(public_key)
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:10:18 +03:00
|
|
|
#[fce]
|
2021-02-10 15:23:51 +03:00
|
|
|
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();
|
2021-02-10 15:23:51 +03:00
|
|
|
|
|
|
|
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, &[])?;
|
2021-02-10 15:23:51 +03:00
|
|
|
Ok(certs.into_iter().map(|c| c.into()).collect())
|
|
|
|
}
|
|
|
|
|
2021-01-20 00:17:24 +03:00
|
|
|
#[fce]
|
2021-01-20 17:21:02 +03:00
|
|
|
fn test() -> String {
|
|
|
|
let mut tg = get_data().lock();
|
|
|
|
|
|
|
|
let root_kp = KeyPair::generate();
|
2021-01-21 20:26:17 +03:00
|
|
|
let root_kp2 = KeyPair::generate();
|
2021-01-20 17:21:02 +03:00
|
|
|
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,
|
|
|
|
);
|
2021-02-10 15:23:51 +03:00
|
|
|
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();
|
2021-01-20 17:21:02 +03:00
|
|
|
|
2021-02-10 15:23:51 +03:00
|
|
|
let a = tg.get(second_kp.public_key()).unwrap();
|
2021-01-20 17:21:02 +03:00
|
|
|
let str = format!("{:?}", a);
|
|
|
|
log::info!("{}", &str);
|
|
|
|
|
|
|
|
str
|
2021-01-20 16:14:01 +03:00
|
|
|
}
|