improve example: take hex-encoded seed instead of WIF in bip32 example

This commit is contained in:
KaFai Choi 2022-01-09 22:10:57 +07:00
parent d82afc6ef5
commit bb70820fed
No known key found for this signature in database
GPG Key ID: C93944C459E21542
2 changed files with 14 additions and 14 deletions

View File

@ -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

View File

@ -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();