zombienet_orchestrator/generators/
keystore.rs

1use std::{
2    path::{Path, PathBuf},
3    vec,
4};
5
6use hex::encode;
7use support::{constants::THIS_IS_A_BUG, fs::FileSystem};
8
9use super::errors::GeneratorError;
10use crate::{shared::types::NodeAccounts, ScopedFilesystem};
11
12const PREFIXES: [&str; 11] = [
13    "aura", "babe", "imon", "gran", "audi", "asgn", "para", "beef", // Beffy
14    "nmbs", // Nimbus
15    "rand", // Randomness (Moonbeam)
16    "rate", // Equilibrium rate module
17];
18
19pub async fn generate<'a, T>(
20    acc: &NodeAccounts,
21    node_files_path: impl AsRef<Path>,
22    scoped_fs: &ScopedFilesystem<'a, T>,
23    asset_hub_polkadot: bool,
24) -> Result<Vec<PathBuf>, GeneratorError>
25where
26    T: FileSystem,
27{
28    // Create local keystore
29    scoped_fs.create_dir_all(node_files_path.as_ref()).await?;
30    let mut filenames = vec![];
31
32    let f = PREFIXES.map(|k| {
33        // let filename = encode(k);
34
35        let filename = match k {
36            "aura" if asset_hub_polkadot => {
37                let pk = acc
38                    .accounts
39                    .get("ed")
40                    .expect(&format!("Key 'ed' should be set for node {THIS_IS_A_BUG}"))
41                    .public_key
42                    .as_str();
43                format!("{}{}", encode(k), pk)
44            },
45            "babe" | "imon" | "audi" | "asgn" | "para" | "nmbs" | "rand" | "aura" => {
46                let pk = acc
47                    .accounts
48                    .get("sr")
49                    .expect(&format!("Key 'sr' should be set for node {THIS_IS_A_BUG}"))
50                    .public_key
51                    .as_str();
52                format!("{}{}", encode(k), pk)
53            },
54            "gran" | "rate" => {
55                let pk = acc
56                    .accounts
57                    .get("ed")
58                    .expect(&format!("Key 'ed' should be set for node {THIS_IS_A_BUG}"))
59                    .public_key
60                    .as_str();
61                format!("{}{}", encode(k), pk)
62            },
63            "beef" => {
64                let pk = acc
65                    .accounts
66                    .get("ec")
67                    .expect(&format!("Key 'ec' should be set for node {THIS_IS_A_BUG}"))
68                    .public_key
69                    .as_str();
70                format!("{}{}", encode(k), pk)
71            },
72            _ => unreachable!(),
73        };
74        let file_path = PathBuf::from(format!(
75            "{}/{}",
76            node_files_path.as_ref().to_string_lossy(),
77            filename
78        ));
79        filenames.push(PathBuf::from(filename));
80        let content = format!("\"{}\"", acc.seed);
81        scoped_fs.write(file_path, content)
82    });
83
84    // TODO: implement logic for filter keys
85    //   node.keystoreKeyTypes?.forEach((key_type: string) => {
86    // if (DEFAULT_KEYSTORE_KEY_TYPES.includes(key_type))
87    // keystore_key_types[key_type] = default_keystore_key_types[key_type];
88    // });
89
90    futures::future::try_join_all(f).await?;
91    Ok(filenames)
92}