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
+19 -19
View File
@@ -17,27 +17,24 @@
use super::*;
use std::sync::Arc;
use hex_literal::{hex, hex_impl};
use assert_matches::assert_matches;
use parity_codec::Encode;
use transaction_pool::{
txpool::Pool,
ChainApi,
};
use primitives::H256;
use test_client::keyring::Keyring;
use test_client::runtime::{Extrinsic, Transfer};
use test_client;
use primitives::{H256, blake2_256, hexdisplay::HexDisplay};
use test_client::{self, AccountKeyring, runtime::{Extrinsic, Transfer}};
use tokio::runtime;
fn uxt(sender: Keyring, nonce: u64) -> Extrinsic {
fn uxt(sender: AccountKeyring, nonce: u64) -> Extrinsic {
let tx = Transfer {
amount: Default::default(),
nonce,
from: sender.to_raw_public().into(),
from: sender.into(),
to: Default::default(),
};
let signature = Keyring::from_raw_public(tx.from.to_fixed_bytes()).unwrap().sign(&tx.encode()).into();
let signature = AccountKeyring::from_public(&tx.from).unwrap().sign(&tx.encode()).into();
Extrinsic::Transfer(tx, signature)
}
@@ -50,14 +47,15 @@ fn submit_transaction_should_not_cause_error() {
pool: Arc::new(Pool::new(Default::default(), ChainApi::new(client))),
subscriptions: Subscriptions::new(runtime.executor()),
};
let h: H256 = hex!("81897a4890fb7554e7f77c533a865846a11583a56a8ad5e307543188d55e64f1").into();
let xt = uxt(AccountKeyring::Alice, 1).encode();
let h: H256 = blake2_256(&xt).into();
assert_matches!(
AuthorApi::submit_extrinsic(&p, uxt(Keyring::Alice, 1).encode().into()),
AuthorApi::submit_extrinsic(&p, xt.clone().into()),
Ok(h2) if h == h2
);
assert!(
AuthorApi::submit_extrinsic(&p, uxt(Keyring::Alice, 1).encode().into()).is_err()
AuthorApi::submit_extrinsic(&p, xt.into()).is_err()
);
}
@@ -70,14 +68,15 @@ fn submit_rich_transaction_should_not_cause_error() {
pool: Arc::new(Pool::new(Default::default(), ChainApi::new(client.clone()))),
subscriptions: Subscriptions::new(runtime.executor()),
};
let h: H256 = hex!("9ec8469b5dcfe29cc274ac1d07ad73d80be57566ace0fcdbe51ebcf4b51e925b").into();
let xt = uxt(AccountKeyring::Alice, 0).encode();
let h: H256 = blake2_256(&xt).into();
assert_matches!(
AuthorApi::submit_extrinsic(&p, uxt(Keyring::Alice, 0).encode().into()),
AuthorApi::submit_extrinsic(&p, xt.clone().into()),
Ok(h2) if h == h2
);
assert!(
AuthorApi::submit_extrinsic(&p, uxt(Keyring::Alice, 0).encode().into()).is_err()
AuthorApi::submit_extrinsic(&p, xt.into()).is_err()
);
}
@@ -95,7 +94,7 @@ fn should_watch_extrinsic() {
let (subscriber, id_rx, data) = ::jsonrpc_pubsub::typed::Subscriber::new_test("test");
// when
p.watch_extrinsic(Default::default(), subscriber, uxt(Keyring::Alice, 0).encode().into());
p.watch_extrinsic(Default::default(), subscriber, uxt(AccountKeyring::Alice, 0).encode().into());
// then
assert_eq!(runtime.block_on(id_rx), Ok(Ok(1.into())));
@@ -104,10 +103,10 @@ fn should_watch_extrinsic() {
let tx = Transfer {
amount: 5,
nonce: 0,
from: Keyring::Alice.to_raw_public().into(),
from: AccountKeyring::Alice.into(),
to: Default::default(),
};
let signature = Keyring::from_raw_public(tx.from.to_fixed_bytes()).unwrap().sign(&tx.encode()).into();
let signature = AccountKeyring::from_public(&tx.from).unwrap().sign(&tx.encode()).into();
Extrinsic::Transfer(tx, signature)
};
AuthorApi::submit_extrinsic(&p, replacement.encode().into()).unwrap();
@@ -116,9 +115,10 @@ fn should_watch_extrinsic() {
res,
Some(r#"{"jsonrpc":"2.0","method":"test","params":{"result":"ready","subscription":1}}"#.into())
);
let h = blake2_256(&replacement.encode());
assert_eq!(
runtime.block_on(data.into_future()).unwrap().0,
Some(r#"{"jsonrpc":"2.0","method":"test","params":{"result":{"usurped":"0x53daed816610aa6b22dedbcee43aba44a7ca7155cc71f2919c5e79ebbc7de58c"},"subscription":1}}"#.into())
Some(format!(r#"{{"jsonrpc":"2.0","method":"test","params":{{"result":{{"usurped":"0x{}"}},"subscription":1}}}}"#, HexDisplay::from(&h)))
);
}
@@ -132,7 +132,7 @@ fn should_return_pending_extrinsics() {
pool: pool.clone(),
subscriptions: Subscriptions::new(runtime.executor()),
};
let ex = uxt(Keyring::Alice, 0);
let ex = uxt(AccountKeyring::Alice, 0);
AuthorApi::submit_extrinsic(&p, ex.encode().into()).unwrap();
assert_matches!(
p.pending_extrinsics(),
+17 -14
View File
@@ -17,10 +17,10 @@
use super::*;
use self::error::{Error, ErrorKind};
use sr_io::twox_128;
use assert_matches::assert_matches;
use consensus::BlockOrigin;
use rustc_hex::FromHex;
use test_client::{self, runtime, keyring::Keyring, TestClient, BlockBuilderExt};
use test_client::{self, runtime, AccountKeyring, TestClient, BlockBuilderExt};
#[test]
fn should_return_storage() {
@@ -64,8 +64,8 @@ fn should_notify_about_storage_changes() {
let mut builder = api.client.new_block().unwrap();
builder.push_transfer(runtime::Transfer {
from: Keyring::Alice.to_raw_public().into(),
to: Keyring::Ferdie.to_raw_public().into(),
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 42,
nonce: 0,
}).unwrap();
@@ -88,8 +88,10 @@ fn should_send_initial_storage_changes_and_notifications() {
{
let api = State::new(Arc::new(test_client::new()), Subscriptions::new(remote));
let alice_balance_key = twox_128(&test_runtime::system::balance_of_key(AccountKeyring::Alice.into()));
api.subscribe_storage(Default::default(), subscriber, Some(vec![
StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap()),
StorageKey(alice_balance_key.to_vec()),
]).into());
// assert id assigned
@@ -97,8 +99,8 @@ fn should_send_initial_storage_changes_and_notifications() {
let mut builder = api.client.new_block().unwrap();
builder.push_transfer(runtime::Transfer {
from: Keyring::Alice.to_raw_public().into(),
to: Keyring::Ferdie.to_raw_public().into(),
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 42,
nonce: 0,
}).unwrap();
@@ -131,8 +133,8 @@ fn should_query_storage() {
let add_block = |nonce| {
let mut builder = client.new_block().unwrap();
builder.push_transfer(runtime::Transfer {
from: Keyring::Alice.to_raw_public().into(),
to: Keyring::Ferdie.to_raw_public().into(),
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 42,
nonce,
}).unwrap();
@@ -145,13 +147,14 @@ fn should_query_storage() {
let block2_hash = add_block(1);
let genesis_hash = client.genesis_hash();
let alice_balance_key = twox_128(&test_runtime::system::balance_of_key(AccountKeyring::Alice.into()));
let mut expected = vec![
StorageChangeSet {
block: genesis_hash,
changes: vec![
(
StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap()),
StorageKey(alice_balance_key.to_vec()),
Some(StorageData(vec![232, 3, 0, 0, 0, 0, 0, 0]))
),
],
@@ -160,7 +163,7 @@ fn should_query_storage() {
block: block1_hash,
changes: vec![
(
StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap()),
StorageKey(alice_balance_key.to_vec()),
Some(StorageData(vec![190, 3, 0, 0, 0, 0, 0, 0]))
),
],
@@ -169,7 +172,7 @@ fn should_query_storage() {
// Query changes only up to block1
let result = api.query_storage(
vec![StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap())],
vec![StorageKey(alice_balance_key.to_vec())],
genesis_hash,
Some(block1_hash).into(),
);
@@ -178,7 +181,7 @@ fn should_query_storage() {
// Query all changes
let result = api.query_storage(
vec![StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap())],
vec![StorageKey(alice_balance_key.to_vec())],
genesis_hash,
None.into(),
);
@@ -187,7 +190,7 @@ fn should_query_storage() {
block: block2_hash,
changes: vec![
(
StorageKey("a52da2b7c269da1366b3ed1cdb7299ce".from_hex().unwrap()),
StorageKey(alice_balance_key.to_vec()),
Some(StorageData(vec![148, 3, 0, 0, 0, 0, 0, 0]))
),
],