Skip to content

Commit

Permalink
Merge pull request #2379 from subspace/update-ed25519-dalek
Browse files Browse the repository at this point in the history
Update ed25519-dalek to latest version
  • Loading branch information
nazar-pc authored Jan 3, 2024
2 parents 8de7d53 + 7380e68 commit e7926ce
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 53 deletions.
38 changes: 9 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/pallet-grandpa-finality-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sp-runtime = { git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3
sp-std = { git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5", default-features = false }

[dev-dependencies]
ed25519-dalek = { version = "1.0", default-features = false, features = ["u64_backend"] }
ed25519-dalek = { version = "2.1.0", default-features = false }
sp-io = { git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sp-application-crypto = { git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }

Expand Down
36 changes: 13 additions & 23 deletions crates/pallet-grandpa-finality-verifier/src/tests/keyring.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Utilities for working with test accounts.
use codec::Encode;
use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature};
use ed25519_dalek::{
SecretKey, Signature, Signer, SigningKey, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH,
};
use finality_grandpa::voter_set::VoterSet;
use sp_consensus_grandpa::{AuthorityId, AuthorityList, AuthorityWeight};
use sp_std::prelude::*;
Expand All @@ -18,41 +20,29 @@ pub(crate) const EVE: Account = Account(4);
pub(crate) struct Account(pub u16);

impl Account {
pub(crate) fn public(&self) -> PublicKey {
PublicKey::from(&self.secret())
pub(crate) fn public(&self) -> [u8; PUBLIC_KEY_LENGTH] {
self.signing_key().to_keypair_bytes()[SECRET_KEY_LENGTH..][..PUBLIC_KEY_LENGTH]
.try_into()
.unwrap()
}

pub(crate) fn secret(&self) -> SecretKey {
pub(crate) fn signing_key(&self) -> SigningKey {
let data = self.0.encode();
let mut bytes = [0_u8; 32];
bytes[0..data.len()].copy_from_slice(&data);
SecretKey::from_bytes(&bytes)
.expect("A static array of the correct length is a known good.")
}

pub(crate) fn pair(&self) -> Keypair {
let mut pair: [u8; 64] = [0; 64];

let secret = self.secret();
pair[..32].copy_from_slice(&secret.to_bytes());

let public = self.public();
pair[32..].copy_from_slice(&public.to_bytes());
let mut secret_key: SecretKey = [0_u8; 32];
secret_key[0..data.len()].copy_from_slice(&data);

Keypair::from_bytes(&pair)
.expect("We expect the SecretKey to be good, so this must also be good.")
SigningKey::from_bytes(&secret_key)
}

pub(crate) fn sign(&self, msg: &[u8]) -> Signature {
use ed25519_dalek::Signer;
self.pair().sign(msg)
self.signing_key().sign(msg)
}
}

impl From<Account> for AuthorityId {
#[inline]
fn from(p: Account) -> Self {
sp_application_crypto::UncheckedFrom::unchecked_from(p.public().to_bytes())
sp_application_crypto::UncheckedFrom::unchecked_from(p.public())
}
}

Expand Down

0 comments on commit e7926ce

Please sign in to comment.