Skip to content

Commit c14400a

Browse files
authored
JEQB paritytech#196: Keyring account (#26)
* Fix generating from seed and phrase * Format * Fix build warnings and test configurations
1 parent 2129ebc commit c14400a

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

primitives/core/src/dilithium2.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
2929
#[cfg(feature = "std")]
3030
use substrate_bip39::seed_from_entropy;
3131

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

420420
#[cfg(feature = "std")]
421-
fn from_phrase(_: &str, _: Option<&str>) -> Result<(Self, Self::Seed), SecretStringError> {
421+
fn from_phrase(phrase: &str, password: Option<&str>) -> Result<(Self, Self::Seed), SecretStringError> {
422+
let big_seed = seed_from_entropy(
423+
Mnemonic::from_phrase(phrase, Language::English)
424+
.map_err(|_| SecretStringError::InvalidPhrase)?
425+
.entropy(),
426+
password.unwrap_or("")
427+
)
428+
.map_err(|_| SecretStringError::InvalidSeed)?;
429+
422430
let mut seed = Seed::default();
431+
seed.copy_from_slice(&big_seed[0..32]);
423432
Self::from_seed_slice(&seed).map(|x| (x, seed))
424433
}
425434

426435
fn derive<Iter: Iterator<Item=DeriveJunction>>(
427436
&self,
428-
_: Iter,
437+
path: Iter,
429438
_: Option<Seed>,
430439
) -> Result<(Self, Option<Seed>), Self::DeriveError> {
431-
let seed = Seed::default();
440+
let acc = self.secret.0;
441+
let mut seed = [0u8; 32];
442+
seed.copy_from_slice(&acc[0..32]);
443+
444+
for j in path {
445+
match j {
446+
DeriveJunction::Soft(_cc) => return Err(DeriveError::SoftKeyInPath),
447+
DeriveJunction::Hard(cc) => seed = derive_hard_junction(&seed, &cc),
448+
}
449+
}
450+
432451
Ok((Self::from_seed(&seed), Some(seed)))
433452
}
434453
fn from_seed(seed: &Self::Seed) -> Self {

primitives/io/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,8 @@ pub trait Crypto {
869869
}
870870

871871
/// Returns all `dilithium2` public keys for the given key id from the keystore.
872-
fn dilithium2_public_keys(&mut self, id: KeyTypeId) -> Vec<dilithium2::Public> {
873-
let keystore = &***self
872+
fn dilithium2_public_keys(&mut self, _: KeyTypeId) -> Vec<dilithium2::Public> {
873+
let _keystore = &***self
874874
.extension::<KeystoreExt>()
875875
.expect("No `keystore` associated for the current context!");
876876
// TODO JEQB-194 implement dilithium keystore
@@ -884,9 +884,9 @@ pub trait Crypto {
884884
/// The `seed` needs to be a valid utf8.
885885
///
886886
/// Returns the public key.
887-
fn dilithium2_generate(&mut self, id: KeyTypeId, seed: Option<Vec<u8>>) -> dilithium2::Public {
888-
let seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!"));
889-
let keystore = &***self
887+
fn dilithium2_generate(&mut self, _: KeyTypeId, seed: Option<Vec<u8>>) -> dilithium2::Public {
888+
let _seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!"));
889+
let _keystore = &***self
890890
.extension::<KeystoreExt>()
891891
.expect("No `keystore` associated for the current context!");
892892
// TODO JEQB-194 implement dilithium2 keystore

primitives/keyring/src/dilithium2.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ use std::{collections::HashMap, ops::Deref};
2121

2222
use lazy_static::lazy_static;
2323

24-
use sp_core::{
25-
ByteArray,
26-
dilithium2::{Pair, Public, Signature}, Pair as PairT,
27-
};
24+
use sp_core::{ByteArray, dilithium2::{Pair, Public, Signature}, Pair as PairT};
2825
pub use sp_core::dilithium2;
2926

3027
/// Set of test accounts.
@@ -206,13 +203,11 @@ mod tests {
206203
// ));
207204
}
208205

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

216-
assert_eq!(alice, bob);
211+
assert_ne!(alice, bob);
217212
}
218213
}

primitives/runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ impl Verify for MultiSignature {
450450
Ok(signer) => sig.verify(msg, &signer),
451451
Err(()) => false,
452452
},
453-
(Self::Dilithium2(ref sig), who) => true,
453+
(Self::Dilithium2(ref _sig), _who) => true,
454454
(Self::Ecdsa(ref sig), who) => {
455455
let m = sp_io::hashing::blake2_256(msg.get());
456456
match sp_io::crypto::secp256k1_ecdsa_recover_compressed(sig.as_ref(), &m) {

0 commit comments

Comments
 (0)