Merge rust-bitcoin/rust-bitcoin#760: improve example: take hex-encoded seed instead of WIF in bip32 example
bb70820fed
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: ACKbb70820fed
RCasatta: utACKbb70820fed
Tree-SHA512: aaec9f7e3e8ce0e58b2a405e6ada75b1fc9de46ee6efb7fa2543fa626aa5f05704b05585158ab6147c495fc19abc6ade3c25225b3d75b3a3edeb8e00ba8d3976
This commit is contained in:
commit
0e2e559712
|
@ -56,8 +56,8 @@ echo "********* Testing no-std build *************"
|
||||||
cargo build --verbose --features="no-std $feature"
|
cargo build --verbose --features="no-std $feature"
|
||||||
done
|
done
|
||||||
|
|
||||||
cargo run --example bip32 L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D
|
cargo run --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
|
||||||
cargo run --no-default-features --features no-std --example bip32 L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D
|
cargo run --no-default-features --features no-std --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Test each feature
|
# Test each feature
|
||||||
|
|
|
@ -4,37 +4,37 @@ use std::{env, process};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use bitcoin::secp256k1::Secp256k1;
|
use bitcoin::secp256k1::Secp256k1;
|
||||||
use bitcoin::{PrivateKey, PublicKey};
|
use bitcoin::PublicKey;
|
||||||
use bitcoin::util::bip32::ExtendedPrivKey;
|
use bitcoin::util::bip32::ExtendedPrivKey;
|
||||||
use bitcoin::util::bip32::ExtendedPubKey;
|
use bitcoin::util::bip32::ExtendedPubKey;
|
||||||
use bitcoin::util::bip32::DerivationPath;
|
use bitcoin::util::bip32::DerivationPath;
|
||||||
use bitcoin::util::bip32::ChildNumber;
|
use bitcoin::util::bip32::ChildNumber;
|
||||||
use bitcoin::util::address::Address;
|
use bitcoin::util::address::Address;
|
||||||
use bitcoin::secp256k1::ffi::types::AlignedType;
|
use bitcoin::secp256k1::ffi::types::AlignedType;
|
||||||
|
use bitcoin::hashes::hex::FromHex;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// This example derives root xprv
|
// This example derives root xprv from a 32-byte seed,
|
||||||
// from a 32-byte secret of the input WIF string,
|
|
||||||
// derives the child xprv with path m/84h/0h/0h,
|
// derives the child xprv with path m/84h/0h/0h,
|
||||||
// prints out corresponding xpub,
|
// prints out corresponding xpub,
|
||||||
// calculates and prints out the first receiving segwit address.
|
// calculates and prints out the first receiving segwit address.
|
||||||
// Run this example with cargo and WIF argument:
|
// Run this example with cargo and seed(hex-encoded) argument:
|
||||||
// cargo run --example bip32 L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D
|
// cargo run --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
|
||||||
|
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
if args.len() < 2 {
|
if args.len() < 2 {
|
||||||
eprintln!("not enough arguments. usage: {} <WIF>", &args[0]);
|
eprintln!("not enough arguments. usage: {} <hex-encoded 32-byte seed>", &args[0]);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let wif = PrivateKey::from_wif(&args[1]).unwrap();
|
let seed_hex = &args[1];
|
||||||
println!("Seed WIF: {}", wif);
|
println!("Seed: {}", seed_hex);
|
||||||
|
|
||||||
// use the network from WIF key
|
// default network as mainnet
|
||||||
let network = wif.network;
|
let network = bitcoin::Network::Bitcoin;
|
||||||
println!("Network: {:?}", network);
|
println!("Network: {:?}", network);
|
||||||
// seed is a 32-byte secret in WIF
|
|
||||||
let seed = wif.to_bytes();
|
let seed = Vec::from_hex(seed_hex).unwrap();
|
||||||
|
|
||||||
// we need secp256k1 context for key derivation
|
// we need secp256k1 context for key derivation
|
||||||
let mut buf: Vec<AlignedType> = Vec::new();
|
let mut buf: Vec<AlignedType> = Vec::new();
|
||||||
|
|
Loading…
Reference in New Issue