Skip to content

Commit

Permalink
JEQB paritytech#196: Keyring account (#26)
Browse files Browse the repository at this point in the history
* Fix generating from seed and phrase

* Format

* Fix build warnings and test configurations
  • Loading branch information
Wolmin authored Jan 25, 2023
1 parent 2129ebc commit c14400a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
27 changes: 23 additions & 4 deletions primitives/core/src/dilithium2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "std")]
use substrate_bip39::seed_from_entropy;

use sp_runtime_interface::pass_by::{Inner, PassBy, PassByInner};
use sp_runtime_interface::pass_by::PassByInner;
use sp_std::ops::Deref;
#[cfg(feature = "full_crypto")]
use sp_std::vec::Vec;
Expand Down Expand Up @@ -418,17 +418,36 @@ impl TraitPair for Pair {
}

#[cfg(feature = "std")]
fn from_phrase(_: &str, _: Option<&str>) -> Result<(Self, Self::Seed), SecretStringError> {
fn from_phrase(phrase: &str, password: Option<&str>) -> Result<(Self, Self::Seed), SecretStringError> {
let big_seed = seed_from_entropy(
Mnemonic::from_phrase(phrase, Language::English)
.map_err(|_| SecretStringError::InvalidPhrase)?
.entropy(),
password.unwrap_or("")
)
.map_err(|_| SecretStringError::InvalidSeed)?;

let mut seed = Seed::default();
seed.copy_from_slice(&big_seed[0..32]);
Self::from_seed_slice(&seed).map(|x| (x, seed))
}

fn derive<Iter: Iterator<Item=DeriveJunction>>(
&self,
_: Iter,
path: Iter,
_: Option<Seed>,
) -> Result<(Self, Option<Seed>), Self::DeriveError> {
let seed = Seed::default();
let acc = self.secret.0;
let mut seed = [0u8; 32];
seed.copy_from_slice(&acc[0..32]);

for j in path {
match j {
DeriveJunction::Soft(_cc) => return Err(DeriveError::SoftKeyInPath),
DeriveJunction::Hard(cc) => seed = derive_hard_junction(&seed, &cc),
}
}

Ok((Self::from_seed(&seed), Some(seed)))
}
fn from_seed(seed: &Self::Seed) -> Self {
Expand Down
10 changes: 5 additions & 5 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,8 @@ pub trait Crypto {
}

/// Returns all `dilithium2` public keys for the given key id from the keystore.
fn dilithium2_public_keys(&mut self, id: KeyTypeId) -> Vec<dilithium2::Public> {
let keystore = &***self
fn dilithium2_public_keys(&mut self, _: KeyTypeId) -> Vec<dilithium2::Public> {
let _keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
// TODO JEQB-194 implement dilithium keystore
Expand All @@ -884,9 +884,9 @@ pub trait Crypto {
/// The `seed` needs to be a valid utf8.
///
/// Returns the public key.
fn dilithium2_generate(&mut self, id: KeyTypeId, seed: Option<Vec<u8>>) -> dilithium2::Public {
let seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!"));
let keystore = &***self
fn dilithium2_generate(&mut self, _: KeyTypeId, seed: Option<Vec<u8>>) -> dilithium2::Public {
let _seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!"));
let _keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
// TODO JEQB-194 implement dilithium2 keystore
Expand Down
9 changes: 2 additions & 7 deletions primitives/keyring/src/dilithium2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ use std::{collections::HashMap, ops::Deref};

use lazy_static::lazy_static;

use sp_core::{
ByteArray,
dilithium2::{Pair, Public, Signature}, Pair as PairT,
};
use sp_core::{ByteArray, dilithium2::{Pair, Public, Signature}, Pair as PairT};
pub use sp_core::dilithium2;

/// Set of test accounts.
Expand Down Expand Up @@ -206,13 +203,11 @@ mod tests {
// ));
}

// TODO JEQB-196 account keys should be different
#[test]
#[ignore]
fn account_should_be_different() {
let alice = Keyring::Alice.public();
let bob = Keyring::Bob.public();

assert_eq!(alice, bob);
assert_ne!(alice, bob);
}
}
2 changes: 1 addition & 1 deletion primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ impl Verify for MultiSignature {
Ok(signer) => sig.verify(msg, &signer),
Err(()) => false,
},
(Self::Dilithium2(ref sig), who) => true,
(Self::Dilithium2(ref _sig), _who) => true,
(Self::Ecdsa(ref sig), who) => {
let m = sp_io::hashing::blake2_256(msg.get());
match sp_io::crypto::secp256k1_ecdsa_recover_compressed(sig.as_ref(), &m) {
Expand Down

0 comments on commit c14400a

Please sign in to comment.