fmt, bin sub project for wasm

This commit is contained in:
DieMyst 2021-01-19 17:25:28 +03:00
parent dd761dd61c
commit 37de2ff336
12 changed files with 38 additions and 32 deletions

1
bin/Cargo.lock generated
View File

@ -1400,6 +1400,7 @@ name = "trust-graph-wasm"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"fluence", "fluence",
"log",
"trust-graph", "trust-graph",
] ]

View File

@ -12,4 +12,5 @@ path = "src/main.rs"
[dependencies] [dependencies]
trust-graph = { path = "../" } trust-graph = { path = "../" }
log = "0.4.8"
fluence = { version = "0.2.18", features = ["logger"] } fluence = { version = "0.2.18", features = ["logger"] }

View File

@ -1,7 +1,5 @@
use fluence::WasmLoggerBuilder; use fluence::WasmLoggerBuilder;
pub(crate) type Result<T> = std::result::Result<T, errors::HistoryError>;
pub fn main() { pub fn main() {
WasmLoggerBuilder::new() WasmLoggerBuilder::new()
.with_log_level(log::Level::Info) .with_log_level(log::Level::Info)

View File

@ -14,13 +14,13 @@
* limitations under the License. * limitations under the License.
*/ */
use crate::ed25519::{Keypair as Libp2pKeyPair}; use crate::ed25519::Keypair as Libp2pKeyPair;
use ed25519_dalek::SignatureError; use ed25519_dalek::SignatureError;
use ed25519_dalek::{PublicKey, Signer, SecretKey}; use ed25519_dalek::{PublicKey, SecretKey, Signer};
use core::fmt::{Debug}; use core::fmt::Debug;
use std::fmt;
use rand::rngs::OsRng; use rand::rngs::OsRng;
use std::fmt;
pub type Signature = ed25519_dalek::Signature; pub type Signature = ed25519_dalek::Signature;
@ -34,14 +34,14 @@ impl KeyPair {
/// Generate a new Ed25519 keypair. /// Generate a new Ed25519 keypair.
#[allow(dead_code)] #[allow(dead_code)]
pub fn generate() -> Self { pub fn generate() -> Self {
let mut csprng = OsRng { }; let mut csprng = OsRng {};
let kp = ed25519_dalek::Keypair::generate(&mut csprng); let kp = ed25519_dalek::Keypair::generate(&mut csprng);
kp.into() kp.into()
} }
pub fn from_bytes(sk_bytes: &[u8]) -> Result<Self, SignatureError> { pub fn from_bytes(sk_bytes: &[u8]) -> Result<Self, SignatureError> {
let kp = ed25519_dalek::Keypair::from_bytes(sk_bytes)?; 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 /// Encode the keypair into a byte array by concatenating the bytes
@ -55,9 +55,7 @@ impl KeyPair {
#[allow(dead_code)] #[allow(dead_code)]
pub fn decode(kp: &[u8]) -> Result<KeyPair, SignatureError> { pub fn decode(kp: &[u8]) -> Result<KeyPair, SignatureError> {
let kp = ed25519_dalek::Keypair::from_bytes(kp)?; let kp = ed25519_dalek::Keypair::from_bytes(kp)?;
Ok(Self { Ok(Self { key_pair: kp })
key_pair: kp,
})
} }
/// Get the public key of this keypair. /// Get the public key of this keypair.
@ -142,6 +140,8 @@ impl Clone for KeyPair {
.expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k"); .expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k");
let public = PublicKey::from_bytes(&self.key_pair.public.to_bytes()) let public = PublicKey::from_bytes(&self.key_pair.public.to_bytes())
.expect("ed25519::PublicKey::from_bytes(to_bytes(k)) != k"); .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 },
}
} }
} }

View File

