diff --git a/substrate/primitives/core/Cargo.toml b/substrate/primitives/core/Cargo.toml index 83ef683a39..ddffb92ec5 100644 --- a/substrate/primitives/core/Cargo.toml +++ b/substrate/primitives/core/Cargo.toml @@ -33,7 +33,6 @@ parking_lot = { version = "0.10.0", optional = true } sp-debug-derive = { version = "2.0.0-alpha.2", path = "../debug-derive" } sp-externalities = { version = "0.8.0-alpha.2", optional = true, path = "../externalities" } sp-storage = { version = "2.0.0-alpha.2", default-features = false, path = "../storage" } -libsecp256k1 = { version = "0.3.2", default-features = false, features = ["hmac"] } parity-util-mem = { version = "0.5.1", default-features = false, features = ["primitive-types"] } # full crypto @@ -44,6 +43,7 @@ schnorrkel = { version = "0.8.5", features = ["preaudit_deprecated", "u64_backen sha2 = { version = "0.8.0", default-features = false, optional = true } hex = { version = "0.4", default-features = false, optional = true } twox-hash = { version = "1.5.0", default-features = false, optional = true } +libsecp256k1 = { version = "0.3.2", default-features = false, features = ["hmac"], optional = true } sp-runtime-interface = { version = "2.0.0-alpha.2", default-features = false, path = "../runtime-interface" } @@ -116,5 +116,6 @@ full_crypto = [ "hex", "sha2", "twox-hash", + "libsecp256k1", "sp-runtime-interface/disable_target_static_assertions", ] diff --git a/substrate/primitives/core/src/ecdsa.rs b/substrate/primitives/core/src/ecdsa.rs index 99db0e6b2d..548ae49b55 100644 --- a/substrate/primitives/core/src/ecdsa.rs +++ b/substrate/primitives/core/src/ecdsa.rs @@ -92,6 +92,17 @@ impl Public { pub fn from_raw(data: [u8; 33]) -> Self { Self(data) } + + /// Create a new instance from the given full public key. + /// + /// This will convert the full public key into the compressed format. + #[cfg(feature = "std")] + pub fn from_full(full: &[u8]) -> Result { + secp256k1::PublicKey::parse_slice(full, None) + .map(|k| k.serialize_compressed()) + .map(Self) + .map_err(|_| ()) + } } impl TraitPublic for Public { @@ -133,10 +144,8 @@ impl sp_std::convert::TryFrom<&[u8]> for Public { if data.len() == 33 { Ok(Self::from_slice(data)) } else { - secp256k1::PublicKey::parse_slice(data, None) - .map(|k| k.serialize_compressed()) - .map(Self) - .map_err(|_| ()) + + Err(()) } } } @@ -544,7 +553,7 @@ mod test { let public = pair.public(); assert_eq!( public, - Public::try_from( + Public::from_full( &hex!("8db55b05db86c0b1786ca49f095d76344c9e6056b2f02701a7e7f3c20aabfd913ebbe148dd17c56551a52952371071a6c604b3f3abe8f2c8fa742158ea6dd7d4")[..], ).unwrap(), ); @@ -564,7 +573,7 @@ mod test { let public = pair.public(); assert_eq!( public, - Public::try_from( + Public::from_full( &hex!("8db55b05db86c0b1786ca49f095d76344c9e6056b2f02701a7e7f3c20aabfd913ebbe148dd17c56551a52952371071a6c604b3f3abe8f2c8fa742158ea6dd7d4")[..], ).unwrap(), ); @@ -591,7 +600,7 @@ mod test { let public = pair.public(); assert_eq!( public, - Public::try_from( + Public::from_full( &hex!("5676109c54b9a16d271abeb4954316a40a32bcce023ac14c8e26e958aa68fba995840f3de562156558efbfdac3f16af0065e5f66795f4dd8262a228ef8c6d813")[..], ).unwrap(), );