From 4905e4e24dcde5ee37bfdbce0b253aa32f2f6859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Ver=C5=A1i=C4=87?= Date: Thu, 14 Apr 2022 03:02:37 +0200 Subject: [PATCH] fix review comments, add proper defaults for key pairs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marin Veršić --- Cargo.lock | 3 +- cli/src/config.rs | 12 ++- client/src/config.rs | 13 ++- client/tests/integration/roles.rs | 7 +- core/benches/sumeragi.rs | 4 +- core/src/block.rs | 20 ++-- core/src/kura.rs | 3 +- core/src/lib.rs | 2 +- core/src/smartcontracts/isi/query.rs | 4 +- core/src/smartcontracts/isi/tx.rs | 2 +- core/src/sumeragi/config.rs | 17 +++- core/src/sumeragi/fault.rs | 2 +- core/src/sumeragi/network_topology.rs | 40 ++++---- core/src/sumeragi/view_change.rs | 45 +++++---- core/src/wsv.rs | 2 +- core/test_network/Cargo.toml | 1 - core/test_network/src/lib.rs | 14 +-- crypto/src/hash.rs | 43 +++++---- crypto/src/lib.rs | 132 ++++++++++++++++++-------- {data_model => crypto}/src/merkle.rs | 76 +++++++-------- crypto/src/signature.rs | 15 ++- data_model/Cargo.toml | 1 - data_model/src/lib.rs | 1 - data_model/src/role.rs | 6 +- data_model/tests/data_model.rs | 11 +-- docs/source/references/config.md | 24 ++--- schema/bin/Cargo.toml | 3 +- schema/bin/src/lib.rs | 2 +- tools/crypto_cli/src/main.rs | 11 +-- 29 files changed, 300 insertions(+), 216 deletions(-) rename {data_model => crypto}/src/merkle.rs (81%) diff --git a/Cargo.lock b/Cargo.lock index 8428c2532b8..99f3b118479 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1855,7 +1855,6 @@ dependencies = [ "criterion", "derive_more", "getset", - "hex-literal", "iroha", "iroha_client", "iroha_core", @@ -2005,6 +2004,7 @@ name = "iroha_schema_bin" version = "2.0.0-pre-rc.3" dependencies = [ "iroha_core", + "iroha_crypto", "iroha_data_model", "iroha_schema", "serde_json", @@ -3494,7 +3494,6 @@ dependencies = [ "async-trait", "eyre", "futures", - "hex-literal", "iroha", "iroha_actor", "iroha_client", diff --git a/cli/src/config.rs b/cli/src/config.rs index d7726338983..33249acd760 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -9,7 +9,7 @@ use iroha_core::{ sumeragi::config::SumeragiConfiguration, wsv::config::Configuration as WorldStateViewConfiguration, }; -use iroha_crypto::{PrivateKey, PublicKey}; +use iroha_crypto::prelude::*; use iroha_data_model::prelude::*; use iroha_logger::Configuration as LoggerConfiguration; use serde::{Deserialize, Serialize}; @@ -63,8 +63,16 @@ pub struct Configuration { } impl Default for Configuration { + #[allow(clippy::expect_used)] fn default() -> Self { - let (public_key, private_key) = iroha_crypto::KeyPair::default().into(); + let public_key = + r#"ed0120954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10"# + .parse() + .expect("Public key not in mulithash format"); + let private_key = PrivateKey::from_hex( + Algorithm::Ed25519, + "1B038DDD 463090FC B30CFA9A 24341679 20BC90CD D7C045BC 64FBCB51 49135100 954C83A4 220FAFFB 2C1D23FC 5225B3E7 952D53AC BB2A065F F30C631E 5E1D6B10" + ).expect("Private key not hex encoded"); Self { public_key, diff --git a/client/src/config.rs b/client/src/config.rs index 11f7aa2f2ae..286fd4c6dc5 100644 --- a/client/src/config.rs +++ b/client/src/config.rs @@ -2,7 +2,7 @@ use std::{fmt, fs::File, io::BufReader, path::Path, str::FromStr}; use eyre::{eyre, Result, WrapErr}; use iroha_config::derive::Configurable; -use iroha_crypto::{PrivateKey, PublicKey}; +use iroha_crypto::prelude::*; use iroha_data_model::{prelude::*, transaction}; use iroha_logger::Configuration as LoggerConfiguration; use serde::{Deserialize, Serialize}; @@ -97,10 +97,17 @@ pub struct Configuration { } impl Default for Configuration { + #[allow(clippy::expect_used)] fn default() -> Self { - let (public_key, private_key) = iroha_crypto::KeyPair::default().into(); + let public_key = + r#"ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b"# + .parse() + .expect("Public key not in mulithash format"); + let private_key = PrivateKey::from_hex( + Algorithm::Ed25519, + "282ED9F3 CF92811C 3818DBC4 AE594ED5 9DC1A2F7 8E4241E3 1924E101 D6B1FB83 1C61FAF8 FE94E253 B9311424 0394F79A 607B7FA5 5F9E5A41 EBEC74B8 8055768B" + ).expect("Private key not hex encoded"); - #[allow(clippy::expect_used)] Self { public_key, private_key, diff --git a/client/tests/integration/roles.rs b/client/tests/integration/roles.rs index a145ee47d3e..b30a8b89f5a 100644 --- a/client/tests/integration/roles.rs +++ b/client/tests/integration/roles.rs @@ -1,6 +1,6 @@ #![allow(clippy::restriction)] -use std::{collections::BTreeMap, str::FromStr as _, time::Duration}; +use std::{str::FromStr as _, time::Duration}; use eyre::{eyre, Result}; use iroha_client::client::{self, Client}; @@ -113,10 +113,7 @@ fn register_role_with_empty_token_params() -> Result<()> { let role_id = iroha_data_model::role::Id::new("root".parse::().expect("Valid")); let mut permissions = Permissions::new(); - permissions.insert(PermissionToken { - name: "token".parse().expect("Valid"), - params: BTreeMap::new(), - }); + permissions.insert(PermissionToken::new("token".parse().expect("Valid"))); let register_role = RegisterBox::new(Role::new(role_id, permissions)); test_client.submit(register_role)?; diff --git a/core/benches/sumeragi.rs b/core/benches/sumeragi.rs index 3678ac542a2..a0f1c34ae25 100644 --- a/core/benches/sumeragi.rs +++ b/core/benches/sumeragi.rs @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Criterion}; use iroha_core::sumeragi::network_topology; -use iroha_crypto::{Hash, KeyPair}; +use iroha_crypto::{Hash, HashOf, KeyPair}; use iroha_data_model::prelude::*; const N_PEERS: usize = 255; @@ -22,7 +22,7 @@ fn get_n_peers(n: usize) -> Vec { fn sort_peers(criterion: &mut Criterion) { let peers = get_n_peers(N_PEERS); let _ = criterion.bench_function("sort_peers", |b| { - let hash = Hash::new([1_u8; Hash::LENGTH]).into(); + let hash = HashOf::new(&[1_u8; Hash::LENGTH]).transmute(); b.iter(|| network_topology::sort_peers_by_hash(peers.clone(), &hash)); }); } diff --git a/core/src/block.rs b/core/src/block.rs index f1c54917798..7c7d0f89a64 100644 --- a/core/src/block.rs +++ b/core/src/block.rs @@ -10,10 +10,8 @@ use std::{collections::BTreeSet, error::Error, iter, marker::PhantomData}; use dashmap::{mapref::one::Ref as MapRef, DashMap}; use eyre::{eyre, Context, Result}; -use iroha_crypto::{Hash, HashOf, KeyPair, SignatureOf, SignaturesOf}; -use iroha_data_model::{ - current_time, events::prelude::*, merkle::MerkleTree, transaction::prelude::*, -}; +use iroha_crypto::{merkle::MerkleTree, HashOf, KeyPair, SignatureOf, SignaturesOf}; +use iroha_data_model::{current_time, events::prelude::*, transaction::prelude::*}; use iroha_schema::IntoSchema; use iroha_version::{declare_versioned_with_scale, version_with_scale}; use parity_scale_codec::{Decode, Encode}; @@ -49,7 +47,7 @@ impl Default for EmptyChainHash { impl From> for HashOf { fn from(EmptyChainHash(PhantomData): EmptyChainHash) -> Self { - Hash::zeroed().into() + HashOf::zeroed() } } @@ -220,8 +218,8 @@ impl PendingBlock { consensus_estimation: DEFAULT_CONSENSUS_ESTIMATION_MS, height: height + 1, previous_block_hash, - transactions_hash: Hash::zeroed().into(), - rejected_transactions_hash: Hash::zeroed().into(), + transactions_hash: HashOf::zeroed(), + rejected_transactions_hash: HashOf::zeroed(), view_change_proofs, invalidated_blocks_hashes, genesis_topology: None, @@ -239,8 +237,8 @@ impl PendingBlock { consensus_estimation: DEFAULT_CONSENSUS_ESTIMATION_MS, height: 1, previous_block_hash: EmptyChainHash::default().into(), - transactions_hash: Hash::zeroed().into(), - rejected_transactions_hash: Hash::zeroed().into(), + transactions_hash: HashOf::zeroed(), + rejected_transactions_hash: HashOf::zeroed(), view_change_proofs: ViewChangeProofs::empty(), invalidated_blocks_hashes: Vec::new(), genesis_topology: Some(genesis_topology), @@ -258,8 +256,8 @@ impl PendingBlock { consensus_estimation: DEFAULT_CONSENSUS_ESTIMATION_MS, height: 1, previous_block_hash: EmptyChainHash::default().into(), - transactions_hash: Hash::zeroed().into(), - rejected_transactions_hash: Hash::zeroed().into(), + transactions_hash: HashOf::zeroed(), + rejected_transactions_hash: HashOf::zeroed(), view_change_proofs: ViewChangeProofs::empty(), invalidated_blocks_hashes: Vec::new(), genesis_topology: None, diff --git a/core/src/kura.rs b/core/src/kura.rs index 898d7db5692..61efb9d65cb 100644 --- a/core/src/kura.rs +++ b/core/src/kura.rs @@ -14,8 +14,7 @@ use std::{ use async_trait::async_trait; use futures::{Stream, StreamExt, TryStreamExt}; use iroha_actor::{broker::*, prelude::*}; -use iroha_crypto::HashOf; -use iroha_data_model::merkle::MerkleTree; +use iroha_crypto::{merkle::MerkleTree, HashOf}; use iroha_logger::prelude::*; use iroha_version::scale::{DecodeVersioned, EncodeVersioned}; use pin_project::pin_project; diff --git a/core/src/lib.rs b/core/src/lib.rs index 244ebf58e96..3b219c4ba93 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -61,7 +61,7 @@ pub mod prelude { //! Re-exports important traits and types. Meant to be glob imported when using `Iroha`. #[doc(inline)] - pub use iroha_crypto::{Hash, KeyPair, PrivateKey, PublicKey}; + pub use iroha_crypto::{Algorithm, Hash, KeyPair, PrivateKey, PublicKey}; #[doc(inline)] pub use crate::{ diff --git a/core/src/smartcontracts/isi/query.rs b/core/src/smartcontracts/isi/query.rs index 80e3cf4f2a2..8c16e7fb50d 100644 --- a/core/src/smartcontracts/isi/query.rs +++ b/core/src/smartcontracts/isi/query.rs @@ -215,7 +215,7 @@ mod tests { use std::{str::FromStr, sync::Arc}; - use iroha_crypto::{Hash, KeyPair}; + use iroha_crypto::{Hash, HashOf, KeyPair}; use iroha_data_model::transaction::TransactionLimits; use once_cell::sync::Lazy; @@ -351,7 +351,7 @@ mod tests { .commit(); wsv.apply(vcb).await?; - let wrong_hash = Hash::new([2_u8]); + let wrong_hash: Hash = HashOf::new(&2_u8).into(); let not_found = FindTransactionByHash::new(wrong_hash).execute(&wsv); assert!(matches!(not_found, Err(_))); diff --git a/core/src/smartcontracts/isi/tx.rs b/core/src/smartcontracts/isi/tx.rs index f2bc022dbfd..adb183cb2fc 100644 --- a/core/src/smartcontracts/isi/tx.rs +++ b/core/src/smartcontracts/isi/tx.rs @@ -29,7 +29,7 @@ impl ValidQuery for FindTransactionByHash { .evaluate(wsv, &Context::default()) .wrap_err("Failed to get hash") .map_err(|e| query::Error::Evaluate(e.to_string()))?; - let hash = hash.into(); + let hash = hash.typed(); if !wsv.has_transaction(&hash) { return Err(FindError::Transaction(hash).into()); }; diff --git a/core/src/sumeragi/config.rs b/core/src/sumeragi/config.rs index fa9d20b4785..ef7291f8375 100644 --- a/core/src/sumeragi/config.rs +++ b/core/src/sumeragi/config.rs @@ -52,15 +52,24 @@ pub struct SumeragiConfiguration { } impl Default for SumeragiConfiguration { + #[allow(clippy::expect_used)] fn default() -> Self { - let key_pair = iroha_crypto::KeyPair::default(); + let public_key: PublicKey = + r#"ed0120e6ba36a2f2442152cf0a691b2a238f8de69affb6016aec44493627784543d4b6"# + .parse() + .expect("Public key not in mulithash format"); + let private_key = PrivateKey::from_hex( + Algorithm::Ed25519, + "077CC547 30A0C341 5837DB47 C169FAFA 93B6E2E6 35D5DD73 49B0CDF6 CC73997F E6BA36A2 F2442152 CF0A691B 2A238F8D E69AFFB6 016AEC44 49362778 4543D4B6" + ).expect("Private key not hex encoded"); + let peer_id = PeerId { - address: "localhost".to_owned(), - public_key: key_pair.public_key().clone(), + address: "127.0.0.1".to_owned(), + public_key: public_key.clone(), }; Self { - key_pair, + key_pair: KeyPair::new(public_key, private_key), trusted_peers: TrustedPeers::default(), peer_id, block_time_ms: DEFAULT_BLOCK_TIME_MS, diff --git a/core/src/sumeragi/fault.rs b/core/src/sumeragi/fault.rs index 6e320ce07e7..ac927c610d2 100644 --- a/core/src/sumeragi/fault.rs +++ b/core/src/sumeragi/fault.rs @@ -899,7 +899,7 @@ impl Con info!("Starting Sumeragi"); self.connect_peers().await; - if height != 0 && *last_block != Hash::zeroed() { + if height != 0 && last_block != HashOf::zeroed() { self.init(last_block, height); } else if let Some(genesis_network) = self.genesis_network.take() { let addr = self.network.clone(); diff --git a/core/src/sumeragi/network_topology.rs b/core/src/sumeragi/network_topology.rs index 4e55c5ce26d..d8a70a99e81 100644 --- a/core/src/sumeragi/network_topology.rs +++ b/core/src/sumeragi/network_topology.rs @@ -21,7 +21,7 @@ pub fn sort_peers_by_hash( } /// Sorts peers based on the `hash` and `counter` combined as a seed. -pub fn sort_peers_by_hash_and_counter( +fn sort_peers_by_hash_and_counter( mut peers: Vec, hash: &HashOf, counter: u64, @@ -531,45 +531,47 @@ mod tests { .collect() } + fn get_hash(seed: u8) -> HashOf { + HashOf::new(&seed).transmute() + } + #[test] fn different_order() { + let hash1 = get_hash(1); + let hash2 = get_hash(2); let peers: Vec<_> = topology_test_peers().into_iter().collect(); - let peers_1 = sort_peers_by_hash(peers.clone(), &Hash::new([1_u8; Hash::LENGTH]).into()); - let peers_2 = sort_peers_by_hash(peers, &Hash::new([2_u8; Hash::LENGTH]).into()); + let peers_1 = sort_peers_by_hash(peers.clone(), &hash1); + let peers_2 = sort_peers_by_hash(peers, &hash2); assert_ne!(peers_1, peers_2); } #[test] fn same_order() { + let hash1 = get_hash(2); + let hash2 = get_hash(2); let peers: Vec<_> = topology_test_peers().into_iter().collect(); - let peers_1 = sort_peers_by_hash(peers.clone(), &Hash::new([2_u8; Hash::LENGTH]).into()); - let peers_2 = sort_peers_by_hash(peers, &Hash::new([2_u8; Hash::LENGTH]).into()); + let peers_1 = sort_peers_by_hash(peers.clone(), &hash1); + let peers_2 = sort_peers_by_hash(peers, &hash2); assert_eq!(peers_1, peers_2); } #[test] fn same_order_by_hash_and_counter() { + let hash1 = get_hash(2); + let hash2 = get_hash(2); let peers: Vec<_> = topology_test_peers().into_iter().collect(); - let peers_1 = sort_peers_by_hash_and_counter( - peers.clone(), - &Hash::new([2_u8; Hash::LENGTH]).into(), - 1, - ); - let peers_2 = - sort_peers_by_hash_and_counter(peers, &Hash::new([2_u8; Hash::LENGTH]).into(), 1); + let peers_1 = sort_peers_by_hash_and_counter(peers.clone(), &hash1, 1); + let peers_2 = sort_peers_by_hash_and_counter(peers, &hash2, 1); assert_eq!(peers_1, peers_2); } #[test] fn different_order_by_hash_and_counter() { + let hash1 = get_hash(1); + let hash2 = get_hash(2); let peers: Vec<_> = topology_test_peers().into_iter().collect(); - let peers_1 = sort_peers_by_hash_and_counter( - peers.clone(), - &Hash::new([2_u8; Hash::LENGTH]).into(), - 1, - ); - let peers_2 = - sort_peers_by_hash_and_counter(peers, &Hash::new([2_u8; Hash::LENGTH]).into(), 2); + let peers_1 = sort_peers_by_hash_and_counter(peers.clone(), &hash1, 1); + let peers_2 = sort_peers_by_hash_and_counter(peers, &hash2, 2); assert_ne!(peers_1, peers_2); } } diff --git a/core/src/sumeragi/view_change.rs b/core/src/sumeragi/view_change.rs index e62f7794c68..b730252a47e 100644 --- a/core/src/sumeragi/view_change.rs +++ b/core/src/sumeragi/view_change.rs @@ -89,7 +89,7 @@ impl Proof { /// /// # Errors /// Can fail during creation of signature - pub fn sign(mut self, key_pair: KeyPair) -> Result { + pub fn sign(mut self, key_pair: KeyPair) -> Result { let signature = SignatureOf::new(key_pair, &self.payload)?.transmute(); self.signatures.insert(signature); Ok(self) @@ -123,7 +123,7 @@ impl Proof { pub fn has_same_state( &self, latest_block: &HashOf, - latest_view_change: &HashOf, + latest_view_change: &HashOf, ) -> bool { &self.payload.latest_block == latest_block && &self.payload.previous_proof == latest_view_change @@ -195,7 +195,8 @@ pub struct ProofChain { impl ProofChain { /// Initialize an empty proof chain. - pub const fn empty() -> ProofChain { + #[must_use] + pub const fn empty() -> Self { Self { proofs: Vec::new() } } @@ -249,15 +250,19 @@ mod tests { use iroha_crypto::Hash; use super::*; + use crate::ValidBlock; #[test] fn proof_is_valid() -> Result<()> { + let valid_block: VersionedValidBlock = ValidBlock::new_dummy().into(); + let key_pair_1 = KeyPair::generate()?; let key_pair_2 = KeyPair::generate()?; + let proof = Proof::commit_timeout( - Hash::new([1_u8; Hash::LENGTH]).into(), - Hash::new([2_u8; Hash::LENGTH]).into(), - Hash::new([3_u8; Hash::LENGTH]).into(), + valid_block.hash(), + HashOf::new(&[2_u8; Hash::LENGTH]).transmute(), + valid_block.commit().hash(), key_pair_1.clone(), )? .sign(key_pair_2.clone())?; @@ -270,12 +275,15 @@ mod tests { #[test] fn proof_has_not_enough_signatures() -> Result<()> { + let valid_block: VersionedValidBlock = ValidBlock::new_dummy().into(); + let key_pair_1 = KeyPair::generate()?; let key_pair_2 = KeyPair::generate()?; + let proof = Proof::commit_timeout( - Hash::new([1_u8; Hash::LENGTH]).into(), - Hash::new([2_u8; Hash::LENGTH]).into(), - Hash::new([3_u8; Hash::LENGTH]).into(), + valid_block.hash(), + HashOf::new(&[2_u8; Hash::LENGTH]).transmute(), + valid_block.commit().hash(), key_pair_1.clone(), )?; let peer_1 = PeerId::new("127.0.0.1:1001", key_pair_1.public_key()); @@ -287,13 +295,16 @@ mod tests { #[test] fn proof_has_not_enough_valid_signatures() -> Result<()> { + let valid_block: VersionedValidBlock = ValidBlock::new_dummy().into(); + let key_pair_1 = KeyPair::generate()?; let key_pair_2 = KeyPair::generate()?; let key_pair_3 = KeyPair::generate()?; + let proof = Proof::commit_timeout( - Hash::new([1_u8; Hash::LENGTH]).into(), - Hash::new([2_u8; Hash::LENGTH]).into(), - Hash::new([3_u8; Hash::LENGTH]).into(), + valid_block.hash(), + HashOf::new(&[2_u8; Hash::LENGTH]).transmute(), + valid_block.commit().hash(), key_pair_1.clone(), )? .sign(key_pair_3)?; @@ -311,10 +322,10 @@ mod tests { let key_pair_2 = KeyPair::generate()?; let peer_1 = PeerId::new("127.0.0.1:1001", key_pair_1.public_key()); let peer_2 = PeerId::new("127.0.0.1:1002", key_pair_2.public_key()); - let latest_block = Hash::new([3_u8; Hash::LENGTH]).into(); + let latest_block = HashOf::new(&[3_u8; Hash::LENGTH]).transmute(); for i in 0..10 { let proof = Proof::commit_timeout( - Hash::new([i; Hash::LENGTH]).into(), + HashOf::new(&[i; Hash::LENGTH]).transmute(), proof_chain.latest_hash(), latest_block, key_pair_1.clone(), @@ -334,15 +345,15 @@ mod tests { let key_pair_2 = KeyPair::generate()?; let peer_1 = PeerId::new("127.0.0.1:1001", key_pair_1.public_key()); let peer_2 = PeerId::new("127.0.0.1:1002", key_pair_2.public_key()); - let latest_block = Hash::new([3_u8; Hash::LENGTH]).into(); + let latest_block = HashOf::new(&[3_u8; Hash::LENGTH]).transmute(); for i in 0..10 { let latest_proof_hash = if i == 2 { - Hash::new([1_u8; Hash::LENGTH]).into() + HashOf::new(&[1_u8; Hash::LENGTH]).transmute() } else { proof_chain.latest_hash() }; let proof = Proof::commit_timeout( - Hash::new([i; Hash::LENGTH]).into(), + HashOf::new(&[i; Hash::LENGTH]).transmute(), latest_proof_hash, latest_block, key_pair_1.clone(), diff --git a/core/src/wsv.rs b/core/src/wsv.rs index 0c93d19a248..21a18114736 100644 --- a/core/src/wsv.rs +++ b/core/src/wsv.rs @@ -629,7 +629,7 @@ impl WorldStateView { pub fn latest_block_hash(&self) -> HashOf { self.blocks .latest_block() - .map_or(Hash::zeroed().into(), |block| block.value().hash()) + .map_or(HashOf::zeroed(), |block| block.value().hash()) } /// Get `Account` and pass it to closure. diff --git a/core/test_network/Cargo.toml b/core/test_network/Cargo.toml index f90a41065fe..67caf708752 100644 --- a/core/test_network/Cargo.toml +++ b/core/test_network/Cargo.toml @@ -19,7 +19,6 @@ iroha_p2p = { version = "=2.0.0-pre-rc.3", path = "../../p2p" } iroha_actor = { version = "=2.0.0-pre-rc.3", path = "../../actor" } iroha = { path = "../../cli", features = ["test-network"] } -hex-literal = "0.3.4" eyre = "0.6.5" tempfile = "3" unique_port = "0.1.0" diff --git a/core/test_network/src/lib.rs b/core/test_network/src/lib.rs index cd4f35e0140..4af7404ad2c 100644 --- a/core/test_network/src/lib.rs +++ b/core/test_network/src/lib.rs @@ -2,7 +2,7 @@ #![allow(clippy::restriction, clippy::future_not_send)] -use core::{fmt::Debug, str::FromStr, time::Duration}; +use core::{fmt::Debug, str::FromStr as _, time::Duration}; use std::{collections::HashMap, thread}; use eyre::{Error, Result}; @@ -101,17 +101,17 @@ impl std::cmp::Eq for Peer {} /// Get a standardised key-pair from the hard-coded literals. /// /// # Panics -/// Programmer error. The key must be given in Multihash format. +/// Programmer error. Given keys must be in proper format. pub fn get_key_pair() -> KeyPair { KeyPair::new( PublicKey::from_str( r#"ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"#, ) - .expect("Works"), - PrivateKey::new( - "ed25519".to_owned(), - hex_literal::hex!("9AC47ABF 59B356E0 BD7DCBBB B4DEC080 E302156A 48CA907E 47CB6AEA 1D32719E 7233BFC8 9DCBD68C 19FDE6CE 61582252 98EC1131 B6A130D1 AEB454C1 AB5183C0").into(), - ) + .expect("Public key not in mulithash format"), + PrivateKey::from_hex( + Algorithm::Ed25519, + "9AC47ABF 59B356E0 BD7DCBBB B4DEC080 E302156A 48CA907E 47CB6AEA 1D32719E 7233BFC8 9DCBD68C 19FDE6CE 61582252 98EC1131 B6A130D1 AEB454C1 AB5183C0", + ).expect("Private key not hex encoded") ) } diff --git a/crypto/src/hash.rs b/crypto/src/hash.rs index 1b05b51a750..542193e461f 100644 --- a/crypto/src/hash.rs +++ b/crypto/src/hash.rs @@ -37,14 +37,15 @@ impl Hash { /// Length of hash pub const LENGTH: usize = 32; - /// Construct zeroed hash - pub const fn zeroed() -> Self { - Hash([0; Self::LENGTH]) + /// Wrap the given bytes; they must be prehashed with `VarBlake2b` + pub fn prehashed(bytes: impl Into<[u8; Self::LENGTH]>) -> Self { + Self(bytes.into()) } - /// new hash from bytes + /// Hash given bytes #[cfg(feature = "std")] #[allow(clippy::expect_used)] + #[must_use] pub fn new(bytes: impl AsRef<[u8]>) -> Self { let vec_hash = VarBlake2b::new(Self::LENGTH) .expect("Failed to initialize variable size hash") @@ -54,6 +55,12 @@ impl Hash { hash.copy_from_slice(&vec_hash); Hash(hash) } + + #[must_use] + /// Return hash marked with a type + pub fn typed(self) -> HashOf { + HashOf(self, PhantomData) + } } impl Display for Hash { @@ -71,21 +78,18 @@ impl Debug for Hash { } impl From for [u8; Hash::LENGTH] { - #[inline] fn from(Hash(bytes): Hash) -> Self { bytes } } impl AsRef<[u8; Hash::LENGTH]> for Hash { - #[inline] fn as_ref(&self) -> &[u8; Hash::LENGTH] { &self.0 } } impl From> for Hash { - #[inline] fn from(HashOf(hash, _): HashOf) -> Self { hash } @@ -113,7 +117,6 @@ impl fmt::Debug for HashOf { } impl Clone for HashOf { - #[inline] fn clone(&self) -> Self { Self(self.0, PhantomData) } @@ -121,7 +124,6 @@ impl Clone for HashOf { impl Copy for HashOf {} impl PartialEq for HashOf { - #[inline] fn eq(&self, other: &Self) -> bool { self.0.eq(&other.0) } @@ -129,41 +131,33 @@ impl PartialEq for HashOf { impl Eq for HashOf {} impl PartialOrd for HashOf { - #[inline] fn partial_cmp(&self, other: &Self) -> Option { self.0.partial_cmp(&other.0) } } impl Ord for HashOf { - #[inline] fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.0.cmp(&other.0) } } impl hash::Hash for HashOf { - #[inline] fn hash(&self, state: &mut H) { self.0.hash(state) } } impl AsRef<[u8; Hash::LENGTH]> for HashOf { - #[inline] fn as_ref(&self) -> &[u8; Hash::LENGTH] { self.0.as_ref() } } -impl From for HashOf { - fn from(hash: Hash) -> Self { - Self(hash, PhantomData) - } -} - impl HashOf { - /// Transmutes hash to some specific type + /// Transmutes hash to some specific type. + /// Don't use this method if not required. #[inline] + #[must_use] pub const fn transmute(self) -> HashOf { HashOf(self.0, PhantomData) } @@ -172,11 +166,20 @@ impl HashOf { impl HashOf { /// Construct typed hash #[cfg(feature = "std")] + #[must_use] pub fn new(value: &T) -> Self { Self(Hash::new(value.encode()), PhantomData) } } +impl HashOf { + /// Construct zeroed hash + #[must_use] + pub const fn zeroed() -> Self { + HashOf(Hash([0; Hash::LENGTH]), PhantomData) + } +} + impl IntoSchema for HashOf { fn schema(map: &mut MetaMap) { Hash::schema(map); diff --git a/crypto/src/lib.rs b/crypto/src/lib.rs index 19136be447a..64f7d46d6a0 100644 --- a/crypto/src/lib.rs +++ b/crypto/src/lib.rs @@ -6,6 +6,7 @@ extern crate alloc; mod hash; +pub mod merkle; pub mod multihash; mod signature; mod varint; @@ -21,7 +22,7 @@ use getset::Getters; pub use hash::*; use iroha_schema::IntoSchema; use multihash::{DigestFunction as MultihashDigestFunction, Multihash}; -use parity_scale_codec::{Decode, Encode}; +use parity_scale_codec::{Decode, Encode, Error as ScaleError}; use serde::{Deserialize, Serialize}; pub use signature::*; #[cfg(feature = "std")] @@ -55,7 +56,7 @@ pub struct NoSuchAlgorithm; impl std::error::Error for NoSuchAlgorithm {} /// Algorithm for hashing -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Algorithm { /// Ed25519 Ed25519, @@ -115,7 +116,9 @@ impl TryFrom for UrsaKeyGenOption { match key_gen_option { KeyGenOption::UseSeed(seed) => Ok(UrsaKeyGenOption::UseSeed(seed)), KeyGenOption::FromPrivateKey(key) => { - if key.digest_function == ED_25519 || key.digest_function == SECP_256_K1 { + if key.digest_function() == Algorithm::Ed25519 + || key.digest_function() == Algorithm::Secp256k1 + { return Ok(Self::FromSecretKey(UrsaPrivateKey(key.payload))); } @@ -167,24 +170,6 @@ pub struct KeyPair { private_key: PrivateKey, } -impl Default for KeyPair { - fn default() -> Self { - #[cfg(not(feature = "std"))] - use alloc::string::ToString as _; - - Self::new( - PublicKey { - digest_function: Algorithm::default().to_string(), - payload: Vec::new(), - }, - PrivateKey { - digest_function: Algorithm::default().to_string(), - payload: Vec::new(), - }, - ) - } -} - /// Error when dealing with cryptographic functions #[derive(Debug, Display)] pub enum Error { @@ -304,15 +289,24 @@ impl From for KeyParseError { impl std::error::Error for KeyParseError {} /// Public Key used in signatures. -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters, Decode, Encode, IntoSchema)] +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters, Encode, IntoSchema)] #[getset(get = "pub")] pub struct PublicKey { /// Digest function + #[getset(skip)] digest_function: String, /// payload of key payload: Vec, } +impl PublicKey { + /// Digest function + pub fn digest_function(&self) -> Algorithm { + #[allow(clippy::expect_used)] + self.digest_function.parse().expect("Valid") + } +} + impl FromStr for PublicKey { type Err = KeyParseError; @@ -327,7 +321,7 @@ impl FromStr for PublicKey { impl fmt::Debug for PublicKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("PublicKey") - .field("digest_function", &self.digest_function) + .field("digest_function", &self.digest_function()) .field("payload", &hex::encode_upper(self.payload.as_slice())) .finish() } @@ -368,12 +362,12 @@ impl TryFrom for Multihash { fn try_from(public_key: PublicKey) -> Result { let digest_function = match public_key.digest_function.as_str() { - ED_25519 => Ok(MultihashDigestFunction::Ed25519Pub), - SECP_256_K1 => Ok(MultihashDigestFunction::Secp256k1Pub), - BLS_NORMAL => Ok(MultihashDigestFunction::Bls12381G1Pub), - BLS_SMALL => Ok(MultihashDigestFunction::Bls12381G2Pub), - _ => Err(Self::Error {}), - }?; + ED_25519 => MultihashDigestFunction::Ed25519Pub, + SECP_256_K1 => MultihashDigestFunction::Secp256k1Pub, + BLS_NORMAL => MultihashDigestFunction::Bls12381G1Pub, + BLS_SMALL => MultihashDigestFunction::Bls12381G2Pub, + _ => return Err(Self::Error {}), + }; Ok(Self { digest_function, @@ -408,11 +402,27 @@ impl<'de> Deserialize<'de> for PublicKey { } } +impl Decode for PublicKey { + fn decode(input: &mut I) -> Result { + let digest_function = String::decode(input)?; + + if Algorithm::from_str(&digest_function).is_err() { + return Err(ScaleError::from("Algorithm not supported")); + } + + Ok(Self { + digest_function, + payload: Decode::decode(input)?, + }) + } +} + /// Private Key used in signatures. -#[derive(Clone, PartialEq, Eq, Getters, Deserialize, Serialize)] +#[derive(Clone, PartialEq, Eq, Getters, Serialize)] #[getset(get = "pub")] pub struct PrivateKey { /// Digest function + #[getset(skip)] digest_function: String, /// key payload. WARNING! Do not use `"string".as_bytes()` to obtain the key. #[serde(with = "hex::serde")] @@ -420,11 +430,59 @@ pub struct PrivateKey { } impl PrivateKey { - /// Construct `PrivateKey` - pub fn new(digest_function: String, payload: Vec) -> Self { - Self { - digest_function, - payload, + /// Construct `PrivateKey` from hex encoded string + /// + /// # Errors + /// + /// If given payload is not hex encoded + pub fn from_hex( + digest_function: Algorithm, + payload: &(impl AsRef<[u8]> + ?Sized), + ) -> Result { + #[cfg(not(feature = "std"))] + use alloc::string::ToString as _; + + let payload: Vec = payload + .as_ref() + .iter() + .filter(|&e| *e as char != ' ') + .copied() + .collect(); + + Ok(Self { + digest_function: digest_function.to_string(), + payload: hex::decode(payload)?, + }) + } + + /// Digest function + pub fn digest_function(&self) -> Algorithm { + #[allow(clippy::expect_used)] + self.digest_function.parse().expect("Valid") + } +} + +impl<'de> Deserialize<'de> for PrivateKey { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + use serde::de::Error as _; + + #[derive(Deserialize)] + struct PrivateKey { + digest_function: String, + #[serde(with = "hex::serde")] + payload: Vec, + } + + let private_key = PrivateKey::deserialize(deserializer)?; + match Algorithm::from_str(&private_key.digest_function) { + Ok(_) => Ok(Self { + digest_function: private_key.digest_function, + payload: private_key.payload, + }), + Err(err) => Err(D::Error::custom(err)), } } } @@ -432,7 +490,7 @@ impl PrivateKey { impl fmt::Debug for PrivateKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("PrivateKey") - .field("digest_function", &self.digest_function) + .field("digest_function", &self.digest_function()) .field("payload", &format!("{:X?}", self.payload)) .finish() } @@ -446,7 +504,7 @@ impl fmt::Display for PrivateKey { /// The prelude re-exports most commonly used traits, structs and macros from this crate. pub mod prelude { - pub use super::{Hash, KeyPair, PrivateKey, PublicKey, Signature}; + pub use super::{Algorithm, Hash, KeyPair, PrivateKey, PublicKey, Signature}; } #[cfg(test)] diff --git a/data_model/src/merkle.rs b/crypto/src/merkle.rs similarity index 81% rename from data_model/src/merkle.rs rename to crypto/src/merkle.rs index 505ce6a123a..b3853c35e7d 100644 --- a/data_model/src/merkle.rs +++ b/crypto/src/merkle.rs @@ -5,9 +5,10 @@ use alloc::{boxed::Box, format, string::String, vec, vec::Vec}; #[cfg(feature = "std")] use std::collections::VecDeque; -use iroha_crypto::{Hash, HashOf}; use iroha_schema::IntoSchema; +use crate::HashOf; + /// [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree) used to validate and prove data at /// each block height. /// Our implementation uses binary hash tree. @@ -156,7 +157,7 @@ impl Node { match self { Node::Subtree(Subtree { hash, .. }) => *hash, Node::Leaf(Leaf { hash }) => (*hash).transmute(), - Node::Empty => Hash::zeroed().into(), + Node::Empty => HashOf::zeroed(), } } @@ -179,7 +180,7 @@ impl Node { .zip(right_hash.as_ref().iter()) .map(|(l, r)| l.saturating_add(*r)) .collect(); - Hash::new(sum).into() + crate::Hash::new(sum).typed() } } @@ -224,25 +225,26 @@ impl<'a, T> IntoIterator for &'a MerkleTree { #[cfg(test)] mod tests { use super::*; + use crate::Hash; #[test] fn tree_with_two_layers_should_reach_all_nodes() { let tree = MerkleTree::<()> { root_node: Node::Subtree(Subtree { left: Box::new(Node::Leaf(Leaf { - hash: Hash::new([1; Hash::LENGTH]).into(), + hash: Hash::prehashed([1; Hash::LENGTH]).typed(), })), right: Box::new(Node::Leaf(Leaf { - hash: Hash::new([2; Hash::LENGTH]).into(), + hash: Hash::prehashed([2; Hash::LENGTH]).typed(), })), - hash: Hash::new([3; Hash::LENGTH]).into(), + hash: Hash::prehashed([3; Hash::LENGTH]).typed(), }), }; assert_eq!(3, tree.into_iter().count()); } fn get_hashes(hash: [u8; Hash::LENGTH]) -> impl Iterator> { - let hash = Hash::new(hash).into(); + let hash = Hash::prehashed(hash).typed(); std::iter::repeat_with(move || hash) } @@ -265,20 +267,20 @@ mod tests { #[test] fn same_root_hash_for_same_hashes() { let merkle_tree_1 = [ - Hash::new([1_u8; Hash::LENGTH]), - Hash::new([2_u8; Hash::LENGTH]), - Hash::new([3_u8; Hash::LENGTH]), + Hash::prehashed([1_u8; Hash::LENGTH]), + Hash::prehashed([2_u8; Hash::LENGTH]), + Hash::prehashed([3_u8; Hash::LENGTH]), ] .into_iter() - .map(Into::into) + .map(Hash::typed) .collect::>(); let merkle_tree_2 = [ - Hash::new([2_u8; Hash::LENGTH]), - Hash::new([1_u8; Hash::LENGTH]), - Hash::new([3_u8; Hash::LENGTH]), + Hash::prehashed([2_u8; Hash::LENGTH]), + Hash::prehashed([1_u8; Hash::LENGTH]), + Hash::prehashed([3_u8; Hash::LENGTH]), ] .into_iter() - .map(Into::into) + .map(Hash::typed) .collect::>(); assert_eq!(merkle_tree_1.root_hash(), merkle_tree_2.root_hash()); } @@ -286,56 +288,56 @@ mod tests { #[test] fn different_root_hash_for_different_hashes() { let merkle_tree_1 = [ - Hash::new([1_u8; Hash::LENGTH]), - Hash::new([2_u8; Hash::LENGTH]), - Hash::new([3_u8; Hash::LENGTH]), + Hash::prehashed([1_u8; Hash::LENGTH]), + Hash::prehashed([2_u8; Hash::LENGTH]), + Hash::prehashed([3_u8; Hash::LENGTH]), ] .into_iter() - .map(Into::into) + .map(Hash::typed) .collect::>(); let merkle_tree_2 = [ - Hash::new([1_u8; Hash::LENGTH]), - Hash::new([4_u8; Hash::LENGTH]), - Hash::new([5_u8; Hash::LENGTH]), + Hash::prehashed([1_u8; Hash::LENGTH]), + Hash::prehashed([4_u8; Hash::LENGTH]), + Hash::prehashed([5_u8; Hash::LENGTH]), ] .into_iter() - .map(Into::into) + .map(Hash::typed) .collect::>(); assert_ne!(merkle_tree_1.root_hash(), merkle_tree_2.root_hash()); } #[test] fn get_leaf() { - let hash1 = Hash::new([1; Hash::LENGTH]).into(); - let hash2 = Hash::new([2; Hash::LENGTH]).into(); - let hash3 = Hash::new([3; Hash::LENGTH]).into(); - assert!(hash2 < hash1 && hash1 < hash3); + let hash1 = Hash::prehashed([1; Hash::LENGTH]).typed(); + let hash2 = Hash::prehashed([2; Hash::LENGTH]).typed(); + let hash3 = Hash::prehashed([3; Hash::LENGTH]).typed(); + assert!(hash1 < hash2 && hash2 < hash3); let tree = [hash1, hash2, hash3] .into_iter() .collect::>(); - assert_eq!(tree.get_leaf(0), Some(hash2)); - assert_eq!(tree.get_leaf(1), Some(hash1)); + assert_eq!(tree.get_leaf(0), Some(hash1)); + assert_eq!(tree.get_leaf(1), Some(hash2)); assert_eq!(tree.get_leaf(2), Some(hash3)); assert_eq!(tree.get_leaf(3), None); } #[test] fn add() { - let hash1 = Hash::new([1; Hash::LENGTH]).into(); - let hash2 = Hash::new([2; Hash::LENGTH]).into(); - let hash3 = Hash::new([3; Hash::LENGTH]).into(); - let hash4 = Hash::new([4; Hash::LENGTH]).into(); - assert!(hash4 < hash2 && hash2 < hash1 && hash1 < hash3); + let hash1 = Hash::prehashed([1; Hash::LENGTH]).typed(); + let hash2 = Hash::prehashed([2; Hash::LENGTH]).typed(); + let hash3 = Hash::prehashed([3; Hash::LENGTH]).typed(); + let hash4 = Hash::prehashed([4; Hash::LENGTH]).typed(); + assert!(hash1 < hash2 && hash2 < hash3 && hash3 < hash4); let tree = [hash1, hash2, hash4] .into_iter() .collect::>(); let tree = tree.add(hash3); - assert_eq!(tree.get_leaf(0), Some(hash4)); + assert_eq!(tree.get_leaf(0), Some(hash1)); assert_eq!(tree.get_leaf(1), Some(hash2)); - assert_eq!(tree.get_leaf(2), Some(hash1)); - assert_eq!(tree.get_leaf(3), Some(hash3)); + assert_eq!(tree.get_leaf(2), Some(hash3)); + assert_eq!(tree.get_leaf(3), Some(hash4)); assert_eq!(tree.get_leaf(4), None); } } diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 80f826cc007..b1006b17f76 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -31,6 +31,9 @@ use ursa::{ use crate::{Algorithm, HashOf, KeyPair}; use crate::{Error, PublicKey}; +/// Signature payload(i.e THE signature) +pub type Payload = Vec; + /// Represents signature of the data (`Block` or `Transaction` for example). #[derive( Clone, @@ -52,7 +55,7 @@ pub struct Signature { public_key: PublicKey, /// Actual signature payload is placed here. #[getset(skip)] - signature: Vec, + signature: Payload, } #[cfg(feature = "std")] @@ -116,7 +119,7 @@ impl fmt::Debug for Signature { } } -impl From for (PublicKey, Vec) { +impl From for (PublicKey, Payload) { fn from( Signature { public_key, @@ -394,7 +397,6 @@ impl SignaturesOf { /// /// This method uses [`core::mem::transmute`] internally #[allow(unsafe_code)] - #[inline] pub fn transmute(self) -> SignaturesOf { // SAFETY: Safe because we are transmuting to a pointer of // type `` which is related to type ``. @@ -435,16 +437,11 @@ impl SignaturesOf { /// Number of signatures. #[inline] + #[allow(clippy::len_without_is_empty)] pub fn len(&self) -> usize { self.signatures.len() } - /// Number of signatures. - #[inline] - pub fn is_empty(&self) -> bool { - self.signatures.is_empty() - } - /// Verify signatures for this hash /// /// # Errors diff --git a/data_model/Cargo.toml b/data_model/Cargo.toml index ae48b36f1f9..065c4f56057 100644 --- a/data_model/Cargo.toml +++ b/data_model/Cargo.toml @@ -48,7 +48,6 @@ iroha = { path = "../cli" } test_network = { path = "../core/test_network", version = "=2.0.0-pre-rc.3" } tokio = { version = "1.6.0", features = ["rt", "rt-multi-thread"]} -hex-literal = "0.3.4" trybuild = "1.0.53" criterion = "0.3" diff --git a/data_model/src/lib.rs b/data_model/src/lib.rs index ddbec0da3dc..0587de2bfdd 100644 --- a/data_model/src/lib.rs +++ b/data_model/src/lib.rs @@ -30,7 +30,6 @@ pub mod domain; pub mod events; pub mod expression; pub mod isi; -pub mod merkle; pub mod metadata; pub mod pagination; pub mod peer; diff --git a/data_model/src/role.rs b/data_model/src/role.rs index c19de1bc12c..5e47f52d58f 100644 --- a/data_model/src/role.rs +++ b/data_model/src/role.rs @@ -93,17 +93,17 @@ impl Role { #[inline] pub fn new( id: ::Id, - permissions: impl Into, + permissions: impl IntoIterator, ) -> ::RegisteredWith { Self { id, - permissions: permissions.into(), + permissions: permissions.into_iter().collect(), } } /// Get an iterator over [`permissions`](PermissionToken) of the `Role` #[inline] - pub fn permissions(&self) -> impl Iterator { + pub fn permissions(&self) -> impl ExactSizeIterator { self.permissions.iter() } } diff --git a/data_model/tests/data_model.rs b/data_model/tests/data_model.rs index a4a737ef93e..65c778d7839 100644 --- a/data_model/tests/data_model.rs +++ b/data_model/tests/data_model.rs @@ -149,12 +149,11 @@ fn find_rate_and_make_exchange_isi_should_succeed() { PublicKey::from_str( r#"ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"#, ) - .unwrap(), - PrivateKey::new( - "ed25519".to_string(), - hex_literal::hex!("9AC47ABF 59B356E0 BD7DCBBB B4DEC080 E302156A 48CA907E 47CB6AEA 1D32719E 7233BFC8 9DCBD68C 19FDE6CE 61582252 98EC1131 B6A130D1 AEB454C1 AB5183C0") - .into(), - ), + .expect("Valid"), + PrivateKey::from_hex( + Algorithm::Ed25519, + "9AC47ABF 59B356E0 BD7DCBBB B4DEC080 E302156A 48CA907E 47CB6AEA 1D32719E 7233BFC8 9DCBD68C 19FDE6CE 61582252 98EC1131 B6A130D1 AEB454C1 AB5183C0" + ).expect("Valid"), ); let mut peer = ::new().expect("Failed to create peer"); let configuration = get_config(std::iter::once(peer.id.clone()).collect(), Some(kp.clone())); diff --git a/docs/source/references/config.md b/docs/source/references/config.md index ca3761769dc..5d85bf7075e 100644 --- a/docs/source/references/config.md +++ b/docs/source/references/config.md @@ -8,10 +8,10 @@ The following is the default configuration used by Iroha. ```json { - "PUBLIC_KEY": "ed0100", + "PUBLIC_KEY": "ed0120954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10", "PRIVATE_KEY": { "digest_function": "ed25519", - "payload": "" + "payload": "1b038ddd463090fcb30cfa9a2434167920bc90cdd7c045bc64fbcb5149135100954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10" }, "DISABLE_PANIC_TERMINAL_COLORS": false, "KURA": { @@ -22,8 +22,8 @@ The following is the default configuration used by Iroha. }, "SUMERAGI": { "PEER_ID": { - "address": "localhost", - "public_key": "ed0100" + "address": "127.0.0.1", + "public_key": "ed0120e6ba36a2f2442152cf0a691b2a238f8de69affb6016aec44493627784543d4b6" }, "BLOCK_TIME_MS": 1000, "TRUSTED_PEERS": [], @@ -377,7 +377,7 @@ Has type `PrivateKey`. Can be configured via environment variable `IROHA_PRIVATE ```json { "digest_function": "ed25519", - "payload": "" + "payload": "1b038ddd463090fcb30cfa9a2434167920bc90cdd7c045bc64fbcb5149135100954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10" } ``` @@ -388,7 +388,7 @@ Public key of this peer. Has type `PublicKey`. Can be configured via environment variable `IROHA_PUBLIC_KEY` ```json -"ed0100" +"ed0120954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10" ``` ## `queue` @@ -461,8 +461,8 @@ Has type `SumeragiConfiguration`. Can be configured via environment variable `IR "MAILBOX": 100, "N_TOPOLOGY_SHIFTS_BEFORE_RESHUFFLE": 1, "PEER_ID": { - "address": "localhost", - "public_key": "ed0100" + "address": "127.0.0.1", + "public_key": "ed0120e6ba36a2f2442152cf0a691b2a238f8de69affb6016aec44493627784543d4b6" }, "TRANSACTION_LIMITS": { "max_instruction_number": 4096, @@ -523,9 +523,9 @@ Has type `KeyPair`. Can be configured via environment variable `SUMERAGI_KEY_PAI { "private_key": { "digest_function": "ed25519", - "payload": "" + "payload": "077cc54730a0c3415837db47c169fafa93b6e2e635d5dd7349b0cdf6cc73997fe6ba36a2f2442152cf0a691b2a238f8de69affb6016aec44493627784543d4b6" }, - "public_key": "ed0100" + "public_key": "ed0120e6ba36a2f2442152cf0a691b2a238f8de69affb6016aec44493627784543d4b6" } ``` @@ -557,8 +557,8 @@ Has type `PeerId`. Can be configured via environment variable `SUMERAGI_PEER_ID` ```json { - "address": "localhost", - "public_key": "ed0100" + "address": "127.0.0.1", + "public_key": "ed0120e6ba36a2f2442152cf0a691b2a238f8de69affb6016aec44493627784543d4b6" } ``` diff --git a/schema/bin/Cargo.toml b/schema/bin/Cargo.toml index cded71f56d2..54338d546c2 100644 --- a/schema/bin/Cargo.toml +++ b/schema/bin/Cargo.toml @@ -7,7 +7,8 @@ edition = "2021" [dependencies] iroha_data_model = { version = "=2.0.0-pre-rc.3", path = "../../data_model", features = ["roles"] } -iroha_schema = { version = "=2.0.0-pre-rc.3", path = "../../schema" } iroha_core = { version = "=2.0.0-pre-rc.3", path = "../../core", features = ["roles"] } +iroha_crypto = { version = "=2.0.0-pre-rc.3", path = "../../crypto" } +iroha_schema = { version = "=2.0.0-pre-rc.3", path = "../../schema" } serde_json = "1" diff --git a/schema/bin/src/lib.rs b/schema/bin/src/lib.rs index 3e102365f7a..88944b5edfd 100644 --- a/schema/bin/src/lib.rs +++ b/schema/bin/src/lib.rs @@ -7,7 +7,6 @@ use iroha_core::{ genesis::RawGenesisBlock, smartcontracts::isi::query::Error as QueryError, }; -use iroha_data_model::merkle::MerkleTree; use iroha_schema::prelude::*; macro_rules! schemas { @@ -23,6 +22,7 @@ macro_rules! schemas { /// You should only include the top-level types, because other types /// shall be included automatically. pub fn build_schemas() -> MetaMap { + use iroha_crypto::merkle::MerkleTree; use iroha_data_model::prelude::*; schemas! { diff --git a/tools/crypto_cli/src/main.rs b/tools/crypto_cli/src/main.rs index cde8b0c03c9..915705019b1 100644 --- a/tools/crypto_cli/src/main.rs +++ b/tools/crypto_cli/src/main.rs @@ -73,13 +73,10 @@ fn main() -> Result<(), Report> { }, |private_key| { KeyPair::generate_with_configuration( - key_gen_configuration - .clone() - .use_private_key(PrivateKey::new( - algorithm.to_string(), - hex::decode(private_key) - .wrap_err("Failed to decode private key.")?, - )), + key_gen_configuration.clone().use_private_key( + PrivateKey::from_hex(algorithm, &private_key) + .wrap_err("Failed to decode private key.")?, + ), ) .wrap_err("Failed to generate key pair") },