mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 04:01:10 +00:00
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:
@@ -18,12 +18,18 @@
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::fs::{self, File};
|
||||
use std::io::{self, Write};
|
||||
use std::{collections::HashMap, path::PathBuf, fs::{self, File}, io::{self, Write}, sync::Arc};
|
||||
|
||||
use primitives::crypto::{KeyTypeId, Pair, Public};
|
||||
use primitives::{
|
||||
crypto::{KeyTypeId, Pair as PairT, Public, IsWrappedBy, Protected}, traits::BareCryptoStore,
|
||||
};
|
||||
|
||||
use app_crypto::{AppKey, AppPublic, AppPair, ed25519, sr25519};
|
||||
|
||||
use parking_lot::RwLock;
|
||||
|
||||
/// Keystore pointer
|
||||
pub type KeyStorePtr = Arc<RwLock<Store>>;
|
||||
|
||||
/// Keystore error.
|
||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||
@@ -41,6 +47,9 @@ pub enum Error {
|
||||
/// Invalid seed
|
||||
#[display(fmt="Invalid seed")]
|
||||
InvalidSeed,
|
||||
/// Keystore unavailable
|
||||
#[display(fmt="Keystore unavailable")]
|
||||
Unavailable,
|
||||
}
|
||||
|
||||
/// Keystore Result
|
||||
@@ -57,80 +66,159 @@ impl std::error::Error for Error {
|
||||
}
|
||||
|
||||
/// Key store.
|
||||
///
|
||||
/// Stores key pairs in a file system store + short lived key pairs in memory.
|
||||
///
|
||||
/// Every pair that is being generated by a `seed`, will be placed in memory.
|
||||
pub struct Store {
|
||||
path: PathBuf,
|
||||
additional: HashMap<(KeyTypeId, Vec<u8>), Vec<u8>>,
|
||||
password: Option<Protected<String>>,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
/// Create a new store at the given path.
|
||||
pub fn open(path: PathBuf) -> Result<Self> {
|
||||
/// Open the store at the given path.
|
||||
///
|
||||
/// Optionally takes a password that will be used to encrypt/decrypt the keys.
|
||||
pub fn open<T: Into<PathBuf>>(path: T, password: Option<Protected<String>>) -> Result<KeyStorePtr> {
|
||||
let path = path.into();
|
||||
fs::create_dir_all(&path)?;
|
||||
Ok(Store { path, additional: HashMap::new() })
|
||||
|
||||
let instance = Self { path, additional: HashMap::new(), password };
|
||||
Ok(Arc::new(RwLock::new(instance)))
|
||||
}
|
||||
|
||||
fn get_pair<TPair: Pair>(&self, public: &TPair::Public) -> Result<Option<TPair>> {
|
||||
let key = (TPair::KEY_TYPE, public.to_raw_vec());
|
||||
if let Some(bytes) = self.additional.get(&key) {
|
||||
let pair = TPair::from_seed_slice(bytes)
|
||||
.map_err(|_| Error::InvalidSeed)?;
|
||||
return Ok(Some(pair));
|
||||
}
|
||||
Ok(None)
|
||||
/// Get the public/private key pair for the given public key and key type.
|
||||
fn get_additional_pair<Pair: PairT>(
|
||||
&self,
|
||||
public: &Pair::Public,
|
||||
key_type: KeyTypeId,
|
||||
) -> Result<Option<Pair>> {
|
||||
let key = (key_type, public.to_raw_vec());
|
||||
self.additional
|
||||
.get(&key)
|
||||
.map(|bytes| Pair::from_seed_slice(bytes).map_err(|_| Error::InvalidSeed))
|
||||
.transpose()
|
||||
}
|
||||
|
||||
fn insert_pair<TPair: Pair>(&mut self, pair: &TPair) {
|
||||
let key = (TPair::KEY_TYPE, pair.public().to_raw_vec());
|
||||
/// Insert the given public/private key pair with the given key type.
|
||||
///
|
||||
/// Does not place it into the file system store.
|
||||
fn insert_ephemeral_pair<Pair: PairT>(&mut self, pair: &Pair, key_type: KeyTypeId) {
|
||||
let key = (key_type, pair.public().to_raw_vec());
|
||||
self.additional.insert(key, pair.to_raw_vec());
|
||||
}
|
||||
|
||||
/// Generate a new key, placing it into the store.
|
||||
pub fn generate<TPair: Pair>(&self, password: &str) -> Result<TPair> {
|
||||
let (pair, phrase, _) = TPair::generate_with_phrase(Some(password));
|
||||
let mut file = File::create(self.key_file_path::<TPair>(&pair.public()))?;
|
||||
::serde_json::to_writer(&file, &phrase)?;
|
||||
/// Insert a new key with anonymous crypto.
|
||||
///
|
||||
/// Places it into the file system store.
|
||||
fn insert_unknown(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<()> {
|
||||
let mut file = File::create(self.key_file_path(public, key_type)).map_err(Error::Io)?;
|
||||
serde_json::to_writer(&file, &suri).map_err(Error::Json)?;
|
||||
file.flush().map_err(Error::Io)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Insert a new key.
|
||||
///
|
||||
/// Places it into the file system store.
|
||||
pub fn insert_by_type<Pair: PairT>(&self, key_type: KeyTypeId, suri: &str) -> Result<Pair> {
|
||||
let pair = Pair::from_string(
|
||||
suri,
|
||||
self.password.as_ref().map(|p| &***p)
|
||||
).map_err(|_| Error::InvalidSeed)?;
|
||||
self.insert_unknown(key_type, suri, pair.public().as_slice())
|
||||
.map_err(|_| Error::Unavailable)?;
|
||||
Ok(pair)
|
||||
}
|
||||
|
||||
/// Insert a new key.
|
||||
///
|
||||
/// Places it into the file system store.
|
||||
pub fn insert<Pair: AppPair>(&self, suri: &str) -> Result<Pair> {
|
||||
self.insert_by_type::<Pair::Generic>(Pair::ID, suri).map(Into::into)
|
||||
}
|
||||
|
||||
/// Generate a new key.
|
||||
///
|
||||
/// Places it into the file system store.
|
||||
pub fn generate_by_type<Pair: PairT>(&self, key_type: KeyTypeId) -> Result<Pair> {
|
||||
let (pair, phrase, _) = Pair::generate_with_phrase(self.password.as_ref().map(|p| &***p));
|
||||
let mut file = File::create(self.key_file_path(pair.public().as_slice(), key_type))?;
|
||||
serde_json::to_writer(&file, &phrase)?;
|
||||
file.flush()?;
|
||||
Ok(pair)
|
||||
}
|
||||
|
||||
/// Create a new key from seed. Do not place it into the store.
|
||||
pub fn generate_from_seed<TPair: Pair>(&mut self, seed: &str) -> Result<TPair> {
|
||||
let pair = TPair::from_string(seed, None)
|
||||
.ok().ok_or(Error::InvalidSeed)?;
|
||||
self.insert_pair(&pair);
|
||||
/// Generate a new key.
|
||||
///
|
||||
/// Places it into the file system store.
|
||||
pub fn generate<Pair: AppPair>(&self) -> Result<Pair> {
|
||||
self.generate_by_type::<Pair::Generic>(Pair::ID).map(Into::into)
|
||||
}
|
||||
|
||||
/// Create a new key from seed.
|
||||
///
|
||||
/// Does not place it into the file system store.
|
||||
pub fn insert_ephemeral_from_seed_by_type<Pair: PairT>(
|
||||
&mut self,
|
||||
seed: &str,
|
||||
key_type: KeyTypeId,
|
||||
) -> Result<Pair> {
|
||||
let pair = Pair::from_string(seed, None).map_err(|_| Error::InvalidSeed)?;
|
||||
self.insert_ephemeral_pair(&pair, key_type);
|
||||
Ok(pair)
|
||||
}
|
||||
|
||||
/// Load a key file with given public key.
|
||||
pub fn load<TPair: Pair>(&self, public: &TPair::Public, password: &str) -> Result<TPair> {
|
||||
if let Some(pair) = self.get_pair(public)? {
|
||||
/// Create a new key from seed.
|
||||
///
|
||||
/// Does not place it into the file system store.
|
||||
pub fn insert_ephemeral_from_seed<Pair: AppPair>(&mut self, seed: &str) -> Result<Pair> {
|
||||
self.insert_ephemeral_from_seed_by_type::<Pair::Generic>(seed, Pair::ID).map(Into::into)
|
||||
}
|
||||
|
||||
/// Get a key pair for the given public key and key type.
|
||||
pub fn key_pair_by_type<Pair: PairT>(&self,
|
||||
public: &Pair::Public,
|
||||
key_type: KeyTypeId,
|
||||
) -> Result<Pair> {
|
||||
if let Some(pair) = self.get_additional_pair(public, key_type)? {
|
||||
return Ok(pair)
|
||||
}
|
||||
|
||||
let path = self.key_file_path::<TPair>(public);
|
||||
let path = self.key_file_path(public.as_slice(), key_type);
|
||||
let file = File::open(path)?;
|
||||
|
||||
let phrase: String = ::serde_json::from_reader(&file)?;
|
||||
let (pair, _) = TPair::from_phrase(&phrase, Some(password))
|
||||
.ok().ok_or(Error::InvalidPhrase)?;
|
||||
if &pair.public() != public {
|
||||
return Err(Error::InvalidPassword);
|
||||
let phrase: String = serde_json::from_reader(&file)?;
|
||||
let pair = Pair::from_phrase(
|
||||
&phrase,
|
||||
self.password.as_ref().map(|p| &***p),
|
||||
).map_err(|_| Error::InvalidPhrase)?.0;
|
||||
|
||||
if &pair.public() == public {
|
||||
Ok(pair)
|
||||
} else {
|
||||
Err(Error::InvalidPassword)
|
||||
}
|
||||
Ok(pair)
|
||||
}
|
||||
|
||||
/// Get public keys of all stored keys.
|
||||
pub fn contents<TPublic: Public>(&self) -> Result<Vec<TPublic>> {
|
||||
/// Get a key pair for the given public key.
|
||||
pub fn key_pair<Pair: AppPair>(&self, public: &<Pair as AppKey>::Public) -> Result<Pair> {
|
||||
self.key_pair_by_type::<Pair::Generic>(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into)
|
||||
}
|
||||
|
||||
/// Get public keys of all stored keys that match the given key type.
|
||||
pub fn public_keys_by_type<TPublic: Public>(&self, key_type: KeyTypeId) -> Result<Vec<TPublic>> {
|
||||
let mut public_keys: Vec<TPublic> = self.additional.keys()
|
||||
.filter_map(|(ty, public)| {
|
||||
if *ty != TPublic::KEY_TYPE {
|
||||
return None
|
||||
if *ty == key_type {
|
||||
Some(TPublic::from_slice(public))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
Some(TPublic::from_slice(public))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let key_type: [u8; 4] = TPublic::KEY_TYPE.to_le_bytes();
|
||||
for entry in fs::read_dir(&self.path)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
@@ -139,7 +227,7 @@ impl Store {
|
||||
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
|
||||
match hex::decode(name) {
|
||||
Ok(ref hex) => {
|
||||
if hex[0..4] != key_type { continue }
|
||||
if &hex[0..4] != &key_type.0 { continue }
|
||||
let public = TPublic::from_slice(&hex[4..]);
|
||||
public_keys.push(public);
|
||||
}
|
||||
@@ -151,48 +239,165 @@ impl Store {
|
||||
Ok(public_keys)
|
||||
}
|
||||
|
||||
fn key_file_path<TPair: Pair>(&self, public: &TPair::Public) -> PathBuf {
|
||||
/// Get public keys of all stored keys that match the key type.
|
||||
///
|
||||
/// This will just use the type of the public key (a list of which to be returned) in order
|
||||
/// to determine the key type. Unless you use a specialised application-type public key, then
|
||||
/// this only give you keys registered under generic cryptography, and will not return keys
|
||||
/// registered under the application type.
|
||||
pub fn public_keys<Public: AppPublic>(&self) -> Result<Vec<Public>> {
|
||||
self.public_keys_by_type::<Public::Generic>(Public::ID)
|
||||
.map(|v| v.into_iter().map(Into::into).collect())
|
||||
}
|
||||
|
||||
/// Returns the file path for the given public key and key type.
|
||||
fn key_file_path(&self, public: &[u8], key_type: KeyTypeId) -> PathBuf {
|
||||
let mut buf = self.path.clone();
|
||||
let bytes: [u8; 4] = TPair::KEY_TYPE.to_le_bytes();
|
||||
let key_type = hex::encode(bytes);
|
||||
let key = hex::encode(public.as_slice());
|
||||
let key_type = hex::encode(key_type.0);
|
||||
let key = hex::encode(public);
|
||||
buf.push(key_type + key.as_str());
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
impl BareCryptoStore for Store {
|
||||
fn sr25519_public_keys(&self, key_type: KeyTypeId) -> Vec<sr25519::Public> {
|
||||
self.public_keys_by_type::<sr25519::Public>(key_type).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn sr25519_generate_new(
|
||||
&mut self,
|
||||
id: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> std::result::Result<sr25519::Public, String> {
|
||||
let pair = match seed {
|
||||
Some(seed) => self.insert_ephemeral_from_seed_by_type::<sr25519::Pair>(seed, id),
|
||||
None => self.generate_by_type::<sr25519::Pair>(id),
|
||||
}.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(pair.public())
|
||||
}
|
||||
|
||||
fn sr25519_key_pair(&self, id: KeyTypeId, pub_key: &sr25519::Public) -> Option<sr25519::Pair> {
|
||||
self.key_pair_by_type::<sr25519::Pair>(pub_key, id).ok()
|
||||
}
|
||||
|
||||
fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public> {
|
||||
self.public_keys_by_type::<ed25519::Public>(key_type).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn ed25519_generate_new(
|
||||
&mut self,
|
||||
id: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> std::result::Result<ed25519::Public, String> {
|
||||
let pair = match seed {
|
||||
Some(seed) => self.insert_ephemeral_from_seed_by_type::<ed25519::Pair>(seed, id),
|
||||
None => self.generate_by_type::<ed25519::Pair>(id),
|
||||
}.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(pair.public())
|
||||
}
|
||||
|
||||
fn ed25519_key_pair(&self, id: KeyTypeId, pub_key: &ed25519::Public) -> Option<ed25519::Pair> {
|
||||
self.key_pair_by_type::<ed25519::Pair>(pub_key, id).ok()
|
||||
}
|
||||
|
||||
fn insert_unknown(&mut self, key_type: KeyTypeId, suri: &str, public: &[u8])
|
||||
-> std::result::Result<(), ()>
|
||||
{
|
||||
Store::insert_unknown(self, key_type, suri, public).map_err(|_| ())
|
||||
}
|
||||
|
||||
fn password(&self) -> Option<&str> {
|
||||
self.password.as_ref().map(|x| x.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempdir::TempDir;
|
||||
use primitives::ed25519;
|
||||
use primitives::crypto::Ss58Codec;
|
||||
|
||||
#[test]
|
||||
fn basic_store() {
|
||||
let temp_dir = TempDir::new("keystore").unwrap();
|
||||
let store = Store::open(temp_dir.path().to_owned()).unwrap();
|
||||
let store = Store::open(temp_dir.path(), None).unwrap();
|
||||
|
||||
assert!(store.contents::<ed25519::Public>().unwrap().is_empty());
|
||||
assert!(store.read().public_keys::<ed25519::AppPublic>().unwrap().is_empty());
|
||||
|
||||
let key: ed25519::Pair = store.generate("thepassword").unwrap();
|
||||
let key2: ed25519::Pair = store.load(&key.public(), "thepassword").unwrap();
|
||||
|
||||
assert!(store.load::<ed25519::Pair>(&key.public(), "notthepassword").is_err());
|
||||
let key: ed25519::AppPair = store.write().generate().unwrap();
|
||||
let key2: ed25519::AppPair = store.read().key_pair(&key.public()).unwrap();
|
||||
|
||||
assert_eq!(key.public(), key2.public());
|
||||
|
||||
assert_eq!(store.contents::<ed25519::Public>().unwrap()[0], key.public());
|
||||
assert_eq!(store.read().public_keys::<ed25519::AppPublic>().unwrap()[0], key.public());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_from_seed() {
|
||||
fn test_insert_ephemeral_from_seed() {
|
||||
let temp_dir = TempDir::new("keystore").unwrap();
|
||||
let mut store = Store::open(temp_dir.path().to_owned()).unwrap();
|
||||
let store = Store::open(temp_dir.path(), None).unwrap();
|
||||
|
||||
let pair: ed25519::Pair = store
|
||||
.generate_from_seed("0x3d97c819d68f9bafa7d6e79cb991eebcd77d966c5334c0b94d9e1fa7ad0869dc")
|
||||
let pair: ed25519::AppPair = store
|
||||
.write()
|
||||
.insert_ephemeral_from_seed("0x3d97c819d68f9bafa7d6e79cb991eebcd77d966c5334c0b94d9e1fa7ad0869dc")
|
||||
.unwrap();
|
||||
assert_eq!("5DKUrgFqCPV8iAXx9sjy1nyBygQCeiUYRFWurZGhnrn3HJCA", pair.public().to_ss58check());
|
||||
assert_eq!(
|
||||
"5DKUrgFqCPV8iAXx9sjy1nyBygQCeiUYRFWurZGhnrn3HJCA",
|
||||
pair.public().to_ss58check()
|
||||
);
|
||||
|
||||
drop(store);
|
||||
let store = Store::open(temp_dir.path(), None).unwrap();
|
||||
// Keys generated from seed should not be persisted!
|
||||
assert!(store.read().key_pair::<ed25519::AppPair>(&pair.public()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn password_being_used() {
|
||||
let password = String::from("password");
|
||||
let temp_dir = TempDir::new("keystore").unwrap();
|
||||
let store = Store::open(temp_dir.path(), Some(password.clone().into())).unwrap();
|
||||
|
||||
let pair: ed25519::AppPair = store.write().generate().unwrap();
|
||||
assert_eq!(
|
||||
pair.public(),
|
||||
store.read().key_pair::<ed25519::AppPair>(&pair.public()).unwrap().public(),
|
||||
);
|
||||
|
||||
// Without the password the key should not be retrievable
|
||||
let store = Store::open(temp_dir.path(), None).unwrap();
|
||||
assert!(store.read().key_pair::<ed25519::AppPair>(&pair.public()).is_err());
|
||||
|
||||
let store = Store::open(temp_dir.path(), Some(password.into())).unwrap();
|
||||
assert_eq!(
|
||||
pair.public(),
|
||||
store.read().key_pair::<ed25519::AppPair>(&pair.public()).unwrap().public(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn public_keys_are_returned() {
|
||||
let temp_dir = TempDir::new("keystore").unwrap();
|
||||
let store = Store::open(temp_dir.path(), None).unwrap();
|
||||
|
||||
let mut public_keys = Vec::new();
|
||||
for i in 0..10 {
|
||||
public_keys.push(store.write().generate::<ed25519::AppPair>().unwrap().public());
|
||||
public_keys.push(store.write().insert_ephemeral_from_seed::<ed25519::AppPair>(
|
||||
&format!("0x3d97c819d68f9bafa7d6e79cb991eebcd7{}d966c5334c0b94d9e1fa7ad0869dc", i),
|
||||
).unwrap().public());
|
||||
}
|
||||
|
||||
// Generate a key of a different type
|
||||
store.write().generate::<sr25519::AppPair>().unwrap();
|
||||
|
||||
public_keys.sort();
|
||||
let mut store_pubs = store.read().public_keys::<ed25519::AppPublic>().unwrap();
|
||||
store_pubs.sort();
|
||||
|
||||
assert_eq!(public_keys, store_pubs);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user