Skip to content

Commit 0e2e559

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#760: improve example: take hex-encoded seed instead of WIF in bip32 example
bb70820 improve example: take hex-encoded seed instead of WIF in bip32 example (KaFai Choi) Pull request description: This is my understanding of what we want to fix the confusing bip32 example. Apologize in advance if I misunderstand it. Closes #748 ACKs for top commit: dr-orlovsky: ACK bb70820 RCasatta: utACK bb70820 Tree-SHA512: aaec9f7e3e8ce0e58b2a405e6ada75b1fc9de46ee6efb7fa2543fa626aa5f05704b05585158ab6147c495fc19abc6ade3c25225b3d75b3a3edeb8e00ba8d3976
2 parents b91058c + bb70820 commit 0e2e559

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

contrib/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ echo "********* Testing no-std build *************"
5656
cargo build --verbose --features="no-std $feature"
5757
done
5858

59-
cargo run --example bip32 L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D
60-
cargo run --no-default-features --features no-std --example bip32 L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D
59+
cargo run --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
60+
cargo run --no-default-features --features no-std --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
6161
fi
6262

6363
# Test each feature

examples/bip32.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@ use std::{env, process};
44
use std::str::FromStr;
55

66
use bitcoin::secp256k1::Secp256k1;
7-
use bitcoin::{PrivateKey, PublicKey};
7+
use bitcoin::PublicKey;
88
use bitcoin::util::bip32::ExtendedPrivKey;
99
use bitcoin::util::bip32::ExtendedPubKey;
1010
use bitcoin::util::bip32::DerivationPath;
1111
use bitcoin::util::bip32::ChildNumber;
1212
use bitcoin::util::address::Address;
1313
use bitcoin::secp256k1::ffi::types::AlignedType;
14+
use bitcoin::hashes::hex::FromHex;
1415

1516
fn main() {
16-
// This example derives root xprv
17-
// from a 32-byte secret of the input WIF string,
17+
// This example derives root xprv from a 32-byte seed,
1818
// derives the child xprv with path m/84h/0h/0h,
1919
// prints out corresponding xpub,
2020
// calculates and prints out the first receiving segwit address.
21-
// Run this example with cargo and WIF argument:
22-
// cargo run --example bip32 L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D
21+
// Run this example with cargo and seed(hex-encoded) argument:
22+
// cargo run --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
2323

2424
let args: Vec<String> = env::args().collect();
2525
if args.len() < 2 {
26-
eprintln!("not enough arguments. usage: {} <WIF>", &args[0]);
26+
eprintln!("not enough arguments. usage: {} <hex-encoded 32-byte seed>", &args[0]);
2727
process::exit(1);
2828
}
2929

30-
let wif = PrivateKey::from_wif(&args[1]).unwrap();
31-
println!("Seed WIF: {}", wif);
30+
let seed_hex = &args[1];
31+
println!("Seed: {}", seed_hex);
3232

33-
// use the network from WIF key
34-
let network = wif.network;
33+
// default network as mainnet
34+
let network = bitcoin::Network::Bitcoin;
3535
println!("Network: {:?}", network);
36-
// seed is a 32-byte secret in WIF
37-
let seed = wif.to_bytes();
36+
37+
let seed = Vec::from_hex(seed_hex).unwrap();
3838

3939
// we need secp256k1 context for key derivation
4040
let mut buf: Vec<AlignedType> = Vec::new();

0 commit comments

Comments
 (0)