Generating ETH key pairs and address from mnemonic.
UUID uuid = UUID.randomUUID();
String[] digits = uuid.toString().split("\\-");
StringBuilder randomDigits = new StringBuilder();
for (String digit : digits) {
randomDigits.append(digit);
}
//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)];
}
String[] segments = new String[12];
for (int i = 0; i <= 11; i++) {
segments[i] = bin_entropy.substring(i * 11, (i + 1) * 11);
}
mnemonic += wordlist[Integer.valueOf(segments[0], 2)];
for (int j = 1; j < segments.length; j++) {
mnemonic += " " + (wordlist[Integer.valueOf(segments[j], 2)]);
}
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);
###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);
###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);