dto conversion, more errors

This commit is contained in:
DieMyst 2021-02-12 14:27:22 +03:00
parent fb13bc1cdf
commit 66ace9d3df
4 changed files with 69 additions and 14 deletions

View File

@ -17,6 +17,13 @@
use serde::{Deserialize, Serialize};
use signature::Error as SigError;
use signature::Signature as SigSignature;
use thiserror::Error as ThisError;
#[derive(ThisError, Debug)]
pub enum SignatureError {
#[error("{0}")]
Error(#[from] SigError),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Signature(pub ed25519_dalek::Signature);
@ -34,7 +41,7 @@ impl Signature {
self.0.to_bytes()
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, SigError> {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, SignatureError> {
let sig = ed25519_dalek::Signature::from_bytes(bytes)?;
Ok(Signature(sig))
}

View File

@ -15,13 +15,12 @@
*/
use crate::trust::TrustError::{
Base58DecodeError, DecodePublicKeyError, IncorrectTrustLength, ParseError, PublicKeyError,
SignatureError, SignatureFromBytesError,
Base58DecodeError, DecodePublicKeyError, IncorrectTrustLength, ParseError, SignatureError,
};
use derivative::Derivative;
use fluence_identity::key_pair::KeyPair;
use fluence_identity::public_key::{PKError, PublicKey};
use fluence_identity::signature::Signature;
use fluence_identity::signature::{Signature, SignatureError as SigError};
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::num::ParseIntError;
@ -69,7 +68,7 @@ pub enum TrustError {
/// Errors occured on signature verification
#[error("{0}")]
SignatureError(ed25519_dalek::SignatureError),
SignatureError(#[from] ed25519_dalek::SignatureError),
/// Errors occured on trust decoding from differrent formats
#[error("Cannot decode the public key: {0} in the trust: {1}")]
@ -82,10 +81,10 @@ pub enum TrustError {
Base58DecodeError(String, String, bs58::decode::Error),
#[error("Cannot decode a signature from bytes: {0}")]
SignatureFromBytesError(signature::Error),
SignatureFromBytesError(#[from] SigError),
#[error("{0}")]
PublicKeyError(PKError),
PublicKeyError(#[from] PKError),
#[error(
"Trust length should be 104: public key(32) + signature(64) + expiration date(8), was: {0}"
@ -179,10 +178,10 @@ impl Trust {
return Err(IncorrectTrustLength(arr.len()));
}
let pk = PublicKey::from_bytes(&arr[0..PK_LEN]).map_err(PublicKeyError)?;
let pk = PublicKey::from_bytes(&arr[0..PK_LEN])?;
let signature = &arr[PK_LEN..PK_LEN + SIG_LEN];
let signature = Signature::from_bytes(signature).map_err(SignatureFromBytesError)?;
let signature = Signature::from_bytes(signature)?;
let expiration_bytes = &arr[PK_LEN + SIG_LEN..PK_LEN + SIG_LEN + EXPIRATION_LEN];
let expiration_date = u64::from_le_bytes(expiration_bytes.try_into().unwrap());
@ -226,7 +225,7 @@ impl Trust {
// 64 bytes signature
let signature = Self::bs58_str_to_vec(signature, "signature")?;
let signature = Signature::from_bytes(&signature).map_err(SignatureFromBytesError)?;
let signature = Signature::from_bytes(&signature)?;
// Duration
let expires_at = Self::str_to_duration(expires_at, "expires_at")?;

View File

@ -1,4 +1,20 @@
use fluence::fce;
use fluence_identity::public_key::PKError;
use fluence_identity::signature::SignatureError;
use fluence_identity::{PublicKey, Signature};
use std::convert::TryFrom;
use std::time::Duration;
use thiserror::Error as ThisError;
#[derive(ThisError, Debug)]
pub enum DtoConversionError {
#[error("Cannot convert base58 string to bytes: {0}")]
Base58Error(#[from] bs58::decode::Error),
#[error("Cannot convert string to PublicKey: {0}")]
PublicKeyDecodeError(#[from] PKError),
#[error("Cannot convert string to PublicKey: {0}")]
SignatureDecodeError(#[from] SignatureError),
}
#[fce]
pub struct Certificate {
@ -12,6 +28,20 @@ impl From<trust_graph::Certificate> for Certificate {
}
}
impl TryFrom<Certificate> for trust_graph::Certificate {
type Error = DtoConversionError;
fn try_from(c: Certificate) -> Result<Self, Self::Error> {
let chain: Result<Vec<trust_graph::Trust>, DtoConversionError> = c
.chain
.into_iter()
.map(|t| trust_graph::Trust::try_from(t))
.collect();
let chain = chain?;
return Ok(trust_graph::Certificate { chain });
}
}
#[fce]
pub struct Trust {
/// For whom this certificate is issued, base58
@ -25,6 +55,24 @@ pub struct Trust {
pub issued_at: u64,
}
impl TryFrom<Trust> for trust_graph::Trust {
type Error = DtoConversionError;
fn try_from(t: Trust) -> Result<Self, Self::Error> {
let issued_for = PublicKey::from_base58(&t.issued_for)?;
let signature = bs58::decode(&t.signature).into_vec()?;
let signature = Signature::from_bytes(&signature)?;
let expires_at = Duration::from_secs(t.expires_at);
let issued_at = Duration::from_secs(t.issued_at);
return Ok(trust_graph::Trust {
issued_for,
expires_at,
signature,
issued_at,
});
}
}
impl From<trust_graph::Trust> for Trust {
fn from(t: trust_graph::Trust) -> Self {
let issued_for = bs58::encode(t.issued_for.to_bytes()).into_string();

View File

@ -14,7 +14,6 @@ use trust_graph::{CertificateError, TrustGraphError};
pub enum ServiceError {
#[error("{0}")]
PublicKeyDecodeError(#[from] PKError),
#[error("{0}")]
TGError(#[from] TrustGraphError),
#[error("{0}")]
@ -31,9 +30,11 @@ fn insert_cert_impl(certificate: String, duration: u64) -> Result<(), ServiceErr
}
#[fce]
// TODO: some sort of auth?
fn insert_cert(certificate: String, duration: u64) -> InsertResult {
insert_cert_impl(certificate, duration).into()
/// 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()
}
fn get_weight_impl(public_key: String) -> Result<Option<u32>, ServiceError> {