Skip to content

Commit

Permalink
added RandomAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Oct 8, 2023
1 parent 1fe565a commit ba34f6e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ strum = { version = "0.24", features = ["derive"] }
strum_macros = "0.24"
interactive-clap = "0.2.5"
interactive-clap-derive = "0.2.5"
linked-hash-map = { version = "0.5", features = ["serde_impl"] }
names = { version = "0.14.0", default-features = false }
near-cli-rs = { git = "https://github.com/near/near-cli-rs", rev = "65fb0e45e1631c9e162ec2b84b3de6396939cf44" }
derive_more = "0.99.9"
shell-words = "1.0.0"
4 changes: 3 additions & 1 deletion cargo-near/src/commands/create_dev_account/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use strum::{EnumDiscriminants, EnumIter, EnumMessage};

mod use_random_account_id;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = near_cli_rs::GlobalContext)]
pub struct CreateAccount {
Expand All @@ -16,7 +18,7 @@ pub enum CreateAccountMethod {
message = "use-random-account-id - I would like to create a random account"
))]
/// I would like to create a random account
UseRandomAccountId,
UseRandomAccountId(self::use_random_account_id::RandomAccount),
#[strum_discriminants(strum(
message = "use-specific-account-id - I would like to create a specific account"
))]
Expand Down
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
}
}

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);
}
}
}

0 comments on commit ba34f6e

Please sign in to comment.