Struct HDKey

Source
pub struct HDKey {
    pub master_seed: Seed,
    pub derivation_path: HDPath,
    pub derivation_purpose: HDPurpose,
    pub chain_code: [u8; 32],
    pub depth: u8,
    pub parent_fingerprint: [u8; 4],
    pub extended_private_key: Option<ExtendedPrivateKey>,
    pub extended_public_key: Option<ExtendedPublicKey>,
    pub child_index: u32,
    pub network: HDNetworkType,
}
Expand description

Represents a master or a derived child HD (Hierarchical Deterministic) key.

The HDKey struct contains detailed information about a master node or derived child node HD key and provides methods to create and derive HD keys.

HDKey follows the BIP32 scheme: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki HDKey also follows the purpose scheme described in BIP43: https://github.com/bitcoin/bips/blob/master/bip-0043.mediawiki The HDPurpose enum supports the following purpose types: BIP32, BIP44, BIP49, and BIP84.

Fields§

§master_seed: Seed

The seed used to create the master node

§derivation_path: HDPath

The derivation path of the HDKey

§derivation_purpose: HDPurpose

The derivation purpose associated with the HDKey

§chain_code: [u8; 32]

The chain code

§depth: u8

The depth used

§parent_fingerprint: [u8; 4]

The fingerprint of the parent key

§extended_private_key: Option<ExtendedPrivateKey>

The extended private key

§extended_public_key: Option<ExtendedPublicKey>

The extended public key

§child_index: u32

The child index value

§network: HDNetworkType

The network type

Implementations§

Source§

impl HDKey

Source

pub fn new_master( seed: Seed, network_type: HDNetworkType, ) -> Result<Self, Error>

Create new master node for a HD wallet based on a seed

Follows the method described in BIP32: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki to convert the seed to the master node extended private and public keys Multiple purpose types can be derived from the master node using the HDPurpose type

If this function encounters an error, it will return an Error type. this can happen if the seed is invalid or an error is encountered when specifying the extended private key and extended public key

Examples found in repository?
examples/hd_key_basic.rs (line 9)
4fn main() -> () {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    println!("seed_hex: {}", seed_hex);
7
8    let keys =
9        HDKey::new_master(Seed::from_str(&seed_hex).unwrap(), HDNetworkType::MainNet).unwrap();
10    println!("{:#?}", keys);
11
12    println!("wif of master hd key {}", keys.to_wif().unwrap());
13}
More examples
Hide additional examples
examples/hd_quickstart_guide.rs (line 10)
4fn main() -> Result<(), walletd_hd_key::Error> {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    let master_seed = Seed::from_str(seed_hex)?;
7    // Setting a network type on the HDKey is required, you should select HDNetworkType::TestNet during development and testing purposes and to avoid using real funds and HDNetworkType::MainNet for production level code with caution.
8    // Be sure to be consistent with HDNetworkType when connecting to the blockchain, make sure to use a compatible blockchain for the specified network type category
9
10    let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
11
12    // Wallet Import Format (WIF) is a standard way to encode private keys
13    println!("wif of master hd key {}", master_hd_key.to_wif().unwrap());
14    // The extended public key and extended private key can be serialized using the serialized string format
15    println!(
16        "master hd key extended public key: {}",
17        master_hd_key.extended_public_key_serialized()?
18    );
19    println!(
20        "master hd key extended private key: {}",
21        master_hd_key.extended_private_key_serialized()?
22    );
23    assert_eq!(master_hd_key.depth(), 0);
24
25    let default_deriv_path = HDPath::builder().build().to_string();
26    // without specifying the purpose the default derivation path is "m
27    assert_eq!(default_deriv_path, "m");
28    println!("default derivation path: {}", default_deriv_path);
29
30    let account_deriv_path = HDPath::builder()
31        .purpose_index(HDPurpose::BIP44.to_shortform_num())
32        .coin_type_index(Coin::from(Symbol::ETH).id())
33        .account_index(0)
34        .no_change_index()
35        .no_address_index()
36        .build()
37        .to_string();
38
39    println!("account derivation path: {}", account_deriv_path);
40
41    assert_eq!(&account_deriv_path, "m/44'/60'/0'");
42    let eth_first_account_key = master_hd_key.derive(&account_deriv_path)?;
43    assert_eq!(
44        eth_first_account_key.master_seed(),
45        master_hd_key.master_seed()
46    );
47    println!(
48        "eth_first_account_key depth {}",
49        eth_first_account_key.depth()
50    );
51    assert_eq!(eth_first_account_key.depth(), 3);
52    println!(
53        "wif of eth_first_account_key {}",
54        eth_first_account_key.to_wif()?
55    );
56
57    // Can derive a child key from a master key or a parent key, the derivation path must be a valid derivation path starting from the master node
58    let derive_from_master = master_hd_key.derive("m/44'/60'/0'/0/0")?;
59    let derive_from_parent = eth_first_account_key.derive("m/44'/60'/0'/0/0")?;
60    assert_eq!(derive_from_master, derive_from_parent);
61    println!(
62        "derive_from_master == derive_from_parent: {}",
63        derive_from_master == derive_from_parent
64    );
65
66    // Can flexibly specify the derivation path using the HDPathBuilder, specifying the derivation path using a string is even more flexible
67    let custom_key_path = HDPath::builder()
68        .purpose_index(HDPurpose::BIP84.to_shortform_num())
69        .coin_type_index(Coin::Testnet.id())
70        .account_index(0)
71        .change_index(1)
72        .address_index(0)
73        .hardened_address()
74        .build()
75        .to_string();
76
77    assert_eq!(custom_key_path, "m/84'/1'/0'/1/0'");
78    // Can use ' or h to specify hardened derivation
79    let custom_key = master_hd_key.derive(&custom_key_path)?;
80    let key_compare = master_hd_key.derive("m/84h/1h/0h/1/0h")?;
81
82    assert_eq!(custom_key, key_compare);
83
84    // Shortcut to create a derived key directly from master seed
85    let derived_key = HDKey::new(
86        Seed::from_str(seed_hex)?,
87        HDNetworkType::TestNet,
88        &custom_key_path,
89    )?;
90    assert_eq!(derived_key, custom_key);
91    println!("derived_key: {:?}", derived_key);
92    println!("derived_key depth: {}", derived_key.depth());
93    println!("derived_key wif: {}", derived_key.to_wif()?);
94    // Can display the extended public key and extended private key using the serialized string format
95    println!(
96        "derived_key public key: {}",
97        derived_key.extended_public_key_serialized()?
98    );
99    println!(
100        "derived_key private key: {}",
101        derived_key.extended_private_key_serialized()?
102    );
103
104    Ok(())
105}
Source

pub fn new( seed: Seed, network_type: HDNetworkType, derivation_path: &str, ) -> Result<Self, Error>

Returns a new HDKey from a seed (Seed), network type (HDNetworkType) and derivation path string.

The HDKey returned will be the child key derived from the master node specified from the seed using the derivation path.

Returns an Error with further details if the seed is invalid or the derivation path is invalid

Examples found in repository?
examples/hd_quickstart_guide.rs (lines 85-89)
4fn main() -> Result<(), walletd_hd_key::Error> {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    let master_seed = Seed::from_str(seed_hex)?;
7    // Setting a network type on the HDKey is required, you should select HDNetworkType::TestNet during development and testing purposes and to avoid using real funds and HDNetworkType::MainNet for production level code with caution.
8    // Be sure to be consistent with HDNetworkType when connecting to the blockchain, make sure to use a compatible blockchain for the specified network type category
9
10    let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
11
12    // Wallet Import Format (WIF) is a standard way to encode private keys
13    println!("wif of master hd key {}", master_hd_key.to_wif().unwrap());
14    // The extended public key and extended private key can be serialized using the serialized string format
15    println!(
16        "master hd key extended public key: {}",
17        master_hd_key.extended_public_key_serialized()?
18    );
19    println!(
20        "master hd key extended private key: {}",
21        master_hd_key.extended_private_key_serialized()?
22    );
23    assert_eq!(master_hd_key.depth(), 0);
24
25    let default_deriv_path = HDPath::builder().build().to_string();
26    // without specifying the purpose the default derivation path is "m
27    assert_eq!(default_deriv_path, "m");
28    println!("default derivation path: {}", default_deriv_path);
29
30    let account_deriv_path = HDPath::builder()
31        .purpose_index(HDPurpose::BIP44.to_shortform_num())
32        .coin_type_index(Coin::from(Symbol::ETH).id())
33        .account_index(0)
34        .no_change_index()
35        .no_address_index()
36        .build()
37        .to_string();
38
39    println!("account derivation path: {}", account_deriv_path);
40
41    assert_eq!(&account_deriv_path, "m/44'/60'/0'");
42    let eth_first_account_key = master_hd_key.derive(&account_deriv_path)?;
43    assert_eq!(
44        eth_first_account_key.master_seed(),
45        master_hd_key.master_seed()
46    );
47    println!(
48        "eth_first_account_key depth {}",
49        eth_first_account_key.depth()
50    );
51    assert_eq!(eth_first_account_key.depth(), 3);
52    println!(
53        "wif of eth_first_account_key {}",
54        eth_first_account_key.to_wif()?
55    );
56
57    // Can derive a child key from a master key or a parent key, the derivation path must be a valid derivation path starting from the master node
58    let derive_from_master = master_hd_key.derive("m/44'/60'/0'/0/0")?;
59    let derive_from_parent = eth_first_account_key.derive("m/44'/60'/0'/0/0")?;
60    assert_eq!(derive_from_master, derive_from_parent);
61    println!(
62        "derive_from_master == derive_from_parent: {}",
63        derive_from_master == derive_from_parent
64    );
65
66    // Can flexibly specify the derivation path using the HDPathBuilder, specifying the derivation path using a string is even more flexible
67    let custom_key_path = HDPath::builder()
68        .purpose_index(HDPurpose::BIP84.to_shortform_num())
69        .coin_type_index(Coin::Testnet.id())
70        .account_index(0)
71        .change_index(1)
72        .address_index(0)
73        .hardened_address()
74        .build()
75        .to_string();
76
77    assert_eq!(custom_key_path, "m/84'/1'/0'/1/0'");
78    // Can use ' or h to specify hardened derivation
79    let custom_key = master_hd_key.derive(&custom_key_path)?;
80    let key_compare = master_hd_key.derive("m/84h/1h/0h/1/0h")?;
81
82    assert_eq!(custom_key, key_compare);
83
84    // Shortcut to create a derived key directly from master seed
85    let derived_key = HDKey::new(
86        Seed::from_str(seed_hex)?,
87        HDNetworkType::TestNet,
88        &custom_key_path,
89    )?;
90    assert_eq!(derived_key, custom_key);
91    println!("derived_key: {:?}", derived_key);
92    println!("derived_key depth: {}", derived_key.depth());
93    println!("derived_key wif: {}", derived_key.to_wif()?);
94    // Can display the extended public key and extended private key using the serialized string format
95    println!(
96        "derived_key public key: {}",
97        derived_key.extended_public_key_serialized()?
98    );
99    println!(
100        "derived_key private key: {}",
101        derived_key.extended_private_key_serialized()?
102    );
103
104    Ok(())
105}
Source

pub fn derive(&self, derivation_path: &str) -> Result<Self, Error>

Derives and returns a HDKey following the specified derivation path from the HDKey given as the self parameter as the parent key.

Examples found in repository?
examples/hd_quickstart_guide.rs (line 42)
4fn main() -> Result<(), walletd_hd_key::Error> {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    let master_seed = Seed::from_str(seed_hex)?;
7    // Setting a network type on the HDKey is required, you should select HDNetworkType::TestNet during development and testing purposes and to avoid using real funds and HDNetworkType::MainNet for production level code with caution.
8    // Be sure to be consistent with HDNetworkType when connecting to the blockchain, make sure to use a compatible blockchain for the specified network type category
9
10    let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
11
12    // Wallet Import Format (WIF) is a standard way to encode private keys
13    println!("wif of master hd key {}", master_hd_key.to_wif().unwrap());
14    // The extended public key and extended private key can be serialized using the serialized string format
15    println!(
16        "master hd key extended public key: {}",
17        master_hd_key.extended_public_key_serialized()?
18    );
19    println!(
20        "master hd key extended private key: {}",
21        master_hd_key.extended_private_key_serialized()?
22    );
23    assert_eq!(master_hd_key.depth(), 0);
24
25    let default_deriv_path = HDPath::builder().build().to_string();
26    // without specifying the purpose the default derivation path is "m
27    assert_eq!(default_deriv_path, "m");
28    println!("default derivation path: {}", default_deriv_path);
29
30    let account_deriv_path = HDPath::builder()
31        .purpose_index(HDPurpose::BIP44.to_shortform_num())
32        .coin_type_index(Coin::from(Symbol::ETH).id())
33        .account_index(0)
34        .no_change_index()
35        .no_address_index()
36        .build()
37        .to_string();
38
39    println!("account derivation path: {}", account_deriv_path);
40
41    assert_eq!(&account_deriv_path, "m/44'/60'/0'");
42    let eth_first_account_key = master_hd_key.derive(&account_deriv_path)?;
43    assert_eq!(
44        eth_first_account_key.master_seed(),
45        master_hd_key.master_seed()
46    );
47    println!(
48        "eth_first_account_key depth {}",
49        eth_first_account_key.depth()
50    );
51    assert_eq!(eth_first_account_key.depth(), 3);
52    println!(
53        "wif of eth_first_account_key {}",
54        eth_first_account_key.to_wif()?
55    );
56
57    // Can derive a child key from a master key or a parent key, the derivation path must be a valid derivation path starting from the master node
58    let derive_from_master = master_hd_key.derive("m/44'/60'/0'/0/0")?;
59    let derive_from_parent = eth_first_account_key.derive("m/44'/60'/0'/0/0")?;
60    assert_eq!(derive_from_master, derive_from_parent);
61    println!(
62        "derive_from_master == derive_from_parent: {}",
63        derive_from_master == derive_from_parent
64    );
65
66    // Can flexibly specify the derivation path using the HDPathBuilder, specifying the derivation path using a string is even more flexible
67    let custom_key_path = HDPath::builder()
68        .purpose_index(HDPurpose::BIP84.to_shortform_num())
69        .coin_type_index(Coin::Testnet.id())
70        .account_index(0)
71        .change_index(1)
72        .address_index(0)
73        .hardened_address()
74        .build()
75        .to_string();
76
77    assert_eq!(custom_key_path, "m/84'/1'/0'/1/0'");
78    // Can use ' or h to specify hardened derivation
79    let custom_key = master_hd_key.derive(&custom_key_path)?;
80    let key_compare = master_hd_key.derive("m/84h/1h/0h/1/0h")?;
81
82    assert_eq!(custom_key, key_compare);
83
84    // Shortcut to create a derived key directly from master seed
85    let derived_key = HDKey::new(
86        Seed::from_str(seed_hex)?,
87        HDNetworkType::TestNet,
88        &custom_key_path,
89    )?;
90    assert_eq!(derived_key, custom_key);
91    println!("derived_key: {:?}", derived_key);
92    println!("derived_key depth: {}", derived_key.depth());
93    println!("derived_key wif: {}", derived_key.to_wif()?);
94    // Can display the extended public key and extended private key using the serialized string format
95    println!(
96        "derived_key public key: {}",
97        derived_key.extended_public_key_serialized()?
98    );
99    println!(
100        "derived_key private key: {}",
101        derived_key.extended_private_key_serialized()?
102    );
103
104    Ok(())
105}
Source

