From 37de2ff3364e6dbaefe1181f4fbad8990492edbf Mon Sep 17 00:00:00 2001 From: DieMyst Date: Tue, 19 Jan 2021 17:25:28 +0300 Subject: [PATCH] fmt, bin sub project for wasm --- bin/Cargo.lock | 1 + bin/Cargo.toml | 1 + bin/{ => src}/main.rs | 4 +--- bin/{ => src}/service_api.rs | 0 bin/{ => src}/storage_impl.rs | 0 identity/src/key_pair.rs | 20 ++++++++++---------- src/certificate.rs | 4 ++-- src/public_key_hashable.rs | 21 ++++++++++++--------- src/revoke.rs | 2 +- src/trust.rs | 11 +++++++---- src/trust_graph.rs | 4 ++-- src/trust_node.rs | 2 +- 12 files changed, 38 insertions(+), 32 deletions(-) rename bin/{ => src}/main.rs (67%) rename bin/{ => src}/service_api.rs (100%) rename bin/{ => src}/storage_impl.rs (100%) diff --git a/bin/Cargo.lock b/bin/Cargo.lock index 8e53f3b..4265d78 100644 --- a/bin/Cargo.lock +++ b/bin/Cargo.lock @@ -1400,6 +1400,7 @@ name = "trust-graph-wasm" version = "0.2.0" dependencies = [ "fluence", + "log", "trust-graph", ] diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 7aeba59..fac534a 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -12,4 +12,5 @@ path = "src/main.rs" [dependencies] trust-graph = { path = "../" } +log = "0.4.8" fluence = { version = "0.2.18", features = ["logger"] } \ No newline at end of file diff --git a/bin/main.rs b/bin/src/main.rs similarity index 67% rename from bin/main.rs rename to bin/src/main.rs index deba21a..1e07879 100644 --- a/bin/main.rs +++ b/bin/src/main.rs @@ -1,10 +1,8 @@ use fluence::WasmLoggerBuilder; -pub(crate) type Result = std::result::Result; - pub fn main() { WasmLoggerBuilder::new() .with_log_level(log::Level::Info) .build() .unwrap(); -} \ No newline at end of file +} diff --git a/bin/service_api.rs b/bin/src/service_api.rs similarity index 100% rename from bin/service_api.rs rename to bin/src/service_api.rs diff --git a/bin/storage_impl.rs b/bin/src/storage_impl.rs similarity index 100% rename from bin/storage_impl.rs rename to bin/src/storage_impl.rs diff --git a/identity/src/key_pair.rs b/identity/src/key_pair.rs index c5a7694..b4e2dd0 100644 --- a/identity/src/key_pair.rs +++ b/identity/src/key_pair.rs @@ -14,13 +14,13 @@ * limitations under the License. */ -use crate::ed25519::{Keypair as Libp2pKeyPair}; +use crate::ed25519::Keypair as Libp2pKeyPair; use ed25519_dalek::SignatureError; -use ed25519_dalek::{PublicKey, Signer, SecretKey}; +use ed25519_dalek::{PublicKey, SecretKey, Signer}; -use core::fmt::{Debug}; -use std::fmt; +use core::fmt::Debug; use rand::rngs::OsRng; +use std::fmt; pub type Signature = ed25519_dalek::Signature; @@ -34,14 +34,14 @@ impl KeyPair { /// Generate a new Ed25519 keypair. #[allow(dead_code)] pub fn generate() -> Self { - let mut csprng = OsRng { }; + let mut csprng = OsRng {}; let kp = ed25519_dalek::Keypair::generate(&mut csprng); kp.into() } pub fn from_bytes(sk_bytes: &[u8]) -> Result { let kp = ed25519_dalek::Keypair::from_bytes(sk_bytes)?; - Ok(KeyPair {key_pair: kp}) + Ok(KeyPair { key_pair: kp }) } /// Encode the keypair into a byte array by concatenating the bytes @@ -55,9 +55,7 @@ impl KeyPair { #[allow(dead_code)] pub fn decode(kp: &[u8]) -> Result { let kp = ed25519_dalek::Keypair::from_bytes(kp)?; - Ok(Self { - key_pair: kp, - }) + Ok(Self { key_pair: kp }) } /// Get the public key of this keypair. @@ -142,6 +140,8 @@ impl Clone for KeyPair { .expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k"); let public = PublicKey::from_bytes(&self.key_pair.public.to_bytes()) .expect("ed25519::PublicKey::from_bytes(to_bytes(k)) != k"); - KeyPair { key_pair: ed25519_dalek::Keypair { secret, public } } + KeyPair { + key_pair: ed25519_dalek::Keypair { secret, public }, + } } } diff --git a/src/certificate.rs b/src/certificate.rs index a2522f9..882378c 100644 --- a/src/certificate.rs +++ b/src/certificate.rs @@ -14,9 +14,9 @@ * limitations under the License. */ +use crate::trust::{Trust, TRUST_LEN}; use ed25519_dalek::PublicKey; use fluence_identity::key_pair::KeyPair; -use crate::trust::{Trust, TRUST_LEN}; use std::str::FromStr; use std::time::Duration; @@ -236,8 +236,8 @@ impl FromStr for Certificate { mod tests { use super::*; use crate::misc::current_time; - use std::time::{Duration, SystemTime, UNIX_EPOCH}; use fluence_identity::key_pair::KeyPair; + use std::time::{Duration, SystemTime, UNIX_EPOCH}; pub fn one_second() -> Duration { Duration::from_secs(1) diff --git a/src/public_key_hashable.rs b/src/public_key_hashable.rs index 3f01d11..abad57a 100644 --- a/src/public_key_hashable.rs +++ b/src/public_key_hashable.rs @@ -16,12 +16,12 @@ use ed25519_dalek::PublicKey; +use core::fmt; use ref_cast::RefCast; use std::{ fmt::{Display, Formatter}, hash::{Hash, Hasher}, }; -use core::fmt; /// Wrapper to use PublicKey in HashMap #[derive(PartialEq, Eq, Debug, Clone, RefCast)] @@ -79,10 +79,11 @@ impl Display for PublicKeyHashable { } } -impl <'de>serde::Deserialize<'de> for PublicKeyHashable { +impl<'de> serde::Deserialize<'de> for PublicKeyHashable { fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de> { + where + D: serde::Deserializer<'de>, + { use serde::de::{Error, Visitor}; struct PKVisitor; @@ -95,19 +96,21 @@ impl <'de>serde::Deserialize<'de> for PublicKeyHashable { } fn visit_str(self, s: &str) -> Result - where - E: Error, + where + E: Error, { bs58::decode(s) .into_vec() .map_err(|err| Error::custom(format!("Invalid string '{}': {}", s, err))) .and_then(|v| self.visit_bytes(v.as_slice())) - .map_err(|err: E| Error::custom(format!("Parsed string '{}' as base58, but {}", s, err))) + .map_err(|err: E| { + Error::custom(format!("Parsed string '{}' as base58, but {}", s, err)) + }) } fn visit_bytes(self, b: &[u8]) -> Result - where - E: Error, + where + E: Error, { let pk = PublicKey::from_bytes(b) .map_err(|err| Error::custom(format!("Invalid bytes {:?}: {}", b, err)))?; diff --git a/src/revoke.rs b/src/revoke.rs index 27acf8d..c148d98 100644 --- a/src/revoke.rs +++ b/src/revoke.rs @@ -14,10 +14,10 @@ * limitations under the License. */ +use crate::trust::{EXPIRATION_LEN, PK_LEN}; use ed25519_dalek::PublicKey; use fluence_identity::key_pair::KeyPair; use fluence_identity::key_pair::Signature; -use crate::trust::{EXPIRATION_LEN, PK_LEN}; use std::time::Duration; /// "A document" that cancels trust created before. diff --git a/src/trust.rs b/src/trust.rs index 8db00e7..26fda13 100644 --- a/src/trust.rs +++ b/src/trust.rs @@ -14,12 +14,12 @@ * limitations under the License. */ -use ed25519_dalek::{PublicKey}; -use fluence_identity::key_pair::{KeyPair, Signature}; use derivative::Derivative; +use ed25519_dalek::PublicKey; +use fluence_identity::key_pair::{KeyPair, Signature}; +use signature::Signature as SigSignature; use std::convert::TryInto; use std::time::Duration; -use signature::{Signature as SigSignature}; pub const SIG_LEN: usize = 64; pub const PK_LEN: usize = 32; @@ -50,7 +50,10 @@ fn show_pubkey(key: &PublicKey, f: &mut std::fmt::Formatter<'_>) -> Result<(), s write!(f, "{}", bs58::encode(&key.to_bytes()).into_string()) } -fn show_sig(sig: &ed25519_dalek::Signature, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { +fn show_sig( + sig: &ed25519_dalek::Signature, + f: &mut std::fmt::Formatter<'_>, +) -> Result<(), std::fmt::Error> { write!(f, "{}", bs58::encode(&sig.to_bytes()).into_string()) } diff --git a/src/trust_graph.rs b/src/trust_graph.rs index f947024..46fecd6 100644 --- a/src/trust_graph.rs +++ b/src/trust_graph.rs @@ -15,12 +15,12 @@ */ use crate::certificate::Certificate; -use ed25519_dalek::PublicKey; use crate::public_key_hashable::PublicKeyHashable; use crate::revoke::Revoke; use crate::trust::Trust; use crate::trust_graph_storage::Storage; use crate::trust_node::{Auth, TrustNode}; +use ed25519_dalek::PublicKey; use std::borrow::Borrow; use std::collections::{HashSet, VecDeque}; use std::time::Duration; @@ -279,10 +279,10 @@ impl TrustGraph { #[cfg(test)] mod tests { use super::*; - use fluence_identity::key_pair::KeyPair; use crate::misc::current_time; use crate::trust_graph_storage::InMemoryStorage; use failure::_core::time::Duration; + use fluence_identity::key_pair::KeyPair; use std::collections::HashMap; pub fn one_minute() -> Duration { diff --git a/src/trust_node.rs b/src/trust_node.rs index 830299d..7713821 100644 --- a/src/trust_node.rs +++ b/src/trust_node.rs @@ -14,10 +14,10 @@ * limitations under the License. */ -use ed25519_dalek::PublicKey; use crate::public_key_hashable::PublicKeyHashable; use crate::revoke::Revoke; use crate::trust::Trust; +use ed25519_dalek::PublicKey; use failure::_core::time::Duration; use std::collections::HashMap;