Skip to content

Commit

Permalink
fix: upgrade ed25519-dalek-fiat, anychain-core, sp-core for anychain-…
Browse files Browse the repository at this point in the history
…polkadot
  • Loading branch information
shuimuliang committed Mar 3, 2024
1 parent e99d71f commit 6d9b80d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
8 changes: 4 additions & 4 deletions anychain-polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
19 changes: 14 additions & 5 deletions anychain-polkadot/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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::<Westend>::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::<Westend>::from_payload(hash).unwrap();
println!("address = {}", address);
assert_eq!(
"5FJ4gu9eVX6DG4qYi1hxkUgu1yaTm1CnQ4MiiZPjPVaXiATo",
address.addr
);
}

#[test]
Expand All @@ -127,6 +133,9 @@ mod tests {
let addr = PolkadotAddress::<Westend>::from_str(addr).unwrap();
let payload = addr.to_payload().unwrap();
let payload = hex::encode(payload);
println!("{}", payload);
assert_eq!(
"4ce05abd387b560855a3d486eba6237b9a08c6e9dfe351302a5ceda90be801fe",
payload
);
}
}
9 changes: 5 additions & 4 deletions anychain-polkadot/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -36,8 +36,9 @@ impl<N: PolkadotNetwork> PublicKey for PolkadotPublicKey<N> {
}
}
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,
Expand Down
52 changes: 35 additions & 17 deletions anychain-polkadot/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -301,7 +302,14 @@ mod tests {
let from = PolkadotAddress::<Westend>::from_secret_key(&sk_from, format).unwrap();
let to = PolkadotAddress::<Westend>::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]
Expand All @@ -318,16 +326,27 @@ 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);

let from = PolkadotAddress::<Westend>::from_secret_key(&sk_from, format).unwrap();
let to = PolkadotAddress::<Westend>::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]
Expand Down Expand Up @@ -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",
Expand All @@ -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
);
}
}

0 comments on commit 6d9b80d

Please sign in to comment.