LtbLightning / bdk-flutter

Bitcoin Development Kit - Flutter Package
MIT License
60 stars 27 forks source link

How to set the derivation path. #68

Closed awaik closed 1 year ago

awaik commented 1 year ago

When creating a descriptor, I need to set the derivation path to m/87’/0’/0’.

In this issue https://github.com/LtbLightning/bdk-flutter/issues/30 was mentioned that we can create descriptors with a plain string This means we'll be able to create all types of descriptors just like we used to using plain strings

What is the correct sequence to create a descriptor with m/87’/0’/0’ derivation path?

For example, can I use this logic:

        final mnemonic = await Mnemonic.fromString(mnemonicStr);
        final descriptorSecretKey = await DescriptorSecretKey.create(
          network: Network.Testnet,
          mnemonic: mnemonic,
        );
        final Descriptor descriptorSecret = await Descriptor.newBip84(
          secretKey: descriptorSecretKey,
          network: Network.Testnet,
          keychain: keychain,
        );
        final String publicString = await descriptorSecret.asString();

Now in publicString I have [39bdba89/84'/1'/0']tpubDDZ8efTTNLhArXifVXs1zup2D9cw22P7tMjx9bZfKj9XgwdEbcFawXiksofrzjjSRNCcvPp25aL8MFtekNXQUTD64oAZZVDGvK3a1TahQAJ/1/*)#yl95k4wh

So the question is - how to create a descriptor with derivation path m/87’/0’/0’?

One more option: I can create xpub with derivation path to m/87’/0’/0’ with code

String deriveXpubFromMnemonic(String mnemonic, String derivationPath) {
  // Convert mnemonic to a seed
  Uint8List seed = bip39.mnemonicToSeed(mnemonic);

  // Derive the BIP32 master node from the seed
  bip32.BIP32 rootNode = bip32.BIP32.fromSeed(seed);

  // Derive the BIP32 node at the desired level using the derivation path
  bip32.BIP32 accountNode = rootNode.derivePath(derivationPath);

  // Get the extended public key (xpub) of the account node
  String xpub = accountNode.neutered().toBase58();

  return xpub;
}

I will have a string wpkh(xpub6CLX1NNnfyLxZJjGsMoftMUJT1iTCR4iFWWsTMTueRyLpFLbiVnR85t9rWxB8FZRM4p8NWLterUJvg9RZ9JHkLuc94RnisFp4mV5uwDxfgT/0/*).

But how to create descriptors for creating a wallet with xpub and BDK lib?

awaik commented 1 year ago

Sorry, you have it in the library, just missed the final DerivationPath bip87 = await DerivationPath.create(path: "m/87'/0'/0'");