From 75be6e2a6cc86cfa11390a05f113a00c270cdb24 Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Mon, 26 Jun 2023 04:40:03 +0900 Subject: [PATCH] Remove k256 crate from frame-support dependencies (#14452) * Add test for to_eth_address * Replace k256 with secp256k1 * Bump Cargo.lock * Reformat --- Cargo.lock | 208 +++++------------------------- Cargo.toml | 1 - frame/support/Cargo.toml | 5 +- frame/support/src/crypto/ecdsa.rs | 28 +++- 4 files changed, 53 insertions(+), 189 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8d374c15df0a..d9fe360a25485 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -776,12 +776,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" -[[package]] -name = "base16ct" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" - [[package]] name = "base64" version = "0.13.1" @@ -1702,18 +1696,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "crypto-bigint" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4c2f4e1afd912bc40bfd6fed5d9dc1f288e0ba01bfcc835cc5bc3eb13efe15" -dependencies = [ - "generic-array 0.14.7", - "rand_core 0.6.4", - "subtle", - "zeroize", -] - [[package]] name = "crypto-common" version = "0.1.6" @@ -1929,16 +1911,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "der" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56acb310e15652100da43d130af8d97b509e95af61aab1c5a7939ef24337ee17" -dependencies = [ - "const-oid", - "zeroize", -] - [[package]] name = "der-parser" version = "7.0.0" @@ -2068,7 +2040,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer 0.10.4", - "const-oid", "crypto-common", "subtle", ] @@ -2215,24 +2186,10 @@ version = "0.14.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "413301934810f597c1d19ca71c8710e99a3f1ba28a0d2ebc01551a2daeea3c5c" dependencies = [ - "der 0.6.1", - "elliptic-curve 0.12.3", - "rfc6979 0.3.1", - "signature 1.6.4", -] - -[[package]] -name = "ecdsa" -version = "0.16.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0997c976637b606099b9985693efa3581e84e41f5c11ba5255f88711058ad428" -dependencies = [ - "der 0.7.6", - "digest 0.10.7", - "elliptic-curve 0.13.5", - "rfc6979 0.4.0", - "signature 2.1.0", - "spki 0.7.2", + "der", + "elliptic-curve", + "rfc6979", + "signature", ] [[package]] @@ -2241,7 +2198,7 @@ version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91cff35c70bba8a626e3185d8cd48cc11b5437e1a5bcd15b9b5fa3c64b6dfee7" dependencies = [ - "signature 1.6.4", + "signature", ] [[package]] @@ -2284,37 +2241,18 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7bb888ab5300a19b8e5bceef25ac745ad065f3c9f7efc6de1b91958110891d3" dependencies = [ - "base16ct 0.1.1", - "crypto-bigint 0.4.9", - "der 0.6.1", + "base16ct", + "crypto-bigint", + "der", "digest 0.10.7", - "ff 0.12.1", + "ff", "generic-array 0.14.7", - "group 0.12.1", + "group", "hkdf", "pem-rfc7468", - "pkcs8 0.9.0", + "pkcs8", "rand_core 0.6.4", - "sec1 0.3.0", - "subtle", - "zeroize", -] - -[[package]] -name = "elliptic-curve" -version = "0.13.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "968405c8fdc9b3bf4df0a6638858cc0b52462836ab6b1c87377785dd09cf1c0b" -dependencies = [ - "base16ct 0.2.0", - "crypto-bigint 0.5.2", - "digest 0.10.7", - "ff 0.13.0", - "generic-array 0.14.7", - "group 0.13.0", - "pkcs8 0.10.2", - "rand_core 0.6.4", - "sec1 0.7.2", + "sec1", "subtle", "zeroize", ] @@ -2478,16 +2416,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "ff" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" -dependencies = [ - "rand_core 0.6.4", - "subtle", -] - [[package]] name = "fiat-crypto" version = "0.1.20" @@ -2807,6 +2735,7 @@ dependencies = [ name = "frame-support" version = "4.0.0-dev" dependencies = [ + "array-bytes 4.2.0", "assert_matches", "bitflags", "environmental", @@ -2814,13 +2743,13 @@ dependencies = [ "frame-support-procedural", "frame-system", "impl-trait-for-tuples", - "k256", "log", "macro_magic", "parity-scale-codec", "paste", "pretty_assertions", "scale-info", + "secp256k1", "serde", "serde_json", "smallvec", @@ -3182,7 +3111,6 @@ checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", - "zeroize", ] [[package]] @@ -3275,18 +3203,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5dfbfb3a6cfbd390d5c9564ab283a0349b9b9fcd46a706c1eb10e0db70bfbac7" dependencies = [ - "ff 0.12.1", - "rand_core 0.6.4", - "subtle", -] - -[[package]] -name = "group" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" -dependencies = [ - "ff 0.13.0", + "ff", "rand_core 0.6.4", "subtle", ] @@ -3997,19 +3914,6 @@ dependencies = [ "jsonrpsee-types", ] -[[package]] -name = "k256" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cadb76004ed8e97623117f3df85b17aaa6626ab0b0831e6573f104df16cd1bcc" -dependencies = [ - "cfg-if", - "ecdsa 0.16.7", - "elliptic-curve 0.13.5", - "once_cell", - "sha2 0.10.6", -] - [[package]] name = "keccak" version = "0.1.4" @@ -5928,8 +5832,8 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51f44edd08f51e2ade572f141051021c5af22677e42b7dd28a88155151c33594" dependencies = [ - "ecdsa 0.14.8", - "elliptic-curve 0.12.3", + "ecdsa", + "elliptic-curve", "sha2 0.10.6", ] @@ -5939,8 +5843,8 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc8c5bf642dde52bb9e87c0ecd8ca5a76faac2eeed98dedb7c717997e1080aa" dependencies = [ - "ecdsa 0.14.8", - "elliptic-curve 0.12.3", + "ecdsa", + "elliptic-curve", "sha2 0.10.6", ] @@ -7930,18 +7834,8 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9eca2c590a5f85da82668fa685c09ce2888b9430e83299debf1f34b65fd4a4ba" dependencies = [ - "der 0.6.1", - "spki 0.6.0", -] - -[[package]] -name = "pkcs8" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" -dependencies = [ - "der 0.7.6", - "spki 0.7.2", + "der", + "spki", ] [[package]] @@ -8622,21 +8516,11 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7743f17af12fa0b03b803ba12cd6a8d9483a587e89c69445e3909655c0b9fabb" dependencies = [ - "crypto-bigint 0.4.9", + "crypto-bigint", "hmac 0.12.1", "zeroize", ] -[[package]] -name = "rfc6979" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" -dependencies = [ - "hmac 0.12.1", - "subtle", -] - [[package]] name = "ring" version = "0.16.20" @@ -10453,24 +10337,10 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3be24c1842290c45df0a7bf069e0c268a747ad05a192f2fd7dcfdbc1cba40928" dependencies = [ - "base16ct 0.1.1", - "der 0.6.1", + "base16ct", + "der", "generic-array 0.14.7", - "pkcs8 0.9.0", - "subtle", - "zeroize", -] - -[[package]] -name = "sec1" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0aec48e813d6b90b15f0b8948af3c63483992dee44c03e9930b3eebdabe046e" -dependencies = [ - "base16ct 0.2.0", - "der 0.7.6", - "generic-array 0.14.7", - "pkcs8 0.10.2", + "pkcs8", "subtle", "zeroize", ] @@ -10702,16 +10572,6 @@ dependencies = [ "rand_core 0.6.4", ] -[[package]] -name = "signature" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e1788eed21689f9cf370582dfc467ef36ed9c707f073528ddafa8d83e3b8500" -dependencies = [ - "digest 0.10.7", - "rand_core 0.6.4", -] - [[package]] name = "simba" version = "0.8.1" @@ -11792,17 +11652,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67cf02bbac7a337dc36e4f5a693db6c21e7863f45070f7064577eb4367a3212b" dependencies = [ "base64ct", - "der 0.6.1", -] - -[[package]] -name = "spki" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d1e996ef02c474957d681f1b05213dfb0abab947b446a62d37770b23500184a" -dependencies = [ - "base64ct", - "der 0.7.6", + "der", ] [[package]] @@ -13719,7 +13569,7 @@ dependencies = [ "ccm", "curve25519-dalek 3.2.0", "der-parser 8.2.0", - "elliptic-curve 0.12.3", + "elliptic-curve", "hkdf", "hmac 0.12.1", "log", @@ -13731,11 +13581,11 @@ dependencies = [ "rcgen 0.9.3", "ring", "rustls 0.19.1", - "sec1 0.3.0", + "sec1", "serde", "sha1", "sha2 0.10.6", - "signature 1.6.4", + "signature", "subtle", "thiserror", "tokio", diff --git a/Cargo.toml b/Cargo.toml index e0887b0007289..8e2ba85f43088 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -298,7 +298,6 @@ hash-db = { opt-level = 3 } hmac = { opt-level = 3 } httparse = { opt-level = 3 } integer-sqrt = { opt-level = 3 } -k256 = { opt-level = 3 } keccak = { opt-level = 3 } libm = { opt-level = 3 } librocksdb-sys = { opt-level = 3 } diff --git a/frame/support/Cargo.toml b/frame/support/Cargo.toml index 6fbb0a026ed96..d95c2d7a45d7b 100644 --- a/frame/support/Cargo.toml +++ b/frame/support/Cargo.toml @@ -38,7 +38,7 @@ impl-trait-for-tuples = "0.2.2" smallvec = "1.8.0" log = { version = "0.4.17", default-features = false } sp-core-hashing-proc-macro = { version = "9.0.0", path = "../../primitives/core/hashing/proc-macro" } -k256 = { version = "0.13.0", default-features = false, features = ["ecdsa"] } +secp256k1 = { version = "0.24.0", default-features = false } environmental = { version = "1.1.4", default-features = false } [dev-dependencies] @@ -46,12 +46,13 @@ serde_json = "1.0.85" assert_matches = "1.3.0" pretty_assertions = "1.2.1" frame-system = { version = "4.0.0-dev", path = "../system" } +array-bytes = "4.1" [features] default = ["std"] std = [ "sp-core/std", - "k256/std", + "secp256k1/std", "serde/std", "sp-api/std", "sp-io/std", diff --git a/frame/support/src/crypto/ecdsa.rs b/frame/support/src/crypto/ecdsa.rs index afa45e296d3fd..ac817fa8c6afd 100644 --- a/frame/support/src/crypto/ecdsa.rs +++ b/frame/support/src/crypto/ecdsa.rs @@ -34,16 +34,30 @@ pub trait ECDSAExt { impl ECDSAExt for Public { fn to_eth_address(&self) -> Result<[u8; 20], ()> { - use k256::{elliptic_curve::sec1::ToEncodedPoint, PublicKey}; + use secp256k1::PublicKey; - PublicKey::from_sec1_bytes(self.as_slice()).map_err(drop).and_then(|pub_key| { + PublicKey::from_slice(self.as_slice()).map_err(drop).and_then(|pub_key| { // uncompress the key - let uncompressed = pub_key.to_encoded_point(false); + let uncompressed = pub_key.serialize_uncompressed(); // convert to ETH address - <[u8; 20]>::try_from( - sp_io::hashing::keccak_256(&uncompressed.as_bytes()[1..])[12..].as_ref(), - ) - .map_err(drop) + <[u8; 20]>::try_from(sp_io::hashing::keccak_256(&uncompressed[1..])[12..].as_ref()) + .map_err(drop) }) } } + +#[cfg(test)] +mod tests { + use super::*; + use sp_core::{ecdsa, Pair}; + + #[test] + fn to_eth_address_works() { + let pair = ecdsa::Pair::from_string("//Alice//password", None).unwrap(); + let eth_address = pair.public().to_eth_address().unwrap(); + assert_eq!( + array_bytes::bytes2hex("0x", ð_address), + "0xdc1cce4263956850a3c8eb349dc6fc3f7792cb27" + ); + } +}