Refactor key management (#3296)

* Add Call type to extensible transactions.

Cleanup some naming

* Merge Resource and BlockExhausted into just Exhausted

* Fix

* Another fix

* Call

* Some fixes

* Fix srml tests.

* Fix all tests.

* Refactor crypto so each application of it has its own type.

* Introduce new AuthorityProvider API into Aura

This will eventually allow for dynamic determination of authority
keys and avoid having to set them directly on CLI.

* Introduce authority determinator for Babe.

Experiment with modular consensus API.

* Work in progress to introduce KeyTypeId and avoid polluting API
with validator IDs

* Finish up drafting imonline

* Rework offchain workers API.

* Rework API implementation.

* Make it compile for wasm, simplify app_crypto.

* Fix compilation of im-online.

* Fix compilation of im-online.

* Fix more compilation errors.

* Make it compile.

* Fixing tests.

* Rewrite `keystore`

* Fix session tests

* Bring back `TryFrom`'s'

* Fix `srml-grandpa`

* Fix `srml-aura`

* Fix consensus babe

* More fixes

* Make service generate keys from dev_seed

* Build fixes

* Remove offchain tests

* More fixes and cleanups

* Fixes finality grandpa

* Fix `consensus-aura`

* Fix cli

* Fix `node-cli`

* Fix chain_spec builder

* Fix doc tests

* Add authority getter for grandpa.

* Test fix

* Fixes

* Make keystore accessible from the runtime

* Move app crypto to its own crate

* Update `Cargo.lock`

* Make the crypto stuff usable from the runtime

* Adds some runtime crypto tests

* Use last finalized block for grandpa authority

* Fix warning

* Adds `SessionKeys` runtime api

* Remove `FinalityPair` and `ConsensusPair`

* Minor governance tweaks to get it inline with docs.

* Make the governance be up to date with the docs.

* Build fixes.

* Generate the inital session keys

* Failing keystore is a hard error

* Make babe work again

* Fix grandpa

* Fix tests

* Disable `keystore` in consensus critical stuff

* Build fix.

* ImOnline supports multiple authorities at once.

* Update core/application-crypto/src/ed25519.rs

* Merge branch 'master' into gav-in-progress

* Remove unneeded code for now.

* Some `session` testing

* Support querying the public keys

* Cleanup offchain

* Remove warnings

* More cleanup

* Apply suggestions from code review

Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com>

* More cleanups

* JSONRPC API for setting keys.

Also, rename traits::KeyStore* -> traits::BareCryptoStore*

* Bad merge

* Fix integration tests

* Fix test build

* Test fix

* Fixes

* Warnings

* Another warning

* Bump version.
This commit is contained in:
Gavin Wood
2019-08-07 20:47:48 +02:00
committed by GitHub
parent a6a6779f01
commit 1a524b8207
160 changed files with 4467 additions and 2769 deletions
+55 -27
View File
@@ -26,7 +26,6 @@ use crate::codec::{Codec, Encode, Decode, HasCompact};
use crate::transaction_validity::{ValidTransaction, TransactionValidity};
use crate::generic::{Digest, DigestItem};
use crate::weights::DispatchInfo;
pub use primitives::crypto::TypedKey;
pub use integer_sqrt::IntegerSquareRoot;
pub use num_traits::{
Zero, One, Bounded, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv,
@@ -60,14 +59,14 @@ pub trait Verify {
impl Verify for primitives::ed25519::Signature {
type Signer = primitives::ed25519::Public;
fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &Self::Signer) -> bool {
runtime_io::ed25519_verify(self.as_ref(), msg.get(), signer)
runtime_io::ed25519_verify(self, msg.get(), signer)
}
}
impl Verify for primitives::sr25519::Signature {
type Signer = primitives::sr25519::Public;
fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &Self::Signer) -> bool {
runtime_io::sr25519_verify(self.as_ref(), msg.get(), signer)
runtime_io::sr25519_verify(self, msg.get(), signer)
}
}
@@ -745,7 +744,7 @@ pub enum DispatchError {
Payment,
/// General error to do with the exhaustion of block resources.
Resource,
Exhausted,
/// General error to do with the permissions of the sender.
NoPermission,
@@ -761,16 +760,13 @@ pub enum DispatchError {
/// General error to do with the transaction's proofs (e.g. signature).
BadProof,
/* /// General error to do with actually executing the dispatched logic.
User(&'static str),*/
}
impl From<DispatchError> for i8 {
fn from(e: DispatchError) -> i8 {
match e {
DispatchError::Payment => -64,
DispatchError::Resource => -65,
DispatchError::Exhausted => -65,
DispatchError::NoPermission => -66,
DispatchError::BadState => -67,
DispatchError::Stale => -68,
@@ -805,6 +801,9 @@ pub trait SignedExtension:
/// The type which encodes the sender identity.
type AccountId;
/// The type which encodes the call to be dispatched.
type Call;
/// Any additional data that will go into the signed payload. This may be created dynamically
/// from the transaction using the `additional_signed` function.
type AdditionalSigned: Encode;
@@ -813,35 +812,45 @@ pub trait SignedExtension:
/// also perform any pre-signature-verification checks and return an error if needed.
fn additional_signed(&self) -> Result<Self::AdditionalSigned, &'static str>;
/// Validate a signed transaction for the transaction queue.
/// Validate a signed transaction for the transaction queue.
fn validate(
&self,
_who: &Self::AccountId,
_call: &Self::Call,
_info: DispatchInfo,
_len: usize,
) -> Result<ValidTransaction, DispatchError> { Ok(Default::default()) }
) -> Result<ValidTransaction, DispatchError> {
Ok(Default::default())
}
/// Do any pre-flight stuff for a signed transaction.
fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: DispatchInfo,
len: usize,
) -> Result<(), DispatchError> { self.validate(who, info, len).map(|_| ()) }
) -> Result<(), DispatchError> {
self.validate(who, call, info, len).map(|_| ())
}
/// Validate an unsigned transaction for the transaction queue. Normally the default
/// implementation is fine since `ValidateUnsigned` is a better way of recognising and
/// validating unsigned transactions.
fn validate_unsigned(
_call: &Self::Call,
_info: DispatchInfo,
_len: usize,
) -> Result<ValidTransaction, DispatchError> { Ok(Default::default()) }
/// Do any pre-flight stuff for a unsigned transaction.
fn pre_dispatch_unsigned(
call: &Self::Call,
info: DispatchInfo,
len: usize,
) -> Result<(), DispatchError> { Self::validate_unsigned(info, len).map(|_| ()) }
) -> Result<(), DispatchError> {
Self::validate_unsigned(call, info, len).map(|_| ())
}
}
macro_rules! tuple_impl_indexed {
@@ -851,9 +860,11 @@ macro_rules! tuple_impl_indexed {
([$($direct:ident)+] ; [$($index:tt,)+]) => {
impl<
AccountId,
$($direct: SignedExtension<AccountId=AccountId>),+
Call,
$($direct: SignedExtension<AccountId=AccountId, Call=Call>),+
> SignedExtension for ($($direct),+,) {
type AccountId = AccountId;
type Call = Call;
type AdditionalSigned = ($($direct::AdditionalSigned,)+);
fn additional_signed(&self) -> Result<Self::AdditionalSigned, &'static str> {
Ok(( $(self.$index.additional_signed()?,)+ ))
@@ -861,33 +872,37 @@ macro_rules! tuple_impl_indexed {
fn validate(
&self,
who: &Self::AccountId,
call: &Self::Call,
info: DispatchInfo,
len: usize,
) -> Result<ValidTransaction, DispatchError> {
let aggregator = vec![$(<$direct as SignedExtension>::validate(&self.$index, who, info, len)?),+];
let aggregator = vec![$(<$direct as SignedExtension>::validate(&self.$index, who, call, info, len)?),+];
Ok(aggregator.into_iter().fold(ValidTransaction::default(), |acc, a| acc.combine_with(a)))
}
fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: DispatchInfo,
len: usize,
) -> Result<(), DispatchError> {
$(self.$index.pre_dispatch(who, info, len)?;)+
$(self.$index.pre_dispatch(who, call, info, len)?;)+
Ok(())
}
fn validate_unsigned(
call: &Self::Call,
info: DispatchInfo,
len: usize,
) -> Result<ValidTransaction, DispatchError> {
let aggregator = vec![$($direct::validate_unsigned(info, len)?),+];
let aggregator = vec![$($direct::validate_unsigned(call, info, len)?),+];
Ok(aggregator.into_iter().fold(ValidTransaction::default(), |acc, a| acc.combine_with(a)))
}
fn pre_dispatch_unsigned(
call: &Self::Call,
info: DispatchInfo,
len: usize,
) -> Result<(), DispatchError> {
$($direct::pre_dispatch_unsigned(info, len)?;)+
$($direct::pre_dispatch_unsigned(call, info, len)?;)+
Ok(())
}
}
@@ -910,11 +925,12 @@ macro_rules! tuple_impl_indexed {
#[allow(non_snake_case)]
tuple_impl_indexed!(A, B, C, D, E, F, G, H, I, J, ; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,);
/// Only for base bone testing when you don't care about signed extensions at all.\
/// Only for bare bone testing when you don't care about signed extensions at all.
#[cfg(feature = "std")]
impl SignedExtension for () {
type AccountId = u64;
type AdditionalSigned = ();
type Call = ();
fn additional_signed(&self) -> rstd::result::Result<(), &'static str> { Ok(()) }
}
@@ -1208,14 +1224,14 @@ macro_rules! count {
/// just the bytes of the key.
///
/// ```rust
/// use sr_primitives::{impl_opaque_keys, key_types, KeyTypeId};
/// use sr_primitives::{impl_opaque_keys, key_types, KeyTypeId, app_crypto::{sr25519, ed25519}};
///
/// impl_opaque_keys! {
/// pub struct Keys {
/// #[id(key_types::ED25519)]
/// pub ed25519: [u8; 32],
/// pub ed25519: ed25519::AppPublic,
/// #[id(key_types::SR25519)]
/// pub sr25519: [u8; 32],
/// pub sr25519: sr25519::AppPublic,
/// }
/// }
/// ```
@@ -1237,22 +1253,34 @@ macro_rules! impl_opaque_keys {
)*
}
impl $name {
/// Generate a set of keys with optionally using the given seed.
///
/// The generated key pairs are stored in the keystore.
///
/// Returns the concatenated SCALE encoded public keys.
pub fn generate(seed: Option<&str>) -> $crate::rstd::vec::Vec<u8> {
let keys = Self{
$(
$field: <$type as $crate::app_crypto::RuntimeAppPublic>::generate_pair(seed),
)*
};
$crate::codec::Encode::encode(&keys)
}
}
impl $crate::traits::OpaqueKeys for $name {
type KeyTypeIds = $crate::rstd::iter::Cloned<
$crate::rstd::slice::Iter<'static, $crate::KeyTypeId>
>;
fn key_ids() -> Self::KeyTypeIds {
[
$($key_id),*
].iter().cloned()
[ $($key_id),* ].iter().cloned()
}
fn get_raw(&self, i: $crate::KeyTypeId) -> &[u8] {
match i {
$(
i if i == $key_id => self.$field.as_ref(),
)*
$( i if i == $key_id => self.$field.as_ref(), )*
_ => &[],
}
}