Code Monkey home page Code Monkey logo

bip3x's Introduction

bip3x (with java bindings)

Bip39 mnemonic C++ implementation. Contains java bindings.

CircleCI Download

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

Install using RPM package

Supported distributions:

  • centos 7, 8
  • fedora 32-34

Steps:

  • Create edwardstock.repo file inside /etc/yum.repos.d
  • Add below to this file
[edwardstock]
name=edwardstock
baseurl=https://edwardstock.jfrog.io/artifactory/rhel/[centos or fedora]/\$releasever/\$basearch
enabled=1
gpgcheck=0
gpgkey=https://edwardstock.jfrog.io/artifactory/rhel/[centos or fedora]/\$releasever/\$basearch/repodata/repomd.xml.key
repo_gpgcheck=1
  • Update repository yum -y update or dnf update
  • Install lib yum install bip3x-devel

Install using DEB package

Supported distributions:

  • debian: stretch, buster
  • ubuntu: xenial, bionic, focal, groovy
echo "deb https://edwardstock.jfrog.io/artifactory/debian {distribution} main" | sudo tee -a /etc/apt/sources.list
curl -s https://edwardstock.jfrog.io/artifactory/api/gpg/key/public | sudo apt-key add -
apt update && apt install libbip3x-dev



After installing package from repository, you can use cmake-find module to find library:

find_package(bip3x 2.2.1 REQUIRED)
target_link_libraries(MY_PROJECT bip3x::bip39)

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 -DENABLE_SHARED=On -DENABLE_BIP39_JNI=On -DENABLE_BIP39_C=Off -DENABLE_TEST=Off

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

Cross-compile for Windows under mingw64

  • Setup mingw64 for linux or macOS
  • Setup Windows JDK (or just copy from windows machine to somewhere path)
  • IMPORTANT: using c++ (mt19937, and PCGRand not works properly too) random generator is not secure. Use -DUSE_OPENSSL_RANDOM=On while configuring to use OpenSSL random generator.
  • 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

Build under the Windows

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

Examples

C++

#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
    // copy key to leave root key
    HDKey ethereumKey = bip32RootKey;
    // makeExtendedKey modifies source key
    HDKeyEncoder::makeExtendedKey(ethereumKey, "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/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)
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!

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)

bip3x's People

Contributors

edwardstock avatar

Watchers

 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.