Skip to content
Fork of the libwally for Arduino and Mbed
C M4 C++ Shell Makefile Python Other
Branch: master
Clone or download
Pull request Compare This branch is 3 commits ahead, 50 commits behind ElementsProject:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
contrib
docs/source
examples/basic_example
src
tools
.gitignore
.gitlab-ci.yml
.travis.yml
CHANGES.md
INSTALL
LICENSE
MANIFEST.in
Makefile.am
README.md
configure.ac
keywords.txt
library.properties
setup.py

README.md

libwally for microcontrollers

About this fork

This fork includes minimal changes in the source files of the original library libwally by Blockstream.

It moves around files and changes some #include paths such that Arduino and Mbed IDEs can understand it. It also includes BIP-174 PSBT parsing and serialization (frmo the pull request of original library)

Tested on ESP32 (M5Stack) and STM32F469I-Discovery (Mbed OS - bare metal, no RTOS).

Installation

You should have secp256k1 library installed.

For Arduino:

Clone this repo to the Arduino/libraries/ folder (or download zip file and select Sketch->Include Library->Add .ZIP Library. Check out the example to see it in action.

For Mbed:

Clone this repo to the project folder, for Mbed-CLI use mbed add https://github.com/diybitcoinhardware/libwally-core.git, for online IDE do Import Library and put there a link to this repo. Don't forget to add secp256k1 library as well.

You can also import this project and start from it.

Usage

Library documentation

PSBT documentation is not available - look at the comments in the source code (wally_psbt.h) or in the examples mentioned above.

First init the library with wally_init(0);

A simple example for HD keys and address derivation:

wally_init(0);

size_t len; // to store serialization length
int res;

// converting recovery phrase to seed
uint8_t seed[BIP39_SEED_LEN_512];
bip39_mnemonic_to_seed("transfer craft short window major caught laugh hungry timber mistake skin parade", "my password", seed, sizeof(seed), &len);

// getting HD root key
struct ext_key * root = NULL;
bip32_key_from_seed_alloc(seed, sizeof(seed), BIP32_VER_TEST_PRIVATE, 0, &root);

// deriving account xpub (m/84h/1h/0h)
struct ext_key * account = NULL;
uint32_t path[] = {
 BIP32_INITIAL_HARDENED_CHILD+84, // 84h
 BIP32_INITIAL_HARDENED_CHILD+1,  // 1h
 BIP32_INITIAL_HARDENED_CHILD     // 0h
};
bip32_key_from_parent_path_alloc(root, path, 3, BIP32_FLAG_KEY_PRIVATE, &account);
// we don't need root anymore
bip32_key_free(root);
// get xpub of the account to print / send to the host
char *xpub = NULL;
bip32_key_to_base58(account, BIP32_FLAG_KEY_PUBLIC, &xpub);
// send it to the host or show to the user
// when done - clean up
wally_free_string(xpub);

// derive address
struct ext_key * first_recv = NULL;
uint32_t recv_path[] = {0, 0}; // first receiving address: m/0/0

// we only need public key here, no need in private key
bip32_key_from_parent_path_alloc(account, recv_path, 2, BIP32_FLAG_KEY_PUBLIC, &first_recv);
// we don't need account key anymore
bip32_key_free(account);

// native segwit address
char * addr = NULL;
res = wally_bip32_key_to_addr_segwit(first_recv, "tb", 0, &addr);
// we don't need ext_key anymore
bip32_key_free(first_recv);
// print or show it, then clean up
wally_free_string(addr);
You can’t perform that action at this time.