Overhaul crypto (Schnorr/Ristretto, HDKD, BIP39) (#1795)

* Rijig to Ristretto

* Rebuild wasm

* adds compatibility test with the wasm module

* Add Ed25519-BIP39 support

* Bump subkey version

* Update CLI output

* New keys.

* Standard phrase/password/path keys.

* Subkey uses S-URI for secrets

* Move everything to use new HDKD crypto.

* Test fixes

* Ignore old test vector.

* fix the ^^ old test vector.

* Fix tests

* Test fixes

* Cleanups

* Fix broken key conversion logic in grandpa

CC @rphmeier

* Remove legacy Keyring usage

* Traitify `Pair`

* Replace Ed25519AuthorityId with ed25519::Public

* Expunge Ed25519AuthorityId type!

* Replace Sr25519AuthorityId with sr25519::Public

* Remove dodgy crypto type-punning conversions

* Fix some tests

* Avoid trait

* Deduplicate DeriveJunction string decode

* Remove cruft code

* Fix test

* Minor removals

* Build fix

* Subkey supports sign and verify

* Inspect works for public key URIs

* Remove more crypto type-punning

* Fix typo

* Fix tests
This commit is contained in:
Gav Wood
2019-03-13 14:08:31 +01:00
committed by GitHub
parent 17f093da13
commit d7fcf5dc9d
83 changed files with 2636 additions and 1687 deletions
+2 -2
View File
@@ -34,7 +34,7 @@ use inherents::{
};
#[cfg(any(feature = "std", test))]
use substrate_primitives::Ed25519AuthorityId;
use substrate_primitives::ed25519::Public as AuthorityId;
mod mock;
mod tests;
@@ -135,7 +135,7 @@ impl<SessionKey: Member> RawLog<SessionKey> {
// Implementation for tests outside of this crate.
#[cfg(any(feature = "std", test))]
impl<N> From<RawLog<N>> for primitives::testing::DigestItem where N: Into<Ed25519AuthorityId> {
impl<N> From<RawLog<N>> for primitives::testing::DigestItem where N: Into<AuthorityId> {
fn from(log: RawLog<N>) -> primitives::testing::DigestItem {
match log {
RawLog::AuthoritiesChange(authorities) =>
+3 -2
View File
@@ -67,6 +67,7 @@ use crate::account_db::AccountDb;
#[cfg(feature = "std")]
use serde_derive::{Serialize, Deserialize};
use substrate_primitives::crypto::UncheckedFrom;
use rstd::prelude::*;
use rstd::marker::PhantomData;
use parity_codec::{Codec, Encode, Decode};
@@ -120,7 +121,7 @@ pub trait Trait: fees::Trait + balances::Trait + timestamp::Trait {
pub struct SimpleAddressDeterminator<T: Trait>(PhantomData<T>);
impl<T: Trait> ContractAddressFor<CodeHash<T>, T::AccountId> for SimpleAddressDeterminator<T>
where
T::AccountId: From<T::Hash> + AsRef<[u8]>
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>
{
fn contract_address_for(code_hash: &CodeHash<T>, data: &[u8], origin: &T::AccountId) -> T::AccountId {
let data_hash = T::Hashing::hash(data);
@@ -130,7 +131,7 @@ where
buf.extend_from_slice(data_hash.as_ref());
buf.extend_from_slice(origin.as_ref());
T::Hashing::hash(&buf[..]).into()
UncheckedFrom::unchecked_from(T::Hashing::hash(&buf[..]))
}
}
+2 -2
View File
@@ -580,7 +580,7 @@ mod tests {
/// calls `ext_caller`, loads the address from the scratch buffer and
/// compares it with the constant 42.
const CODE_CALLER: &'static str = r#"
const CODE_CALLER: &str = r#"
(module
(import "env" "ext_caller" (func $ext_caller))
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
@@ -645,7 +645,7 @@ mod tests {
/// calls `ext_address`, loads the address from the scratch buffer and
/// compares it with the constant 69.
const CODE_ADDRESS: &'static str = r#"
const CODE_ADDRESS: &str = r#"
(module
(import "env" "ext_address" (func $ext_address))
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
+2
View File
@@ -16,6 +16,7 @@ primitives = { package = "sr-primitives", path = "../../core/sr-primitives", def
srml-support = { path = "../support", default-features = false }
system = { package = "srml-system", path = "../system", default-features = false }
session = { package = "srml-session", path = "../session", default-features = false }
consensus = { package = "srml-consensus", path = "../consensus", default-features = false }
finality-tracker = { package = "srml-finality-tracker", path = "../finality-tracker", default-features = false }
[dev-dependencies]
@@ -33,6 +34,7 @@ std = [
"srml-support/std",
"primitives/std",
"system/std",
"consensus/std",
"session/std",
"finality-tracker/std",
]
+11 -20
View File
@@ -40,10 +40,11 @@ use srml_support::{Parameter, decl_event, decl_storage, decl_module};
use srml_support::dispatch::Result;
use srml_support::storage::StorageValue;
use srml_support::storage::unhashed::StorageVec;
use primitives::traits::{CurrentHeight, Convert};
use substrate_primitives::Ed25519AuthorityId;
use primitives::traits::CurrentHeight;
use substrate_primitives::ed25519;
use system::ensure_signed;
use primitives::traits::MaybeSerializeDebug;
use ed25519::Public as AuthorityId;
mod mock;
mod tests;
@@ -100,7 +101,7 @@ impl<N: Clone, SessionKey> RawLog<N, SessionKey> {
}
impl<N, SessionKey> GrandpaChangeSignal<N> for RawLog<N, SessionKey>
where N: Clone, SessionKey: Clone + Into<Ed25519AuthorityId>,
where N: Clone, SessionKey: Clone + Into<AuthorityId>,
{
fn as_signal(&self) -> Option<ScheduledChange<N>> {
RawLog::as_signal(self).map(|(delay, next_authorities)| ScheduledChange {
@@ -309,7 +310,7 @@ impl<T: Trait> Module<T> {
}
}
impl<T: Trait> Module<T> where Ed25519AuthorityId: core::convert::From<<T as Trait>::SessionKey> {
impl<T: Trait> Module<T> where AuthorityId: core::convert::From<<T as Trait>::SessionKey> {
/// See if the digest contains any standard scheduled change.
pub fn scrape_digest_change(log: &Log<T>)
-> Option<ScheduledChange<T::BlockNumber>>
@@ -340,19 +341,14 @@ impl<T> Default for SyncedAuthorities<T> {
}
impl<X, T> session::OnSessionChange<X> for SyncedAuthorities<T> where
T: Trait,
T: session::Trait,
<T as session::Trait>::ConvertAccountIdToSessionKey: Convert<
<T as system::Trait>::AccountId,
<T as Trait>::SessionKey,
>,
T: Trait + consensus::Trait<SessionKey=<T as Trait>::SessionKey>,
<T as consensus::Trait>::Log: From<consensus::RawLog<<T as Trait>::SessionKey>>
{
fn on_session_change(_: X, _: bool) {
use primitives::traits::Zero;
let next_authorities = <session::Module<T>>::validators()
let next_authorities = <consensus::Module<T>>::authorities()
.into_iter()
.map(T::ConvertAccountIdToSessionKey::convert)
.map(|key| (key, 1)) // evenly-weighted.
.collect::<Vec<(<T as Trait>::SessionKey, u64)>>();
@@ -365,22 +361,17 @@ impl<X, T> session::OnSessionChange<X> for SyncedAuthorities<T> where
}
impl<T> finality_tracker::OnFinalizationStalled<T::BlockNumber> for SyncedAuthorities<T> where
T: Trait,
T: session::Trait,
T: Trait + consensus::Trait<SessionKey=<T as Trait>::SessionKey>,
<T as consensus::Trait>::Log: From<consensus::RawLog<<T as Trait>::SessionKey>>,
T: finality_tracker::Trait,
<T as session::Trait>::ConvertAccountIdToSessionKey: Convert<
<T as system::Trait>::AccountId,
<T as Trait>::SessionKey,
>,
{
fn on_stalled(further_wait: T::BlockNumber) {
// when we record old authority sets, we can use `finality_tracker::median`
// to figure out _who_ failed. until then, we can't meaningfully guard
// against `next == last` the way that normal session changes do.
let next_authorities = <session::Module<T>>::validators()
let next_authorities = <consensus::Module<T>>::authorities()
.into_iter()
.map(T::ConvertAccountIdToSessionKey::convert)
.map(|key| (key, 1)) // evenly-weighted.
.collect::<Vec<(<T as Trait>::SessionKey, u64)>>();
+1
View File
@@ -193,5 +193,6 @@ fn dispatch_forced_change() {
Grandpa::on_finalise(11);
header = System::finalise();
}
let _ = header;
});
}
+5 -4
View File
@@ -51,7 +51,7 @@ macro_rules! impl_session_change {
for_each_tuple!(impl_session_change);
pub trait Trait: timestamp::Trait + consensus::Trait {
type ConvertAccountIdToSessionKey: Convert<Self::AccountId, Self::SessionKey>;
type ConvertAccountIdToSessionKey: Convert<Self::AccountId, Option<Self::SessionKey>>;
type OnSessionChange: OnSessionChange<Self::Moment>;
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
@@ -184,16 +184,17 @@ impl<T: Trait> Module<T> {
<LastLengthChange<T>>::put(block_number);
}
T::OnSessionChange::on_session_change(time_elapsed, apply_rewards);
// Update any changes in session keys.
for (i, v) in Self::validators().into_iter().enumerate() {
<consensus::Module<T>>::set_authority(
i as u32,
&<NextKeyFor<T>>::get(&v)
.unwrap_or_else(|| T::ConvertAccountIdToSessionKey::convert(v))
.or_else(|| T::ConvertAccountIdToSessionKey::convert(v))
.unwrap_or_default()
);
};
T::OnSessionChange::on_session_change(time_elapsed, apply_rewards);
}
/// Get the time that should have elapsed over a session if everything was working perfectly.