Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added polkadot to genesis keys script #2352

Merged
merged 2 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions engine/generate-genesis-keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ hex = "0.4.3"
bincode = "1.3.3"
csv = "1.1.6"
serde_json = "1.0"
serde = {version = "1.0", features = ["derive"]}
state-chain-runtime = {path = "../../state-chain/runtime"}
chainflip-node = {path = "../../state-chain/node"}
# have to use the older version until secp256k1 updates its dependency (https://github.com/rust-bitcoin/rust-secp256k1/issues/328)
Expand All @@ -32,3 +33,6 @@ version = "0.18.0"
# https://github.com/rust-rocksdb/rust-rocksdb/pull/555
default-features = false
features = ["lz4"]

[dev-dependencies]
tempfile = "3.3.0"
61 changes: 52 additions & 9 deletions engine/generate-genesis-keys/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use chainflip_engine::{
logging::utils::new_discard_logger,
multisig::{client::keygen::generate_key_data_until_compatible, eth, PersistentKeyDB, Rng},
multisig::{
client::keygen::generate_key_data_until_compatible, eth::EthSigning,
polkadot::PolkadotSigning, CryptoScheme, PersistentKeyDB, Rng,
},
};
use chainflip_node::chain_spec::use_chainflip_account_id_encoding;
use rand_legacy::FromEntropy;
Expand All @@ -11,12 +14,20 @@ use std::{
path::Path,
};

use serde::{Deserialize, Serialize};

const ENV_VAR_INPUT_FILE: &str = "GENESIS_NODE_IDS";

const DB_EXTENSION: &str = "db";

type Record = (String, AccountId);

#[derive(Serialize, Deserialize)]
struct AggKeys {
eth_agg_key: String,
dot_agg_key: String,
}

fn load_node_ids_from_csv<R>(mut reader: csv::Reader<R>) -> HashMap<AccountId, String>
where
R: io::Read,
Expand Down Expand Up @@ -59,7 +70,19 @@ fn main() {
.expect("Should read from csv file"),
);

let (eth_key_id, key_shares) = generate_key_data_until_compatible::<eth::EthSigning>(
let agg_keys = AggKeys {
eth_agg_key: generate_and_save_keys::<EthSigning>(&node_id_to_name_map),
dot_agg_key: generate_and_save_keys::<PolkadotSigning>(&node_id_to_name_map),
};

// output to stdout - CI can read the json from stdout
println!("{}", serde_json::to_string_pretty(&agg_keys).expect("Should prettify json"));
}

fn generate_and_save_keys<Crypto: CryptoScheme>(
node_id_to_name_map: &HashMap<AccountId, String>,
) -> String {
let (key_id, key_shares) = generate_key_data_until_compatible::<Crypto>(
BTreeSet::from_iter(node_id_to_name_map.keys().cloned()),
20,
Rng::from_entropy(),
Expand All @@ -80,13 +103,33 @@ fn main() {
&new_discard_logger(),
)
.expect("Should create database at latest version")
.update_key::<eth::EthSigning>(&eth_key_id, &key_share);
.update_key::<Crypto>(&key_id, &key_share);
}

// output to stdout - CI can read the json from stdout
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({ "eth_agg_key": eth_key_id.to_string() }))
.expect("Should prettify_json")
);
key_id.to_string()
}

#[cfg(test)]
#[test]
fn should_generate_and_save_all_keys() {
let tempdir = tempfile::TempDir::new().unwrap();
let db_path = tempdir.path().to_owned().join("test");

// Using the db_path as the node name, so the db is created within the temp directory
let node_id_to_name_map =
HashMap::from_iter(vec![(AccountId::new([0; 32]), db_path.to_string_lossy().to_string())]);

generate_and_save_keys::<EthSigning>(&node_id_to_name_map);
generate_and_save_keys::<PolkadotSigning>(&node_id_to_name_map);

// Open the db and check the keys
let db = PersistentKeyDB::new_and_migrate_to_latest(
&db_path.with_extension(DB_EXTENSION),
None,
&new_discard_logger(),
)
.unwrap();

assert_eq!(db.load_keys::<EthSigning>().len(), 1);
assert_eq!(db.load_keys::<PolkadotSigning>().len(), 1);
}
2 changes: 1 addition & 1 deletion engine/src/multisig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod crypto;
/// Storage for the keys
pub mod db;

pub use crypto::{eth, ChainTag, CryptoScheme, Rng};
pub use crypto::{eth, polkadot, ChainTag, CryptoScheme, Rng};

#[cfg(test)]
mod tests;
Expand Down