Add API for generating master keys directly - #218
Conversation
504671d to
be2eec1
Compare
This API allows developers to build master extended keys directly, without going through the mnemonics step
be2eec1 to
d8cdad2
Compare
|
Would be happy to hear initial feedback from @kirillzh and @jesseposner. Does this address your need? Did you have another API shape in mind? In Kotlin the call site currently looks like this: val bip32RootKey: DescriptorSecretKey = DescriptorSecretKey.generate(Network.TESTNET)
val bip84ExtendedKey: DescriptorSecretKey = bip32RootKey.extend(DerivationPath("m/84h/1h/0h/0"))
val descriptor = "wpkh(${bip84ExtendedKey.asString()})"
println(descriptor)
// wpkh(tprv8ZgxMBicQKsPeSUx4F4fC6Vc4rWBxfJV1FVBWWLxqapQpSdZ2huNuhcRUdWPjNFbQmQ285bjXV7jayWU7RyL5XSNMSC5J6wf6gj1RCCh43G/84'/1'/0'/0/*) |
a87c0f4 to
7b12b60
Compare
|
I wonder if the names We don't have a direct match for these methods in BDK on the |
7b12b60 to
4270f9c
Compare
| Balance as BdkBalance, BlockTime, Error as BdkError, FeeRate, KeychainKind, SignOptions, | ||
| SyncOptions as BdkSyncOptions, Wallet as BdkWallet, | ||
| }; | ||
| use rand::{thread_rng, Rng}; |
There was a problem hiding this comment.
Would it be okay to use thread_rng, Rng this way?
use bdk::bitcoin::secp256k1::rand::{thread_rng, Rng};
This way we don't have to add rand to Cargo.toml
There was a problem hiding this comment.
Oh good idea. @notmandatory do I understand correctly that if this is coming from re-exports it's good to go? I think in this case bdk re-exports bitcoin which re-exports secp256k1 which re-exports rand... lol.
There was a problem hiding this comment.
The latest version of bdk (0.24) updated rand version to 0.8, and I confirmed rand is exported by the secp256k1 lib as @dhruv-1001 shows above should work, and we don't need to add rand as a dependency to bdk-ffi.
|
I just took another look at how entropy and Mnemonics work in |
This API allows developers to build master extended keys directly, without going through the mnemonics step
4270f9c to
869a193
Compare
|
@notmandatory Does the fn test_keys_generate_xprv() {
let generated_xprv: GeneratedKey<_, miniscript::Segwitv0> =
bip32::ExtendedPrivKey::generate_with_entropy_default(TEST_ENTROPY).unwrap();
assert_eq!(generated_xprv.valid_networks, any_network());
assert_eq!(generated_xprv.to_string(), "xprv9s21ZrQH143K4Xr1cJyqTvuL2FWR8eicgY9boWqMBv8MDVUZ65AXHnzBrK1nyomu6wdcabRgmGTaAKawvhAno1V5FowGpTLVx3jxzE5uk3Q");
} |
@jesseposner , for the Rust lib you're correct that We could certainly also give users both options, to create |
|
@notmandatory The only other reason not to create a mnemonic first is that a mnemonic is limited to 256 bits of entropy, whereas BIP32 allows for 512 bits of entropy. However, anything over 256 bits is overkill and BIP32 recommends 256 bits, so as a practical matter I don't think much is being lost with the 256 limit. So the benefits of the additional flexibility of not requiring a mnemonic are pretty minor. It's nice to have but I also agree that it's a reasonable simplification to require a mnemonic. |
|
So we've merged #219, which allows us to create entropy/mnemonics, or use custom entropy to generate those mnemonics, and for now we'll keep that mnemonic as a required input for the If you don't wish to store the mnemonic, the new call-site looks like this: val descriptorSecretKey = DescriptorSecretKey(
network = Network.TESTNET,
mnemonic = Mnemonic(WordCount.WORDS12),
password = "",
)
// generate from custom entropy
val entropy: List<UByte> = listOf<UByte>(...)
val descriptorSecretKey = DescriptorSecretKey(
network = Network.TESTNET,
mnemonic = Mnemonic.fromEntropy(entropy),
password = "",
) |
Rough draft of the new API for generating
DescriptorSecretKeywithout going through the mnemonics step.A few questions still left to figure out:
generate_from_entropy()method? I added it as a bonus but we should still discuss if there is a need for it.randcrate imported here (we don't need it on the mnemonic step for example). There might be a way to get this done internally in bdk without generating the entropy at the ffi layer.