Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] #140: Comprehensive key generation #194

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions docs-recipes/2.1.1.key-pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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"))
2 changes: 1 addition & 1 deletion docs-recipes/4.register-account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
60 changes: 46 additions & 14 deletions src/data_model/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,17 @@ impl PyMirror for PrivateKey {
}
}

#[pymethods]
impl PyPrivateKey {
#[new]
fn new(encoded: &str) -> PyResult<Self> {
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<Self> {
#[staticmethod]
fn from_string(encoded: &str) -> PyResult<Self> {
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))
}

Expand Down Expand Up @@ -68,13 +58,55 @@ impl PyMirror for KeyPair {
}
}

fn string_to_algorithm(string: &str) -> PyResult<Algorithm> {
match string {
"Ed25519" => Ok(Algorithm::Ed25519),
"Secp256k1" => Ok(Algorithm::Secp256k1),
"BlsNormal" => Ok(Algorithm::BlsNormal),
"BlsSmall" => Ok(Algorithm::BlsSmall),
_ => Err(PyErr::new::<PyValueError, _>(format!(
"Error: '{}' is not a supported cryptography algorithm. Supported ones are 'Ed25519', 'Secp256k1', 'BlsNormal' and 'BlsSmall'.",
string
))),
}
}

#[pymethods]
impl PyKeyPair {
#[staticmethod]
fn generate() -> PyResult<Self> {
fn random() -> PyResult<Self> {
Ok(PyKeyPair(KeyPair::random()))
}
#[staticmethod]
fn random_with_algorithm(algorithm: &str) -> PyResult<Self> {
Ok(PyKeyPair(KeyPair::random_with_algorithm(
string_to_algorithm(algorithm)?,
)))
}

#[staticmethod]
fn from_hex_seed(hex_seed: &str) -> PyResult<Self> {
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<Self> {
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<Self> {
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<Self> {
serde_json::from_str::<KeyPair>(json_str)
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return iroha.KeyPair.from_hex_seed(seed).public_key
Loading