-
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.
- Loading branch information
FroVolod
committed
Oct 8, 2023
1 parent
1fe565a
commit ba34f6e
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |