proxy structs, get_all_certs and insert functions

This commit is contained in:
DieMyst 2021-02-10 15:23:51 +03:00
parent e89452c0b0
commit 3a9530b92d
4 changed files with 166 additions and 28 deletions

View File

@ -2,6 +2,8 @@ use fluence::WasmLoggerBuilder;
mod service_api;
mod storage_impl;
mod results;
mod proxy_structs;
pub fn main() {
WasmLoggerBuilder::new()

46
bin/src/proxy_structs.rs Normal file
View File

@ -0,0 +1,46 @@
use trust_graph::{Trust as TGTrust, Certificate as TGCertificate};
use fluence::fce;
#[fce]
pub struct Certificate {
pub chain: Vec<Trust>,
}
impl From<TGCertificate> for Certificate {
fn from(t: TGCertificate) -> Self {
let chain: Vec<Trust> = t.chain.into_iter().map(|t| t.into()).collect();
return Certificate {
chain
}
}
}
#[fce]
pub struct Trust {
/// For whom this certificate is issued, base58
pub issued_for: String,
/// Expiration date of a trust, in secs
pub expires_at: u64,
/// Signature of a previous trust in a chain.
/// Signature is self-signed if it is a root trust, base58
pub signature: String,
/// Creation time of a trust, in secs
pub issued_at: u64,
}
impl From<TGTrust> for Trust {
fn from(t: TGTrust) -> Self {
let issued_for = bs58::encode(t.issued_for.to_bytes()).into_string();
let signature = bs58::encode(t.signature.to_bytes()).into_string();
let expires_at = t.expires_at.as_secs();
let issued_at = t.issued_at.as_secs();
return Trust {
issued_for,
expires_at,
signature,
issued_at
}
}
}

71
bin/src/results.rs Normal file
View File

@ -0,0 +1,71 @@
use fluence::fce;
use crate::proxy_structs::Certificate;
#[fce]
pub struct InsertResult {
pub ret_code: u32,
pub error: String,
}
impl From<Result<(), String>> for InsertResult {
fn from(result: Result<(), String>) -> Self {
match result {
Ok(()) => InsertResult {
ret_code: 0,
error: "".to_string(),
},
Err(e) => InsertResult {
ret_code: 1,
error: e,
},
}
}
}
#[fce]
pub struct WeightResult {
pub ret_code: u32,
pub weight: Vec<u32>,
pub error: String
}
impl From<Result<Option<u32>, String>> for WeightResult {
fn from(result: Result<Option<u32>, String>) -> Self {
match result {
Ok(wo) => WeightResult {
ret_code: 0,
weight: wo.map(|w| vec![w]).unwrap_or(vec![]),
error: "".to_string(),
},
Err(e) => WeightResult {
ret_code: 1,
weight: vec![],
error: e,
},
}
}
}
#[fce]
pub struct AllCertsResult {
pub ret_code: u32,
pub certificates: Vec<Certificate>,
pub error: String
}
impl From<Result<Vec<Certificate>, String>> for AllCertsResult {
fn from(result: Result<Vec<Certificate>, String>) -> Self {
match result {
Ok(certs) => AllCertsResult {
ret_code: 0,
certificates: certs,
error: "".to_string(),
},
Err(e) => AllCertsResult {
ret_code: 1,
certificates: vec![],
error: e,
},
}
}
}

View File

@ -1,46 +1,65 @@
use crate::storage_impl::get_data;
use fluence::fce;
use fluence_identity::KeyPair;
use fluence_identity::{KeyPair, PublicKey};
use std::convert::{From, Into};
use std::fmt::Display;
use std::str::FromStr;
use std::time::Duration;
use trust_graph::Certificate;
struct InsertResult {
ret_code: u32,
error: String,
}
impl From<Result<(), String>> for InsertResult {
fn from(result: Result<(), String>) -> Self {
match result {
Ok(()) => InsertResult {
ret_code: 0,
error: "".to_string(),
},
Err(e) => InsertResult {
ret_code: 1,
error: e,
},
}
}
}
use trust_graph::{Certificate as TGCertificate};
use crate::proxy_structs::Certificate;
use crate::results::{WeightResult, InsertResult, AllCertsResult};
fn insert_cert_impl(certificate: String, duration: u64) -> Result<(), String> {
let duration = Duration::from_millis(duration);
let certificate = Certificate::from_str(&certificate)?;
let certificate = TGCertificate::from_str(&certificate)?;
let mut tg = get_data().lock();
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()
}
fn get_weight_impl(public_key: String) -> Result<Option<u32>, String> {
let mut 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<PublicKey, String> {
let public_key = bs58::decode(public_key)
.into_vec().map_err(|e| format!("Couldn't decode public_key from base58: {}", e))?;
let public_key = PublicKey::from_bytes(&public_key)
.map_err(|e| format!("Couldn't decode public_key: {}", e))?;
Ok(public_key)
}
fn get_all_certs(issued_for: String) -> AllCertsResult {
get_all_certs_impl(issued_for).into()
}
fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, String> {
let mut 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();
@ -52,12 +71,12 @@ fn test() -> String {
let expires_at = Duration::new(15, 15);
let issued_at = Duration::new(5, 5);
let cert = Certificate::issue_root(&root_kp, second_kp.public_key(), expires_at, issued_at);
tg.add_root_weight(root_kp.public().into(), 0);
tg.add_root_weight(root_kp2.public().into(), 1);
tg.add(cert, Duration::new(10, 10));
let cert = TGCertificate::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());
let a = tg.get(second_kp.public_key()).unwrap();
let str = format!("{:?}", a);
log::info!("{}", &str);