nunchuk-io / libnunchuk

GNU General Public License v3.0
57 stars 10 forks source link

Nunchuk Logo

About

libnunchuk is a lean & cross-platform C++ multisig library powered by Bitcoin Core.

libnunchuk is used by the Nunchuk Desktop and Mobile applications.

Why Bitcoin Core

Why does libnunchuk reuse Bitcoin Core code?

By leveraging Core, libnunchuk can gain access to powerful and reliable Bitcoin tools, while staying lean and cutting down on the number of dependencies.

Usage

Include <nunchuk.h> where you want to use libnunchuk.

#include <nunchuk.h>

using namespace nunchuk;

Create a Nunchuk instance.

// Create an AppSettings object to pass into the Nunchuk instance
AppSettings settings;
settings.set_chain(Chain::TESTNET);
settings.set_hwi_path("bin/hwi");
settings.enable_proxy(false);
settings.set_testnet_servers({"127.0.0.1:50001"});

auto nunchuk = MakeNunchuk(settings);
// ... use nunchuk as you like, see examples below

Examples

Get a list of plugged-in hardware devices.

auto devices = nunchuk.get()->GetDevices();

Create a new signer.

// ... using the first device returned from above
auto master_signer = nunchuk.get()->CreateMasterSigner(
    "signer_name", devices[0], [](int percent) { 
        // libnunchuk caches xpubs when adding a new master signer, so this method will take some time
        // Use this callback to check on the progress
        return true;
    });

Create a new multisig wallet.

// Nunchuk supports legacy, nested-segwit and native segwit addresses
AddressType address_type = AddressType::NATIVE_SEGWIT;

// Nunchuk supports multisig, singlesig and escrow wallets
WalletType wallet_type = WalletType::MULTI_SIG;

// Get a list of master signers that we manage
auto master_signers = nunchuk.get()->GetMasterSigners();

// Create 2 signers from the first 2 master signers
auto signer0 = nunchuk.get()->GetUnusedSignerFromMasterSigner(
    master_signers[0].get_id(), wallet_type, address_type);
auto signer1 = nunchuk.get()->GetUnusedSignerFromMasterSigner(
    master_signers[1].get_id(), wallet_type, address_type);

// Create a multisig (2/2) wallet
auto wallet = nunchuk.get()->CreateWallet("wallet_name", 2, 2,
    {signer0, signer1}, address_type, false);

Convenient util methods are also available in nunchuk::Utils.

auto xpub = Utils::SanitizeBIP32Input(Ypub, "xpub");
auto is_valid = Utils::IsValidXPub(xpub);
auto script_pub_key = Utils::AddressToScriptPubKey(address);

For more examples, see a simple multisig cli less than 300 LOC.

Setup

Generally we recommend using libnunchuk as a submodule in a larger CMake project.

$ cd your_project/
$ git submodule add https://github.com/nunchuk-io/libnunchuk
$ git submodule update --init --recursive

Add the following to your CMakeLists.txt.

add_subdirectory(libnunchuk)
target_link_libraries("${PROJECT_NAME}" PUBLIC nunchuk)

Build Bitcoin Core (details).

$ pushd libnunchuk/contrib/bitcoin
$ ./autogen.sh
$ ./configure --without-gui --disable-zmq --with-miniupnpc=no --enable-module-ecdh # important
$ make -j8
$ popd

Build Sqlcipher (details).

$ pushd libnunchuk/contrib/sqlcipher
$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"
$ make -j8
$ popd

Build your project.

$ mkdir build && cd build
$ cmake ..
$ cmake --build .

HWI

A HWI binary is needed to interact with hardware devices.

Set the HWI path by calling set_hwi_path() on AppSettings.

Contributing

Run tests.

$ ctest

Install clang-format.

$ brew install clang-format

Config hooks directory.

$ git config core.hooksPath hooks

For VSCode user

Install clang-format plugins.

Format files using this shortcut: Ctrl+⇧+F on Windows, Ctrl+⇧+I on Linux, and ⇧+⌥+F on macOS.

(Optional) Enable formatOnSave in .vscode/settings.json.

{
    "editor.formatOnSave": true
}

Nunchuk Applications

The Nunchuk Desktop and Mobile apps are available at https://nunchuk.io.

Read about our design philosophy at:

License

libnunchuk is released under the terms of the GPLv3 license. See COPYING for more information or see http://www.gnu.org/licenses/.