Skip to content

Repository files navigation

Ethereum Wallet

Generating ETH key pairs and address from mnemonic.


1. From entropy to mnemonic

1. Generating 128 digits random entropy

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

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

//generate sha-256 from entropyStringencodeStr = "";
byte[] hash = Sha256.sha256(hexStringToByteArray(entropy));
encodeStr = String.valueOf(Hex.encodeHex(hash));
System.out.println(encodeStr);
charfirstSHA = encodeStr.charAt(0);
Stringnew_entropy = entropy + firstSHA;
Stringbin_entropy = "";
for (inti = 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 = newString[12];
for (inti = 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 (intj = 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

Stringseed;
Stringsalt = "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.

ExtendedPrivateKeyrootKey = 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.

AddressIndexethAddressIndex = 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.

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

enter image description here

About

Bitcoin and ETH wallet

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages