Skip to content

Add descriptor code sample#77

Merged
notmandatory merged 2 commits intobitcoindevkit:masterfrom
w0xlt:add_desc_code
Jul 19, 2022
Merged

Add descriptor code sample#77
notmandatory merged 2 commits intobitcoindevkit:masterfrom
w0xlt:add_desc_code

Conversation

@w0xlt
Copy link
Copy Markdown
Contributor

@w0xlt w0xlt commented Dec 24, 2021

There's a Descriptor section in Getting Started page, but there is no code showing how to use descriptors with BDK.

This PR adds a code example. This is based on this video.

But the video uses BDK 0.10.
This code uses the latest version, 0.15.

@netlify
Copy link
Copy Markdown

netlify Bot commented Dec 24, 2021

Deploy Preview for awesome-golick-685c88 ready!

Name Link
🔨 Latest commit 2f40309
🔍 Latest deploy log https://app.netlify.com/sites/awesome-golick-685c88/deploys/62d606f778d4d70008951f3d
😎 Deploy Preview https://deploy-preview-77--awesome-golick-685c88.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Comment thread docs/getting-started.md Outdated


// Wrap the derived key with the "wpkh()" string to produce a descriptor text
if let Secret(key, _, _) = derived_xprv_descriptor_key {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit cleaner way to do this is to use the bip84 template, details here:
https://docs.rs/bdk/0.15.0/bdk/descriptor/template/struct.Bip84.html

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the example in Bip84 template is useful to show how we can use bitcoin::utill to do the same thing. Where as here we wanna show how to use bdk api to do it. I was looking for the same and ended up doing it this way in my rpc tutorial, to only use things exposed in bdk.

I am still not fully satisfied with the amount of code it needs to create descriptors. And probably there's room to compact this out further.

Copy link
Copy Markdown
Contributor Author

@w0xlt w0xlt Jan 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also have another version with the code below:

pub fn mnemonic_to_xprv(network: &Network, mnemonic_words: &str) -> ExtendedPrivKey {
    let mnemonic  = Mnemonic::parse(mnemonic_words).unwrap();
    let xkey: ExtendedKey = mnemonic.into_extended_key().unwrap();
    let xprv = xkey.into_xprv(*network).unwrap();
    xprv
}

let wallet_name = wallet_name_from_descriptor(
    Bip84(xpriv.clone(), KeychainKind::External),
    Some(Bip84(*xpriv, KeychainKind::Internal)),
    *network,
    &Secp256k1::new()
 ).unwrap();
    
println!("mnemonic: {}\n\nrecv desc (pub key): {:#?}\n\nchng desc (pub key): {:#?}",
    mnemonic_words,
    wallet.get_descriptor_for_keychain(KeychainKind::External).to_string(),
    wallet.get_descriptor_for_keychain(KeychainKind::Internal).to_string());

I can change this PR to this version, but this one requires a wallet instance to be created before the descriptors are extracted.

Copy link
Copy Markdown
Contributor

@rajarshimaitra rajarshimaitra Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. I think I missunderstood @notmandatory 's comment. Yes it makes sense to use Bip84 template derivation instead of manually wrapping in wpkh string.

This will be a good modification to have..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR with this code in 60b72fe

Copy link
Copy Markdown
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK 4d3c467

It does make sense to expose the descriptor generation code in getting started page.

I found few more improvements that can be made in other part of the doc. Probably will open a separate PR for that.

Tested that the code works.

Just one nit.

Comment thread docs/getting-started.md Outdated
Copy link
Copy Markdown
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK e2da710

Changes looks good. Would be great to have this code in the tutorial #77 (comment).

@w0xlt
Copy link
Copy Markdown
Contributor Author

w0xlt commented Jan 11, 2022

PR updated with the code suggested in #77 (comment)

Copy link
Copy Markdown
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 60b72fe

@ConorOkus
Copy link
Copy Markdown

Is it possible to add this to the examples directory and make it part of CI?

@w0xlt
Copy link
Copy Markdown
Contributor Author

w0xlt commented Jan 14, 2022

@ConorOkus I added this code to the examples directory in bitcoindevkit/bdk#526

@notmandatory
Copy link
Copy Markdown
Member

Looks like this one is ready to go but needs a final rebase. Sorry for the long delay!

@w0xlt
Copy link
Copy Markdown
Contributor Author

w0xlt commented Jul 17, 2022

No problem. Rebased.

Copy link
Copy Markdown
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 734a74c

@notmandatory notmandatory merged commit 2f40309 into bitcoindevkit:master Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants