-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_gen.rs
More file actions
62 lines (48 loc) · 2.21 KB
/
key_gen.rs
File metadata and controls
62 lines (48 loc) · 2.21 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
mod create;
use create::create_key_manager_example;
use key_manager::key_type::BitcoinKeyType;
fn main() {
// see function code, main is just a wrapper to run the example
key_generation_example();
}
fn key_generation_example() {
let key_manager = create_key_manager_example("key_gen");
// --- Key generation & Derivation
// Internally the key manager generates a key pair,
// stores the private key and the corresponding public key in the encrypted keystore.
// The public key is later used to select the corresponding private key for signing.
// next_keypair is always the preferred way to get a new keypair, as it manages the derivation index automatically.
let next_p2tr_keypair_pubkey = key_manager.next_keypair(BitcoinKeyType::P2tr).unwrap();
println!("Next p2tr keypair public key: {}", next_p2tr_keypair_pubkey);
let next_p2pkh_keypair_pubkey = key_manager.next_keypair(BitcoinKeyType::P2pkh).unwrap();
println!(
"Next p2pkh keypair public key: {}",
next_p2pkh_keypair_pubkey
);
let next_p2sh_p2wpkh_keypair_pubkey = key_manager
.next_keypair(BitcoinKeyType::P2shP2wpkh)
.unwrap();
println!(
"Next p2sh_p2wpkh keypair public key: {}",
next_p2sh_p2wpkh_keypair_pubkey
);
let next_p2wpkh_keypair_pubkey = key_manager.next_keypair(BitcoinKeyType::P2wpkh).unwrap();
println!(
"Next p2wpkh keypair public key: {}",
next_p2wpkh_keypair_pubkey
);
// Derive a child keypair (e.g., for indexed wallets)
let derived_0_pubkey = key_manager.derive_keypair(BitcoinKeyType::P2tr, 0).unwrap();
println!("derived_0_pubkey: {}", derived_0_pubkey);
// Generate a master extended x public key
let account_xpub = key_manager.get_account_xpub(BitcoinKeyType::P2tr).unwrap();
// Derive public key only
let pubkey = key_manager
.derive_public_key_from_account_xpub(account_xpub, BitcoinKeyType::P2tr, 1, false)
.unwrap();
println!("Derived pubkey from xpub: {}", pubkey);
// OR ...
let same_pubkey = key_manager.derive_keypair(BitcoinKeyType::P2tr, 1).unwrap();
println!("Derived pubkey using derive_keypair: {}", same_pubkey);
assert_eq!(pubkey, same_pubkey);
}