|
| 1 | +use crate::commands::shared::{ |
| 2 | + derive_keypair, init_logger, store_key_in_keystore, KeystoreOptions, |
| 3 | +}; |
| 4 | +use bip39::Mnemonic; |
| 5 | +use clap::Parser; |
| 6 | +use sc_cli::{Error, KeystoreParams}; |
| 7 | +use sc_service::config::KeystoreConfig; |
| 8 | +use sp_core::crypto::{ExposeSecret, SecretString}; |
| 9 | +use sp_core::Pair; |
| 10 | +use sp_domains::DomainId; |
| 11 | +use std::path::PathBuf; |
| 12 | +use tracing::{info, warn}; |
| 13 | + |
| 14 | +/// Options for creating domain key |
| 15 | +#[derive(Debug, Parser)] |
| 16 | +pub struct CreateDomainKeyOptions { |
| 17 | + /// Base path where to store node files |
| 18 | + #[arg(long)] |
| 19 | + base_path: PathBuf, |
| 20 | + /// ID of the domain to store key for |
| 21 | + #[arg(long, required = true)] |
| 22 | + domain_id: DomainId, |
| 23 | + /// Options for domain keystore |
| 24 | + #[clap(flatten)] |
| 25 | + keystore_options: KeystoreOptions, |
| 26 | +} |
| 27 | + |
| 28 | +pub fn create_domain_key(options: CreateDomainKeyOptions) -> Result<(), Error> { |
| 29 | + init_logger(); |
| 30 | + |
| 31 | + let CreateDomainKeyOptions { |
| 32 | + base_path, |
| 33 | + domain_id, |
| 34 | + keystore_options, |
| 35 | + } = options; |
| 36 | + let domain_path = base_path.join("domains").join(domain_id.to_string()); |
| 37 | + |
| 38 | + let keystore_params = KeystoreParams { |
| 39 | + keystore_path: None, |
| 40 | + password_interactive: keystore_options.keystore_password_interactive, |
| 41 | + password: keystore_options.keystore_password, |
| 42 | + password_filename: keystore_options.keystore_password_filename, |
| 43 | + }; |
| 44 | + |
| 45 | + let keystore_config = keystore_params.keystore_config(&domain_path)?; |
| 46 | + |
| 47 | + let (path, password) = match &keystore_config { |
| 48 | + KeystoreConfig::Path { path, password, .. } => (path.clone(), password.clone()), |
| 49 | + KeystoreConfig::InMemory => { |
| 50 | + unreachable!("Just constructed non-memory keystore config; qed"); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + let has_password = password.is_some(); |
| 55 | + |
| 56 | + let mnemonic = Mnemonic::generate(12) |
| 57 | + .map_err(|error| Error::Input(format!("Mnemonic generation failed: {error}")))?; |
| 58 | + let phrase = SecretString::from(mnemonic.to_string()); |
| 59 | + |
| 60 | + let public_key = derive_keypair(&phrase, &password)?.public(); |
| 61 | + |
| 62 | + store_key_in_keystore(path, &phrase, password)?; |
| 63 | + |
| 64 | + info!("Successfully generated and imported keypair!"); |
| 65 | + info!("Public key: {}", hex::encode(public_key.0)); |
| 66 | + info!("Seed: \"{}\"", phrase.expose_secret()); |
| 67 | + if has_password { |
| 68 | + info!("Password: as specified in CLI options"); |
| 69 | + } |
| 70 | + warn!("⚠ Make sure to keep ^ seed secure and never share with anyone to avoid loss of funds ⚠"); |
| 71 | + |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +/// Options for inserting domain key |
| 76 | +#[derive(Debug, Parser)] |
| 77 | +pub struct InsertDomainKeyOptions { |
| 78 | + /// Base path where to store node files |
| 79 | + #[arg(long)] |
| 80 | + base_path: PathBuf, |
| 81 | + /// ID of the domain to store key for |
| 82 | + #[arg(long, required = true)] |
| 83 | + domain_id: DomainId, |
| 84 | + /// Operator secret key URI to insert into keystore. |
| 85 | + /// |
| 86 | + /// Example: "//Alice". |
| 87 | + /// |
| 88 | + /// If the value is a file, the file content is used as URI. |
| 89 | + #[arg(long, required = true)] |
| 90 | + keystore_suri: SecretString, |
| 91 | + /// Options for domain keystore |
| 92 | + #[clap(flatten)] |
| 93 | + keystore_options: KeystoreOptions, |
| 94 | +} |
| 95 | + |
| 96 | +pub fn insert_domain_key(options: InsertDomainKeyOptions) -> Result<(), Error> { |
| 97 | + init_logger(); |
| 98 | + |
| 99 | + let InsertDomainKeyOptions { |
| 100 | + base_path, |
| 101 | + domain_id, |
| 102 | + keystore_suri, |
| 103 | + keystore_options, |
| 104 | + } = options; |
| 105 | + let domain_path = base_path.join("domains").join(domain_id.to_string()); |
| 106 | + |
| 107 | + let keystore_params = KeystoreParams { |
| 108 | + keystore_path: None, |
| 109 | + password_interactive: keystore_options.keystore_password_interactive, |
| 110 | + password: keystore_options.keystore_password, |
| 111 | + password_filename: keystore_options.keystore_password_filename, |
| 112 | + }; |
| 113 | + |
| 114 | + let keystore_config = keystore_params.keystore_config(&domain_path)?; |
| 115 | + |
| 116 | + let (path, password) = match &keystore_config { |
| 117 | + KeystoreConfig::Path { path, password, .. } => (path.clone(), password.clone()), |
| 118 | + KeystoreConfig::InMemory => { |
| 119 | + unreachable!("Just constructed non-memory keystore config; qed"); |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + store_key_in_keystore(path, &keystore_suri, password)?; |
| 124 | + |
| 125 | + info!("Success"); |
| 126 | + |
| 127 | + Ok(()) |
| 128 | +} |
0 commit comments