From 0e2590d8944ba60bc39a3243cd1f61180e8b3ae3 Mon Sep 17 00:00:00 2001 From: DieMyst Date: Wed, 20 Jan 2021 16:14:01 +0300 Subject: [PATCH] push it till it compiles, WIP serde to structs, wip implementing sqlite requests --- Cargo.lock | 81 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 +- bin/Cargo.lock | 82 ++++++++++++++++++++++++++++++++++++++ bin/Cargo.toml | 3 +- bin/src/main.rs | 2 +- bin/src/service_api.rs | 2 +- bin/src/storage_impl.rs | 42 +++++++++++++------ identity/Cargo.lock | 11 +++++ identity/Cargo.toml | 2 +- identity/src/public_key.rs | 3 +- identity/src/secret_key.rs | 2 + identity/src/signature.rs | 3 +- src/public_key_hashable.rs | 9 +++++ src/revoke.rs | 3 +- src/trust.rs | 3 +- src/trust_node.rs | 10 +++-- 16 files changed, 237 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b8d4eb..2406d27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,6 +191,41 @@ dependencies = [ "zeroize", ] +[[package]] +name = "darling" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "data-encoding" version = "2.3.1" @@ -232,6 +267,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c66a534cbb46ab4ea03477eae19d5c22c01da8258030280b7bd9d8433fb6ef" dependencies = [ + "serde", "signature", ] @@ -245,6 +281,7 @@ dependencies = [ "ed25519", "rand 0.7.3", "serde", + "serde_bytes", "sha2 0.9.2", "zeroize", ] @@ -580,6 +617,12 @@ dependencies = [ "hmac", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.2.0" @@ -1160,6 +1203,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.118" @@ -1182,6 +1234,28 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f6201e064705553ece353a736a64be975680bd244908cf63e8fa71e478a51a" +dependencies = [ + "serde", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1197ff7de45494f290c1e3e1a6f80e108974681984c87a3e480991ef3d0f1950" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha2" version = "0.8.2" @@ -1237,6 +1311,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" + [[package]] name = "subtle" version = "1.0.0" @@ -1345,6 +1425,7 @@ dependencies = [ "ref-cast", "serde", "serde_json", + "serde_with", "signature", ] diff --git a/Cargo.toml b/Cargo.toml index 3d17ace..118552c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ license = "Apache-2.0" [dependencies] libp2p-core = { package = "fluence-fork-libp2p-core", version = "0.26.0" } serde = { version = "=1.0.118", features = ["derive"] } + fluence-identity = { path = "identity" } serde_json = "1.0.58" bs58 = "0.3.1" @@ -16,6 +17,7 @@ failure = "0.1.6" log = "0.4.11" ref-cast = "1.0.2" derivative = "2.1.1" -ed25519-dalek = "1.0.1" +ed25519-dalek = { version = "1.0.1", features = ["serde"] } rand = "0.7.0" signature = "1.3.0" +serde_with = "1.6.0" diff --git a/bin/Cargo.lock b/bin/Cargo.lock index 7ec2f33..53e88ed 100644 --- a/bin/Cargo.lock +++ b/bin/Cargo.lock @@ -197,6 +197,41 @@ dependencies = [ "zeroize", ] +[[package]] +name = "darling" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "data-encoding" version = "2.3.1" @@ -238,6 +273,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c66a534cbb46ab4ea03477eae19d5c22c01da8258030280b7bd9d8433fb6ef" dependencies = [ + "serde", "signature", ] @@ -251,6 +287,7 @@ dependencies = [ "ed25519", "rand 0.7.3", "serde", + "serde_bytes", "sha2 0.9.2", "zeroize", ] @@ -639,6 +676,12 @@ dependencies = [ "hmac", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.2.0" @@ -1219,6 +1262,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.118" @@ -1241,6 +1293,28 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f6201e064705553ece353a736a64be975680bd244908cf63e8fa71e478a51a" +dependencies = [ + "serde", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1197ff7de45494f290c1e3e1a6f80e108974681984c87a3e480991ef3d0f1950" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha2" version = "0.8.2" @@ -1296,6 +1370,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" + [[package]] name = "subtle" version = "1.0.0" @@ -1404,6 +1484,7 @@ dependencies = [ "ref-cast", "serde", "serde_json", + "serde_with", "signature", ] @@ -1419,6 +1500,7 @@ dependencies = [ "log", "once_cell", "parking_lot", + "serde_json", "trust-graph", ] diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 91e8474..203d6bd 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -19,4 +19,5 @@ anyhow = "1.0.31" boolinator = "2.4.0" once_cell = "1.4.1" parking_lot = "0.11.1" -fce-sqlite-connector = "0.1.3" \ No newline at end of file +fce-sqlite-connector = "0.1.3" +serde_json = "1.0" diff --git a/bin/src/main.rs b/bin/src/main.rs index cf92a55..9503684 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -1,7 +1,7 @@ use fluence::WasmLoggerBuilder; -mod storage_impl; mod service_api; +mod storage_impl; pub fn main() { WasmLoggerBuilder::new() diff --git a/bin/src/service_api.rs b/bin/src/service_api.rs index 22a0823..471461e 100644 --- a/bin/src/service_api.rs +++ b/bin/src/service_api.rs @@ -3,4 +3,4 @@ use fluence::{fce, CallParameters}; #[fce] fn test(a: String) -> String { a -} \ No newline at end of file +} diff --git a/bin/src/storage_impl.rs b/bin/src/storage_impl.rs index b7bde9c..19d7f21 100644 --- a/bin/src/storage_impl.rs +++ b/bin/src/storage_impl.rs @@ -2,13 +2,14 @@ // check if trust is already in list before adding // if there is an older trust - don't add received trust -use trust_graph::{Storage, TrustGraph, PublicKeyHashable, TrustNode, Weight, Auth, Revoke}; +use fce_sqlite_connector; +use fce_sqlite_connector::Value; +use fce_sqlite_connector::{Connection, State}; use fluence_identity::public_key::PublicKey; use once_cell::sync::OnceCell; use parking_lot::Mutex; use std::time::Duration; -use fce_sqlite_connector; -use fce_sqlite_connector::{State, Connection}; +use trust_graph::{Auth, PublicKeyHashable, Revoke, Storage, TrustGraph, TrustNode, Weight}; static INSTANCE: OnceCell> = OnceCell::new(); @@ -26,12 +27,12 @@ struct SqliteStorage { impl SqliteStorage { pub fn init(&self) { - let init_sql = "CREATE TABLE IF NOT EXISTS trusts(\ - peer_id TEXT PRIMARY KEY,\ - relay TEXT NOT NULL,\ - sig TEXT NOT NULL,\ - name TEXT NOT NULL\ + let init_sql = "CREATE TABLE IF NOT EXISTS trustnodes(\ + public_key TEXT PRIMARY KEY,\ + trustnode TEXT NOT NULL,\ );"; + + self.connection.execute(init_sql).unwrap(); } } @@ -39,22 +40,38 @@ impl Storage for SqliteStorage { fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode> { None } - fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) { + fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) { + let mut cursor = self + .connection + .prepare("INSERT INTO trustnodes VALUES (?, ?)") + .unwrap() + .cursor(); + + let tn_str = serde_json::to_string(&node).unwrap(); + + cursor.bind(&[Value::String(format!("{}", pk))]).unwrap(); + cursor + .bind(&[Value::String(format!("{}", tn_str))]) + .unwrap(); + + cursor.next().unwrap(); } fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight> { None } - fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) { - } + fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {} + fn root_keys(&self) -> Vec { vec![] } + fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String> { Err("not implemented".to_string()) } + fn update_auth( &mut self, pk: &PublicKeyHashable, @@ -62,6 +79,5 @@ impl Storage for SqliteStorage { issued_for: &PublicKey, cur_time: Duration, ) { - } -} \ No newline at end of file +} diff --git a/identity/Cargo.lock b/identity/Cargo.lock index 2dbed0d..f6e0a04 100644 --- a/identity/Cargo.lock +++ b/identity/Cargo.lock @@ -192,6 +192,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c66a534cbb46ab4ea03477eae19d5c22c01da8258030280b7bd9d8433fb6ef" dependencies = [ + "serde", "signature", ] @@ -205,6 +206,7 @@ dependencies = [ "ed25519", "rand 0.7.3", "serde", + "serde_bytes", "sha2 0.9.2", "zeroize", ] @@ -1050,6 +1052,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.118" diff --git a/identity/Cargo.toml b/identity/Cargo.toml index a31ed7e..5b4a345 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -11,7 +11,7 @@ libp2p-core = { package = "fluence-fork-libp2p-core", version = "0.26.0" } serde = { version = "=1.0.118", features = ["derive"] } serde_json = "1.0.58" bs58 = "0.3.1" -ed25519-dalek = "1.0.1" +ed25519-dalek = { version = "1.0.1", features = ["serde"] } rand = "0.7.0" signature = "1.3.0" ed25519 = "1.0.3" diff --git a/identity/src/public_key.rs b/identity/src/public_key.rs index b9bafc3..10273c4 100644 --- a/identity/src/public_key.rs +++ b/identity/src/public_key.rs @@ -17,8 +17,9 @@ use crate::signature::Signature; use core::fmt::Debug; use ed25519_dalek::SignatureError; +use serde::{Serialize, Deserialize}; -#[derive(Copy, Clone, Default, Eq, PartialEq)] +#[derive(Copy, Clone, Default, Eq, PartialEq, Serialize, Deserialize)] pub struct PublicKey(pub(crate) ed25519_dalek::PublicKey); impl Debug for PublicKey { diff --git a/identity/src/secret_key.rs b/identity/src/secret_key.rs index ea81223..7d89440 100644 --- a/identity/src/secret_key.rs +++ b/identity/src/secret_key.rs @@ -15,7 +15,9 @@ */ use ed25519_dalek::SignatureError; +use serde::{Serialize, Deserialize}; +#[derive(Serialize, Deserialize)] pub struct SecretKey(ed25519_dalek::SecretKey); impl SecretKey { diff --git a/identity/src/signature.rs b/identity/src/signature.rs index 05d9c71..8ccacc0 100644 --- a/identity/src/signature.rs +++ b/identity/src/signature.rs @@ -15,8 +15,9 @@ */ use signature::Signature as SigSignature; +use serde::{Serialize, Deserialize}; -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Signature(pub ed25519_dalek::Signature); pub const SIGNATURE_LENGTH: usize = 64; diff --git a/src/public_key_hashable.rs b/src/public_key_hashable.rs index bb56ff5..2b440fc 100644 --- a/src/public_key_hashable.rs +++ b/src/public_key_hashable.rs @@ -22,6 +22,7 @@ use std::{ fmt::{Display, Formatter}, hash::{Hash, Hasher}, }; +use serde::ser::{Serializer}; /// Wrapper to use PublicKey in HashMap #[derive(PartialEq, Eq, Debug, Clone, RefCast)] @@ -79,6 +80,14 @@ impl Display for PublicKeyHashable { } } +impl serde::Serialize for PublicKeyHashable { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer { + serializer.serialize_bytes(&self.0.to_bytes()) + } +} + impl<'de> serde::Deserialize<'de> for PublicKeyHashable { fn deserialize(deserializer: D) -> Result where diff --git a/src/revoke.rs b/src/revoke.rs index 12087aa..c9e9db8 100644 --- a/src/revoke.rs +++ b/src/revoke.rs @@ -19,10 +19,11 @@ use fluence_identity::key_pair::KeyPair; use fluence_identity::public_key::PublicKey; use fluence_identity::signature::Signature; use std::time::Duration; +use serde::{Serialize, Deserialize}; /// "A document" that cancels trust created before. /// TODO delete pk from Revoke (it is already in a trust node) -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Revoke { /// who is revoked pub pk: PublicKey, diff --git a/src/trust.rs b/src/trust.rs index daf1467..1c772dc 100644 --- a/src/trust.rs +++ b/src/trust.rs @@ -20,6 +20,7 @@ use fluence_identity::public_key::PublicKey; use fluence_identity::signature::Signature; use std::convert::TryInto; use std::time::Duration; +use serde::{Serialize, Deserialize}; pub const SIG_LEN: usize = 64; pub const PK_LEN: usize = 32; @@ -30,7 +31,7 @@ pub const TRUST_LEN: usize = SIG_LEN + PK_LEN + EXPIRATION_LEN + ISSUED_LEN; /// One element in chain of trust in a certificate. /// TODO delete pk from Trust (it is already in a trust node) -#[derive(Clone, PartialEq, Derivative, Eq)] +#[derive(Clone, PartialEq, Derivative, Eq, Serialize, Deserialize)] #[derivative(Debug)] pub struct Trust { /// For whom this certificate is issued diff --git a/src/trust_node.rs b/src/trust_node.rs index f613459..fc2f7ec 100644 --- a/src/trust_node.rs +++ b/src/trust_node.rs @@ -20,8 +20,10 @@ use crate::trust::Trust; use failure::_core::time::Duration; use fluence_identity::public_key::PublicKey; use std::collections::HashMap; +use serde::{Serialize, Deserialize}; +use serde_with::serde_as; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] enum TrustRelation { Auth(Auth), Revoke(Revoke), @@ -46,7 +48,7 @@ impl TrustRelation { } /// Represents who give a certificate -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Auth { /// proof of this authorization pub trust: Trust, @@ -56,12 +58,14 @@ pub struct Auth { /// An element of trust graph that store relations (trust or revoke) /// that given by some owners of public keys. -#[derive(Debug)] +#[serde_as] +#[derive(Debug, Serialize, Deserialize)] pub struct TrustNode { /// identity key of this element pub pk: PublicKey, /// one public key could be authorized or revoked by multiple certificates + #[serde_as(as = "Vec<(_, _)>")] trust_relations: HashMap, /// for maintain