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 fd45932
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 65 deletions.
12 changes: 3 additions & 9 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
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 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::default_keypair().into();

Self {
public_key,
private_key,
account_id: AccountId::from_str("").expect("Empty strings are valid"),
account_id: Self::default_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 default_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 default_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
28 changes: 24 additions & 4 deletions core/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ 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;
Expand All @@ -323,7 +323,7 @@ pub mod config {
/// 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`.
/// 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>,
/// Genesis account private key, only needed on the peer that submits the genesis block.
Expand All @@ -340,18 +340,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::default_keypair().into();

Self {
account_public_key: None,
account_private_key: None,
account_public_key: Some(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 default_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
30 changes: 18 additions & 12 deletions core/src/sumeragi/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,18 @@ 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 key_pair = Self::default_keypair();

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

Self {
key_pair: KeyPair::new(public_key, private_key),
trusted_peers: TrustedPeers::default(),
key_pair,
peer_id,
trusted_peers: TrustedPeers::default(),
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 +80,20 @@ impl Default for SumeragiConfiguration {
}

impl SumeragiConfiguration {
/// Key-pair used by default for demo purposes
#[allow(clippy::expect_used)]
fn default_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)
}

/// 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
26 changes: 13 additions & 13 deletions data_model/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub mod role {
Serialize,
IntoSchema,
)]
pub struct FindAllRoles {}
pub struct FindAllRoles;

impl Query for FindAllRoles {
type Output = Vec<Role>;
Expand Down Expand Up @@ -309,7 +309,7 @@ pub mod account {
Serialize,
IntoSchema,
)]
pub struct FindAllAccounts {}
pub struct FindAllAccounts;

impl Query for FindAllAccounts {
type Output = Vec<Account>;
Expand Down Expand Up @@ -415,7 +415,7 @@ pub mod account {
impl FindAllAccounts {
/// Construct [`FindAllAccounts`].
pub const fn new() -> Self {
FindAllAccounts {}
FindAllAccounts
}
}

Expand Down Expand Up @@ -494,7 +494,7 @@ pub mod asset {
Serialize,
IntoSchema,
)]
pub struct FindAllAssets {}
pub struct FindAllAssets;

impl Query for FindAllAssets {
type Output = Vec<Asset>;
Expand All @@ -517,7 +517,7 @@ pub mod asset {
Serialize,
IntoSchema,
)]
pub struct FindAllAssetsDefinitions {}
pub struct FindAllAssetsDefinitions;

impl Query for FindAllAssetsDefinitions {
type Output = Vec<AssetDefinition>;
Expand Down Expand Up @@ -748,14 +748,14 @@ pub mod asset {
impl FindAllAssets {
/// Construct [`FindAllAssets`].
pub const fn new() -> Self {
FindAllAssets {}
FindAllAssets
}
}

impl FindAllAssetsDefinitions {
/// Construct [`FindAllAssetsDefinitions`].
pub const fn new() -> Self {
FindAllAssetsDefinitions {}
FindAllAssetsDefinitions
}
}

Expand Down Expand Up @@ -874,7 +874,7 @@ pub mod domain {
Serialize,
IntoSchema,
)]
pub struct FindAllDomains {}
pub struct FindAllDomains;

impl Query for FindAllDomains {
type Output = Vec<Domain>;
Expand Down Expand Up @@ -906,7 +906,7 @@ pub mod domain {
impl FindAllDomains {
/// Construct [`FindAllDomains`].
pub const fn new() -> Self {
FindAllDomains {}
FindAllDomains
}
}

Expand Down Expand Up @@ -991,7 +991,7 @@ pub mod peer {
Serialize,
IntoSchema,
)]
pub struct FindAllPeers {}
pub struct FindAllPeers;

impl Query for FindAllPeers {
type Output = Vec<Peer>;
Expand All @@ -1013,7 +1013,7 @@ pub mod peer {
Serialize,
IntoSchema,
)]
pub struct FindAllParameters {}
pub struct FindAllParameters;

impl Query for FindAllParameters {
type Output = Vec<Parameter>;
Expand All @@ -1022,14 +1022,14 @@ pub mod peer {
impl FindAllPeers {
///Construct [`FindAllPeers`].
pub const fn new() -> Self {
FindAllPeers {}
FindAllPeers
}
}

impl FindAllParameters {
/// Construct [`FindAllParameters`].
pub const fn new() -> Self {
FindAllParameters {}
FindAllParameters
}
}
/// The prelude re-exports most commonly used traits, structs and macros from this crate.
Expand Down
Loading

0 comments on commit fd45932

Please sign in to comment.