Skip to content

Commit

Permalink
[fix] #2081: Fix role registration.
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Petrosyan <a-p-petrosyan@yandex.ru>
  • Loading branch information
appetrosyan committed Apr 15, 2022
1 parent d7bed07 commit 10362e5
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 148 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion client/tests/integration/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use eyre::{eyre, Result};
use iroha_client::client::{self, Client};
use iroha_core::{prelude::AllowAll, smartcontracts::permissions::ValidatorBuilder};
use iroha_data_model::{permissions::Permissions, prelude::*};
use iroha_permissions_validators::public_blockchain::transfer;
use iroha_permissions_validators::public_blockchain::{
key_value::{CAN_REMOVE_KEY_VALUE_IN_USER_METADATA, CAN_SET_KEY_VALUE_IN_USER_METADATA},
transfer,
};
use test_network::{Peer as TestPeer, *};
use tokio::runtime::Runtime;

Expand Down Expand Up @@ -126,3 +129,30 @@ fn register_role_with_empty_token_params() -> Result<()> {

// TODO: When we have more sane default permissions, see if we can
// test more about whether or not roles actually work.

#[test]
fn register_and_grant_metadata_role_to_account() -> Result<()> {
let (_rt, _peer, mut test_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(&vec![test_client.clone()], 0);

let bob_id = <Account as Identifiable>::Id::from_str("bob@wonderland")?;
let register_bob = RegisterBox::new(Account::new(bob_id.clone(), []));
test_client.submit_blocking(register_bob)?;

let role_id = iroha_data_model::role::Id::new("USER_METADATA_ACCESS".parse::<Name>()?);
let mut permissions = Permissions::new();
let mut params = BTreeMap::new();
params.insert(Name::from_str("account_id")?, bob_id.into());
permissions.insert(PermissionToken {
name: CAN_SET_KEY_VALUE_IN_USER_METADATA.clone(),
params: params.clone(),
});
permissions.insert(PermissionToken {
name: CAN_REMOVE_KEY_VALUE_IN_USER_METADATA.clone(),
params,
});
let register_role = RegisterBox::new(Role::new(role_id, permissions));

test_client.submit(register_role)?;
Ok(())
}
17 changes: 12 additions & 5 deletions core/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{collections::HashSet, fmt::Debug, fs::File, io::BufReader, ops::Deref,
use eyre::{eyre, Result, WrapErr};
use iroha_actor::Addr;
use iroha_crypto::{KeyPair, PublicKey};
use iroha_data_model::{asset::AssetDefinition, domain::NewDomain, prelude::*};
use iroha_data_model::{asset::AssetDefinition, prelude::*};
use iroha_schema::prelude::*;
use serde::{Deserialize, Serialize};
use small::SmallVec;
Expand Down Expand Up @@ -398,7 +398,7 @@ impl RawGenesisBlockBuilder {
/// be used to create assets and accounts.
pub fn domain(mut self, domain_name: Name) -> RawGenesisDomainBuilder {
let domain_id = DomainId::new(domain_name);
let new_domain = NewDomain::new(domain_id.clone());
let new_domain = Domain::new(domain_id.clone());
self.transaction
.isi
.push(Instruction::from(RegisterBox::new(new_domain)));
Expand Down Expand Up @@ -481,6 +481,13 @@ mod tests {
Ok(())
}

#[test]
fn new_domain_does_the_right_thing() {
let new_domain = RegisterBox::new(Domain::new("wonderland".parse().expect("Valid")));
let new_new_domain = RegisterBox::new(Domain::new("wonderland".parse().expect("Valid")));
assert_eq!(new_domain, new_new_domain);
}

#[allow(clippy::unwrap_used)]
#[test]
fn genesis_block_builder_example() {
Expand All @@ -505,7 +512,7 @@ mod tests {
let domain_id: DomainId = "wonderland".parse().unwrap();
assert_eq!(
finished_genesis_block.transactions[0].isi[0],
Instruction::from(RegisterBox::new(NewDomain::new(domain_id.clone())))
Instruction::from(RegisterBox::new(Domain::new(domain_id.clone())))
);
assert_eq!(
finished_genesis_block.transactions[0].isi[1],
Expand All @@ -521,7 +528,7 @@ mod tests {
let domain_id: DomainId = "tulgey_wood".parse().unwrap();
assert_eq!(
finished_genesis_block.transactions[0].isi[3],
Instruction::from(RegisterBox::new(NewDomain::new(domain_id.clone())))
Instruction::from(RegisterBox::new(Domain::new(domain_id.clone())))
);
assert_eq!(
finished_genesis_block.transactions[0].isi[4],
Expand All @@ -532,7 +539,7 @@ mod tests {
let domain_id: DomainId = "meadow".parse().unwrap();
assert_eq!(
finished_genesis_block.transactions[0].isi[5],
Instruction::from(RegisterBox::new(NewDomain::new(domain_id.clone())))
Instruction::from(RegisterBox::new(Domain::new(domain_id.clone())))
);
assert_eq!(
finished_genesis_block.transactions[0].isi[6],
Expand Down
4 changes: 2 additions & 2 deletions data_model/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct NewDomain {
/// The (IPFS) link to the logo of this domain.
logo: Option<IpfsPath>,
/// metadata associated to the domain builder.
pub metadata: Metadata,
metadata: Metadata,
}

impl PartialOrd for NewDomain {
Expand All @@ -94,7 +94,7 @@ impl Ord for NewDomain {
impl NewDomain {
/// Create a [`NewDomain`], reserved for internal use.
#[must_use]
pub fn new(id: <Domain as Identifiable>::Id) -> Self {
fn new(id: <Domain as Identifiable>::Id) -> Self {
Self {
id,
logo: None,
Expand Down
107 changes: 0 additions & 107 deletions tools/crypto_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,108 +1 @@
//! `iroha_crypto_cli` is a command line tool used to generate keys for Iroha peers and clients.

use clap::{App, Arg, ArgGroup};
use color_eyre::{
eyre::{self, eyre, WrapErr},
Report, Result,
};
use iroha_crypto::{Algorithm, KeyGenConfiguration, KeyPair, PrivateKey};

fn main() -> Result<(), Report> {
color_eyre::install()?;
let default_algorithm = Algorithm::default().to_string();
let matches = App::new("iroha_crypto_cli")
.version("0.1")
.author("Soramitsu")
.about("iroha_crypto_cli is a command line tool used to generate keys for Iroha peers and clients.")
.arg(
Arg::with_name("seed")
.long("seed")
.value_name("seed")
.help("Sets a seed for random number generator. Should be used separately from `private_key`.")
.required(false)
.takes_value(true)
)
.arg(
Arg::with_name("private_key")
.long("private_key")
.value_name("private_key")
.help("Sets a private key. Should be used separately from `seed`.")
.required(false)
.takes_value(true)
)
.arg(
Arg::with_name("algorithm")
.long("algorithm")
.value_name("algorithm")
.help("Function used to generate the key pair.")
.takes_value(true)
.possible_value(iroha_crypto::ED_25519)
.possible_value(iroha_crypto::SECP_256_K1)
.possible_value(iroha_crypto::BLS_NORMAL)
.possible_value(iroha_crypto::BLS_SMALL)
.default_value(&default_algorithm)
)
.arg(
Arg::with_name("json")
.long("json")
.help("If specified the output will be formatted as json.")
.takes_value(false)
.multiple(false)
)
.group(
ArgGroup::with_name("key_gen_options")
.args(&["seed", "private_key"])
.required(false)
.multiple(false)
)
.get_matches();
let seed_option = matches.value_of("seed");
let private_key_option = matches.value_of("private_key");
let algorithm = matches
.value_of("algorithm")
.ok_or_else(|| eyre!("Failed to get algorithm name."))?
.parse::<Algorithm>()
.wrap_err("Failed to parse algorithm.")?;
let key_gen_configuration = KeyGenConfiguration::default().with_algorithm(algorithm);
let keypair: KeyPair = seed_option.map_or_else(
|| -> eyre::Result<_> {
private_key_option.map_or_else(
|| {
KeyPair::generate_with_configuration(key_gen_configuration.clone())
.wrap_err("failed to generate key pair")
},
|private_key| {
KeyPair::generate_with_configuration(
key_gen_configuration.clone().use_private_key(PrivateKey {
digest_function: algorithm.to_string(),
payload: hex::decode(private_key)
.wrap_err("Failed to decode private key.")?,
}),
)
.wrap_err("Failed to generate key pair")
},
)
},
|seed| -> eyre::Result<_> {
KeyPair::generate_with_configuration(
key_gen_configuration
.clone()
.use_seed(seed.as_bytes().into()),
)
.wrap_err("Failed to generate key pair")
},
)?;

#[allow(clippy::print_stdout)]
if matches.is_present("json") {
let json =
serde_json::to_string_pretty(&keypair).wrap_err("Failed to serialize to json.")?;
println!("{}", json);
} else {
println!("Public key (multihash): {}", &keypair.public_key);
println!("Private key: {}", &keypair.private_key);
println!("Digest function: {}", &keypair.public_key.digest_function);
}

Ok(())
}
9 changes: 8 additions & 1 deletion tools/generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

# Explanation: You want the option to remove configuration docs if you distribute e.g. a `deb`, but want the person who just cloned the repo to be able to see at a glance what kinds of features are accessible to them, what they do and how to use them. #1975 should be fixed soon.
# Explanation: You want the option to remove configuration docs if you
# distribute e.g. a `deb`, but want the person who just cloned the
# repo to be able to see at a glance what kinds of features are
# accessible to them, what they do and how to use them. #1975 should
# be fixed soon.
[features]
roles = ["iroha_core/roles"]
dex = ["iroha_core/dex"]
Expand All @@ -17,10 +21,13 @@ default = ["roles", "dex", "telemetry", "dev-telemetry"]

[dependencies]
iroha_core = { version = "=2.0.0-pre-rc.3", path = "../../core" }
iroha_crypto = { version = "=2.0.0-pre-rc.3", path = "../../crypto" }
iroha_config = { version = "=2.0.0-pre-rc.3", path = "../../config" }
iroha_data_model = { version = "=2.0.0-pre-rc.3", path = "../../data_model" }
iroha_schema_bin = { version = "=2.0.0-pre-rc.3", path = "../../schema/bin" }
iroha = { path = "../../cli" }

color-eyre = "0.5.11"
hex = "0.4.0"
clap = "2.33.0"
serde_json = "1"
Loading

0 comments on commit 10362e5

Please sign in to comment.