From 3c33d73b852b589ce51704a36b76321e30b26b81 Mon Sep 17 00:00:00 2001 From: luo4lu Date: Mon, 27 Apr 2020 17:39:27 +0800 Subject: [PATCH 1/6] base interface realize function and test example --- .github/workflows/ci.yml | 33 +++++++ .gitignore | 3 + Cargo.toml | 21 +++++ src/lib.rs | 1 + src/node_information.rs | 138 +++++++++++++++++++++++++++++ test/test.db/conf | 4 + test/test.db/db | Bin 0 -> 270 bytes test/test.db/snap.00000000000000D6 | Bin 0 -> 172 bytes 8 files changed, 200 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/node_information.rs create mode 100644 test/test.db/conf create mode 100644 test/test.db/db create mode 100644 test/test.db/snap.00000000000000D6 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..b5d37f8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,33 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Install latest nightly + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + components: rustfmt + - name: Format code. + uses: actions-rs/cargo@v1 + with: + command: fmt + - name: Build + uses: actions-rs/cargo@v1 + with: + command: build + - name: Tests + uses: actions-rs/cargo@v1 + with: + command: test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7764d73 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/test +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5ca214a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "libnode" +version = "0.1.0" +authors = ["luo4lu "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +kv-hal-sled = { git = "https://github.com/Yinet-project/kv-hal-sled" } +kv-hal = { git = "https://github.com/Yinet-project/kv-hal" } +rust-crypto = "0.2.36" +rand = "0.7.3" +async-trait = "0.1" +sled = "0.30.3" +tokio = { version = "0.2", features = ["full"] } + +[dev-dependencies.cargo-husky] +version = "1" +default-features = false +features = ["precommit-hook", "run-cargo-fmt", "run-cargo-test", "run-cargo-clippy"] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ba32935 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod node_information; \ No newline at end of file diff --git a/src/node_information.rs b/src/node_information.rs new file mode 100644 index 0000000..a954042 --- /dev/null +++ b/src/node_information.rs @@ -0,0 +1,138 @@ +use kv_hal_sled::SledStorage; +use kv_hal::kv::Storage; +use std::convert::TryFrom; +use crypto::{ed25519, digest::Digest, sha3::Sha3, ripemd160::Ripemd160}; +//use sled; + +pub struct NodeInfo{ + seed: [u8; 32], + secret_key: [u8; 64], + public_key: [u8; 32], + node_id: [u8; 20], +} + +impl NodeInfo{ + + async fn get_value(string: &str, key: &[u8]) -> Result>, sled::Error> + { + let stor = SledStorage::new(string); + stor.get(key).await + } + pub async fn new(string: &str, key: &[u8]) ->Self{ + let seed = NodeInfo::get_value(string, key).await; + if let Ok(seed) = seed{ + if let Some(seed) = seed{ + NodeInfo::init_node(&seed) + } + else{ + NodeInfo::rebuild_key() + } + } + else{ + NodeInfo::rebuild_key() + } + } + //如果value存在初始化结构体 + fn init_node(seed_s: &[u8]) ->Self{ + //于rebuild_key接口功能类似,不产生随机种子而是根据入参计算秘钥,秘钥-》nodeid + let seed: [u8; 32] = TryFrom::try_from(seed_s).unwrap(); + let(secret_key,public_key) = ed25519::keypair(&seed); + //两次hash let nodeID = ripemd160(sha3(public key)); + //创建一个SHA3-256的对象 + let mut hasher = Sha3::sha3_256(); + //传入公钥 + hasher.input(&public_key); + let mut hex: Vec = Vec::new(); + hasher.result(&mut hex); + + let mut ripemd = Ripemd160::new(); + ripemd.input(&hex); + let mut node_id: [u8; 20] = [0; 20]; + ripemd.result(&mut node_id); + + Self { + seed, + secret_key, + public_key, + node_id, + } + } + + //通过公钥私钥计算出NodeID + pub fn get_node(&self) ->[u8; 20] + { + //获取nodeid,rebuild_key里面的算法获取 + let mut hasher = Sha3::sha3_256(); + //传入公钥 + hasher.input(&self.public_key); + let mut hex: Vec = Vec::new(); + hasher.result(&mut hex); + + let mut ripemd = Ripemd160::new(); + ripemd.input(&hex); + let mut node_id: [u8; 20] = [0; 20]; + ripemd.result(&mut node_id); + assert_eq!(node_id, self.node_id); + node_id + } + + //检查是否存在公钥私钥 + pub fn check_key(&self) ->bool{ + //由get_value获取的私钥计算出公钥与数据结构中存在的公秘钥比较 + let(secret_key,public_key) = ed25519::keypair(&self.seed); + for (i,_) in secret_key.iter().enumerate(){ + assert_eq!(secret_key[i], self.secret_key[i]); + } + assert_eq!(public_key, self.public_key); + if self.secret_key.len()!=64 && self.public_key.len()!=32{ + return false; + } + true + } + + //如果value不存在生成新的公私钥 + fn rebuild_key() ->Self + { + let iter :[u8;32] = [0;32]; + let mut seed:[u8;32] = [0;32]; + for(i,_) in iter.iter().enumerate() + { + seed[i]=rand::random::(); + } + let(secret_key,public_key) = ed25519::keypair(&seed); + //两次hash let nodeID = ripemd160(sha3(public key)); + //创建一个SHA3-256的对象 + let mut hasher = Sha3::sha3_256(); + //传入公钥 + hasher.input(&public_key); + let mut hex: Vec = Vec::new(); + hasher.result(&mut hex); + + let mut ripemd = Ripemd160::new(); + ripemd.input(&hex); + let mut node_id: [u8; 20] = [0; 20]; + ripemd.result(&mut node_id); + + Self { + seed, + secret_key, + public_key, + node_id, + } + } + +} + +#[cfg(test)] +mod tests{ + use super::*; + + #[tokio::test] + async fn test_node(){ + let node_inf = NodeInfo::new("test/test.db",b"seed").await; + + let value = node_inf.check_key(); + assert_eq!(value,false||true); + + } +} \ No newline at end of file diff --git a/test/test.db/conf b/test/test.db/conf new file mode 100644 index 0000000..4ac854e --- /dev/null +++ b/test/test.db/conf @@ -0,0 +1,4 @@ +segment_size: 8388608 +use_compression: false +version: 0.30 +Z. \ No newline at end of file diff --git a/test/test.db/db b/test/test.db/db new file mode 100644 index 0000000000000000000000000000000000000000..14f57a815f32a576fe1fc34e96fea1ef040a4844 GIT binary patch literal 270 zcmeyr>p1^^2&iX(fCe@wgFytw0Ma_=-6pa!LIrf8GzXBrRI%F=%4OhSg0lRev@($P z$VpxaQ-Fu&V1`O|L1|+k9r9@BEj)UlQZQ#Rz^sS+;2KngJdkcXko=k-%8!pP&Ph#) Pk55TWODxSPfmsaziUS^Y literal 0 HcmV?d00001 diff --git a/test/test.db/snap.00000000000000D6 b/test/test.db/snap.00000000000000D6 new file mode 100644 index 0000000000000000000000000000000000000000..4820df133f136fdf588b71184f82a6f35032d889 GIT binary patch literal 172 zcmcb{00GyaG&;oymw+)Wph}paG@~C(4~$_0lY!B?FcmO{B2<7GrlJd`0> Date: Mon, 27 Apr 2020 17:42:46 +0800 Subject: [PATCH 2/6] Delete conf --- test/test.db/conf | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 test/test.db/conf diff --git a/test/test.db/conf b/test/test.db/conf deleted file mode 100644 index 4ac854e..0000000 --- a/test/test.db/conf +++ /dev/null @@ -1,4 +0,0 @@ -segment_size: 8388608 -use_compression: false -version: 0.30 -Z. \ No newline at end of file From 8b62d52a1abb3fa928752e39e2210a9b90595b23 Mon Sep 17 00:00:00 2001 From: luo4lu <979375551@qq.com> Date: Mon, 27 Apr 2020 17:43:06 +0800 Subject: [PATCH 3/6] Delete db --- test/test.db/db | Bin 270 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/test.db/db diff --git a/test/test.db/db b/test/test.db/db deleted file mode 100644 index 14f57a815f32a576fe1fc34e96fea1ef040a4844..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 270 zcmeyr>p1^^2&iX(fCe@wgFytw0Ma_=-6pa!LIrf8GzXBrRI%F=%4OhSg0lRev@($P z$VpxaQ-Fu&V1`O|L1|+k9r9@BEj)UlQZQ#Rz^sS+;2KngJdkcXko=k-%8!pP&Ph#) Pk55TWODxSPfmsaziUS^Y From 12434f231104c07390a441981622b647bca27f9f Mon Sep 17 00:00:00 2001 From: luo4lu <979375551@qq.com> Date: Mon, 27 Apr 2020 17:43:20 +0800 Subject: [PATCH 4/6] Delete snap.00000000000000D6 --- test/test.db/snap.00000000000000D6 | Bin 172 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/test.db/snap.00000000000000D6 diff --git a/test/test.db/snap.00000000000000D6 b/test/test.db/snap.00000000000000D6 deleted file mode 100644 index 4820df133f136fdf588b71184f82a6f35032d889..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 172 zcmcb{00GyaG&;oymw+)Wph}paG@~C(4~$_0lY!B?FcmO{B2<7GrlJd`0> Date: Mon, 27 Apr 2020 17:47:35 +0800 Subject: [PATCH 5/6] add interface test and modified .gitignore file --- .gitignore | 2 +- src/lib.rs | 2 +- src/node_information.rs | 88 +++++++++++++++++++---------------------- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 7764d73..2b6292f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target -/test +/test/* Cargo.lock diff --git a/src/lib.rs b/src/lib.rs index ba32935..40f23cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1 @@ -pub mod node_information; \ No newline at end of file +pub mod node_information; diff --git a/src/node_information.rs b/src/node_information.rs index a954042..d64aaee 100644 --- a/src/node_information.rs +++ b/src/node_information.rs @@ -1,42 +1,38 @@ -use kv_hal_sled::SledStorage; +use crypto::{digest::Digest, ed25519, ripemd160::Ripemd160, sha3::Sha3}; use kv_hal::kv::Storage; +use kv_hal_sled::SledStorage; use std::convert::TryFrom; -use crypto::{ed25519, digest::Digest, sha3::Sha3, ripemd160::Ripemd160}; //use sled; -pub struct NodeInfo{ +pub struct NodeInfo { seed: [u8; 32], secret_key: [u8; 64], public_key: [u8; 32], node_id: [u8; 20], } -impl NodeInfo{ - - async fn get_value(string: &str, key: &[u8]) -> Result>, sled::Error> - { +impl NodeInfo { + async fn get_value(string: &str, key: &[u8]) -> Result>, sled::Error> { let stor = SledStorage::new(string); stor.get(key).await } - pub async fn new(string: &str, key: &[u8]) ->Self{ + pub async fn new(string: &str, key: &[u8]) -> Self { let seed = NodeInfo::get_value(string, key).await; - if let Ok(seed) = seed{ - if let Some(seed) = seed{ + if let Ok(seed) = seed { + if let Some(seed) = seed { NodeInfo::init_node(&seed) - } - else{ + } else { NodeInfo::rebuild_key() } - } - else{ + } else { NodeInfo::rebuild_key() } } //如果value存在初始化结构体 - fn init_node(seed_s: &[u8]) ->Self{ + fn init_node(seed_s: &[u8]) -> Self { //于rebuild_key接口功能类似,不产生随机种子而是根据入参计算秘钥,秘钥-》nodeid let seed: [u8; 32] = TryFrom::try_from(seed_s).unwrap(); - let(secret_key,public_key) = ed25519::keypair(&seed); + let (secret_key, public_key) = ed25519::keypair(&seed); //两次hash let nodeID = ripemd160(sha3(public key)); //创建一个SHA3-256的对象 let mut hasher = Sha3::sha3_256(); @@ -44,10 +40,10 @@ impl NodeInfo{ hasher.input(&public_key); let mut hex: Vec = Vec::new(); hasher.result(&mut hex); - + let mut ripemd = Ripemd160::new(); ripemd.input(&hex); - let mut node_id: [u8; 20] = [0; 20]; + let mut node_id: [u8; 20] = [0; 20]; ripemd.result(&mut node_id); Self { @@ -55,62 +51,59 @@ impl NodeInfo{ secret_key, public_key, node_id, - } + } } //通过公钥私钥计算出NodeID - pub fn get_node(&self) ->[u8; 20] - { + pub fn get_node(&self) -> [u8; 20] { //获取nodeid,rebuild_key里面的算法获取 let mut hasher = Sha3::sha3_256(); //传入公钥 hasher.input(&self.public_key); let mut hex: Vec = Vec::new(); hasher.result(&mut hex); - + let mut ripemd = Ripemd160::new(); ripemd.input(&hex); - let mut node_id: [u8; 20] = [0; 20]; + let mut node_id: [u8; 20] = [0; 20]; ripemd.result(&mut node_id); assert_eq!(node_id, self.node_id); node_id } //检查是否存在公钥私钥 - pub fn check_key(&self) ->bool{ + pub fn check_key(&self) -> bool { //由get_value获取的私钥计算出公钥与数据结构中存在的公秘钥比较 - let(secret_key,public_key) = ed25519::keypair(&self.seed); - for (i,_) in secret_key.iter().enumerate(){ + let (secret_key, public_key) = ed25519::keypair(&self.seed); + for (i, _) in secret_key.iter().enumerate() { assert_eq!(secret_key[i], self.secret_key[i]); } assert_eq!(public_key, self.public_key); - if self.secret_key.len()!=64 && self.public_key.len()!=32{ + if self.secret_key.len() != 64 && self.public_key.len() != 32 { return false; } true } //如果value不存在生成新的公私钥 - fn rebuild_key() ->Self - { - let iter :[u8;32] = [0;32]; - let mut seed:[u8;32] = [0;32]; - for(i,_) in iter.iter().enumerate() - { - seed[i]=rand::random::(); + fn rebuild_key() -> Self { + let iter: [u8; 32] = [0; 32]; + let mut seed: [u8; 32] = [0; 32]; + for (i, _) in iter.iter().enumerate() { + seed[i] = rand::random::(); } - let(secret_key,public_key) = ed25519::keypair(&seed); - //两次hash let nodeID = ripemd160(sha3(public key)); - //创建一个SHA3-256的对象 + let (secret_key, public_key) = ed25519::keypair(&seed); + //两次hash let nodeID = ripemd160(sha3(public key)); + //创建一个SHA3-256的对象 let mut hasher = Sha3::sha3_256(); - //传入公钥 + //传入公钥 hasher.input(&public_key); let mut hex: Vec = Vec::new(); hasher.result(&mut hex); - + let mut ripemd = Ripemd160::new(); ripemd.input(&hex); - let mut node_id: [u8; 20] = [0; 20]; + let mut node_id: [u8; 20] = [0; 20]; ripemd.result(&mut node_id); Self { @@ -120,19 +113,20 @@ impl NodeInfo{ node_id, } } - } #[cfg(test)] -mod tests{ +mod tests { use super::*; #[tokio::test] - async fn test_node(){ - let node_inf = NodeInfo::new("test/test.db",b"seed").await; + async fn test_node() { + let node_inf = NodeInfo::new("test/test.db", b"seed").await; let value = node_inf.check_key(); - assert_eq!(value,false||true); + assert_eq!(value, false || true); - } -} \ No newline at end of file + let node = node_inf.get_node(); + assert_eq!(node, node_inf.node_id); + } +} From 01a0356a26449b9b90e28693555401b681d4b9ec Mon Sep 17 00:00:00 2001 From: luo4lu Date: Thu, 30 Apr 2020 11:15:52 +0800 Subject: [PATCH 6/6] interface format modified --- src/node_information.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/node_information.rs b/src/node_information.rs index d64aaee..56c41a0 100644 --- a/src/node_information.rs +++ b/src/node_information.rs @@ -12,10 +12,6 @@ pub struct NodeInfo { } impl NodeInfo { - async fn get_value(string: &str, key: &[u8]) -> Result>, sled::Error> { - let stor = SledStorage::new(string); - stor.get(key).await - } pub async fn new(string: &str, key: &[u8]) -> Self { let seed = NodeInfo::get_value(string, key).await; if let Ok(seed) = seed { @@ -28,6 +24,13 @@ impl NodeInfo { NodeInfo::rebuild_key() } } + + //get seed rand sum from kv-hal-sled trait in the get function + async fn get_value(string: &str, key: &[u8]) -> Result>, sled::Error> { + let stor = SledStorage::new(string); + stor.get(key).await + } + //如果value存在初始化结构体 fn init_node(seed_s: &[u8]) -> Self { //于rebuild_key接口功能类似,不产生随机种子而是根据入参计算秘钥,秘钥-》nodeid