-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New command - create-dev-account (#108)
- Loading branch information
Showing
8 changed files
with
941 additions
and
262 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use strum::{EnumDiscriminants, EnumIter, EnumMessage}; | ||
|
||
pub mod use_random_account_id; | ||
|
||
#[derive(Debug, Clone, interactive_clap::InteractiveClap)] | ||
#[interactive_clap(context = near_cli_rs::GlobalContext)] | ||
pub struct CreateAccount { | ||
#[interactive_clap(subcommand)] | ||
account_actions: CreateAccountMethod, | ||
} | ||
|
||
#[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)] | ||
#[interactive_clap(context = near_cli_rs::GlobalContext)] | ||
#[strum_discriminants(derive(EnumMessage, EnumIter))] | ||
/// How do you cover the costs of account creation? | ||
pub enum CreateAccountMethod { | ||
#[strum_discriminants(strum( | ||
message = "use-random-account-id - I would like to create a random account (useful for quick start development)" | ||
))] | ||
/// Use a random name for account (useful for quick start development) | ||
UseRandomAccountId(self::use_random_account_id::RandomAccount), | ||
#[strum_discriminants(strum( | ||
message = "use-specific-account-id - I would like to create a specific account" | ||
))] | ||
/// I would like to create a specific account | ||
UseSpecificAccountId( | ||
near_cli_rs::commands::account::create_account::sponsor_by_faucet_service::NewAccount, | ||
), | ||
} |
67 changes: 67 additions & 0 deletions
67
cargo-near/src/commands/create_dev_account/use_random_account_id.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::str::FromStr; | ||
|
||
use color_eyre::eyre::ContextCompat; | ||
use names::Generator; | ||
|
||
use near_cli_rs::commands::account::create_account::sponsor_by_faucet_service::{ | ||
add_key, before_creating_account, network, NewAccountContext, | ||
}; | ||
|
||
#[derive(Debug, Clone, interactive_clap::InteractiveClap)] | ||
#[interactive_clap(input_context = near_cli_rs::GlobalContext)] | ||
#[interactive_clap(output_context = RandomAccountContext)] | ||
pub struct RandomAccount { | ||
#[interactive_clap(subcommand)] | ||
access_key_mode: add_key::AccessKeyMode, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct RandomAccountContext(NewAccountContext); | ||
|
||
impl RandomAccountContext { | ||
pub fn from_previous_context( | ||
previous_context: near_cli_rs::GlobalContext, | ||
_scope: &<RandomAccount as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope, | ||
) -> color_eyre::eyre::Result<Self> { | ||
let credentials_home_dir = previous_context.config.credentials_home_dir.clone(); | ||
let random_account_id = random_account_id(&previous_context.config.network_connection)?; | ||
|
||
let on_before_creating_account_callback: network::OnBeforeCreatingAccountCallback = | ||
std::sync::Arc::new({ | ||
move |network_config, new_account_id, public_key| { | ||
before_creating_account( | ||
network_config, | ||
new_account_id, | ||
public_key, | ||
&credentials_home_dir, | ||
) | ||
} | ||
}); | ||
|
||
Ok(Self(NewAccountContext { | ||
config: previous_context.config, | ||
new_account_id: random_account_id, | ||
on_before_creating_account_callback, | ||
})) | ||
} | ||
} | ||
|
||
impl From<RandomAccountContext> for NewAccountContext { | ||
fn from(item: RandomAccountContext) -> Self { | ||
item.0 | ||
} | ||
} | ||
|
||
pub fn random_account_id( | ||
networks: &linked_hash_map::LinkedHashMap<String, near_cli_rs::config::NetworkConfig>, | ||
) -> color_eyre::eyre::Result<near_cli_rs::types::account_id::AccountId> { | ||
loop { | ||
let mut generator = Generator::default(); | ||
let random_name = generator.next().wrap_err("Random name generator error")?; | ||
let account_id = | ||
near_cli_rs::types::account_id::AccountId::from_str(&format!("{random_name}.testnet"))?; | ||
if !near_cli_rs::common::is_account_exist(networks, account_id.clone().into()) { | ||
return Ok(account_id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters