bitcoindevkit / bdk

A modern, lightweight, descriptor-based wallet library written in Rust!
Other
864 stars 311 forks source link

[feature request] Include derivation path in `AddressInfo` #517

Open w0xlt opened 2 years ago

w0xlt commented 2 years ago

The derivation path of an address can be useful information, and AddressInfo struct seems like a good place to retrieve it.

rajarshimaitra commented 2 years ago

The AddressInfo struct already contains an index. Which denotes the position of the address in the last derivation chain. https://github.com/bitcoindevkit/bdk/blob/fdb272e039b650c6f877b20b969fcc07fe086e84/src/wallet/mod.rs#L214-L219

Because BDK wallet starts with a descriptor, the derivation path will always need to be specified at wallet creation time. So in that sense the lib user will always know whats the derivation path before using BDK. So the only information that BDK needs to provide is the last index for each addresses.

w0xlt commented 2 years ago

Got it. But descriptors have more information than the derivation path. A grep can be used. But a more reliable method would be good.

How can I add the derivation path in the code below, for example ?

for n in 0..10 {
    let address_info = wallet.get_address(AddressIndex::Peek(n)).unwrap();
    let addr = Address::from_script(&address_info.script_pubkey(), Network::Testnet).unwrap().to_string();
    println!("{}: {}", n, addr);
}
rajarshimaitra commented 2 years ago

Here the wallet already has the derivation path internally in its descriptor value. So when you are saying Peek(n), the actual derivation path is something like m/84'/0'/0'/1/n (depending on what was the descriptor), where the last index is only what you need to put in get_address and the wallet already knows all the previous segments of the full path.

And you would have this previous path segments stored somewhere in your code before you wrote wallet::new(), as that's an input to it.

In other words, as a lib user, you always know what your path is.

Parse the descriptor you used for wallet creation and append the last index. Thats the full path of the derived address.