GithubHelp home page GithubHelp logo

bitcoinwallet's Introduction

Ethereum Wallet

Generating ETH key pairs and address from mnemonic.


1. From entropy to mnemonic

1. Generating 128 digits random entropy

UUID uuid = UUID.randomUUID();
String[] digits = uuid.toString().split("\\-");
StringBuilder randomDigits = new StringBuilder();
for (String digit : digits) {
    randomDigits.append(digit);
}

2. Doing SHA256 to entropy for checksum, append first 4 bits to the end of entropy

//generate sha-256 from entropy
String encodeStr = "";
byte[] hash = Sha256.sha256(hexStringToByteArray(entropy));
encodeStr = String.valueOf(Hex.encodeHex(hash));
System.out.println(encodeStr);
char firstSHA = encodeStr.charAt(0);
String new_entropy = entropy + firstSHA;
String bin_entropy = "";
for (int i = 0; i < new_entropy.length(); i++) {
    bin_entropy += dict[Integer.parseInt(new_entropy.substring(i, i + 1), 16)];
}

3. Segment 132 bits entropy into 11 bits long parts

String[] segments = new String[12];
for (int i = 0; i <= 11; i++) {
    segments[i] = bin_entropy.substring(i * 11, (i + 1) * 11);
}

4. Generating mnemonic from dictionary

mnemonic += wordlist[Integer.valueOf(segments[0], 2)];
for (int j = 1; j < segments.length; j++) {
    mnemonic += " " + (wordlist[Integer.valueOf(segments[j], 2)]);
}

enter image description here


2. From mnemonic to seed

Using PBKDF2 function to get 512 bits seed from mnemonic. In this part we need a salt string to generate the seed we needed. Normally the value of salt is "mnemonic" for universality

String seed;
String salt = "mnemonic";
seed = getSeed(mnemonic, salt);

enter image description here


###3. From seed to master private key

Divide 512 bits seed into two equal parts, the first 256 bits is master private key and the last 256 bits is chain code. We could use BIP32 library to do the jobs by APIs this library provided.

ExtendedPrivateKey rootKey = ExtendedPrivateKey.fromSeed(hexStringToByteArray(seed), Bitcoin.MAIN_NET);

enter image description here


###4. From master private key to child private key Firstly, generate address index to get 0th private key generated from master private key and chain code.

AddressIndex ethAddressIndex = BIP44.m().purpose44().coinType(60).account(0).external().address(0);

44.60.0.0.0 is eth address index.

And then get key pair and address that we need.

ExtendedPrivateKey childPrivateKey = rootKey.derive(ethAddressIndex, AddressIndex.DERIVATION);
byte[] privateKeyBytes = childPrivateKey.getKey(); 
ECKeyPair keyPair = ECKeyPair.create(privateKeyBytes);
List<String> returnList = EthAddress(childPrivateKey, keyPair);

enter image description here

bitcoinwallet's People

Contributors

terryjiao avatar

Watchers

 avatar  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.