-
Notifications
You must be signed in to change notification settings - Fork 452
Add mnemonic_to_descriptors example #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
notmandatory
merged 1 commit into
bitcoindevkit:master
from
vladimirfomene:mnemonic-to-descriptor-example
Nov 24, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright (c) 2020-2021 Bitcoin Dev Kit Developers | ||
| // | ||
| // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE | ||
| // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. | ||
| // You may not use this file except in accordance with one or both of these | ||
| // licenses. | ||
|
|
||
| use bdk::bitcoin::secp256k1::Secp256k1; | ||
| use bdk::bitcoin::util::bip32::DerivationPath; | ||
| use bdk::bitcoin::Network; | ||
| use bdk::descriptor; | ||
| use bdk::descriptor::IntoWalletDescriptor; | ||
| use bdk::keys::bip39::{Language, Mnemonic, WordCount}; | ||
| use bdk::keys::{GeneratableKey, GeneratedKey}; | ||
| use bdk::miniscript::Tap; | ||
| use bdk::Error as BDK_Error; | ||
| use std::error::Error; | ||
| use std::str::FromStr; | ||
|
|
||
| /// This example demonstrates how to generate a mnemonic phrase | ||
| /// using BDK and use that to generate a descriptor string. | ||
| fn main() -> Result<(), Box<dyn Error>> { | ||
| let secp = Secp256k1::new(); | ||
|
|
||
| // In this example we are generating a 12 words mnemonic phrase | ||
| // but it is also possible generate 15, 18, 21 and 24 words | ||
| // using their respective `WordCount` variant. | ||
| let mnemonic: GeneratedKey<_, Tap> = | ||
| Mnemonic::generate((WordCount::Words12, Language::English)) | ||
| .map_err(|_| BDK_Error::Generic("Mnemonic generation error".to_string()))?; | ||
|
|
||
| println!("Mnemonic phrase: {}", *mnemonic); | ||
| let mnemonic_with_passphrase = (mnemonic, None); | ||
|
|
||
| // define external and internal derivation key path | ||
| let external_path = DerivationPath::from_str("m/86h/0h/0h/0").unwrap(); | ||
| let internal_path = DerivationPath::from_str("m/86h/0h/0h/1").unwrap(); | ||
|
|
||
| // generate external and internal descriptor from mnemonic | ||
| let (external_descriptor, ext_keymap) = | ||
| descriptor!(tr((mnemonic_with_passphrase.clone(), external_path)))? | ||
| .into_wallet_descriptor(&secp, Network::Testnet)?; | ||
| let (internal_descriptor, int_keymap) = | ||
| descriptor!(tr((mnemonic_with_passphrase, internal_path)))? | ||
| .into_wallet_descriptor(&secp, Network::Testnet)?; | ||
|
|
||
| println!("tpub external descriptor: {}", external_descriptor); | ||
| println!("tpub internal descriptor: {}", internal_descriptor); | ||
| println!( | ||
| "tprv external descriptor: {}", | ||
| external_descriptor.to_string_with_secret(&ext_keymap) | ||
| ); | ||
| println!( | ||
| "tprv internal descriptor: {}", | ||
| internal_descriptor.to_string_with_secret(&int_keymap) | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.