Skip to content

Commit

Permalink
refactor: remove public key from transaction and query
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Jun 12, 2024
1 parent 409f50e commit 9ab1290
Show file tree
Hide file tree
Showing 43 changed files with 575 additions and 393 deletions.
3 changes: 3 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ async fn main() -> error_stack::Result<(), MainError> {
iroha_logger::info!(
version = env!("CARGO_PKG_VERSION"),
git_commit_sha = env!("VERGEN_GIT_SHA"),
peer = %config.common.peer,
chain = %config.common.chain,
listening_on = %config.torii.address.value(),
"Hyperledgerいろは2にようこそ!(translation) Welcome to Hyperledger Iroha!"
);

Expand Down
18 changes: 6 additions & 12 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,17 @@ impl Client {
tx_builder.set_nonce(nonce);
};

tx_builder.with_metadata(metadata).sign(&self.key_pair)
tx_builder
.with_metadata(metadata)
.sign(self.key_pair.private_key())
}

/// Signs transaction
///
/// # Errors
/// Fails if signature generation fails
pub fn sign_transaction(&self, transaction: TransactionBuilder) -> SignedTransaction {
transaction.sign(&self.key_pair)
transaction.sign(self.key_pair.private_key())
}

/// Signs query
Expand Down Expand Up @@ -1664,20 +1666,12 @@ mod tests {
use http::Response;

use super::*;
use crate::data_model::{asset::Asset, query::error::QueryExecutionFail, ValidationFail};
use crate::data_model::{asset::Asset, ValidationFail};

#[test]
fn certain_errors() -> Result<()> {
let mut sut = QueryResponseHandler::<Vec<Asset>>::new(QueryRequest::dummy());
let responses = vec![
(
StatusCode::UNAUTHORIZED,
ValidationFail::QueryFailed(QueryExecutionFail::Signature(
"whatever".to_owned(),
)),
),
(StatusCode::UNPROCESSABLE_ENTITY, ValidationFail::TooComplex),
];
let responses = vec![(StatusCode::UNPROCESSABLE_ENTITY, ValidationFail::TooComplex)];
for (status_code, err) in responses {
let resp = Response::builder().status(status_code).body(err.encode())?;

Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ fn find_rate_and_make_exchange_isi_should_succeed() {
asset_id.account().clone(),
)
.with_instructions([instruction])
.sign(&owner_key_pair);
.sign(owner_key_pair.private_key());

test_client
.submit_transaction_blocking(&transaction)
Expand Down
8 changes: 4 additions & 4 deletions client/tests/integration/domain_owner_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn domain_owner_domain_permissions() -> Result<()> {
// Asset definitions can't be registered by "bob@kingdom" by default
let transaction = TransactionBuilder::new(chain_id.clone(), bob_id.clone())
.with_instructions([Register::asset_definition(coin.clone())])
.sign(&bob_keypair);
.sign(bob_keypair.private_key());
let err = test_client
.submit_transaction_blocking(&transaction)
.expect_err("Tx should fail due to permissions");
Expand All @@ -52,7 +52,7 @@ fn domain_owner_domain_permissions() -> Result<()> {
test_client.submit_blocking(Grant::permission(token.clone(), bob_id.clone()))?;
let transaction = TransactionBuilder::new(chain_id, bob_id.clone())
.with_instructions([Register::asset_definition(coin)])
.sign(&bob_keypair);
.sign(bob_keypair.private_key());
test_client.submit_transaction_blocking(&transaction)?;
test_client.submit_blocking(Revoke::permission(token, bob_id.clone()))?;

Expand Down Expand Up @@ -148,7 +148,7 @@ fn domain_owner_asset_definition_permissions() -> Result<()> {
let coin = AssetDefinition::numeric(coin_id.clone());
let transaction = TransactionBuilder::new(chain_id, bob_id.clone())
.with_instructions([Register::asset_definition(coin)])
.sign(&bob_keypair);
.sign(bob_keypair.private_key());
test_client.submit_transaction_blocking(&transaction)?;

// check that "alice@wonderland" as owner of domain can transfer asset definitions in her domain
Expand Down Expand Up @@ -217,7 +217,7 @@ fn domain_owner_asset_permissions() -> Result<()> {
Register::asset_definition(coin),
Register::asset_definition(store),
])
.sign(&bob_keypair);
.sign(bob_keypair.private_key());
test_client.submit_transaction_blocking(&transaction)?;

// check that "alice@wonderland" as owner of domain can register and unregister assets in her domain
Expand Down
1 change: 1 addition & 0 deletions client/tests/integration/extra_functional/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod connected_peers;
mod multiple_blocks_created;
mod normal;
mod offline_peers;
mod restart_peer;
mod unregister_peer;
Expand Down
55 changes: 55 additions & 0 deletions client/tests/integration/extra_functional/normal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::num::NonZeroU32;

use iroha::client::{self, Client};
use iroha_config::parameters::actual::Root as Config;
use iroha_data_model::{asset::AssetDefinitionId, prelude::*};
use test_network::*;
use tokio::runtime::Runtime;

#[test]
fn tranasctions_should_be_applied() {
let rt = Runtime::test();
let (network, iroha) = rt.block_on(async {
let mut configuration = Config::test();
configuration.chain_wide.max_transactions_in_block = NonZeroU32::new(1).unwrap();
let network = Network::new_with_offline_peers(Some(configuration), 4, 0, Some(11_300))
.await
.unwrap();
let iroha = Client::test(&network.genesis.api_address);

(network, iroha)
});
wait_for_genesis_committed(&network.clients(), 0);

let domain_id = "and".parse::<DomainId>().unwrap();
let account_id = "ed01201F803CB23B1AAFB958368DF2F67CB78A2D1DFB47FFFC3133718F165F54DFF677@and"
.parse::<AccountId>()
.unwrap();
let asset_definition_id = "MAY#and".parse::<AssetDefinitionId>().unwrap();
let asset_id =
"MAY##ed01201F803CB23B1AAFB958368DF2F67CB78A2D1DFB47FFFC3133718F165F54DFF677@and"
.parse()
.unwrap();

let create_domain = Register::domain(Domain::new(domain_id));
iroha.submit_blocking(create_domain).unwrap();

let create_asset =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone()));
iroha.submit_blocking(create_asset).unwrap();

let create_account = Register::account(Account::new(account_id.clone()));
iroha.submit_blocking(create_account).unwrap();

let mint_asset = Mint::asset_numeric(
numeric!(57_787_013_353_273_097_936_105_299_296),
AssetId::new(asset_definition_id.clone(), account_id.clone()),
);
iroha.submit_blocking(mint_asset).unwrap();

let mint_asset =
Mint::asset_numeric(numeric!(1), AssetId::new(asset_definition_id, account_id));
iroha.submit_blocking(mint_asset).unwrap();

iroha.request(client::asset::by_id(asset_id)).unwrap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::thread;

use iroha::{
client::{self, Client, QueryResult},
data_model::{prelude::*, Level},
data_model::prelude::*,
};
use iroha_config::parameters::actual::Root as Config;
use rand::seq::SliceRandom;
Expand Down Expand Up @@ -55,7 +55,6 @@ fn unstable_network(
let mut configuration = Config::test();
configuration.chain_wide.max_transactions_in_block =
MAX_TRANSACTIONS_IN_BLOCK.try_into().unwrap();
configuration.logger.level = Level::INFO;
#[cfg(debug_assertions)]
{
configuration.sumeragi.debug_force_soft_fork = force_soft_fork;
Expand Down
10 changes: 5 additions & 5 deletions client/tests/integration/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn permissions_disallow_asset_transfer() {
);
let transfer_tx = TransactionBuilder::new(chain_id, mouse_id)
.with_instructions([transfer_asset])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());
let err = iroha
.submit_transaction_blocking(&transfer_tx)
.expect_err("Transaction was not rejected.");
Expand Down Expand Up @@ -151,7 +151,7 @@ fn permissions_disallow_asset_burn() {
);
let burn_tx = TransactionBuilder::new(chain_id, mouse_id)
.with_instructions([burn_asset])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());

let err = iroha
.submit_transaction_blocking(&burn_tx)
Expand Down Expand Up @@ -239,7 +239,7 @@ fn permissions_differ_not_only_by_names() {

let grant_hats_access_tx = TransactionBuilder::new(chain_id.clone(), mouse_id.clone())
.with_instructions([allow_alice_to_set_key_value_in_hats])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());
client
.submit_transaction_blocking(&grant_hats_access_tx)
.expect("Failed grant permission to modify Mouse's hats");
Expand Down Expand Up @@ -275,7 +275,7 @@ fn permissions_differ_not_only_by_names() {

let grant_shoes_access_tx = TransactionBuilder::new(chain_id, mouse_id)
.with_instructions([allow_alice_to_set_key_value_in_shoes])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());

client
.submit_transaction_blocking(&grant_shoes_access_tx)
Expand Down Expand Up @@ -328,7 +328,7 @@ fn stored_vs_granted_token_payload() -> Result<()> {

let transaction = TransactionBuilder::new(chain_id, mouse_id)
.with_instructions([allow_alice_to_set_key_value_in_mouse_asset])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());
iroha
.submit_transaction_blocking(&transaction)
.expect("Failed to grant permission to alice.");
Expand Down
8 changes: 4 additions & 4 deletions client/tests/integration/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn register_and_grant_role_for_metadata_access() -> Result<()> {
let grant_role = Grant::role(role_id.clone(), alice_id.clone());
let grant_role_tx = TransactionBuilder::new(chain_id, mouse_id.clone())
.with_instructions([grant_role])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());
test_client.submit_transaction_blocking(&grant_role_tx)?;

// Alice modifies Mouse's metadata
Expand Down Expand Up @@ -236,7 +236,7 @@ fn grant_revoke_role_permissions() -> Result<()> {
let grant_role = Grant::role(role_id.clone(), alice_id.clone());
let grant_role_tx = TransactionBuilder::new(chain_id.clone(), mouse_id.clone())
.with_instructions([grant_role])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());
test_client.submit_transaction_blocking(&grant_role_tx)?;

let set_key_value = SetKeyValue::account(
Expand All @@ -263,7 +263,7 @@ fn grant_revoke_role_permissions() -> Result<()> {
// Alice can modify Mouse's metadata after permission token is granted to role
let grant_role_permission_tx = TransactionBuilder::new(chain_id.clone(), mouse_id.clone())
.with_instructions([grant_role_permission])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());
test_client.submit_transaction_blocking(&grant_role_permission_tx)?;
let found_permissions = test_client
.request(FindPermissionsByAccountId::new(alice_id.clone()))?
Expand All @@ -274,7 +274,7 @@ fn grant_revoke_role_permissions() -> Result<()> {
// Alice can't modify Mouse's metadata after permission token is removed from role
let revoke_role_permission_tx = TransactionBuilder::new(chain_id.clone(), mouse_id.clone())
.with_instructions([revoke_role_permission])
.sign(&mouse_keypair);
.sign(mouse_keypair.private_key());
test_client.submit_transaction_blocking(&revoke_role_permission_tx)?;
let found_permissions = test_client
.request(FindPermissionsByAccountId::new(alice_id.clone()))?
Expand Down
5 changes: 2 additions & 3 deletions client/tests/integration/tx_chain_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use iroha::data_model::prelude::*;
use iroha_data_model::asset::AssetDefinitionId;
use iroha_primitives::numeric::numeric;
use test_network::*;
use test_samples::gen_account_in;
Expand Down Expand Up @@ -45,10 +44,10 @@ fn send_tx_with_different_chain_id() {
);
let asset_transfer_tx_0 = TransactionBuilder::new(chain_id_0, sender_id.clone())
.with_instructions([transfer_instruction.clone()])
.sign(&sender_keypair);
.sign(sender_keypair.private_key());
let asset_transfer_tx_1 = TransactionBuilder::new(chain_id_1, sender_id.clone())
.with_instructions([transfer_instruction])
.sign(&sender_keypair);
.sign(sender_keypair.private_key());
test_client
.submit_transaction_blocking(&asset_transfer_tx_0)
.unwrap();
Expand Down
13 changes: 5 additions & 8 deletions client/tests/integration/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use eyre::Result;
use futures_util::TryStreamExt as _;
use iroha::{
client::{self, Client, QueryResult},
crypto::KeyPair,
data_model::prelude::*,
};
use iroha_data_model::parameter::{default::EXECUTOR_FUEL_LIMIT, ParametersBuilder};
Expand All @@ -24,11 +23,9 @@ fn executor_upgrade_should_work() -> Result<()> {
let admin_id: AccountId = format!("{ADMIN_PUBLIC_KEY_MULTIHASH}@admin")
.parse()
.unwrap();
let admin_keypair = KeyPair::new(
admin_id.signatory().clone(),
ADMIN_PRIVATE_KEY_MULTIHASH.parse().unwrap(),
)
.unwrap();
let admin_private_key = ADMIN_PRIVATE_KEY_MULTIHASH
.parse::<iroha::crypto::PrivateKey>()
.unwrap();

let (_rt, _peer, client) = <PeerBuilder>::new().with_port(10_795).start_with_runtime();
wait_for_genesis_committed(&vec![client.clone()], 0);
Expand All @@ -49,7 +46,7 @@ fn executor_upgrade_should_work() -> Result<()> {
let transfer_alice_rose = Transfer::asset_numeric(alice_rose, 1u32, admin_id.clone());
let transfer_rose_tx = TransactionBuilder::new(chain_id.clone(), admin_id.clone())
.with_instructions([transfer_alice_rose.clone()])
.sign(&admin_keypair);
.sign(&admin_private_key);
let _ = client
.submit_transaction_blocking(&transfer_rose_tx)
.expect_err("Should fail");
Expand All @@ -63,7 +60,7 @@ fn executor_upgrade_should_work() -> Result<()> {
// Creating new transaction instead of cloning, because we need to update it's creation time
let transfer_rose_tx = TransactionBuilder::new(chain_id, admin_id.clone())
.with_instructions([transfer_alice_rose])
.sign(&admin_keypair);
.sign(&admin_private_key);
client
.submit_transaction_blocking(&transfer_rose_tx)
.expect("Should succeed");
Expand Down
2 changes: 1 addition & 1 deletion client_cli/pytests/src/client_cli/client_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, config: Config):
self.stdout = None
self.stderr = None
self.transaction_hash = None
self._timeout = 20
self._timeout = 40

def __enter__(self):
"""
Expand Down
1 change: 1 addition & 0 deletions client_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl FromStr for MetadataValueArg {
struct Args {
/// Path to the configuration file
#[arg(short, long, value_name("PATH"), value_hint(clap::ValueHint::FilePath))]
#[clap(default_value = "client.toml")]
config: PathBuf,
/// More verbose output
#[arg(short, long)]
Expand Down
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion core/benches/blocks/apply_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl StateApplyBlocks {
&mut state_block,
instructions,
alice_id.clone(),
&alice_keypair,
alice_keypair.private_key(),
);
let _events = state_block.apply_without_execution(&block);
state_block.commit();
Expand Down
8 changes: 4 additions & 4 deletions core/benches/blocks/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ pub fn create_block(
state: &mut StateBlock<'_>,
instructions: Vec<InstructionBox>,
account_id: AccountId,
key_pair: &KeyPair,
private_key: &PrivateKey,
) -> CommittedBlock {
let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000");

let transaction = TransactionBuilder::new(chain_id.clone(), account_id)
.with_instructions(instructions)
.sign(key_pair);
.sign(private_key);
let limits = state.transaction_executor().transaction_limits;

let (peer_public_key, _) = KeyPair::random().into_parts();
let peer_id = PeerId::new("127.0.0.1:8080".parse().unwrap(), peer_public_key);
let topology = Topology::new(vec![peer_id]);
let block = BlockBuilder::new(
vec![AcceptedTransaction::accept(transaction, &chain_id, &limits).unwrap()],
vec![AcceptedTransaction::accept(transaction, &chain_id, limits).unwrap()],
topology.clone(),
Vec::new(),
)
.chain(0, state)
.sign(key_pair.private_key())
.sign(private_key)
.unpack(|_| {})
.commit(&topology)
.unpack(|_| {})
Expand Down
Loading

0 comments on commit 9ab1290

Please sign in to comment.