LtbLightning / bdk-flutter

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

Descriptor.create error #70

Closed awaik closed 1 year ago

awaik commented 1 year ago

Hi! Can you please help - how to create descriptors with DerivationPath that is not pre-defined in the plugin.

For example, I create xprv + xpub

      // Instantiate the root key
      final rootKey = await DescriptorSecretKey.create(
        network: Network.Testnet,
        mnemonic: mnemonic,
      );
      // Derive the xprv and xpub at the BIP87 path:
      final DescriptorSecretKey descriptorPrivateKey = await rootKey.derive(bip87);
      final DescriptorPublicKey descriptorPublicKey = await descriptorPrivateKey.asPublic();

It works.

After that, I'm trying to create descriptors with

      final Descriptor descriptorPrivate = await Descriptor.create(
        descriptor: descriptorPrivateKey.toString(),
        network: Network.Testnet,
      );
      final Descriptor descriptorPublic = await Descriptor.create(
        descriptor: descriptorPublicKey.asString(),
        network: Network.Testnet,
      );

and got the error

Unhandled Exception: BdkException.miniscript(e: "))))

BitcoinZavior commented 1 year ago

Hi @awaik

When creating a descriptor from a string the descriptor should be correctly formed:

See this https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md and https://bitcoindevkit.org/descriptors/

Updating your code to the following will work:

descriptorTest() async {
  final mnemonic = await Mnemonic.fromString(
      'puppy interest whip tonight dad never sudden response push zone pig patch');
  final descriptorSecretKey = await DescriptorSecretKey.create(
    network: Network.Testnet,
    mnemonic: mnemonic,
  );
  final derivationPath = await DerivationPath.create(path: "m/44h/1h/0h");
  final descriptorPrivateKey =
      await descriptorSecretKey.derive(derivationPath);
  final descriptorPublicKey = await descriptorPrivateKey.asPublic();

  final Descriptor descriptorPrivate = await Descriptor.create(
    descriptor: "pkh(${descriptorPrivateKey.toString()})",
    network: Network.Testnet,
  );
  final Descriptor descriptorPublic = await Descriptor.create(
    descriptor: "pkh(${descriptorPublicKey.asString()})",
    network: Network.Testnet,
  );
  final descriptorPublicString = await descriptorPublic.asString();
  final descriptorPrivateString = await descriptorPrivate.asString();
  print(descriptorPublicString);
  print(descriptorPrivateString);
}
awaik commented 1 year ago

Thank you, it works.