pub fn to_wif(&self) -> Result<String, Error>

Convert the ExtendedPrivateKey associated with the HDKey to a Wallet Import Format (WIF). Using wallet import format: https://en.bitcoin.it/wiki/Wallet_import_format Returns an Error if the extended private key is missing or another error is encountered.

Examples found in repository?
examples/hd_key_basic.rs (line 12)
4fn main() -> () {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    println!("seed_hex: {}", seed_hex);
7
8    let keys =
9        HDKey::new_master(Seed::from_str(&seed_hex).unwrap(), HDNetworkType::MainNet).unwrap();
10    println!("{:#?}", keys);
11
12    println!("wif of master hd key {}", keys.to_wif().unwrap());
13}
More examples
Hide additional examples
examples/hd_quickstart_guide.rs (line 13)
4fn main() -> Result<(), walletd_hd_key::Error> {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    let master_seed = Seed::from_str(seed_hex)?;
7    // Setting a network type on the HDKey is required, you should select HDNetworkType::TestNet during development and testing purposes and to avoid using real funds and HDNetworkType::MainNet for production level code with caution.
8    // Be sure to be consistent with HDNetworkType when connecting to the blockchain, make sure to use a compatible blockchain for the specified network type category
9
10    let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
11
12    // Wallet Import Format (WIF) is a standard way to encode private keys
13    println!("wif of master hd key {}", master_hd_key.to_wif().unwrap());
14    // The extended public key and extended private key can be serialized using the serialized string format
15    println!(
16        "master hd key extended public key: {}",
17        master_hd_key.extended_public_key_serialized()?
18    );
19    println!(
20        "master hd key extended private key: {}",
21        master_hd_key.extended_private_key_serialized()?
22    );
23    assert_eq!(master_hd_key.depth(), 0);
24
25    let default_deriv_path = HDPath::builder().build().to_string();
26    // without specifying the purpose the default derivation path is "m
27    assert_eq!(default_deriv_path, "m");
28    println!("default derivation path: {}", default_deriv_path);
29
30    let account_deriv_path = HDPath::builder()
31        .purpose_index(HDPurpose::BIP44.to_shortform_num())
32        .coin_type_index(Coin::from(Symbol::ETH).id())
33        .account_index(0)
34        .no_change_index()
35        .no_address_index()
36        .build()
37        .to_string();
38
39    println!("account derivation path: {}", account_deriv_path);
40
41    assert_eq!(&account_deriv_path, "m/44'/60'/0'");
42    let eth_first_account_key = master_hd_key.derive(&account_deriv_path)?;
43    assert_eq!(
44        eth_first_account_key.master_seed(),
45        master_hd_key.master_seed()
46    );
47    println!(
48        "eth_first_account_key depth {}",
49        eth_first_account_key.depth()
50    );
51    assert_eq!(eth_first_account_key.depth(), 3);
52    println!(
53        "wif of eth_first_account_key {}",
54        eth_first_account_key.to_wif()?
55    );
56
57    // Can derive a child key from a master key or a parent key, the derivation path must be a valid derivation path starting from the master node
58    let derive_from_master = master_hd_key.derive("m/44'/60'/0'/0/0")?;
59    let derive_from_parent = eth_first_account_key.derive("m/44'/60'/0'/0/0")?;
60    assert_eq!(derive_from_master, derive_from_parent);
61    println!(
62        "derive_from_master == derive_from_parent: {}",
63        derive_from_master == derive_from_parent
64    );
65
66    // Can flexibly specify the derivation path using the HDPathBuilder, specifying the derivation path using a string is even more flexible
67    let custom_key_path = HDPath::builder()
68        .purpose_index(HDPurpose::BIP84.to_shortform_num())
69        .coin_type_index(Coin::Testnet.id())
70        .account_index(0)
71        .change_index(1)
72        .address_index(0)
73        .hardened_address()
74        .build()
75        .to_string();
76
77    assert_eq!(custom_key_path, "m/84'/1'/0'/1/0'");
78    // Can use ' or h to specify hardened derivation
79    let custom_key = master_hd_key.derive(&custom_key_path)?;
80    let key_compare = master_hd_key.derive("m/84h/1h/0h/1/0h")?;
81
82    assert_eq!(custom_key, key_compare);
83
84    // Shortcut to create a derived key directly from master seed
85    let derived_key = HDKey::new(
86        Seed::from_str(seed_hex)?,
87        HDNetworkType::TestNet,
88        &custom_key_path,
89    )?;
90    assert_eq!(derived_key, custom_key);
91    println!("derived_key: {:?}", derived_key);
92    println!("derived_key depth: {}", derived_key.depth());
93    println!("derived_key wif: {}", derived_key.to_wif()?);
94    // Can display the extended public key and extended private key using the serialized string format
95    println!(
96        "derived_key public key: {}",
97        derived_key.extended_public_key_serialized()?
98    );
99    println!(
100        "derived_key private key: {}",
101        derived_key.extended_private_key_serialized()?
102    );
103
104    Ok(())
105}
Source

pub fn extended_private_key(&self) -> Result<ExtendedPrivateKey, Error>

Returns the extended private key

Returns an error if the extended private key is missing

Source

pub fn extended_public_key(&self) -> Result<ExtendedPublicKey, Error>

Returns the extended public key

Returns an error if the extended public key is missing

Source

pub fn master_seed(&self) -> Seed

Returns the master seed

Examples found in repository?
examples/hd_quickstart_guide.rs (line 44)
4fn main() -> Result<(), walletd_hd_key::Error> {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    let master_seed = Seed::from_str(seed_hex)?;
7    // Setting a network type on the HDKey is required, you should select HDNetworkType::TestNet during development and testing purposes and to avoid using real funds and HDNetworkType::MainNet for production level code with caution.
8    // Be sure to be consistent with HDNetworkType when connecting to the blockchain, make sure to use a compatible blockchain for the specified network type category
9
10    let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
11
12    // Wallet Import Format (WIF) is a standard way to encode private keys
13    println!("wif of master hd key {}", master_hd_key.to_wif().unwrap());
14    // The extended public key and extended private key can be serialized using the serialized string format
15    println!(
16        "master hd key extended public key: {}",
17        master_hd_key.extended_public_key_serialized()?
18    );
19    println!(
20        "master hd key extended private key: {}",
21        master_hd_key.extended_private_key_serialized()?
22    );
23    assert_eq!(master_hd_key.depth(), 0);
24
25    let default_deriv_path = HDPath::builder().build().to_string();
26    // without specifying the purpose the default derivation path is "m
27    assert_eq!(default_deriv_path, "m");
28    println!("default derivation path: {}", default_deriv_path);
29
30    let account_deriv_path = HDPath::builder()
31        .purpose_index(HDPurpose::BIP44.to_shortform_num())
32        .coin_type_index(Coin::from(Symbol::ETH).id())
33        .account_index(0)
34        .no_change_index()
35        .no_address_index()
36        .build()
37        .to_string();
38
39    println!("account derivation path: {}", account_deriv_path);
40
41    assert_eq!(&account_deriv_path, "m/44'/60'/0'");
42    let eth_first_account_key = master_hd_key.derive(&account_deriv_path)?;
43    assert_eq!(
44        eth_first_account_key.master_seed(),
45        master_hd_key.master_seed()
46    );
47    println!(
48        "eth_first_account_key depth {}",
49        eth_first_account_key.depth()
50    );
51    assert_eq!(eth_first_account_key.depth(), 3);
52    println!(
53        "wif of eth_first_account_key {}",
54        eth_first_account_key.to_wif()?
55    );
56
57    // Can derive a child key from a master key or a parent key, the derivation path must be a valid derivation path starting from the master node
58    let derive_from_master = master_hd_key.derive("m/44'/60'/0'/0/0")?;
59    let derive_from_parent = eth_first_account_key.derive("m/44'/60'/0'/0/0")?;
60    assert_eq!(derive_from_master, derive_from_parent);
61    println!(
62        "derive_from_master == derive_from_parent: {}",
63        derive_from_master == derive_from_parent
64    );
65
66    // Can flexibly specify the derivation path using the HDPathBuilder, specifying the derivation path using a string is even more flexible
67    let custom_key_path = HDPath::builder()
68        .purpose_index(HDPurpose::BIP84.to_shortform_num())
69        .coin_type_index(Coin::Testnet.id())
70        .account_index(0)
71        .change_index(1)
72        .address_index(0)
73        .hardened_address()
74        .build()
75        .to_string();
76
77    assert_eq!(custom_key_path, "m/84'/1'/0'/1/0'");
78    // Can use ' or h to specify hardened derivation
79    let custom_key = master_hd_key.derive(&custom_key_path)?;
80    let key_compare = master_hd_key.derive("m/84h/1h/0h/1/0h")?;
81
82    assert_eq!(custom_key, key_compare);
83
84    // Shortcut to create a derived key directly from master seed
85    let derived_key = HDKey::new(
86        Seed::from_str(seed_hex)?,
87        HDNetworkType::TestNet,
88        &custom_key_path,
89    )?;
90    assert_eq!(derived_key, custom_key);
91    println!("derived_key: {:?}", derived_key);
92    println!("derived_key depth: {}", derived_key.depth());
93    println!("derived_key wif: {}", derived_key.to_wif()?);
94    // Can display the extended public key and extended private key using the serialized string format
95    println!(
96        "derived_key public key: {}",
97        derived_key.extended_public_key_serialized()?
98    );
99    println!(
100        "derived_key private key: {}",
101        derived_key.extended_private_key_serialized()?
102    );
103
104    Ok(())
105}
Source

pub fn derivation_path(&self) -> HDPath

Returns the derivation path

Source

pub fn chain_code(&self) -> [u8; 32]

Returns the chain code

Source

pub fn depth(&self) -> u8

Returns the depth

Examples found in repository?
examples/hd_quickstart_guide.rs (line 23)
4fn main() -> Result<(), walletd_hd_key::Error> {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    let master_seed = Seed::from_str(seed_hex)?;
7    // Setting a network type on the HDKey is required, you should select HDNetworkType::TestNet during development and testing purposes and to avoid using real funds and HDNetworkType::MainNet for production level code with caution.
8    // Be sure to be consistent with HDNetworkType when connecting to the blockchain, make sure to use a compatible blockchain for the specified network type category
9
10    let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
11
12    // Wallet Import Format (WIF) is a standard way to encode private keys
13    println!("wif of master hd key {}", master_hd_key.to_wif().unwrap());
14    // The extended public key and extended private key can be serialized using the serialized string format
15    println!(
16        "master hd key extended public key: {}",
17        master_hd_key.extended_public_key_serialized()?
18    );
19    println!(
20        "master hd key extended private key: {}",
21        master_hd_key.extended_private_key_serialized()?
22    );
23    assert_eq!(master_hd_key.depth(), 0);
24
25    let default_deriv_path = HDPath::builder().build().to_string();
26    // without specifying the purpose the default derivation path is "m
27    assert_eq!(default_deriv_path, "m");
28    println!("default derivation path: {}", default_deriv_path);
29
30    let account_deriv_path = HDPath::builder()
31        .purpose_index(HDPurpose::BIP44.to_shortform_num())
32        .coin_type_index(Coin::from(Symbol::ETH).id())
33        .account_index(0)
34        .no_change_index()
35        .no_address_index()
36        .build()
37        .to_string();
38
39    println!("account derivation path: {}", account_deriv_path);
40
41    assert_eq!(&account_deriv_path, "m/44'/60'/0'");
42    let eth_first_account_key = master_hd_key.derive(&account_deriv_path)?;
43    assert_eq!(
44        eth_first_account_key.master_seed(),
45        master_hd_key.master_seed()
46    );
47    println!(
48        "eth_first_account_key depth {}",
49        eth_first_account_key.depth()
50    );
51    assert_eq!(eth_first_account_key.depth(), 3);
52    println!(
53        "wif of eth_first_account_key {}",
54        eth_first_account_key.to_wif()?
55    );
56
57    // Can derive a child key from a master key or a parent key, the derivation path must be a valid derivation path starting from the master node
58    let derive_from_master = master_hd_key.derive("m/44'/60'/0'/0/0")?;
59    let derive_from_parent = eth_first_account_key.derive("m/44'/60'/0'/0/0")?;
60    assert_eq!(derive_from_master, derive_from_parent);
61    println!(
62        "derive_from_master == derive_from_parent: {}",
63        derive_from_master == derive_from_parent
64    );
65
66    // Can flexibly specify the derivation path using the HDPathBuilder, specifying the derivation path using a string is even more flexible
67    let custom_key_path = HDPath::builder()
68        .purpose_index(HDPurpose::BIP84.to_shortform_num())
69        .coin_type_index(Coin::Testnet.id())
70        .account_index(0)
71        .change_index(1)
72        .address_index(0)
73        .hardened_address()
74        .build()
75        .to_string();
76
77    assert_eq!(custom_key_path, "m/84'/1'/0'/1/0'");
78    // Can use ' or h to specify hardened derivation
79    let custom_key = master_hd_key.derive(&custom_key_path)?;
80    let key_compare = master_hd_key.derive("m/84h/1h/0h/1/0h")?;
81
82    assert_eq!(custom_key, key_compare);
83
84    // Shortcut to create a derived key directly from master seed
85    let derived_key = HDKey::new(
86        Seed::from_str(seed_hex)?,
87        HDNetworkType::TestNet,
88        &custom_key_path,
89    )?;
90    assert_eq!(derived_key, custom_key);
91    println!("derived_key: {:?}", derived_key);
92    println!("derived_key depth: {}", derived_key.depth());
93    println!("derived_key wif: {}", derived_key.to_wif()?);
94    // Can display the extended public key and extended private key using the serialized string format
95    println!(
96        "derived_key public key: {}",
97        derived_key.extended_public_key_serialized()?
98    );
99    println!(
100        "derived_key private key: {}",
101        derived_key.extended_private_key_serialized()?
102    );
103
104    Ok(())
105}
Source

