Skip to content

Commit

Permalink
add default key pair to GenesisConfiguration
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Apr 18, 2022
1 parent 713e0ed commit 3563569
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 103 deletions.
16 changes: 5 additions & 11 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};
use super::torii::config::ToriiConfiguration;

/// Configuration parameters container.
#[derive(Clone, Deserialize, Serialize, Debug, Configurable)]
#[derive(Debug, Clone, Deserialize, Serialize, Configurable)]
#[serde(default)]
#[serde(rename_all = "UPPERCASE")]
#[config(env_prefix = "IROHA_")]
Expand Down Expand Up @@ -63,22 +63,16 @@ pub struct Configuration {
}

impl Default for Configuration {
#[allow(clippy::expect_used)]
fn default() -> Self {
let public_key = "ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b"
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
Algorithm::Ed25519,
"282ed9f3cf92811c3818dbc4ae594ed59dc1a2f78e4241e31924e101d6b1fb831c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b"
).expect("Private key not hex encoded");
let sumeragi_configuration = SumeragiConfiguration::default();
let (public_key, private_key) = sumeragi_configuration.key_pair.clone().into();

Self {
public_key,
private_key,
disable_panic_terminal_colors: bool::default(),
kura: KuraConfiguration::default(),
sumeragi: SumeragiConfiguration::default(),
sumeragi: sumeragi_configuration,
torii: ToriiConfiguration::default(),
block_sync: BlockSyncConfiguration::default(),
queue: QueueConfiguration::default(),
Expand All @@ -93,7 +87,7 @@ impl Default for Configuration {
}

/// Network Configuration parameters container.
#[derive(Clone, Copy, Deserialize, Serialize, Debug, Configurable, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Configurable)]
#[serde(default)]
#[serde(rename_all = "UPPERCASE")]
#[config(env_prefix = "IROHA_NETWORK_")]
Expand Down
13 changes: 4 additions & 9 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ where
WorldStateView::from_configuration(
config.wsv,
W::with(
domains(&config).wrap_err("Failed to get initial domains")?,
domains(&config),
config.sumeragi.trusted_peers.peers.clone(),
),
)
Expand Down Expand Up @@ -307,12 +307,7 @@ where
///
/// # Errors
/// - Genesis account public key not specified.
fn domains(configuration: &config::Configuration) -> Result<impl Iterator<Item = Domain>> {
let key = configuration
.genesis
.account_public_key
.clone()
.ok_or_else(|| eyre!("Genesis account public key is not specified."))?;

Ok([Domain::from(GenesisDomain::new(key))].into_iter())
fn domains(configuration: &config::Configuration) -> impl Iterator<Item = Domain> {
let key = configuration.genesis.account_public_key.clone();
[Domain::from(GenesisDomain::new(key))].into_iter()
}
2 changes: 1 addition & 1 deletion cli/src/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn get_config(trusted_peers: HashSet<PeerId>, key_pair: Option<KeyPair>) ->
..QueueConfiguration::default()
},
genesis: GenesisConfiguration {
account_public_key: Some(public_key),
account_public_key: public_key,
account_private_key: Some(private_key),
..GenesisConfiguration::default()
},
Expand Down
8 changes: 2 additions & 6 deletions cli/src/torii/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,7 @@ async fn blocks_stream() {
fn domains(
configuration: &crate::config::Configuration,
) -> eyre::Result<impl Iterator<Item = Domain>> {
let key = configuration
.genesis
.account_public_key
.clone()
.ok_or_else(|| eyre!("Genesis account public key is not specified."))?;
let key = configuration.genesis.account_public_key.clone();
Ok([Domain::from(GenesisDomain::new(key))].into_iter())
}

Expand All @@ -709,7 +705,7 @@ fn hash_should_be_the_same() {
Some(key_pair.clone()),
);
config.genesis.account_private_key = Some(key_pair.private_key().clone());
config.genesis.account_public_key = Some(key_pair.public_key().clone());
config.genesis.account_public_key = key_pair.public_key().clone();

let tx = Transaction::new(
AccountId::new(
Expand Down
31 changes: 22 additions & 9 deletions client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,13 @@ pub struct Configuration {
}

impl Default for Configuration {
#[allow(clippy::expect_used)]
fn default() -> Self {
let public_key = "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
Algorithm::Ed25519,
"9ac47abf59b356e0bd7dcbbbb4dec080e302156a48ca907e47cb6aea1d32719e7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"
).expect("Private key not hex encoded");
let (public_key, private_key) = Self::placeholder_keypair().into();

Self {
public_key,
private_key,
account_id: AccountId::from_str("").expect("Empty strings are valid"),
account_id: Self::placeholder_account(),
basic_auth: None,
torii_api_url: small::SmallStr::from_str(uri::DEFAULT_API_URL),
torii_telemetry_url: small::SmallStr::from_str(DEFAULT_TORII_TELEMETRY_URL),
Expand All @@ -127,6 +120,26 @@ impl Default for Configuration {
}

impl Configuration {
/// Key-pair used by default for demo purposes
#[allow(clippy::expect_used)]
fn placeholder_keypair() -> KeyPair {
let public_key = "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
Algorithm::Ed25519,
"9ac47abf59b356e0bd7dcbbbb4dec080e302156a48ca907e47cb6aea1d32719e7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"
).expect("Private key not hex encoded");

KeyPair::new(public_key, private_key)
}

/// Account ID used by default for demo purposes
#[allow(clippy::expect_used)]
fn placeholder_account() -> <Account as Identifiable>::Id {
AccountId::from_str("alice@wonderland").expect("Account ID not valid")
}

/// This method will build `Configuration` from a json *pretty* formatted file (without `:` in
/// key names).
///
Expand Down
39 changes: 28 additions & 11 deletions core/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ impl GenesisNetworkTrait for GenesisNetwork {
.iter()
.map(|raw_transaction| {
let genesis_key_pair = KeyPair::new(
genesis_config
.account_public_key
.clone()
.ok_or_else(|| eyre!("Genesis account public key is empty."))?,
genesis_config.account_public_key.clone(),
genesis_config
.account_private_key
.clone()
Expand Down Expand Up @@ -310,22 +307,22 @@ impl GenesisTransaction {
/// Module with genesis configuration logic.
pub mod config {
use iroha_config::derive::Configurable;
use iroha_crypto::{PrivateKey, PublicKey};
use iroha_crypto::{KeyPair, PrivateKey, PublicKey};
use serde::{Deserialize, Serialize};

const DEFAULT_WAIT_FOR_PEERS_RETRY_COUNT: u64 = 100;
const DEFAULT_WAIT_FOR_PEERS_RETRY_PERIOD_MS: u64 = 500;
const DEFAULT_GENESIS_SUBMISSION_DELAY_MS: u64 = 1000;

#[derive(Clone, Deserialize, Serialize, Debug, Configurable, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Configurable)]
#[serde(default)]
#[serde(rename_all = "UPPERCASE")]
#[config(env_prefix = "IROHA_GENESIS_")]
/// Configuration of the genesis block and the process of its submission.
pub struct GenesisConfiguration {
/// The genesis account public key, should be supplied to all peers.
/// The type is `Option` just because it might be loaded from environment variables and not from `config.json`.
#[config(serde_as_str)]
pub account_public_key: Option<PublicKey>,
pub account_public_key: PublicKey,
/// Genesis account private key, only needed on the peer that submits the genesis block.
pub account_private_key: Option<PrivateKey>,
/// Number of attempts to connect to peers, while waiting for them to submit genesis.
Expand All @@ -340,18 +337,38 @@ pub mod config {
pub genesis_submission_delay_ms: u64,
}

#[allow(clippy::expect_used)]
impl Default for GenesisConfiguration {
fn default() -> Self {
let (public_key, private_key) = Self::placeholder_keypair().into();

Self {
account_public_key: None,
account_private_key: None,
account_public_key: public_key,
account_private_key: Some(private_key),
wait_for_peers_retry_count: DEFAULT_WAIT_FOR_PEERS_RETRY_COUNT,
wait_for_peers_retry_period_ms: DEFAULT_WAIT_FOR_PEERS_RETRY_PERIOD_MS,
genesis_submission_delay_ms: DEFAULT_GENESIS_SUBMISSION_DELAY_MS,
}
}
}

impl GenesisConfiguration {
/// Key-pair used by default for demo purposes
#[allow(clippy::expect_used)]
fn placeholder_keypair() -> KeyPair {
let public_key =
"ed01204cffd0ee429b1bdd36b3910ec570852b8bb63f18750341772fb46bc856c5caaf"
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
iroha_crypto::Algorithm::Ed25519,
"d748e18ce60cb30dea3e73c9019b7af45a8d465e3d71bcc9a5ef99a008205e534cffd0ee429b1bdd36b3910ec570852b8bb63f18750341772fb46bc856c5caaf"
).expect("Private key not hex encoded");

KeyPair::new(public_key, private_key)
}
}

const fn default_wait_for_peers_retry_count() -> u64 {
DEFAULT_WAIT_FOR_PEERS_RETRY_COUNT
}
Expand Down Expand Up @@ -467,7 +484,7 @@ mod tests {
true,
RawGenesisBlock::default(),
&GenesisConfiguration {
account_public_key: Some(public_key),
account_public_key: public_key,
account_private_key: Some(private_key),
..GenesisConfiguration::default()
},
Expand Down
50 changes: 32 additions & 18 deletions core/src/sumeragi/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,11 @@ pub struct SumeragiConfiguration {
}

impl Default for SumeragiConfiguration {
#[allow(clippy::expect_used)]
fn default() -> Self {
let public_key: PublicKey =
"ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b"
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
Algorithm::Ed25519,
"282ed9f3cf92811c3818dbc4ae594ed59dc1a2f78e4241e31924e101d6b1fb831c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b"
).expect("Private key not hex encoded");

let peer_id = PeerId {
address: "127.0.0.1".to_owned(),
public_key: public_key.clone(),
};

Self {
key_pair: KeyPair::new(public_key, private_key),
trusted_peers: TrustedPeers::default(),
peer_id,
key_pair: Self::placeholder_keypair(),
peer_id: Self::placeholder_peer_id(),
trusted_peers: Self::placeholder_trusted_peers(),
block_time_ms: DEFAULT_BLOCK_TIME_MS,
commit_time_ms: DEFAULT_COMMIT_TIME_MS,
tx_receipt_time_ms: DEFAULT_TX_RECEIPT_TIME_MS,
Expand All @@ -88,6 +73,35 @@ impl Default for SumeragiConfiguration {
}

impl SumeragiConfiguration {
/// Key-pair used by default for demo purposes
#[allow(clippy::expect_used)]
fn placeholder_keypair() -> KeyPair {
let public_key = "ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b"
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
Algorithm::Ed25519,
"282ed9f3cf92811c3818dbc4ae594ed59dc1a2f78e4241e31924e101d6b1fb831c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b"
).expect("Private key not hex encoded");

KeyPair::new(public_key, private_key)
}

fn placeholder_peer_id() -> PeerId {
let (public_key, _) = Self::placeholder_keypair().into();

PeerId {
address: "127.0.0.1:1337".to_owned(),
public_key,
}
}

fn placeholder_trusted_peers() -> TrustedPeers {
let mut peers = HashSet::new();
peers.insert(Self::placeholder_peer_id());
TrustedPeers { peers }
}

/// Set `trusted_peers` configuration parameter. Will overwrite
/// existing `trusted_peers` but does not check for duplication.
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions crypto/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<T> MerkleTree<T> {
}

/// Return the `Hash` of the root node.
pub fn root_hash(&self) -> HashOf<Self> {
pub const fn root_hash(&self) -> HashOf<Self> {
self.root_node.hash().transmute()
}

Expand Down Expand Up @@ -165,7 +165,7 @@ impl<T> Node<T> {
}

/// Return the `Hash` of the root node.
pub fn hash(&self) -> HashOf<Self> {
pub const fn hash(&self) -> HashOf<Self> {
match self {
Node::Subtree(Subtree { hash, .. }) => *hash,
Node::Leaf(Leaf { hash }) => (*hash).transmute(),
Expand Down
18 changes: 8 additions & 10 deletions crypto/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct Signature {
public_key: PublicKey,
/// Actual signature payload is placed here.
#[getset(skip)]
signature: Payload,
payload: Payload,
}

impl Signature {
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Signature {

Ok(Self {
public_key,
signature,
payload: signature,
})
}

Expand All @@ -106,14 +106,12 @@ impl Signature {
let public_key = UrsaPublicKey(self.public_key.payload.clone());

match algorithm {
Algorithm::Ed25519 => {
Ed25519Sha512::new().verify(payload, &self.signature, &public_key)
}
Algorithm::Ed25519 => Ed25519Sha512::new().verify(payload, &self.payload, &public_key),
Algorithm::Secp256k1 => {
EcdsaSecp256k1Sha256::new().verify(payload, &self.signature, &public_key)
EcdsaSecp256k1Sha256::new().verify(payload, &self.payload, &public_key)
}
Algorithm::BlsSmall => BlsSmall::new().verify(payload, &self.signature, &public_key),
Algorithm::BlsNormal => BlsNormal::new().verify(payload, &self.signature, &public_key),
Algorithm::BlsSmall => BlsSmall::new().verify(payload, &self.payload, &public_key),
Algorithm::BlsNormal => BlsNormal::new().verify(payload, &self.payload, &public_key),
}?;

Ok(())
Expand All @@ -124,7 +122,7 @@ impl fmt::Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(core::any::type_name::<Self>())
.field("public_key", &self.public_key)
.field("signature", &hex::encode_upper(self.signature.as_slice()))
.field("signature", &hex::encode_upper(self.payload.as_slice()))
.finish()
}
}
Expand All @@ -133,7 +131,7 @@ impl From<Signature> for (PublicKey, Payload) {
fn from(
Signature {
public_key,
signature,
payload: signature,
}: Signature,
) -> Self {
(public_key, signature)
Expand Down
Loading

0 comments on commit 3563569

Please sign in to comment.