@ -14,9 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
use crate::trust::{Trust, TRUST_LEN};
use ed25519_dalek::PublicKey; use ed25519_dalek::PublicKey;
use fluence_identity::key_pair::KeyPair; use fluence_identity::key_pair::KeyPair;
use crate::trust::{Trust, TRUST_LEN};
use std::str::FromStr; use std::str::FromStr;
use std::time::Duration; use std::time::Duration;
@ -236,8 +236,8 @@ impl FromStr for Certificate {
mod tests { mod tests {
use super::*; use super::*;
use crate::misc::current_time; use crate::misc::current_time;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use fluence_identity::key_pair::KeyPair; use fluence_identity::key_pair::KeyPair;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub fn one_second() -> Duration { pub fn one_second() -> Duration {
Duration::from_secs(1) Duration::from_secs(1)

View File

@ -16,12 +16,12 @@
use ed25519_dalek::PublicKey; use ed25519_dalek::PublicKey;
use core::fmt;
use ref_cast::RefCast; use ref_cast::RefCast;
use std::{ use std::{
fmt::{Display, Formatter}, fmt::{Display, Formatter},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
}; };
use core::fmt;
/// Wrapper to use PublicKey in HashMap /// Wrapper to use PublicKey in HashMap
#[derive(PartialEq, Eq, Debug, Clone, RefCast)] #[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<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: serde::Deserializer<'de> { D: serde::Deserializer<'de>,
{
use serde::de::{Error, Visitor}; use serde::de::{Error, Visitor};
struct PKVisitor; struct PKVisitor;
@ -102,7 +103,9 @@ impl <'de>serde::Deserialize<'de> for PublicKeyHashable {
.into_vec() .into_vec()
.map_err(|err| Error::custom(format!("Invalid string '{}': {}", s, err))) .map_err(|err| Error::custom(format!("Invalid string '{}': {}", s, err)))
.and_then(|v| self.visit_bytes(v.as_slice())) .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<E>(self, b: &[u8]) -> Result<Self::Value, E> fn visit_bytes<E>(self, b: &[u8]) -> Result<Self::Value, E>

View File

@ -14,10 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
use crate::trust::{EXPIRATION_LEN, PK_LEN};
use ed25519_dalek::PublicKey; use ed25519_dalek::PublicKey;
use fluence_identity::key_pair::KeyPair; use fluence_identity::key_pair::KeyPair;
use fluence_identity::key_pair::Signature; use fluence_identity::key_pair::Signature;
use crate::trust::{EXPIRATION_LEN, PK_LEN};
use std::time::Duration; use std::time::Duration;
/// "A document" that cancels trust created before. /// "A document" that cancels trust created before.

View File

@ -14,12 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
use ed25519_dalek::{PublicKey};
use fluence_identity::key_pair::{KeyPair, Signature};
use derivative::Derivative; 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::convert::TryInto;
use std::time::Duration; use std::time::Duration;
use signature::{Signature as SigSignature};
pub const SIG_LEN: usize = 64; pub const SIG_LEN: usize = 64;
pub const PK_LEN: usize = 32; 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()) 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()) write!(f, "{}", bs58::encode(&sig.to_bytes()).into_string())
} }

View File

@ -15,12 +15,12 @@
*/ */
use crate::certificate::Certificate; use crate::certificate::Certificate;
use ed25519_dalek::PublicKey;
use crate::public_key_hashable::PublicKeyHashable; use crate::public_key_hashable::PublicKeyHashable;
use crate::revoke::Revoke; use crate::revoke::Revoke;
use crate::trust::Trust; use crate::trust::Trust;
use crate::trust_graph_storage::Storage; use crate::trust_graph_storage::Storage;
use crate::trust_node::{Auth, TrustNode}; use crate::trust_node::{Auth, TrustNode};
use ed25519_dalek::PublicKey;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::{HashSet, VecDeque}; use std::collections::{HashSet, VecDeque};
use std::time::Duration; use std::time::Duration;
@ -279,10 +279,10 @@ impl TrustGraph {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use fluence_identity::key_pair::KeyPair;
use crate::misc::current_time; use crate::misc::current_time;
use crate::trust_graph_storage::InMemoryStorage; use crate::trust_graph_storage::InMemoryStorage;
use failure::_core::time::Duration; use failure::_core::time::Duration;
use fluence_identity::key_pair::KeyPair;
use std::collections::HashMap; use std::collections::HashMap;
pub fn one_minute() -> Duration { pub fn one_minute() -> Duration {

View File

@ -14,10 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
use ed25519_dalek::PublicKey;
use crate::public_key_hashable::PublicKeyHashable; use crate::public_key_hashable::PublicKeyHashable;
use crate::revoke::Revoke; use crate::revoke::Revoke;
use crate::trust::Trust; use crate::trust::Trust;
use ed25519_dalek::PublicKey;
use failure::_core::time::Duration; use failure::_core::time::Duration;
use std::collections::HashMap; use std::collections::HashMap;