pub fn parent_fingerprint(&self) -> [u8; 4]

Returns the parent fingerprint

Source

pub fn child_index(&self) -> u32

Returns the child index

Source

pub fn network(&self) -> HDNetworkType

Returns the network associated with the HD Key

Source

pub fn extended_private_key_serialized(&self) -> Result<String, Error>

Extended Private Key Serialization

Examples found in repository?
examples/hd_quickstart_guide.rs (line 21)
4fn main() -> Result<(), walletd_hd_key::Error> {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    let master_seed = Seed::from_str(seed_hex)?;
7    // Setting a network type on the HDKey is required, you should select HDNetworkType::TestNet during development and testing purposes and to avoid using real funds and HDNetworkType::MainNet for production level code with caution.
8    // Be sure to be consistent with HDNetworkType when connecting to the blockchain, make sure to use a compatible blockchain for the specified network type category
9
10    let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
11
12    // Wallet Import Format (WIF) is a standard way to encode private keys
13    println!("wif of master hd key {}", master_hd_key.to_wif().unwrap());
14    // The extended public key and extended private key can be serialized using the serialized string format
15    println!(
16        "master hd key extended public key: {}",
17        master_hd_key.extended_public_key_serialized()?
18    );
19    println!(
20        "master hd key extended private key: {}",
21        master_hd_key.extended_private_key_serialized()?
22    );
23    assert_eq!(master_hd_key.depth(), 0);
24
25    let default_deriv_path = HDPath::builder().build().to_string();
26    // without specifying the purpose the default derivation path is "m
27    assert_eq!(default_deriv_path, "m");
28    println!("default derivation path: {}", default_deriv_path);
29
30    let account_deriv_path = HDPath::builder()
31        .purpose_index(HDPurpose::BIP44.to_shortform_num())
32        .coin_type_index(Coin::from(Symbol::ETH).id())
33        .account_index(0)
34        .no_change_index()
35        .no_address_index()
36        .build()
37        .to_string();
38
39    println!("account derivation path: {}", account_deriv_path);
40
41    assert_eq!(&account_deriv_path, "m/44'/60'/0'");
42    let eth_first_account_key = master_hd_key.derive(&account_deriv_path)?;
43    assert_eq!(
44        eth_first_account_key.master_seed(),
45        master_hd_key.master_seed()
46    );
47    println!(
48        "eth_first_account_key depth {}",
49        eth_first_account_key.depth()
50    );
51    assert_eq!(eth_first_account_key.depth(), 3);
52    println!(
53        "wif of eth_first_account_key {}",
54        eth_first_account_key.to_wif()?
55    );
56
57    // Can derive a child key from a master key or a parent key, the derivation path must be a valid derivation path starting from the master node
58    let derive_from_master = master_hd_key.derive("m/44'/60'/0'/0/0")?;
59    let derive_from_parent = eth_first_account_key.derive("m/44'/60'/0'/0/0")?;
60    assert_eq!(derive_from_master, derive_from_parent);
61    println!(
62        "derive_from_master == derive_from_parent: {}",
63        derive_from_master == derive_from_parent
64    );
65
66    // Can flexibly specify the derivation path using the HDPathBuilder, specifying the derivation path using a string is even more flexible
67    let custom_key_path = HDPath::builder()
68        .purpose_index(HDPurpose::BIP84.to_shortform_num())
69        .coin_type_index(Coin::Testnet.id())
70        .account_index(0)
71        .change_index(1)
72        .address_index(0)
73        .hardened_address()
74        .build()
75        .to_string();
76
77    assert_eq!(custom_key_path, "m/84'/1'/0'/1/0'");
78    // Can use ' or h to specify hardened derivation
79    let custom_key = master_hd_key.derive(&custom_key_path)?;
80    let key_compare = master_hd_key.derive("m/84h/1h/0h/1/0h")?;
81
82    assert_eq!(custom_key, key_compare);
83
84    // Shortcut to create a derived key directly from master seed
85    let derived_key = HDKey::new(
86        Seed::from_str(seed_hex)?,
87        HDNetworkType::TestNet,
88        &custom_key_path,
89    )?;
90    assert_eq!(derived_key, custom_key);
91    println!("derived_key: {:?}", derived_key);
92    println!("derived_key depth: {}", derived_key.depth());
93    println!("derived_key wif: {}", derived_key.to_wif()?);
94    // Can display the extended public key and extended private key using the serialized string format
95    println!(
96        "derived_key public key: {}",
97        derived_key.extended_public_key_serialized()?
98    );
99    println!(
100        "derived_key private key: {}",
101        derived_key.extended_private_key_serialized()?
102    );
103
104    Ok(())
105}
Source

