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
impl HDKey
Sourcepub fn new_master(
seed: Seed,
network_type: HDNetworkType,
) -> Result<Self, Error>
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?
More examples
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}
Sourcepub fn new(
seed: Seed,
network_type: HDNetworkType,
derivation_path: &str,
) -> Result<Self, Error>
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?
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}
Sourcepub fn derive(&self, derivation_path: &str) -> Result<Self, Error>
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?
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}
Sourcepub fn to_wif(&self) -> Result<String, Error>
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?
More examples
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}
Sourcepub fn extended_private_key(&self) -> Result<ExtendedPrivateKey, Error>
pub fn extended_private_key(&self) -> Result<ExtendedPrivateKey, Error>
Returns the extended private key
Returns an error if the extended private key is missing
Sourcepub fn extended_public_key(&self) -> Result<ExtendedPublicKey, Error>
pub fn extended_public_key(&self) -> Result<ExtendedPublicKey, Error>
Returns the extended public key
Returns an error if the extended public key is missing
Sourcepub fn master_seed(&self) -> Seed
pub fn master_seed(&self) -> Seed
Returns the master seed
Examples found in repository?
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}
Sourcepub fn derivation_path(&self) -> HDPath
pub fn derivation_path(&self) -> HDPath
Returns the derivation path
Sourcepub fn chain_code(&self) -> [u8; 32]
pub fn chain_code(&self) -> [u8; 32]
Returns the chain code
Sourcepub fn depth(&self) -> u8
pub fn depth(&self) -> u8
Returns the depth
Examples found in repository?
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}
Sourcepub fn parent_fingerprint(&self) -> [u8; 4]
pub fn parent_fingerprint(&self) -> [u8; 4]
Returns the parent fingerprint
Sourcepub fn child_index(&self) -> u32
pub fn child_index(&self) -> u32
Returns the child index
Sourcepub fn network(&self) -> HDNetworkType
pub fn network(&self) -> HDNetworkType
Returns the network associated with the HD Key
Sourcepub fn extended_private_key_serialized(&self) -> Result<String, Error>
pub fn extended_private_key_serialized(&self) -> Result<String, Error>
Extended Private Key Serialization
Examples found in repository?
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}
Sourcepub fn extended_public_key_serialized(&self) -> Result<String, Error>
pub fn extended_public_key_serialized(&self) -> Result<String, Error>
Extended Public Key Serialization
Examples found in repository?
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}