diff --git a/docs-recipes/2.1.1.key-pair.py b/docs-recipes/2.1.1.key-pair.py index 7b3c94ab..3813919c 100644 --- a/docs-recipes/2.1.1.key-pair.py +++ b/docs-recipes/2.1.1.key-pair.py @@ -10,16 +10,24 @@ } """) -key_pair2 = iroha.KeyPair.generate() +key_pair2 = iroha.KeyPair.random() print("kp1 =", key_pair1) print("kp2 =", key_pair2) -key_pair3 = iroha.KeyGenConfiguration.default().use_seed_hex("001122").generate() +key_pair3 = iroha.KeyPair.from_hex_seed("001122") -key_pair4 = iroha.KeyGenConfiguration.default().use_private_key(key_pair2.private_key).generate() +key_pair4 = iroha.KeyPair.from_private_key(key_pair2.private_key) -# kp4 and kp4 should have the same value +# kp2 and kp4 should have the same value print("kp3 =", key_pair3) -print("kp4 =", key_pair4) \ No newline at end of file +print("kp4 =", key_pair4) + + +# Different algorithms are supported + +print("kp using Ed25519 =", iroha.KeyPair.random_with_algorithm("Ed25519")) +print("kp using Secp256k1 =", iroha.KeyPair.random_with_algorithm("Secp256k1")) +print("kp using BlsNormal =", iroha.KeyPair.random_with_algorithm("BlsNormal")) +print("kp using BlsSmall =", iroha.KeyPair.random_with_algorithm("BlsSmall")) \ No newline at end of file diff --git a/docs-recipes/4.register-account.py b/docs-recipes/4.register-account.py index d790416c..eaee9178 100644 --- a/docs-recipes/4.register-account.py +++ b/docs-recipes/4.register-account.py @@ -25,7 +25,7 @@ api_url, chain_id) -new_account_key_pair = iroha.KeyPair.generate() +new_account_key_pair = iroha.KeyPair.random() new_account_id = "white_rabbit@wonderland" accounts = client.query_all_accounts_in_domain("wonderland") diff --git a/src/data_model/crypto.rs b/src/data_model/crypto.rs index 64ac4077..32283aa5 100644 --- a/src/data_model/crypto.rs +++ b/src/data_model/crypto.rs @@ -19,27 +19,17 @@ impl PyMirror for PrivateKey { } } -#[pymethods] -impl PyPrivateKey { - #[new] - fn new(encoded: &str) -> PyResult { - let pk = PrivateKey::from_hex(Algorithm::default(), &encoded) - .map_err(|e| PyValueError::new_err(format!("Invalid private key: {e}")))?; - Ok(Self(pk)) - } -} - #[pyclass(name = "PublicKey")] #[derive(Clone, derive_more::From, derive_more::Into, derive_more::Deref)] pub struct PyPublicKey(pub PublicKey); #[pymethods] impl PyPublicKey { - #[new] - fn new(encoded: &str) -> PyResult { + #[staticmethod] + fn from_string(encoded: &str) -> PyResult { let pk = encoded .parse() - .map_err(|e| PyValueError::new_err(format!("Invalid private key: {e}")))?; + .map_err(|e| PyValueError::new_err(format!("Invalid public key: {e}")))?; Ok(Self(pk)) } @@ -68,13 +58,55 @@ impl PyMirror for KeyPair { } } +fn string_to_algorithm(string: &str) -> PyResult { + match string { + "Ed25519" => Ok(Algorithm::Ed25519), + "Secp256k1" => Ok(Algorithm::Secp256k1), + "BlsNormal" => Ok(Algorithm::BlsNormal), + "BlsSmall" => Ok(Algorithm::BlsSmall), + _ => Err(PyErr::new::(format!( + "Error: '{}' is not a supported cryptography algorithm. Supported ones are 'Ed25519', 'Secp256k1', 'BlsNormal' and 'BlsSmall'.", + string + ))), + } +} + #[pymethods] impl PyKeyPair { #[staticmethod] - fn generate() -> PyResult { + fn random() -> PyResult { Ok(PyKeyPair(KeyPair::random())) } + #[staticmethod] + fn random_with_algorithm(algorithm: &str) -> PyResult { + Ok(PyKeyPair(KeyPair::random_with_algorithm( + string_to_algorithm(algorithm)?, + ))) + } + #[staticmethod] + fn from_hex_seed(hex_seed: &str) -> PyResult { + let bytes = hex::decode(hex_seed) + .map_err(|e| PyValueError::new_err(format!("Failed to decode hex: {e}")))?; + Ok(PyKeyPair(KeyPair::from_seed(bytes, Algorithm::default()))) + } + #[staticmethod] + fn from_hex_seed_with_algorithm(hex_seed: &str, algorithm: &str) -> PyResult { + let bytes = hex::decode(hex_seed) + .map_err(|e| PyValueError::new_err(format!("Failed to decode hex: {e}")))?; + Ok(PyKeyPair(KeyPair::from_seed( + bytes, + string_to_algorithm(algorithm)?, + ))) + } + + #[staticmethod] + fn from_private_key(pk: PyPrivateKey) -> PyResult { + Ok(PyKeyPair( + KeyPair::new(PublicKey::from(pk.0.clone()), pk.0) + .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?, + )) + } #[staticmethod] fn from_json(json_str: &str) -> PyResult { serde_json::from_str::(json_str) diff --git a/tests/helpers.py b/tests/helpers.py index fbf3a816..3675dacf 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -5,4 +5,4 @@ def generate_public_key(seed="abcd1122"): """ Generate a public key using Ed25519PrivateKey. """ - return iroha.KeyGenConfiguration.default().use_seed_hex(seed).generate().public_key \ No newline at end of file + return iroha.KeyPair.from_hex_seed(seed).public_key \ No newline at end of file