Git Product home page Git Product logo

native-bip39's Introduction

native-bip39 (with java bindings)

Bip39 mnemonic C++ implementation. Contains java bindings.

CircleCI

Description

Logic almost completely taken from bitcoin-js library for generating mnemonic phrases. Site and github

Features

  • Generate random mnemonic (PCG generator)
  • Create root and extended bip* standard keys using some derivation path

Build native JNI libs

  • 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

make
  • Pickup lib files:
    • libbip39.[a | so | dylib]
    • libbip39_jni.[so | dylib]

Cross-compile for Windows under mingw64 (MSVC didn't tested yet)

  • 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

Compiling under windows and mingw64 can be successfully, i didn't tested.

Examples

C++

#include <minter/utils.h>
#include <minter/Bip39Mnemonic.h>
#include <minter/HDKeyEncoder.h>
#include <iostream>

using namespace minter;

int main(int argc, char** argv) {
    // create random words
    Bip39Mnemonic::MnemonicResult encodedMnemonic = Bip39Mnemonic::generate();
    std::cout << encodedMnemonic.words << std::endl;
    
    // create mnemonic seed
    Data64 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;
}

Java

tbd

Android and java bindings

  • 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/native-bip39.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)
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 network.minter.core.bip39.NativeBip39

class Main {
    public static int main(String[] args) {
        NativeBip39.init();
        
        if(!NativeBip39.isEnabled()) {
            throw new RuntimeException(NativeBip39.getError());
        }
    }
}
  • Done!

Add to CMake project

  • Just add it
# include bip39
include_directories(bip39)
    
# add library
add_subdirectory(bip39)

and

target_link_libraries(bip39)
  • Done!

Documentation

TBD (see src/main.cpp for simple examples)

native-bip39's People

Contributors

edwardstock avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.