Skip to content

Commit

Permalink
feat: Added "use seed phrase" method to create an implicit account (#153
Browse files Browse the repository at this point in the history
)
  • Loading branch information
FroVolod authored Jan 3, 2023
1 parent 9e34cf3 commit b43295a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 6 deletions.
31 changes: 29 additions & 2 deletions docs/GUIDE.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ The file: /Users/frovolod/.near-credentials/testnet/new7.testnet.json already ex

- [use-auto-generation](#use-auto-generation---Use-auto-generation-to-create-an-implicit-account)
- [use-ledger](#use-ledger---Use-ledger-to-create-an-implicit-account)
- [use-seed-phrase](#use-seed-phrase---Use-seed-phrase-to-create-an-implicit-account)

##### use-auto-generation - Use auto-generation to create an implicit account

Expand Down Expand Up @@ -392,8 +393,8 @@ The file "/Users/frovolod/.near-credentials/implicit/1573066d3fa7a2d56357aa5ddbc

##### use-ledger - Use ledger to create an implicit account

This command creates an account with access keys using the ledger and saves them in a file named _implicit-account-id_.
In order to execute this command, in the terminal command line type:
This command generates access keys using the ledger and saves them in a file named _implicit-account-id_.
In order to execute this command, in the terminal command line type:
```txt
./near-cli account \
create-account \
Expand All @@ -415,6 +416,32 @@ The file "/Users/frovolod/.near-credentials/implicit/ledger/739c872c3057cd5d812c
</a>
</details>

##### use-seed-phrase - Use seed phrase to create an implicit account

This command generates access keys using a mnemonic phrase and saves them in a file named _implicit-account-id_.
In order to execute this command, in the terminal command line type:
```txt
./near-cli account \
create-account \
fund-later \
use-seed-phrase 'start vote foot cereal link cabin fantasy universe hero drama bird fiction' \
--seed-phrase-hd-path 'm/44'\''/397'\''/0'\''' \
save-to-folder /Users/frovolod/.near-credentials/implicit
```

<details><summary><i>The result of this command will be as follows:</i></summary>

```txt
The file "/Users/frovolod/.near-credentials/implicit/eca9e1a6e0fa9a6af6d046bcffa6508f90f98e646836647ecd883d1d2b1989e5.json" was saved successfully
```
</details>

<details><summary><i>Demonstration of the command in interactive mode</i></summary>
<a href="https://asciinema.org/a/rtmvhKL9eQXqIKBkvX62oi0qx?autoplay=1&t=1&speed=2">
<img src="https://asciinema.org/a/rtmvhKL9eQXqIKBkvX62oi0qx.png" width="836"/>
</a>
</details>

#### delete-account - Delete an account

This command is designed to delete the current account. It is important to remember that all tokens of the deleted account will be transferred to the "_beneficiary_" account.
Expand Down
31 changes: 29 additions & 2 deletions docs/GUIDE.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,11 @@ The file: /Users/frovolod/.near-credentials/testnet/new7.testnet.json already ex

- [use-auto-generation](#use-auto-generation---Use-auto-generation-to-create-an-implicit-account)
- [use-ledger](#use-ledger---Use-ledger-to-create-an-implicit-account)
- [use-seed-phrase](#use-seed-phrase---Use-seed-phrase-to-create-an-implicit-account)

##### use-auto-generation - Use auto-generation to create an implicit account

Данная команда автоматически генерирует аккаунт с ключами доступа и сохраняет их в файле с именем _implicit-account-id_.
Данная команда автоматически генерирует ключи доступа и сохраняет их в файле с именем _implicit-account-id_.
Для выполнения этой команды необходимо ввести в командной строке терминала:
```txt
./near-cli account \
Expand All @@ -390,7 +391,7 @@ The file "/Users/frovolod/.near-credentials/implicit/1573066d3fa7a2d56357aa5ddbc

##### use-ledger - Use ledger to create an implicit account

Данная команда с помощью леджера создает аккаунт с ключами доступа и сохраняет их в файле с именем _implicit-account-id_.
Данная команда с помощью леджера генерирует ключи доступа и сохраняет их в файле с именем _implicit-account-id_.
Для выполнения этой команды необходимо ввести в командной строке терминала:
```txt
./near-cli account \
Expand All @@ -413,6 +414,32 @@ The file "/Users/frovolod/.near-credentials/implicit/ledger/739c872c3057cd5d812c
</a>
</details>

##### use-seed-phrase - Use seed phrase to create an implicit account

Данная команда с помощью мнемонической фразы генерирует ключи доступа и сохраняет их в файле с именем _implicit-account-id_.
Для выполнения этой команды необходимо ввести в командной строке терминала:
```txt
./near-cli account \
create-account \
fund-later \
use-seed-phrase 'start vote foot cereal link cabin fantasy universe hero drama bird fiction' \
--seed-phrase-hd-path 'm/44'\''/397'\''/0'\''' \
save-to-folder /Users/frovolod/.near-credentials/implicit
```

<details><summary><i>Результат выполнения команды</i></summary>

```txt
The file "/Users/frovolod/.near-credentials/implicit/eca9e1a6e0fa9a6af6d046bcffa6508f90f98e646836647ecd883d1d2b1989e5.json" was saved successfully
```
</details>

<details><summary><i>Демонстрация работы команды в интерактивном режиме</i></summary>
<a href="https://asciinema.org/a/rtmvhKL9eQXqIKBkvX62oi0qx?autoplay=1&t=1&speed=2">
<img src="https://asciinema.org/a/rtmvhKL9eQXqIKBkvX62oi0qx.png" width="836"/>
</a>
</details>

#### delete-account - Delete an account

Данная команда предназначена для удаления текущего аккаунта. Важно помнить, что все средства удаляемого аккаунта перейдут на счет "_beneficiary_".
Expand Down
22 changes: 22 additions & 0 deletions src/commands/account/create_account/create_implicit_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use inquire::Text;
use std::io::Write;
use strum::{EnumDiscriminants, EnumIter, EnumMessage};

mod seed_phrase;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = crate::GlobalContext)]
pub struct ImplicitAccount {
Expand Down Expand Up @@ -31,6 +33,11 @@ pub enum Mode {
))]
///Use ledger to create an implicit account
UseLedger(self::SaveImplicitAccount),
#[strum_discriminants(strum(
message = "use-seed-phrase - Use seed phrase to create an implicit account"
))]
///Use seed phrase to create an implicit account
UseSeedPhrase(self::seed_phrase::SeedPhrase),
}

impl Mode {
Expand Down Expand Up @@ -82,6 +89,21 @@ impl Mode {
file_name = format!("{}.json", implicit_account_id).into();
file_path.push(save_implicit_account.save_to_folder.get_folder_path());
}
Mode::UseSeedPhrase(seed_phrase) => {
let key_pair_properties = seed_phrase.get_key_pair_properties()?;
buf.push_str(
&serde_json::json!({
"master_seed_phrase": key_pair_properties.master_seed_phrase,
"seed_phrase_hd_path": key_pair_properties.seed_phrase_hd_path,
"implicit_account_id": key_pair_properties.implicit_account_id,
"public_key": key_pair_properties.public_key_str,
"private_key": key_pair_properties.secret_keypair_str,
})
.to_string(),
);
file_name.push(format!("{}.json", key_pair_properties.implicit_account_id));
file_path.push(seed_phrase.get_folder_path());
}
}
std::fs::create_dir_all(&file_path)?;
file_path.push(file_name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use inquire::Text;
use std::str::FromStr;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = crate::GlobalContext)]
pub struct SeedPhrase {
///Enter the seed-phrase for this account
master_seed_phrase: String,
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
seed_phrase_hd_path: crate::types::slip10::BIP32Path,
#[interactive_clap(named_arg)]
///Specify a folder to save the implicit account file
save_to_folder: super::SaveToFolder,
}

impl SeedPhrase {
pub fn input_seed_phrase_hd_path(
_context: &crate::GlobalContext,
) -> color_eyre::eyre::Result<crate::types::slip10::BIP32Path> {
Ok(crate::types::slip10::BIP32Path::from_str(
&Text::new("Enter seed phrase HD Path (if you not sure leave blank for default)")
.with_initial_value("m/44'/397'/0'")
.prompt()
.unwrap(),
)
.unwrap())
}

pub fn get_key_pair_properties(
&self,
) -> color_eyre::eyre::Result<crate::common::KeyPairProperties> {
crate::common::get_key_pair_properties_from_seed_phrase(
self.seed_phrase_hd_path.clone(),
self.master_seed_phrase.clone(),
)
}

pub fn get_folder_path(&self) -> std::path::PathBuf {
self.save_to_folder.get_folder_path()
}
}
3 changes: 1 addition & 2 deletions src/commands/account/import_account/using_seed_phrase/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ impl LoginFromSeedPhrase {

pub async fn process(&self, config: crate::config::Config) -> crate::CliResult {
let network_config = self.network_config.get_network_config(config.clone());
let seed_phrase_hd_path_default = slip10::BIP32Path::from_str("m/44'/397'/0'").unwrap();
let key_pair_properties = crate::common::get_key_pair_properties_from_seed_phrase(
seed_phrase_hd_path_default.into(),
self.seed_phrase_hd_path.clone(),
self.master_seed_phrase.clone(),
)?;
let key_pair_properties_buf = serde_json::to_string(&key_pair_properties).unwrap();
Expand Down

0 comments on commit b43295a

Please sign in to comment.