Skip to content

Commit

Permalink
[refactor]: restruct KeyPair methods
Browse files Browse the repository at this point in the history
- revert `from_raw_parts` to `new`
- rename `into_raw_parts` to `into_parts`
- remove `impl From<KeyPair> to (K, K)`

Signed-off-by: Dmitry Balashov <43530070+0x009922@users.noreply.github.com>
  • Loading branch information
0x009922 committed Mar 11, 2024
1 parent ad61c10 commit ec0fa3a
Show file tree
Hide file tree
Showing 25 changed files with 49 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ mod tests {
use super::*;

fn config_factory() -> PartialUserConfig {
let (pubkey, privkey) = KeyPair::random().into();
let (pubkey, privkey) = KeyPair::random().into_parts();

let mut base = PartialUserConfig::default();

Expand Down
2 changes: 1 addition & 1 deletion cli/src/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn get_user_config(
) -> UserConfig {
let chain_id = chain_id.unwrap_or_else(|| ChainId::from("0"));

let (public_key, private_key) = key_pair.unwrap_or_else(KeyPair::random).into();
let (public_key, private_key) = key_pair.unwrap_or_else(KeyPair::random).into_parts();
iroha_logger::info!(%public_key);

let mut config = UserConfig::new();
Expand Down
4 changes: 2 additions & 2 deletions client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn query_requests(criterion: &mut Criterion) {
let domain_id: DomainId = "domain".parse().expect("Valid");
let create_domain = Register::domain(Domain::new(domain_id.clone()));
let account_id = AccountId::new(domain_id.clone(), "account".parse().expect("Valid"));
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let create_account = Register::account(Account::new(account_id.clone(), public_key));
let asset_definition_id = AssetDefinitionId::new(domain_id, "xor".parse().expect("Valid"));
let create_asset =
Expand Down Expand Up @@ -162,7 +162,7 @@ fn instruction_submits(criterion: &mut Criterion) {
let domain_id: DomainId = "domain".parse().expect("Valid");
let create_domain: InstructionBox = Register::domain(Domain::new(domain_id.clone())).into();
let account_id = AccountId::new(domain_id.clone(), "account".parse().expect("Valid"));
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let create_account = Register::account(Account::new(account_id.clone(), public_key)).into();
let asset_definition_id = AssetDefinitionId::new(domain_id, "xor".parse().expect("Valid"));
let client_config = iroha_client::samples::get_client_config(
Expand Down
2 changes: 1 addition & 1 deletion client/examples/million_accounts_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn create_million_accounts_directly() {
let create_domain: InstructionBox = Register::domain(Domain::new(domain_id)).into();
let create_account = Register::account(Account::new(
normal_account_id.clone(),
KeyPair::random().into_raw_parts().0,
KeyPair::random().into_parts().0,
))
.into();
if test_client
Expand Down
2 changes: 1 addition & 1 deletion client/examples/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn account_registration_test(config: Config) -> Result<(), Error> {

// TODO: consider getting a key from white_rabbit
// Generate a new public key for a new account
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();

// #region register_account_generate
// Generate a new account
Expand Down
2 changes: 1 addition & 1 deletion client/src/config/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Root {
))
}

let key_pair = KeyPair::from_raw_parts(public_key, private_key)
let key_pair = KeyPair::new(public_key, private_key)
.wrap_err("failed to construct a key pair")
.map_or_else(
|err| {
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/asset_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount_on_a
let create_domain: InstructionBox =
Register::domain(Domain::new(DomainId::from_str("domain")?)).into();
let account_id = AccountId::from_str("account@domain")?;
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let create_account = Register::account(Account::new(account_id.clone(), public_key)).into();
let asset_definition_id = AssetDefinitionId::from_str("xor#domain")?;
let create_asset =
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/burn_public_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn public_keys_cannot_be_burned_to_nothing() {
assert_eq!(keys_count, 1);

let mint_keys = (0..KEYS_COUNT - 1).map(|_| {
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
Mint::account_public_key(public_key, charlie_id.clone())
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn long_multiple_blocks_created() -> Result<()> {

let create_domain: InstructionBox = Register::domain(Domain::new("domain".parse()?)).into();
let account_id: AccountId = "account@domain".parse()?;
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let create_account = Register::account(Account::new(account_id.clone(), public_key)).into();
let asset_definition_id: AssetDefinitionId = "xor#domain".parse()?;
let create_asset =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn init() -> Result<(
.into_set_parameters();
let create_domain = Register::domain(Domain::new("domain".parse()?));
let account_id: AccountId = "account@domain".parse()?;
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let create_account = Register::account(Account::new(account_id.clone(), public_key));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse()?;
let create_asset =
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ mod tx_rollback;
mod upgrade;

fn new_account_with_random_public_key(account_id: AccountId) -> NewAccount {
Account::new(account_id, KeyPair::random().into_raw_parts().0)
Account::new(account_id, KeyPair::random().into_parts().0)
}
2 changes: 1 addition & 1 deletion client/tests/integration/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn grant_revoke_role_permissions() -> Result<()> {
let mouse_id = AccountId::from_str("mouse@wonderland")?;

// Registering Mouse
let mouse_key_pair = KeyPair::generate();
let mouse_key_pair = KeyPair::random();
let register_mouse = Register::account(Account::new(
mouse_id.clone(),
mouse_key_pair.public_key().clone(),
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/transfer_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,6 @@ fn generate_two_ids() -> (AccountId, AccountId) {
}

fn create_mouse(mouse_id: AccountId) -> Register<Account> {
let (mouse_public_key, _) = KeyPair::random().into();
let (mouse_public_key, _) = KeyPair::random().into_parts();
Register::account(Account::new(mouse_id, mouse_public_key))
}
4 changes: 2 additions & 2 deletions client/tests/integration/tx_chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fn send_tx_with_different_chain_id() {
wait_for_genesis_committed(&[test_client.clone()], 0);
// Given
let sender_account_id = AccountId::from_str("sender@wonderland").unwrap();
let sender_keypair = KeyPair::generate();
let sender_keypair = KeyPair::random();
let receiver_account_id = AccountId::from_str("receiver@wonderland").unwrap();
let receiver_keypair = KeyPair::generate();
let receiver_keypair = KeyPair::random();
let asset_definition_id = AssetDefinitionId::from_str("test_asset#wonderland").unwrap();
let to_transfer = numeric!(1);

Expand Down
4 changes: 2 additions & 2 deletions config/src/parameters/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Root {
let mut emitter = Emitter::new();

let key_pair =
KeyPair::from_raw_parts(self.public_key, self.private_key)
KeyPair::new(self.public_key, self.private_key)
.wrap_err("failed to construct a key pair from `iroha.public_key` and `iroha.private_key` configuration parameters")
.map_or_else(|err| {
emitter.emit(err);
Expand Down Expand Up @@ -318,7 +318,7 @@ impl Genesis {
public_key: self.public_key,
}),
(Some(private_key), Some(file), true) => Ok(actual::Genesis::Full {
key_pair: KeyPair::from_raw_parts(self.public_key, private_key)
key_pair: KeyPair::new(self.public_key, private_key)
.map_err(GenesisConfigError::from)?,
file,
}),
Expand Down
5 changes: 2 additions & 3 deletions core/benches/blocks/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn populate_wsv(
instructions.push(can_unregister_domain.into());
for j in 0..accounts_per_domain {
let account_id = construct_account_id(j, domain_id.clone());
let account = Account::new(account_id.clone(), KeyPair::random().into_raw_parts().0);
let account = Account::new(account_id.clone(), KeyPair::random().into_parts().0);
instructions.push(Register::account(account).into());
let can_unregister_account = Grant::permission(
PermissionToken::new(
Expand Down Expand Up @@ -147,8 +147,7 @@ pub fn restore_every_nth(
for j in 0..accounts_per_domain {
if j % nth == 0 || i % nth == 0 {
let account_id = construct_account_id(j, domain_id.clone());
let account =
Account::new(account_id.clone(), KeyPair::random().into_raw_parts().0);
let account = Account::new(account_id.clone(), KeyPair::random().into_parts().0);
instructions.push(Register::account(account).into());
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/benches/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn build_test_transaction(keys: &KeyPair, chain_id: ChainId) -> SignedTransactio
let domain_id = DomainId::from_str(domain_name).expect("does not panic");
let create_domain: InstructionBox = Register::domain(Domain::new(domain_id)).into();
let account_name = "account";
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let create_account = Register::account(Account::new(
AccountId::new(
domain_name.parse().expect("Valid"),
Expand Down Expand Up @@ -59,7 +59,7 @@ fn build_test_transaction(keys: &KeyPair, chain_id: ChainId) -> SignedTransactio
fn build_test_and_transient_wsv(keys: KeyPair) -> WorldStateView {
let kura = iroha_core::kura::Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let (public_key, _) = keys.into();
let (public_key, _) = keys.into_parts();

let mut wsv = WorldStateView::new(
{
Expand Down
4 changes: 2 additions & 2 deletions core/src/smartcontracts/isi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ mod tests {
let mut wsv = WorldStateView::new(world, kura.clone(), query_handle);
let genesis_account_id = AccountId::from_str("genesis@genesis")?;
let account_id = AccountId::from_str("alice@wonderland")?;
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let asset_definition_id = AssetDefinitionId::from_str("rose#wonderland")?;
Register::domain(Domain::new(DomainId::from_str("wonderland")?))
.execute(&genesis_account_id, &mut wsv)?;
Expand Down Expand Up @@ -366,7 +366,7 @@ mod tests {
let trigger_id = TriggerId::from_str("test_trigger_id")?;

// register fake account
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let register_account = Register::account(Account::new(fake_account_id.clone(), public_key));
register_account.execute(&account_id, &mut wsv)?;

Expand Down
8 changes: 4 additions & 4 deletions core/src/smartcontracts/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ mod tests {

fn world_with_test_account(authority: &AccountId) -> World {
let domain_id = authority.domain_id.clone();
let (public_key, _) = KeyPair::random().into();
let (public_key, _) = KeyPair::random().into_parts();
let account = Account::new(authority.clone(), public_key).build(authority);
let mut domain = Domain::new(domain_id).build(authority);
assert!(domain.add_account(account).is_none());
Expand Down Expand Up @@ -1714,7 +1714,7 @@ mod tests {
let new_authority = AccountId::from_str("mad_hatter@wonderland").expect("Valid");
let register_isi = Register::account(Account::new(
new_authority,
KeyPair::random().into_raw_parts().0,
KeyPair::random().into_parts().0,
));
encode_hex(InstructionBox::from(register_isi))
};
Expand Down Expand Up @@ -1805,7 +1805,7 @@ mod tests {
let new_authority = AccountId::from_str("mad_hatter@wonderland").expect("Valid");
let register_isi = Register::account(Account::new(
new_authority,
KeyPair::random().into_raw_parts().0,
KeyPair::random().into_parts().0,
));
encode_hex(InstructionBox::from(register_isi))
};
Expand Down Expand Up @@ -1857,7 +1857,7 @@ mod tests {
let new_authority = AccountId::from_str("mad_hatter@wonderland").expect("Valid");
let register_isi = Register::account(Account::new(
new_authority,
KeyPair::random().into_raw_parts().0,
KeyPair::random().into_parts().0,
));
encode_hex(InstructionBox::from(register_isi))
};
Expand Down
4 changes: 2 additions & 2 deletions core/test_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn get_chain_id() -> ChainId {

/// Get a standardised key-pair from the hard-coded literals.
pub fn get_key_pair() -> KeyPair {
KeyPair::from_raw_parts(
KeyPair::new(
PublicKey::from_str(
"ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C0",
).unwrap(),
Expand Down Expand Up @@ -759,7 +759,7 @@ impl TestConfig for Config {
)
.merge(RootPartial::from_env(&StdEnv).expect("test env variables should parse properly"));

let (public_key, private_key) = KeyPair::random().into();
let (public_key, private_key) = KeyPair::random().into_parts();
layer.public_key.set(public_key);
layer.private_key.set(private_key);

Expand Down
26 changes: 9 additions & 17 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ impl KeyPair {

/// Construct a [`KeyPair`].
///
/// See [`Self::into_raw_parts`] for an opposite conversion.
/// See [`Self::into_parts`] for an opposite conversion.
///
/// # Errors
/// If public and private keys don't match, i.e. if they don't make a pair
pub fn from_raw_parts(public_key: PublicKey, private_key: PrivateKey) -> Result<Self, Error> {
pub fn new(public_key: PublicKey, private_key: PrivateKey) -> Result<Self, Error> {
let algorithm = private_key.algorithm();

if algorithm != public_key.algorithm() {
Expand All @@ -213,7 +213,7 @@ impl KeyPair {
/// Get [`PublicKey`] and [`PrivateKey`] contained in the [`KeyPair`].
///
/// See [`Self::from_raw_parts`] for an opposite conversion.
pub fn into_raw_parts(self) -> (PublicKey, PrivateKey) {
pub fn into_parts(self) -> (PublicKey, PrivateKey) {
(self.public_key, self.private_key)
}
}
Expand Down Expand Up @@ -297,15 +297,7 @@ impl<'de> Deserialize<'de> for KeyPair {

// NOTE: Verify that key pair is valid
let key_pair = KeyPairCandidate::deserialize(deserializer)?;
Self::from_raw_parts(key_pair.public_key, key_pair.private_key).map_err(D::Error::custom)
}
}

// TODO: enable in ffi_import?
#[cfg(not(feature = "ffi_import"))]
impl From<KeyPair> for (PublicKey, PrivateKey) {
fn from(key_pair: KeyPair) -> Self {
key_pair.into_raw_parts()
Self::new(key_pair.public_key, key_pair.private_key).map_err(D::Error::custom)
}
}

Expand Down Expand Up @@ -938,15 +930,15 @@ mod tests {

#[test]
fn key_pair_match() {
KeyPair::from_raw_parts("ed012059C8A4DA1EBB5380F74ABA51F502714652FDCCE9611FAFB9904E4A3C4D382774"
KeyPair::new("ed012059C8A4DA1EBB5380F74ABA51F502714652FDCCE9611FAFB9904E4A3C4D382774"
.parse()
.expect("Public key not in mulithash format"),
PrivateKey::from_hex(
Algorithm::Ed25519,
"93CA389FC2979F3F7D2A7F8B76C70DE6D5EAF5FA58D4F93CB8B0FB298D398ACC59C8A4DA1EBB5380F74ABA51F502714652FDCCE9611FAFB9904E4A3C4D382774"
).expect("Private key not hex encoded")).unwrap();

KeyPair::from_raw_parts("ea01309060D021340617E9554CCBC2CF3CC3DB922A9BA323ABDF7C271FCC6EF69BE7A8DEBCA7D9E96C0F0089ABA22CDAADE4A2"
KeyPair::new("ea01309060D021340617E9554CCBC2CF3CC3DB922A9BA323ABDF7C271FCC6EF69BE7A8DEBCA7D9E96C0F0089ABA22CDAADE4A2"
.parse()
.expect("Public key not in multihash format"),
PrivateKey::from_hex(
Expand All @@ -965,7 +957,7 @@ mod tests {
Algorithm::BlsSmall,
] {
let key_pair = KeyPair::random_with_algorithm(algorithm);
let (public_key, _) = key_pair.into();
let (public_key, _) = key_pair.into_parts();

let encoded_public_key = public_key.encode();

Expand Down Expand Up @@ -995,15 +987,15 @@ mod tests {

#[test]
fn key_pair_mismatch() {
KeyPair::from_raw_parts("ed012059C8A4DA1EBB5380F74ABA51F502714652FDCCE9611FAFB9904E4A3C4D382774"
KeyPair::new("ed012059C8A4DA1EBB5380F74ABA51F502714652FDCCE9611FAFB9904E4A3C4D382774"
.parse()
.expect("Public key not in mulithash format"),
PrivateKey::from_hex(
Algorithm::Ed25519,
"3A7991AF1ABB77F3FD27CC148404A6AE4439D095A63591B77C788D53F708A02A1509A611AD6D97B01D871E58ED00C8FD7C3917B6CA61A8C2833A19E000AAC2E4"
).expect("Private key not valid")).unwrap_err();

KeyPair::from_raw_parts("ea01309060D021340617E9554CCBC2CF3CC3DB922A9BA323ABDF7C271FCC6EF69BE7A8DEBCA7D9E96C0F0089ABA22CDAADE4A2"
KeyPair::new("ea01309060D021340617E9554CCBC2CF3CC3DB922A9BA323ABDF7C271FCC6EF69BE7A8DEBCA7D9E96C0F0089ABA22CDAADE4A2"
.parse()
.expect("Public key not in mulithash format"),
PrivateKey::from_hex(
Expand Down
2 changes: 1 addition & 1 deletion data_model/src/events/data/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ mod tests {
let account_id = AccountId::new(domain_id.clone(), account_name);
let account = Account::new(
account_id.clone(),
iroha_crypto::KeyPair::random().into_raw_parts().0,
iroha_crypto::KeyPair::random().into_parts().0,
)
.into_account();
let asset_id = AssetId::new(
Expand Down
6 changes: 3 additions & 3 deletions data_model/src/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ pub mod string {

#[test]
fn peer_id() {
let (public_key, _) = iroha_crypto::KeyPair::random().into();
let (public_key, _) = iroha_crypto::KeyPair::random().into_parts();
let id = IdBox::PeerId(peer::PeerId::new(socket_addr!(127.0.0.1:123), public_key));
assert!(StringPredicate::contains("123").applies(&id));
}
Expand Down Expand Up @@ -1066,7 +1066,7 @@ pub mod value {
pred.applies(&Value::Identifiable(IdentifiableBox::NewAccount(
Account::new(
"alice@wonderland".parse().expect("Valid"),
KeyPair::random().into_raw_parts().0,
KeyPair::random().into_parts().0,
)
)))
);
Expand All @@ -1085,7 +1085,7 @@ pub mod value {
}
{
let key_pair = iroha_crypto::KeyPair::random();
let (public_key, _) = key_pair.into();
let (public_key, _) = key_pair.into_parts();
let pred = ValuePredicate::Display(string::StringPredicate::is("alice@wonderland"));
println!("{pred:?}");

Expand Down
Loading

0 comments on commit ec0fa3a

Please sign in to comment.