From 8ce2d61169fd526b947d8a853e8f74e9ac2335bd Mon Sep 17 00:00:00 2001 From: Retssaze Date: Thu, 9 Feb 2023 16:29:32 +0200 Subject: [PATCH] [refactor] #2123: use Algorithm for storing digest function Signed-off-by: Retssaze --- config/iroha_test_config.json | 32 +- config/src/iroha.rs | 11 +- configs/client_cli/config.json | 5 +- configs/peer/genesis.json | 15 +- core/src/sumeragi/main_loop.rs | 1 + crypto/src/lib.rs | 357 ++++++++++++------ docs/source/references/schema.json | 28 +- .../parity_scale_decoder/src/generate_map.rs | 1 + 8 files changed, 310 insertions(+), 140 deletions(-) mode change 100644 => 100755 crypto/src/lib.rs diff --git a/config/iroha_test_config.json b/config/iroha_test_config.json index 055995bbe67..d84472afe69 100644 --- a/config/iroha_test_config.json +++ b/config/iroha_test_config.json @@ -1,5 +1,8 @@ { - "PUBLIC_KEY": "ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b", + "PUBLIC_KEY": { + "digest_function": "ed25519", + "payload": "1c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b" + }, "PRIVATE_KEY": { "digest_function": "ed25519", "payload": "282ed9f3cf92811c3818dbc4ae594ed59dc1a2f78e4241e31924e101d6b1fb831c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b" @@ -17,19 +20,31 @@ "TRUSTED_PEERS": [ { "address": "127.0.0.1:1337", - "public_key": "ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b" + "public_key": { + "digest_function": "ed25519", + "payload": "1c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b" + } }, { "address": "127.0.0.1:1338", - "public_key": "ed0120cc25624d62896d3a0bfd8940f928dc2abf27cc57cefeb442aa96d9081aae58a1" + "public_key": { + "digest_function": "secp256k1", + "payload": "cc25624d62896d3a0bfd8940f928dc2abf27cc57cefeb442aa96d9081aae58a1" + } }, { "address": "127.0.0.1:1339", - "public_key": "ed0120faca9e8aa83225cb4d16d67f27dd4f93fc30ffa11adc1f5c88fd5495ecc91020" + "public_key": { + "digest_function": "bls_normal", + "payload": "faca9e8aa83225cb4d16d67f27dd4f93fc30ffa11adc1f5c88fd5495ecc91020" + } }, { "address": "127.0.0.1:1340", - "public_key": "ed01208e351a70b6a603ed285d666b8d689b680865913ba03ce29fb7d13a166c4e7f1f" + "public_key": { + "digest_function": "bls_small", + "payload": "8e351a70b6a603ed285d666b8d689b680865913ba03ce29fb7d13a166c4e7f1f" + } } ], "COMMIT_TIME_LIMIT_MS": 2000, @@ -70,7 +85,10 @@ "TERMINAL_COLORS": true }, "GENESIS": { - "ACCOUNT_PUBLIC_KEY": "ed01204cffd0ee429b1bdd36b3910ec570852b8bb63f18750341772fb46bc856c5caaf", + "ACCOUNT_PUBLIC_KEY": { + "digest_function": "ed25519", + "payload": "4cffd0ee429b1bdd36b3910ec570852b8bb63f18750341772fb46bc856c5caaf" + }, "ACCOUNT_PRIVATE_KEY": { "digest_function": "ed25519", "payload": "d748e18ce60cb30dea3e73c9019b7af45a8d465e3d71bcc9a5ef99a008205e534cffd0ee429b1bdd36b3910ec570852b8bb63f18750341772fb46bc856c5caaf" @@ -115,4 +133,4 @@ "MAX_RETRY_DELAY_EXPONENT": 4, "FILE": null } -} +} \ No newline at end of file diff --git a/config/src/iroha.rs b/config/src/iroha.rs index 4afe807dda7..57f15d7a21d 100644 --- a/config/src/iroha.rs +++ b/config/src/iroha.rs @@ -172,15 +172,18 @@ mod tests { /// Key-pair used for proptests generation #[allow(clippy::expect_used)] pub fn placeholder_keypair() -> KeyPair { - let public_key = "ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b" - .parse() - .expect("Public key not in mulithash format"); let private_key = PrivateKey::from_hex( Algorithm::Ed25519, "282ed9f3cf92811c3818dbc4ae594ed59dc1a2f78e4241e31924e101d6b1fb831c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b" ).expect("Private key not hex encoded"); - KeyPair::new(public_key, private_key).expect("Key pair mismatch") + KeyPair::new( + "ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b" + .parse() + .expect("Public key not in mulithash format"), + private_key, + ) + .expect("Key pair mismatch") } fn arb_keys() -> BoxedStrategy<(Option, Option)> { diff --git a/configs/client_cli/config.json b/configs/client_cli/config.json index 426f229af38..37e819b4059 100644 --- a/configs/client_cli/config.json +++ b/configs/client_cli/config.json @@ -1,5 +1,8 @@ { - "PUBLIC_KEY": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0", + "PUBLIC_KEY": { + "digest_function": "ed25519", + "payload": "7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" + }, "PRIVATE_KEY": { "digest_function": "ed25519", "payload": "9ac47abf59b356e0bd7dcbbbb4dec080e302156a48ca907e47cb6aea1d32719e7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" diff --git a/configs/peer/genesis.json b/configs/peer/genesis.json index 72137a8d1c6..50428dea661 100644 --- a/configs/peer/genesis.json +++ b/configs/peer/genesis.json @@ -20,7 +20,10 @@ "NewAccount": { "id": "alice@wonderland", "signatories": [ - "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" + { + "digest_function": "ed25519", + "payload": "7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" + } ], "metadata": { "key": { @@ -35,7 +38,10 @@ "NewAccount": { "id": "bob@wonderland", "signatories": [ - "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" + { + "digest_function": "ed25519", + "payload": "7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" + } ], "metadata": { "key": { @@ -69,7 +75,10 @@ "NewAccount": { "id": "carpenter@garden_of_live_flowers", "signatories": [ - "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" + { + "digest_function": "ed25519", + "payload": "7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" + } ], "metadata": {} } diff --git a/core/src/sumeragi/main_loop.rs b/core/src/sumeragi/main_loop.rs index baf6ac738ff..eff6a7bef3a 100644 --- a/core/src/sumeragi/main_loop.rs +++ b/core/src/sumeragi/main_loop.rs @@ -1162,3 +1162,4 @@ fn early_return( Err(TryRecvError::Empty) => Ok(()), } } + diff --git a/crypto/src/lib.rs b/crypto/src/lib.rs old mode 100644 new mode 100755 index 95d59c47ca3..6f0f7b756bc --- a/crypto/src/lib.rs +++ b/crypto/src/lib.rs @@ -16,7 +16,9 @@ mod signature; mod varint; #[cfg(not(feature = "std"))] -use alloc::{borrow::ToOwned, format, string::String, vec::Vec}; +use alloc::string::ToString; +#[cfg(not(feature = "std"))] +use alloc::{borrow::ToOwned, format, string::String, vec, vec::Vec}; use core::{fmt, str::FromStr}; #[cfg(feature = "base64")] @@ -25,7 +27,6 @@ use derive_more::{DebugCustom, Display, From}; use getset::Getters; pub use hash::*; use iroha_ffi::FfiType; -use iroha_primitives::conststr::ConstString; use iroha_schema::IntoSchema; pub use merkle::MerkleTree; use multihash::{DigestFunction as MultihashDigestFunction, Multihash}; @@ -49,6 +50,7 @@ use ursa::{ // create temporaries, but also you're not actually hiding any // information that can be used in malicious ways. If you want to hide // these, I'd prefer inlining them instead. + /// ed25519 pub const ED_25519: &str = "ed25519"; /// secp256k1 @@ -69,24 +71,95 @@ impl std::error::Error for NoSuchAlgorithm {} ffi::ffi_item! { /// Algorithm for hashing - #[derive(Debug, Clone, Copy, PartialEq, Eq, Display, FfiType)] + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, FfiType, IntoSchema)] #[repr(u8)] pub enum Algorithm { + //#[codec(index = 101)] /// Ed25519 - #[display(fmt = "{ED_25519}")] Ed25519, /// Secp256k1 - #[display(fmt = "{SECP_256_K1}")] Secp256k1, /// BlsNormal - #[display(fmt = "{BLS_NORMAL}")] BlsNormal, /// BlsSmall - #[display(fmt = "{BLS_SMALL}")] BlsSmall, } } +impl fmt::Display for Algorithm { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let str = match self { + Algorithm::Ed25519 => ED_25519, + Algorithm::Secp256k1 => SECP_256_K1, + Algorithm::BlsNormal => BLS_NORMAL, + Algorithm::BlsSmall => BLS_SMALL, + }; + + write!(fmt, "{str}") + } +} + +impl From for String { + fn from(a: Algorithm) -> Self { + format!("{a}") + } +} + +impl Encode for Algorithm { + fn encode(&self) -> Vec { + let mut ovec: Vec = vec![]; + + let ostr = self.to_string(); + + let ol = ostr.len().try_into().expect("Failed try"); //Just 256 symbols now + + ovec.push(ol); + ovec.append(&mut self.to_string().as_bytes().to_vec()); + + ovec + } +} + +impl Decode for Algorithm { + fn decode(input: &mut I) -> Result { + let mut buf: Vec = vec![0]; //len + + input.read(&mut buf[..])?; + + let lin = buf[0]; + let mut buf: Vec = vec![0; lin as usize]; //data + + input.read(&mut buf[..])?; + + Algorithm::from_str(&String::from_utf8(buf).unwrap()) + .map_err(|_: _| ScaleError::from("Algorithm not supported")) + } +} + +impl serde::Serialize for Algorithm { + fn serialize(&self, serializer: E) -> Result + where + E: serde::Serializer, + { + serializer.serialize_str(&format!("{self}")) + } +} + +impl<'de> serde::Deserialize<'de> for Algorithm { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + use serde::de::Error as _; + + let ast = String::deserialize(deserializer)?; + + //let public_key_str = >::deserialize(deserializer)?; + + Algorithm::from_str(&ast).map_err(D::Error::custom) + } +} + impl Default for Algorithm { fn default() -> Self { Algorithm::Ed25519 @@ -171,7 +244,7 @@ impl KeyGenConfiguration { ffi::ffi_item! { /// Pair of Public and Private keys. - #[derive(Debug, Clone, PartialEq, Eq, Getters, Serialize, FfiType)] + #[derive(Debug, Clone, PartialEq, Eq, Getters, Serialize, Deserialize, FfiType)] #[getset(get = "pub")] pub struct KeyPair { /// Public Key. @@ -194,7 +267,7 @@ pub enum Error { #[display(fmt = "Signing failed. {_0}")] Signing(String), /// Returned when an error occurs during key generation - #[display(fmt = "Key generation failed. {_0}")] + #[display(fmt = "Key generation failed. {_0} sss")] KeyGen(String), /// Returned when an error occurs during digest generation #[display(fmt = "Digest generation failed. {_0}")] @@ -282,7 +355,7 @@ impl KeyPair { SignatureOf::new(key_pair.clone(), &dummy_payload)? .verify(&dummy_payload) - .map_err(|_err| Error::KeyGen(String::from("Key pair mismatch")))?; + .map_err(|_err| Error::KeyGen(String::from("Key pair mismatch sss")))?; Ok(key_pair) } @@ -304,7 +377,7 @@ impl KeyPair { /// Fails if decoding fails #[cfg(feature = "std")] pub fn generate_with_configuration(configuration: KeyGenConfiguration) -> Result { - let digest_function: ConstString = configuration.algorithm.to_string().into(); + let digest_function: Algorithm = configuration.algorithm; let key_gen_option: Option = configuration .key_gen_option @@ -319,7 +392,7 @@ impl KeyPair { Ok(Self { public_key: PublicKey { - digest_function: digest_function.clone(), + digest_function, payload: core::mem::take(&mut public_key.0), }, private_key: PrivateKey { @@ -330,25 +403,6 @@ impl KeyPair { } } -#[cfg(feature = "std")] -impl<'de> Deserialize<'de> for KeyPair { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - use serde::de::Error as _; - - #[derive(Deserialize)] - struct KeyPair { - public_key: PublicKey, - private_key: PrivateKey, - } - - let key_pair = KeyPair::deserialize(deserializer)?; - Self::new(key_pair.public_key, key_pair.private_key).map_err(D::Error::custom) - } -} - impl From for (PublicKey, PrivateKey) { fn from(key_pair: KeyPair) -> Self { (key_pair.public_key, key_pair.private_key) @@ -372,14 +426,15 @@ impl std::error::Error for KeyParseError {} ffi::ffi_item! { /// Public Key used in signatures. - #[derive(DebugCustom, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, FfiType, IntoSchema)] + #[derive(DebugCustom, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, FfiType, IntoSchema, Serialize, Deserialize, Decode)] #[debug( fmt = "{{ digest: {digest_function}, payload: {} }}", "hex::encode_upper(payload.as_slice())" )] pub struct PublicKey { /// Digest function - digest_function: ConstString, + digest_function: Algorithm, + #[serde(with = "hex::serde")] /// Key payload payload: Vec, } @@ -399,7 +454,7 @@ impl PublicKey { /// Digest function #[allow(clippy::expect_used)] pub fn digest_function(&self) -> Algorithm { - self.digest_function.parse().expect("Valid") + self.digest_function } } @@ -443,7 +498,7 @@ impl From for PublicKey { }; Self { - digest_function: digest_function.to_string().into(), + digest_function, payload: multihash.payload, } } @@ -488,56 +543,15 @@ impl From for Multihash { } } -impl Serialize for PublicKey { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_str(&format!("{self}")) - } -} - -impl<'de> Deserialize<'de> for PublicKey { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - #[cfg(not(feature = "std"))] - use alloc::borrow::Cow; - #[cfg(feature = "std")] - use std::borrow::Cow; - - use serde::de::Error as _; - - let public_key_str = >::deserialize(deserializer)?; - PublicKey::from_str(&public_key_str).map_err(D::Error::custom) - } -} - -impl Decode for PublicKey { - fn decode(input: &mut I) -> Result { - let digest_function = ConstString::decode(input)?; - - if Algorithm::from_str(&digest_function).is_err() { - return Err(ScaleError::from("Algorithm not supported")); - } - - Ok(Self { - digest_function, - payload: Decode::decode(input)?, - }) - } -} - ffi::ffi_item! { /// Private Key used in signatures. - #[derive(DebugCustom, Clone, PartialEq, Eq, Display, Serialize, FfiType)] + #[derive(DebugCustom, Clone, PartialEq, Eq, Display, Serialize, Deserialize, FfiType)] #[debug(fmt = "{{ digest: {digest_function}, payload: {payload:X?}}}")] #[display(fmt = "{}", "hex::encode(payload)")] #[allow(clippy::multiple_inherent_impl)] pub struct PrivateKey { /// Digest function - digest_function: ConstString, + digest_function: Algorithm, /// Key payload. WARNING! Do not use `"string".as_bytes()` to obtain the key. #[serde(with = "hex::serde")] payload: Vec, @@ -558,7 +572,7 @@ impl PrivateKey { /// Digest function #[allow(clippy::expect_used)] pub fn digest_function(&self) -> Algorithm { - self.digest_function.parse().expect("Valid") + self.digest_function } } @@ -583,37 +597,12 @@ impl PrivateKey { .collect(); Ok(Self { - digest_function: digest_function.to_string().into(), + digest_function, payload: hex::decode(payload)?, }) } } -impl<'de> Deserialize<'de> for PrivateKey { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - use serde::de::Error as _; - - #[derive(Deserialize)] - struct PrivateKey { - digest_function: ConstString, - #[serde(with = "hex::serde")] - payload: Vec, - } - - let private_key = PrivateKey::deserialize(deserializer)?; - match Algorithm::from_str(&private_key.digest_function) { - Ok(_) => Ok(Self { - digest_function: private_key.digest_function, - payload: private_key.payload, - }), - Err(err) => Err(D::Error::custom(err)), - } - } -} - pub mod ffi { //! Definitions and implementations of FFI related functionalities @@ -661,8 +650,76 @@ mod tests { #[cfg(not(feature = "std"))] use alloc::borrow::ToString as _; + use parity_scale_codec::{Decode, Encode}; + use super::*; + #[test] + fn algorithm_serialize_deserialize_consistent() { + for algorithm in [ + Algorithm::Ed25519, + Algorithm::Secp256k1, + Algorithm::BlsNormal, + Algorithm::BlsSmall, + ] { + assert_eq!( + algorithm, + serde_json::to_string(&algorithm) + .and_then(|algorithm| serde_json::from_str(&algorithm)) + .unwrap_or_else(|_| panic!("Failed to de/serialize key {:?}", &algorithm)) + ) + } + } + #[test] + fn key_pair_serialize_deserialize_consistent() { + for algorithm in [ + Algorithm::Ed25519, + Algorithm::Secp256k1, + Algorithm::BlsNormal, + Algorithm::BlsSmall, + ] { + let key_pair = KeyPair::generate_with_configuration( + KeyGenConfiguration::default().with_algorithm(algorithm), + ) + .expect("Failed to generate key pair"); + + assert_eq!( + key_pair, + serde_json::to_string(&key_pair) + .and_then(|key_pair| serde_json::from_str(&key_pair)) + .unwrap_or_else(|_| panic!("Failed to de/serialize key {:?}", &key_pair)) + ) + } + } + + #[test] + fn encode_decode_algorithm_consistent() { + for algorithm in [ + Algorithm::Ed25519, + Algorithm::Secp256k1, + Algorithm::BlsNormal, + Algorithm::BlsSmall, + ] { + let ee = algorithm.encode(); + + let mut a: &[u8] = ⅇ + + let _s = match std::str::from_utf8(a) { + Ok(v) => v, + Err(e) => panic!("Invalid UTF-8 sequence: {e}"), + }; + + let uu = Algorithm::decode(&mut a); + + let s = match uu { + Ok(v) => v, + Err(e) => panic!("Incorrect param: {e}"), + }; + + assert_eq!(algorithm, s, "Failed to decode encoded {:?}", &algorithm) + } + } + #[test] fn key_pair_match() { assert!(KeyPair::new("ed012059c8a4da1ebb5380f74aba51f502714652fdcce9611fafb9904e4a3c4d382774" @@ -682,6 +739,38 @@ mod tests { ).expect("Private key not hex encoded")).is_ok()); } + #[test] + fn encode_decode_public_key_consistent() { + for algorithm in [ + Algorithm::Ed25519, + Algorithm::Secp256k1, + Algorithm::BlsNormal, + Algorithm::BlsSmall, + ] { + let key_pair = KeyPair::generate_with_configuration( + KeyGenConfiguration::default().with_algorithm(algorithm), + ) + .expect("Failed to generate key pair"); + + let ee = key_pair.public_key.encode(); + + let mut a: &[u8] = ⅇ + + let uu = PublicKey::decode(&mut a); + + let s = match uu { + Ok(v) => v, + Err(e) => panic!("Incorrect param: {e}"), + }; + + assert_eq!( + key_pair.public_key, s, + "Failed to decode encoded Public Key{:?}", + &key_pair.public_key + ) + } + } + #[test] fn key_pair_mismatch() { assert!(KeyPair::new("ed012059c8a4da1ebb5380f74aba51f502714652fdcce9611fafb9904e4a3c4d382774" @@ -707,7 +796,7 @@ mod tests { format!( "{}", PublicKey { - digest_function: Algorithm::Ed25519.to_string().into(), + digest_function: Algorithm::Ed25519, payload: hex::decode( "1509a611ad6d97b01d871e58ed00c8fd7c3917b6ca61a8c2833a19e000aac2e4" ) @@ -720,7 +809,7 @@ mod tests { format!( "{}", PublicKey { - digest_function: Algorithm::Secp256k1.to_string().into(), + digest_function: Algorithm::Secp256k1, payload: hex::decode( "0312273e8810581e58948d3fb8f9e8ad53aaa21492ebb8703915bbb565a21b7fcc" ) @@ -733,7 +822,7 @@ mod tests { format!( "{}", PublicKey { - digest_function: Algorithm::BlsNormal.to_string().into(), + digest_function: Algorithm::BlsNormal, payload: hex::decode( "04175b1e79b15e8a2d5893bf7f8933ca7d0863105d8bac3d6f976cb043378a0e4b885c57ed14eb85fc2fabc639adc7de7f0020c70c57acc38dee374af2c04a6f61c11de8df9034b12d849c7eb90099b0881267d0e1507d4365d838d7dcc31511e7" ) @@ -746,7 +835,7 @@ mod tests { format!( "{}", PublicKey { - digest_function: Algorithm::BlsSmall.to_string().into(), + digest_function: Algorithm::BlsSmall, payload: hex::decode( "040cb3231f601e7245a6ec9a647b450936f707ca7dc347ed258586c1924941d8bc38576473a8ba3bb2c37e3e121130ab67103498a96d0d27003e3ad960493da79209cf024e2aa2ae961300976aeee599a31a5e1b683eaa1bcffc47b09757d20f21123c594cf0ee0baf5e1bdd272346b7dc98a8f12c481a6b28174076a352da8eae881b90911013369d7fa960716a5abc5314307463fa2285a5bf2a5b5c6220d68c2d34101a91dbfc531c5b9bbfb2245ccc0c50051f79fc6714d16907b1fc40e0c0" ) @@ -757,17 +846,20 @@ mod tests { ) } - #[derive(Debug, PartialEq, Deserialize)] + #[derive(Debug, PartialEq, Serialize, Deserialize)] struct TestJson { public_key: PublicKey, private_key: PrivateKey, } #[test] - fn deserialize_keys() { + fn deserialize_keys_1() { assert_eq!( - serde_json::from_str::<'_, TestJson>("{ - \"public_key\": \"ed01201509a611ad6d97b01d871e58ed00c8fd7c3917b6ca61a8c2833a19e000aac2e4\", + serde_json::from_str::("{ + \"public_key\": { + \"digest_function\": \"ed25519\", + \"payload\": \"1509a611ad6d97b01d871e58ed00c8fd7c3917b6ca61a8c2833a19e000aac2e4\" + }, \"private_key\": { \"digest_function\": \"ed25519\", \"payload\": \"3a7991af1abb77f3fd27cc148404a6ae4439d095a63591b77c788d53f708a02a1509a611ad6d97b01d871e58ed00c8fd7c3917b6ca61a8c2833a19e000aac2e4\" @@ -775,22 +867,29 @@ mod tests { }").expect("Failed to deserialize."), TestJson { public_key: PublicKey { - digest_function: Algorithm::Ed25519.to_string().into(), + digest_function: Algorithm::Ed25519, payload: hex::decode( "1509a611ad6d97b01d871e58ed00c8fd7c3917b6ca61a8c2833a19e000aac2e4" ) .expect("Failed to decode public key.") }, private_key: PrivateKey { - digest_function: Algorithm::Ed25519.to_string().into(), + digest_function: Algorithm::Ed25519, payload: hex::decode("3a7991af1abb77f3fd27cc148404a6ae4439d095a63591b77c788d53f708a02a1509a611ad6d97b01d871e58ed00c8fd7c3917b6ca61a8c2833a19e000aac2e4") .expect("Failed to decode private key"), } } ); + } + + #[test] + fn deserialize_keys_2() { assert_eq!( serde_json::from_str::<'_, TestJson>("{ - \"public_key\": \"e701210312273e8810581e58948d3fb8f9e8ad53aaa21492ebb8703915bbb565a21b7fcc\", + \"public_key\": { + \"digest_function\": \"secp256k1\", + \"payload\": \"0312273e8810581e58948d3fb8f9e8ad53aaa21492ebb8703915bbb565a21b7fcc\" + }, \"private_key\": { \"digest_function\": \"secp256k1\", \"payload\": \"4df4fca10762d4b529fe40a2188a60ca4469d2c50a825b5f33adc2cb78c69445\" @@ -798,22 +897,29 @@ mod tests { }").expect("Failed to deserialize."), TestJson { public_key: PublicKey { - digest_function: Algorithm::Secp256k1.to_string().into(), + digest_function: Algorithm::Secp256k1, payload: hex::decode( "0312273e8810581e58948d3fb8f9e8ad53aaa21492ebb8703915bbb565a21b7fcc" ) .expect("Failed to decode public key.") }, private_key: PrivateKey { - digest_function: Algorithm::Secp256k1.to_string().into(), + digest_function: Algorithm::Secp256k1, payload: hex::decode("4df4fca10762d4b529fe40a2188a60ca4469d2c50a825b5f33adc2cb78c69445") .expect("Failed to decode private key"), } } ); + } + + #[test] + fn deserialize_keys_3() { assert_eq!( serde_json::from_str::<'_, TestJson>("{ - \"public_key\": \"ea016104175b1e79b15e8a2d5893bf7f8933ca7d0863105d8bac3d6f976cb043378a0e4b885c57ed14eb85fc2fabc639adc7de7f0020c70c57acc38dee374af2c04a6f61c11de8df9034b12d849c7eb90099b0881267d0e1507d4365d838d7dcc31511e7\", + \"public_key\": { + \"digest_function\": \"bls_normal\", + \"payload\": \"04175b1e79b15e8a2d5893bf7f8933ca7d0863105d8bac3d6f976cb043378a0e4b885c57ed14eb85fc2fabc639adc7de7f0020c70c57acc38dee374af2c04a6f61c11de8df9034b12d849c7eb90099b0881267d0e1507d4365d838d7dcc31511e7\" + }, \"private_key\": { \"digest_function\": \"bls_normal\", \"payload\": \"000000000000000000000000000000002f57460183837efbac6aa6ab3b8dbb7cffcfc59e9448b7860a206d37d470cba3\" @@ -821,14 +927,14 @@ mod tests { }").expect("Failed to deserialize."), TestJson { public_key: PublicKey { - digest_function: Algorithm::BlsNormal.to_string().into(), + digest_function: Algorithm::BlsNormal, payload: hex::decode( "04175b1e79b15e8a2d5893bf7f8933ca7d0863105d8bac3d6f976cb043378a0e4b885c57ed14eb85fc2fabc639adc7de7f0020c70c57acc38dee374af2c04a6f61c11de8df9034b12d849c7eb90099b0881267d0e1507d4365d838d7dcc31511e7" ) .expect("Failed to decode public key.") }, private_key: PrivateKey { - digest_function: Algorithm::BlsNormal.to_string().into(), + digest_function: Algorithm::BlsNormal, payload: hex::decode("000000000000000000000000000000002f57460183837efbac6aa6ab3b8dbb7cffcfc59e9448b7860a206d37d470cba3") .expect("Failed to decode private key"), } @@ -836,7 +942,10 @@ mod tests { ); assert_eq!( serde_json::from_str::<'_, TestJson>("{ - \"public_key\": \"eb01c1040cb3231f601e7245a6ec9a647b450936f707ca7dc347ed258586c1924941d8bc38576473a8ba3bb2c37e3e121130ab67103498a96d0d27003e3ad960493da79209cf024e2aa2ae961300976aeee599a31a5e1b683eaa1bcffc47b09757d20f21123c594cf0ee0baf5e1bdd272346b7dc98a8f12c481a6b28174076a352da8eae881b90911013369d7fa960716a5abc5314307463fa2285a5bf2a5b5c6220d68c2d34101a91dbfc531c5b9bbfb2245ccc0c50051f79fc6714d16907b1fc40e0c0\", + \"public_key\": { + \"digest_function\": \"bls_small\", + \"payload\": \"040cb3231f601e7245a6ec9a647b450936f707ca7dc347ed258586c1924941d8bc38576473a8ba3bb2c37e3e121130ab67103498a96d0d27003e3ad960493da79209cf024e2aa2ae961300976aeee599a31a5e1b683eaa1bcffc47b09757d20f21123c594cf0ee0baf5e1bdd272346b7dc98a8f12c481a6b28174076a352da8eae881b90911013369d7fa960716a5abc5314307463fa2285a5bf2a5b5c6220d68c2d34101a91dbfc531c5b9bbfb2245ccc0c50051f79fc6714d16907b1fc40e0c0\" + }, \"private_key\": { \"digest_function\": \"bls_small\", \"payload\": \"0000000000000000000000000000000060f3c1ac9addbbed8db83bc1b2ef22139fb049eecb723a557a41ca1a4b1fed63\" @@ -844,14 +953,14 @@ mod tests { }").expect("Failed to deserialize."), TestJson { public_key: PublicKey { - digest_function: Algorithm::BlsSmall.to_string().into(), + digest_function: Algorithm::BlsSmall, payload: hex::decode( "040cb3231f601e7245a6ec9a647b450936f707ca7dc347ed258586c1924941d8bc38576473a8ba3bb2c37e3e121130ab67103498a96d0d27003e3ad960493da79209cf024e2aa2ae961300976aeee599a31a5e1b683eaa1bcffc47b09757d20f21123c594cf0ee0baf5e1bdd272346b7dc98a8f12c481a6b28174076a352da8eae881b90911013369d7fa960716a5abc5314307463fa2285a5bf2a5b5c6220d68c2d34101a91dbfc531c5b9bbfb2245ccc0c50051f79fc6714d16907b1fc40e0c0" ) .expect("Failed to decode public key.") }, private_key: PrivateKey { - digest_function: Algorithm::BlsSmall.to_string().into(), + digest_function: Algorithm::BlsSmall, payload: hex::decode("0000000000000000000000000000000060f3c1ac9addbbed8db83bc1b2ef22139fb049eecb723a557a41ca1a4b1fed63") .expect("Failed to decode private key"), } diff --git a/docs/source/references/schema.json b/docs/source/references/schema.json index c2f0907b2a2..760833977da 100644 --- a/docs/source/references/schema.json +++ b/docs/source/references/schema.json @@ -547,12 +547,38 @@ ] } }, + "iroha_crypto::Algorithm": { + "Enum": { + "variants": [ + { + "name": "Ed25519", + "discriminant": 0, + "ty": null + }, + { + "name": "Secp256k1", + "discriminant": 1, + "ty": null + }, + { + "name": "BlsNormal", + "discriminant": 2, + "ty": null + }, + { + "name": "BlsSmall", + "discriminant": 3, + "ty": null + } + ] + } + }, "iroha_crypto::PublicKey": { "Struct": { "declarations": [ { "name": "digest_function", - "ty": "String" + "ty": "iroha_crypto::Algorithm" }, { "name": "payload", diff --git a/tools/parity_scale_decoder/src/generate_map.rs b/tools/parity_scale_decoder/src/generate_map.rs index da9c831d151..2dae59f33ea 100644 --- a/tools/parity_scale_decoder/src/generate_map.rs +++ b/tools/parity_scale_decoder/src/generate_map.rs @@ -204,6 +204,7 @@ pub fn generate_map() -> DumpDecodedMap { IfInstruction, Instruction, InstructionExecutionFail, + iroha_crypto::Algorithm, Less, LengthLimits, Metadata,