aboutsummaryrefslogtreecommitdiff
path: root/src/cmd.rs
blob: be106f2c71dae58bf341c9ce7f016ac18738b312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Module containing subcommands.

pub mod address;
pub mod export;
pub mod new;
pub mod sign;

use crate::{account::PrivateKey, hdk, mnemonic::Mnemonic};
use anyhow::Result;
use structopt::StructOpt;

// Shared account options.
// NOTE: This is intentionally not a doc-comment as it would hijack the sub-
// command's description.
#[derive(Debug, StructOpt)]
struct AccountOptions {
    /// The BIP-0039 mnemonic phrase for seeding the HD wallet.
    #[structopt(short, long, env)]
    mnemonic: Mnemonic,

    /// The password to use with the mnemonic phrase for salting the seed used
    /// for the HD wallet.
    #[structopt(long, env, default_value)]
    password: String,

    /// The BIP-44 account index for deriving a private from the mnemonic seed
    /// phrase. The derived key will use the path "m/44'/60'/0'/0/{index}".
    #[structopt(long, env, default_value = "0")]
    account_index: usize,

    /// Manually specified HD path for deriving the account key. This option can
    /// not be used in conjunction with the "--account-index" option.
    #[structopt(long, env, conflicts_with = "account-index")]
    hd_path: Option<String>,
}

impl AccountOptions {
    /// Returns the private key for the specified account options.
    pub fn private_key(&self) -> Result<PrivateKey> {
        let seed = self.mnemonic.seed(&self.password);
        let path = match &self.hd_path {
            None => format!("m/44'/60'/0'/0/{}", self.account_index).parse(),
            Some(hd_path) => hd_path.parse(),
        }?;
        hdk::derive(seed, &path)
    }
}