Remove k256 crate from frame-support dependencies (#14452)

* Add test for to_eth_address

* Replace k256 with secp256k1

* Bump Cargo.lock

* Reformat
This commit is contained in:
Jeeyong Um
2023-06-26 04:40:03 +09:00
committed by GitHub
parent 15bd9e6fa7
commit 4d426214af
4 changed files with 53 additions and 189 deletions
+3 -2
View File
@@ -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",
+21 -7
View File
@@ -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", &eth_address),
"0xdc1cce4263956850a3c8eb349dc6fc3f7792cb27"
);
}
}