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
+31 -21
View File
@@ -19,10 +19,14 @@
use std::collections::HashMap;
use std::ops::Deref;
use lazy_static::lazy_static;
use hex_literal::{hex, hex_impl};
use substrate_primitives::ed25519::{Pair, Public, Signature};
use substrate_primitives::{ed25519::{Pair, Public, Signature}, Pair as _Pair, H256};
pub use substrate_primitives::ed25519;
/// The root phrase for our test keys.
///
/// This is the same phrase that's in node::cli, but shouldn't need to be.
pub const DEV_PHRASE: &str = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
/// Set of test accounts.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Keyring {
@@ -37,7 +41,7 @@ pub enum Keyring {
}
impl Keyring {
pub fn from_public(who: Public) -> Option<Keyring> {
pub fn from_public(who: &Public) -> Option<Keyring> {
[
Keyring::Alice,
Keyring::Bob,
@@ -49,17 +53,25 @@ impl Keyring {
Keyring::Two,
].iter()
.map(|i| *i)
.find(|&k| Public::from(k) == who)
.find(|&k| &Public::from(k) == who)
}
pub fn from_raw_public(who: [u8; 32]) -> Option<Keyring> {
Self::from_public(Public::from_raw(who))
Self::from_public(&Public::from_raw(who))
}
pub fn to_raw_public(self) -> [u8; 32] {
*Public::from(self).as_array_ref()
}
pub fn from_h256_public(who: H256) -> Option<Keyring> {
Self::from_public(&Public::from_raw(who.into()))
}
pub fn to_h256_public(self) -> H256 {
Public::from(self).as_array_ref().into()
}
pub fn to_raw_public_vec(self) -> Vec<u8> {
Public::from(self).to_raw_vec()
}
@@ -69,16 +81,8 @@ impl Keyring {
}
pub fn pair(self) -> Pair {
match self {
Keyring::Alice => Pair::from_seed(b"Alice "),
Keyring::Bob => Pair::from_seed(b"Bob "),
Keyring::Charlie => Pair::from_seed(b"Charlie "),
Keyring::Dave => Pair::from_seed(b"Dave "),
Keyring::Eve => Pair::from_seed(b"Eve "),
Keyring::Ferdie => Pair::from_seed(b"Ferdie "),
Keyring::One => Pair::from_seed(b"12345678901234567890123456789012"),
Keyring::Two => Pair::from_seed(&hex!("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60")),
}
Pair::from_string(&format!("{}//{}", DEV_PHRASE, <&'static str>::from(self)), None)
.expect("static values are known good; qed")
}
}
@@ -91,8 +95,8 @@ impl From<Keyring> for &'static str {
Keyring::Dave => "Dave",
Keyring::Eve => "Eve",
Keyring::Ferdie => "Ferdie",
Keyring::One => "one",
Keyring::Two => "two",
Keyring::One => "One",
Keyring::Two => "Two",
}
}
}
@@ -134,6 +138,12 @@ impl From<Keyring> for [u8; 32] {
}
}
impl From<Keyring> for H256 {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref().into()
}
}
impl From<Keyring> for &'static [u8; 32] {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
@@ -162,12 +172,12 @@ impl Deref for Keyring {
#[cfg(test)]
mod tests {
use super::*;
use ed25519::Verifiable;
use substrate_primitives::{ed25519::Pair, Pair as _Pair};
#[test]
fn should_work() {
assert!(Keyring::Alice.sign(b"I am Alice!").verify(b"I am Alice!", Keyring::Alice));
assert!(!Keyring::Alice.sign(b"I am Alice!").verify(b"I am Bob!", Keyring::Alice));
assert!(!Keyring::Alice.sign(b"I am Alice!").verify(b"I am Alice!", Keyring::Bob));
assert!(Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Alice!", Keyring::Alice));
assert!(!Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Bob!", Keyring::Alice));
assert!(!Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Alice!", Keyring::Bob));
}
}
+12 -4
View File
@@ -22,7 +22,15 @@ pub mod sr25519;
/// Test account crypto for ed25519.
pub mod ed25519;
/// The Ed25519 keyring.
///
/// This is deprecated: use `ed25519::Keyring` instead.
pub use ed25519::Keyring;
/// Convenience export: Sr25519's Keyring is exposed as `AccountKeyring`,
/// since it tends to be used for accounts.
pub use sr25519::Keyring as AccountKeyring;
/// Convenience export: Ed25519's Keyring is exposed as `AuthorityKeyring`,
/// since it tends to be used for authorities (session keys &c.).
pub use ed25519::Keyring as AuthorityKeyring;
pub mod test {
/// The keyring for use with accounts when using the test runtime.
pub use super::ed25519::Keyring as AccountKeyring;
}
+31 -21
View File
@@ -19,10 +19,14 @@
use std::collections::HashMap;
use std::ops::Deref;
use lazy_static::lazy_static;
use hex_literal::{hex, hex_impl};
use substrate_primitives::sr25519::{Pair, Public, Signature};
use substrate_primitives::{sr25519::{Pair, Public, Signature}, Pair as _Pair, H256};
pub use substrate_primitives::sr25519;
/// The root phrase for our test keys.
///
/// This is the same phrase that's in node::cli, but shouldn't need to be.
pub const DEV_PHRASE: &str = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
/// Set of test accounts.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Keyring {
@@ -37,7 +41,7 @@ pub enum Keyring {
}
impl Keyring {
pub fn from_public(who: Public) -> Option<Keyring> {
pub fn from_public(who: &Public) -> Option<Keyring> {
[
Keyring::Alice,
Keyring::Bob,
@@ -49,17 +53,25 @@ impl Keyring {
Keyring::Two,
].iter()
.map(|i| *i)
.find(|&k| Public::from(k) == who)
.find(|&k| &Public::from(k) == who)
}
pub fn from_raw_public(who: [u8; 32]) -> Option<Keyring> {
Self::from_public(Public::from_raw(who))
Self::from_public(&Public::from_raw(who))
}
pub fn to_raw_public(self) -> [u8; 32] {
*Public::from(self).as_array_ref()
}
pub fn from_h256_public(who: H256) -> Option<Keyring> {
Self::from_public(&Public::from_raw(who.into()))
}
pub fn to_h256_public(self) -> H256 {
Public::from(self).as_array_ref().into()
}
pub fn to_raw_public_vec(self) -> Vec<u8> {
Public::from(self).to_raw_vec()
}
@@ -69,16 +81,8 @@ impl Keyring {
}
pub fn pair(self) -> Pair {
match self {
Keyring::Alice => Pair::from_seed(b"Alice "),
Keyring::Bob => Pair::from_seed(b"Bob "),
Keyring::Charlie => Pair::from_seed(b"Charlie "),
Keyring::Dave => Pair::from_seed(b"Dave "),
Keyring::Eve => Pair::from_seed(b"Eve "),
Keyring::Ferdie => Pair::from_seed(b"Ferdie "),
Keyring::One => Pair::from_seed(b"12345678901234567890123456789012"),
Keyring::Two => Pair::from_seed(&hex!("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60")),
}
Pair::from_string(&format!("{}//{}", DEV_PHRASE, <&'static str>::from(self)), None)
.expect("static values are known good; qed")
}
}
@@ -91,8 +95,8 @@ impl From<Keyring> for &'static str {
Keyring::Dave => "Dave",
Keyring::Eve => "Eve",
Keyring::Ferdie => "Ferdie",
Keyring::One => "one",
Keyring::Two => "two",
Keyring::One => "One",
Keyring::Two => "Two",
}
}
}
@@ -134,6 +138,12 @@ impl From<Keyring> for [u8; 32] {
}
}
impl From<Keyring> for H256 {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref().into()
}
}
impl From<Keyring> for &'static [u8; 32] {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
@@ -162,12 +172,12 @@ impl Deref for Keyring {
#[cfg(test)]
mod tests {
use super::*;
use sr25519::Verifiable;
use substrate_primitives::{sr25519::Pair, Pair as _Pair};
#[test]
fn should_work() {
assert!(Keyring::Alice.sign(b"I am Alice!").verify(b"I am Alice!", Keyring::Alice));
assert!(!Keyring::Alice.sign(b"I am Alice!").verify(b"I am Bob!", Keyring::Alice));
assert!(!Keyring::Alice.sign(b"I am Alice!").verify(b"I am Alice!", Keyring::Bob));
assert!(Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Alice!", Keyring::Alice));
assert!(!Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Bob!", Keyring::Alice));
assert!(!Pair::verify(&Keyring::Alice.sign(b"I am Alice!"), b"I am Alice!", Keyring::Bob));
}
}