pub fn extended_public_key_serialized(&self) -> Result<String, Error>

Extended Public Key Serialization

Examples found in repository?
examples/hd_quickstart_guide.rs (line 17)
4fn main() -> Result<(), walletd_hd_key::Error> {
5    let seed_hex = "a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee";
6    let master_seed = Seed::from_str(seed_hex)?;
7    // Setting a network type on the HDKey is required, you should select HDNetworkType::TestNet during development and testing purposes and to avoid using real funds and HDNetworkType::MainNet for production level code with caution.
8    // Be sure to be consistent with HDNetworkType when connecting to the blockchain, make sure to use a compatible blockchain for the specified network type category
9
10    let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
11
12    // Wallet Import Format (WIF) is a standard way to encode private keys
13    println!("wif of master hd key {}", master_hd_key.to_wif().unwrap());
14    // The extended public key and extended private key can be serialized using the serialized string format
15    println!(
16        "master hd key extended public key: {}",
17        master_hd_key.extended_public_key_serialized()?
18    );
19    println!(
20        "master hd key extended private key: {}",
21        master_hd_key.extended_private_key_serialized()?
22    );
23    assert_eq!(master_hd_key.depth(), 0);
24
25    let default_deriv_path = HDPath::builder().build().to_string();
26    // without specifying the purpose the default derivation path is "m
27    assert_eq!(default_deriv_path, "m");
28    println!("default derivation path: {}", default_deriv_path);
29
30    let account_deriv_path = HDPath::builder()
31        .purpose_index(HDPurpose::BIP44.to_shortform_num())
32        .coin_type_index(Coin::from(Symbol::ETH).id())
33        .account_index(0)
34        .no_change_index()
35        .no_address_index()
36        .build()
37        .to_string();
38
39    println!("account derivation path: {}", account_deriv_path);
40
41    assert_eq!(&account_deriv_path, "m/44'/60'/0'");
42    let eth_first_account_key = master_hd_key.derive(&account_deriv_path)?;
43    assert_eq!(
44        eth_first_account_key.master_seed(),
45        master_hd_key.master_seed()
46    );
47    println!(
48        "eth_first_account_key depth {}",
49        eth_first_account_key.depth()
50    );
51    assert_eq!(eth_first_account_key.depth(), 3);
52    println!(
53        "wif of eth_first_account_key {}",
54        eth_first_account_key.to_wif()?
55    );
56
57    // Can derive a child key from a master key or a parent key, the derivation path must be a valid derivation path starting from the master node
58    let derive_from_master = master_hd_key.derive("m/44'/60'/0'/0/0")?;
59    let derive_from_parent = eth_first_account_key.derive("m/44'/60'/0'/0/0")?;
60    assert_eq!(derive_from_master, derive_from_parent);
61    println!(
62        "derive_from_master == derive_from_parent: {}",
63        derive_from_master == derive_from_parent
64    );
65
66    // Can flexibly specify the derivation path using the HDPathBuilder, specifying the derivation path using a string is even more flexible
67    let custom_key_path = HDPath::builder()
68        .purpose_index(HDPurpose::BIP84.to_shortform_num())
69        .coin_type_index(Coin::Testnet.id())
70        .account_index(0)
71        .change_index(1)
72        .address_index(0)
73        .hardened_address()
74        .build()
75        .to_string();
76
77    assert_eq!(custom_key_path, "m/84'/1'/0'/1/0'");
78    // Can use ' or h to specify hardened derivation
79    let custom_key = master_hd_key.derive(&custom_key_path)?;
80    let key_compare = master_hd_key.derive("m/84h/1h/0h/1/0h")?;
81
82    assert_eq!(custom_key, key_compare);
83
84    // Shortcut to create a derived key directly from master seed
85    let derived_key = HDKey::new(
86        Seed::from_str(seed_hex)?,
87        HDNetworkType::TestNet,
88        &custom_key_path,
89    )?;
90    assert_eq!(derived_key, custom_key);
91    println!("derived_key: {:?}", derived_key);
92    println!("derived_key depth: {}", derived_key.depth());
93    println!("derived_key wif: {}", derived_key.to_wif()?);
94    // Can display the extended public key and extended private key using the serialized string format
95    println!(
96        "derived_key public key: {}",
97        derived_key.extended_public_key_serialized()?
98    );
99    println!(
100        "derived_key private key: {}",
101        derived_key.extended_private_key_serialized()?
102    );
103
104    Ok(())
105}

Trait Implementations§

Source§

impl Clone for HDKey

Source§

fn clone(&self) -> HDKey

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HDKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for HDKey

Source§

fn eq(&self, other: &HDKey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for HDKey

Source§

impl StructuralPartialEq for HDKey

Auto Trait Implementations§

§

impl Freeze for HDKey

§

impl RefUnwindSafe for HDKey

§

impl Send for HDKey

§

impl Sync for HDKey

§

impl Unpin for HDKey

§

impl UnwindSafe for HDKey

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V