Bip39 mnemonic C++ implementation. Contains java bindings.
Logic almost completely taken from bitcoin-js library for generating mnemonic phrases. Site and github
- Generate random mnemonic (PCG generator)
- Create root and extended bip* standard keys using some derivation path
- Install cmake (if not installed yet)
- Install Oracle JDK or OpenJDK (not tested yet)
- Set environment variable: JAVA_HOME=/path/to/jdk
- Build
mkdir build && cd build
cmake ../ -DCMAKE_BUILD_TYPE=Release -DENABLE_SHARED=On -DENABLE_BIP39_JNI=On -DENABLE_BIP39_C=Off -DENABLE_TEST=Off
make
- Pickup lib files:
- libbip39.[a | so | dylib]
- libbip39_jni.[so | dylib]
- Setup mingw64 for linux or macOS
- Setup Windows JDK (or just copy from windows machine to somewhere path)
- Build
mkdir build && cd build
cmake ../ -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=../cmake/mingw-w64.cmake \
-DCMAKE_SYSROOT=/path/to/mingw \
-DCROSS_ARCH=[choose i686 or x86_64] \
-DJAVA_HOME=/path/to/extracted/windows/jdk
make
- Pickup lib files:
- libbip39.dll
- libbip39.dll.a
- libbip39_jni.dll
- libbip39_jni.dll.a
Requirements
- CMake for Windows
- Visual Studio (at least community edition), tested only version 2019
- Visual Studio C++ compiler
- To build tests, you could install Conan OR just set configure-time flag
-DENABLE_CONAN=Off
and resolve submodules:git submodule update --init --recursive
Step-by-step:
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX="D:\path\you\want\to\install\libs"
cmake --build .
cmake --build . --target install
Then pick-up lib files and headers from D:\path\you\want\to\install\libs
#include <bip3x/utils.h>
#include <bip3x/Bip39Mnemonic.h>
#include <bip3x/HDKeyEncoder.h>
#include <iostream>
using namespace bip3x;
int main(int argc, char** argv) {
// create random words
Bip39Mnemonic::MnemonicResult encodedMnemonic = Bip39Mnemonic::generate();
std::cout << encodedMnemonic.words << std::endl;
// create mnemonic seed
bytes_64 seed = HDKeyEncoder::makeBip39Seed(encodedMnemonic.words);
// create root key from mnemonic seed
HDKey bip32RootKey = HDKeyEncoder::makeBip32RootKey(seed);
// and, finally derive keys
HDKey ethereumKey = HDKeyEncoder::makeExtendedKey(bip32RootKey, "m/44'/60'/0'/0");
// extended private key
std::cout << ethereumKey.extPrivateKey.toString() << std::endl;
// private key
std::cout << ethereumKey.privateKey.toString() << std::endl;
// extended public key
std::cout << ethereumKey.extPublicKey.toString() << std::endl;
// public key
std::cout << ethereumKey.publicKey.toString() << std::endl;
// et cetera..
// the end.
return 0;
}
tbd
- Add this project as submodule (or just copy as sources) to your android module:
cd /path/to/project/app/src/main
mkdir cpp && cd cpp
git submodule add https://github.com/edwardstock/bip3x.git bip39
- Create root cmake project in
/path/to/project/[moduleName]/src/main/cpp
project(moduleName-bindings)
cmake_minimum_required(VERSION 3.4)
# include bip39
include_directories(bip39)
# add library
add_subdirectory(bip39)
- Configure gradle
android {
...
sourceSets {
main {
jni.srcDirs = ['src/main/cpp/bip39/src/java']
java.srcDirs = ['src/main/cpp/bip39/src/java']
}
}
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
...
}
- Initialize it
import com.edwardstock.bip3x.NativeBip39
class Main {
public static int main(String[] args) {
NativeBip39.init();
if(!NativeBip39.isEnabled()) {
throw new RuntimeException(NativeBip39.getError());
}
}
}
- Done!
- Just add it
# include bip39
include_directories(bip39)
# add library
add_subdirectory(bip39)
and
target_link_libraries(bip39)
- Done!
TBD (see src/main.cpp for simple examples)