Bdk Flutter

BDK is released under the MIT license. Docs Current pub package version. Issues Stars Forks Demo App

A Flutter library for the Bitcoin Development Kit. The bdk library aims to be the core building block for Bitcoin Applications of any kind.

Requirements

  • Flutter : 3.0 or higher
  • Android minSdkVersion. : API 23 or higher.
  • Deployment target : iOS 12.0 or greater.

How to Use

To use the bdk_flutter package in your project, add it as a dependency in your project's pubspec.yaml:

dependencies:
  bdk_flutter: ^0.30.0

Examples

Create a Wallet & sync the balance of a descriptor

import 'package:bdk_flutter/bdk_flutter.dart';

// ....

final mnemonic = await Mnemonic.create(WordCount.Words12);
final descriptorSecretKey = await DescriptorSecretKey.create( network: Network.Testnet,
                                                              mnemonic: mnemonic );
final externalDescriptor = await Descriptor.newBip44( descriptorSecretKey: descriptorSecretKey,
                                                      network: Network.Testnet,
                                                      keychain: KeyChainKind.External );
final internalDescriptor = await Descriptor.newBip44( descriptorSecretKey: descriptorSecretKey,
                                                      network: Network.Testnet,
                                                      keychain: KeyChainKind.Internal );
final blockchain = await Blockchain.create( config: BlockchainConfig.electrum(
                                                                        config: ElectrumConfig(
                                                                            stopGap: 10,
                                                                            timeout: 5,
                                                                            retry: 5,
                                                                            url: "ssl://electrum.blockstream.info:60002" )));
final wallet = await Wallet.create( descriptor: externalDescriptor,
                                    changeDescriptor: internalDescriptor,
                                    network: Network.TESTNET,
                                    databaseConfig: const DatabaseConfig.memory() );
final _ = await wallet.sync( blockchain );

Create a public wallet descriptor

import 'package:bdk_flutter/bdk_flutter.dart';

// ....

final mnemonic = await Mnemonic.create(WordCount.Words12);
final descriptorSecretKey = await DescriptorSecretKey.create( network: Network.Testnet,
                                                              mnemonic: mnemonic );
final externalDescriptor = await Descriptor.newBip44( descriptorSecretKey: descriptorSecretKey,
                                                      network: Network.Testnet,
                                                      keychain: KeyChainKind.External );
final externalPublicDescriptorStr = await externalDescriptor.asString();
final externalPublicDescriptor = await Descriptor.( descriptor: externalPublicDescriptorStr,
                                                    network: Network.Testnet);

Get the transaction details

import 'package:bdk_flutter/bdk_flutter.dart';

final bdkWallet = .....

// ....

final txBuilder  = TxBuilder();
final address = await Address.create(address: "mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB");

final script = await address.scriptPubKey();
final feeRate = await estimateFeeRate(25);

final txBuilderResult = await txBuilder.feeRate( feeRate.asSatPerVb() )
                                       .addRecipient( script, 2000 )
                                       .finish( bdkWallet );

final serializedPsbt = await txBuilderResult.psbt.jsonSerialize();
final jsonObject = json.decode(serializedPsbt);
final outputs = jsonObject['unsigned_tx']['output'] as List;
final inputs = jsonObject['inputs'][0]['non_witness_utxo']['output'] as List;

debugPrint("=========Inputs=====");
    for (var e in inputs) {
      debugPrint("amount: ${e['value']}");
      debugPrint("script_pubkey: ${e['script_pubkey']}");
    }

debugPrint("=========Outputs=====");
    for (var e in outputs) {
      debugPrint("amount: ${e['value']}");
      debugPrint("script_pubkey: ${e['script_pubkey']}");
    }

Create an internal and extarnal wallet descriptors from derivation path.

import 'package:bdk_flutter/bdk_flutter.dart';


final mnemonic = await Mnemonic.create(WordCount.Words12);
final descriptorSecretKey = await DescriptorSecretKey.create(
        network: Network.Testnet, mnemonic: mnemonic);

// create external descriptor
final derivationPath = await DerivationPath.create(path: "m/44h/1h/0h/0");
final descriptorPrivateKey =
        await descriptorSecretKey.derive(derivationPath);
final Descriptor descriptorPrivate = await Descriptor.create(
      descriptor: "pkh(${descriptorPrivateKey.toString()})",
      network: Network.Testnet,
    );

// create internal descriptor
final derivationPathInt =
        await DerivationPath.create(path: "m/44h/1h/0h/1");
final descriptorPrivateKeyInt =
        await descriptorSecretKey.derive(derivationPathInt);
final Descriptor descriptorPrivateInt = await Descriptor.create(
      descriptor: "pkh(${descriptorPrivateKeyInt.toString()})",
      network: Network.Testnet,
    );

final bdkWallet = await Wallet.create(
      descriptor: descriptorPrivate,
      changeDescriptor: descriptorPrivateInt,
      network: Network.Testnet,
      databaseConfig: const DatabaseConfig.memory(),
    );

final address =
        await bdkWallet.getAddress(addressIndex: const AddressIndex());
final internalAddress =
        await bdkWallet.getInternalAddress(addressIndex: const AddressIndex());

API Documentation

The latest API documentation is available here

Example Projects

  • *BDK Flutter Demo App: The BDK Flutter Demo App is a simple bitcoin app built in flutter to serve as a reference app to demonstrate bdk-flutter api usage.

How to build

Note that Flutter version 3.0 or later is required to build the plugin.

  1. Install Rust and Cargo The easiest way to get Cargo is to install the current stable release of Rust by using rustup. Installing Rust using rustup will also install cargo.

  2. Clone this repository

    git clone https://github.com/LtbLightning/bdk-flutter
    
  3. Activate dart ffigen

    dart pub global activate ffigen
    
  4. Android Setup

  • The Android NDK, or Native Development Kit, enables code written in other languages to be run on the JVM via the Java Native Interface, or JNI for short. After following the instructions above, the NDK should be installed in your $ANDROID_SDK_HOME/ndk folder, where ANDROID_SDK_HOME usually is:
    Windows: %APPDATA%\Local\Android\sdk
    MacOS: ~/Library/Android/sdk

    An issue regarding building Rust's core library against the latest NDK means that as of writing only NDK versions 22 and older can be used.

    You can alternatively use the latest version of the Android NDK which is greater than 22. However, this requires a hack to prevent the unable to find library -lgcc error.

  1. Build flutter bindings Navigate to rust directory, and run the following commands
    cargo build
    make all
    

Generating Docs Manually (Optional)

Please use the Dart documentation generator to generate the API documentation.

References:

Note: Caution this is Beta at this stage Please consider reviewing, experimenting, and contributing ⚡️

Thanks for taking a look!

Libraries

bdk_flutter
A Flutter library for the Bitcoin Development Kit.