From 6d9b80dacb218134cec74bb1eda33b6959f9ea26 Mon Sep 17 00:00:00 2001 From: shuimuliang Date: Mon, 4 Mar 2024 03:02:40 +0800 Subject: [PATCH] fix: upgrade ed25519-dalek-fiat, anychain-core, sp-core for anychain-polkadot --- anychain-polkadot/Cargo.toml | 8 ++--- anychain-polkadot/src/address.rs | 19 +++++++--- anychain-polkadot/src/public_key.rs | 9 ++--- anychain-polkadot/src/transaction.rs | 52 +++++++++++++++++++--------- 4 files changed, 58 insertions(+), 30 deletions(-) diff --git a/anychain-polkadot/Cargo.toml b/anychain-polkadot/Cargo.toml index 56222a6..1fbfb47 100644 --- a/anychain-polkadot/Cargo.toml +++ b/anychain-polkadot/Cargo.toml @@ -12,10 +12,10 @@ license = { workspace = true } repository = { workspace = true } [dependencies] -anychain-core = { path = "../anychain-core", version = "0.1.1" } -sp-core = "24.0.0" +anychain-core = { path = "../anychain-core", version = "0.1.3" } +sp-core = "28.0.0" base58 = { workspace = true } parity-scale-codec = { version = "3.6.5" } -ed25519-dalek-fiat = "0.1.0" +ed25519-dalek = "2.1.1" serde_json = { workspace = true } -rand.workspace = true +rand = { workspace = true } diff --git a/anychain-polkadot/src/address.rs b/anychain-polkadot/src/address.rs index da28546..11796be 100644 --- a/anychain-polkadot/src/address.rs +++ b/anychain-polkadot/src/address.rs @@ -94,7 +94,7 @@ mod tests { use crate::{PolkadotAddress, PolkadotFormat, PolkadotSecretKey, Westend}; use anychain_core::{hex, Address}; - use ed25519_dalek_fiat::SecretKey; + use ed25519_dalek::SecretKey; #[test] fn test_address() { @@ -105,20 +105,26 @@ mod tests { let h = hex::encode(sk); println!("{}", h); - let sk = SecretKey::from_bytes(&sk).unwrap(); + let sk: SecretKey = sk[..ed25519_dalek::SECRET_KEY_LENGTH].try_into().unwrap(); let sk = PolkadotSecretKey::Ed25519(sk); let address = PolkadotAddress::::from_secret_key(&sk, &PolkadotFormat::Standard).unwrap(); - println!("address = {}", address); + assert_eq!( + "5EpHX5foDtnhZngj4GsKq5eKGpUvuMqbpUG48ZfCCCs7EzKR", + address.addr + ); } #[test] fn test_address_2() { let hash = "8ee504148e75c34e8f051899b3c6e4241ff18dc1c9211260b6a6a434bedb485f"; let address = PolkadotAddress::::from_payload(hash).unwrap(); - println!("address = {}", address); + assert_eq!( + "5FJ4gu9eVX6DG4qYi1hxkUgu1yaTm1CnQ4MiiZPjPVaXiATo", + address.addr + ); } #[test] @@ -127,6 +133,9 @@ mod tests { let addr = PolkadotAddress::::from_str(addr).unwrap(); let payload = addr.to_payload().unwrap(); let payload = hex::encode(payload); - println!("{}", payload); + assert_eq!( + "4ce05abd387b560855a3d486eba6237b9a08c6e9dfe351302a5ceda90be801fe", + payload + ); } } diff --git a/anychain-polkadot/src/public_key.rs b/anychain-polkadot/src/public_key.rs index acb3ac9..67e36cf 100644 --- a/anychain-polkadot/src/public_key.rs +++ b/anychain-polkadot/src/public_key.rs @@ -5,13 +5,13 @@ use std::{fmt::Display, marker::PhantomData, str::FromStr}; pub enum PolkadotSecretKey { Secp256k1(libsecp256k1::SecretKey), - Ed25519(ed25519_dalek_fiat::SecretKey), + Ed25519(ed25519_dalek::SecretKey), } #[derive(Debug, Clone)] pub enum PublicKeyContent { Secp256k1(libsecp256k1::PublicKey), - Ed25519(ed25519_dalek_fiat::PublicKey), + Ed25519(ed25519_dalek::VerifyingKey), } #[derive(Debug, Clone)] @@ -36,8 +36,9 @@ impl PublicKey for PolkadotPublicKey { } } Self::SecretKey::Ed25519(sk) => { - let pk = ed25519_dalek_fiat::PublicKey::from(sk); - let pk = PublicKeyContent::Ed25519(pk); + let signing_key = ed25519_dalek::SigningKey::from_bytes(sk); + let verifying_key = signing_key.verifying_key(); + let pk = PublicKeyContent::Ed25519(verifying_key); Self { key: pk, _network: PhantomData, diff --git a/anychain-polkadot/src/transaction.rs b/anychain-polkadot/src/transaction.rs index 0d52a40..929dc07 100644 --- a/anychain-polkadot/src/transaction.rs +++ b/anychain-polkadot/src/transaction.rs @@ -245,6 +245,7 @@ mod tests { }; use anychain_core::Address; use anychain_core::{hex, libsecp256k1, Transaction}; + use ed25519_dalek::{SecretKey, Signature, Signer}; use serde_json::Value; use std::str::FromStr; @@ -301,7 +302,14 @@ mod tests { let from = PolkadotAddress::::from_secret_key(&sk_from, format).unwrap(); let to = PolkadotAddress::::from_secret_key(&sk_to, format).unwrap(); - println!("from = {}\nto = {}", from, to); + assert_eq!( + "5FnS6tYbCTAtK3QCfNnddwVR61ypLLM7APRrs98paFs7yMSY", + from.to_string() + ); + assert_eq!( + "5DoW9HHuqSfpf55Ux5pLdJbHFWvbngeg8Ynhub9DrdtxmZeV", + to.to_string() + ); } #[test] @@ -318,8 +326,12 @@ mod tests { 5, 8, 13, 17, 29, ]; - let sk_from = ed25519_dalek_fiat::SecretKey::from_bytes(&sk_from).unwrap(); - let sk_to = ed25519_dalek_fiat::SecretKey::from_bytes(&sk_to).unwrap(); + let sk_from: SecretKey = sk_from[..ed25519_dalek::SECRET_KEY_LENGTH] + .try_into() + .unwrap(); + let sk_to: SecretKey = sk_to[..ed25519_dalek::SECRET_KEY_LENGTH] + .try_into() + .unwrap(); let sk_from = PolkadotSecretKey::Ed25519(sk_from); let sk_to = PolkadotSecretKey::Ed25519(sk_to); @@ -327,7 +339,14 @@ mod tests { let from = PolkadotAddress::::from_secret_key(&sk_from, format).unwrap(); let to = PolkadotAddress::::from_secret_key(&sk_to, format).unwrap(); - println!("from = {}\nto = {}", from, to); + assert_eq!( + "5DPaKszR7KpCbvNNtGCGTfrGdeDTUNRt1UdxwXp9G6iWvdk7", + from.to_string() + ); + assert_eq!( + "5D1NKGqfc2Q8hw53icrX74YQryjb3MMySWwFBhM71afKbdad", + to.to_string() + ); } #[test] @@ -361,13 +380,14 @@ mod tests { let signed_tx = tx.sign(sig, rec).unwrap(); let signed_tx = hex::encode(signed_tx); - println!("signed tx = {}", signed_tx); + assert_eq!( + "41028400a487f8cf0c11fd48eae13f819dbb06e5cb97b7103d2434897bd7cb3ea80963e502ba136449919abc037e45cb36fbeae2b1d5dde212f7cd6f9eef604833811a6ac07eba271bdbb4bfb940f6f0ab810e0afea3d0bcdce0b2a51270a2235d42d3816300000c000400004ce05abd387b560855a3d486eba6237b9a08c6e9dfe351302a5ceda90be801fe070010a5d4e8", + signed_tx + ); } #[test] fn test_tx_gen_2() { - use ed25519_dalek_fiat::Signer; - let tx = r#"{ "from": "5DPaKszR7KpCbvNNtGCGTfrGdeDTUNRt1UdxwXp9G6iWvdk7", "to": "5D1NKGqfc2Q8hw53icrX74YQryjb3MMySWwFBhM71afKbdad", @@ -388,18 +408,16 @@ mod tests { 26, 232, 171, 144, 41, 109, 182, 148, 243, 20, 23, 29, 61, ]; - let sk = ed25519_dalek_fiat::SecretKey::from_bytes(&sk).unwrap(); - let pk = ed25519_dalek_fiat::PublicKey::from(&sk); - let kp = ed25519_dalek_fiat::Keypair { - secret: sk, - public: pk, - }; - - let sig = kp.sign(&msg).to_bytes().to_vec(); + let signing_key: &SecretKey = &sk[..ed25519_dalek::SECRET_KEY_LENGTH].try_into().unwrap(); + let signing_key = ed25519_dalek::SigningKey::from_bytes(signing_key); + let sig: Signature = signing_key.sign(&msg); - let signed_tx = tx.sign_ed25519(sig).unwrap(); + let signed_tx = tx.sign_ed25519(sig.to_vec()).unwrap(); let signed_tx = hex::encode(signed_tx); - println!("signed tx = {}", signed_tx); + assert_eq!( + "3d0284003aa08b895131d34e7c1364ca80067f282fc6b2417b4eefcf7e2ecf7c19d7f81900aff4f335398d8584150fae80adc6dcaea686b2a5a2c9cb28a82a5a59314b7fd3ceaac142b91c949e482aec06f16202c9ea8dcd1c82a4b250cc72dfae03a6360400140004000029b0b723f2e8b89f1bcdc0cf2b3d0e624454a0cb898a46b5b59368964c5544f5070010a5d4e8", + signed_tx + ); } }