In this article, we will talk about ETH wallet address generation mode
Previously said some blockchain generated wallet address method said, so how to use code to generate wallet address? When I was making a wallet before, I searched a lot of information, few write how to generate wallet address, of course, English information is not much. Finally, I went to GayHub to see the open source wallet and figured out some of the methods. Here we use Bitcoinj to generate. Generally, there are several steps: generate mnemonic words, according to the mnemonic words generate address
Configure the environment
First, we use Android Studio 3.0 to add dependencies directly to app/build.gradle
implementation group: 'org.bitcoinj', name: 'bitcoinj-core', version: '0.14.6'
implementation 'org. Web3j: core: 3.3.1 - android'
Copy the code
The MnemonicUtils class is used to generate the mnemonicmnemonic. However, the MnemonicUtils class does not load the mnemonic list file on Android
private static List<String> populateWordList() {
URL url = Thread.currentThread().getContextClassLoader()
.getResource("en-mnemonic-word-list.txt");
try {
return readAllLines(url.toURI().getSchemeSpecificPart());
} catch (Exception e) {
returnCollections.emptyList(); }}Copy the code
As you can see, this is Java’s way of loading resources, but android needs to do platform adaptation. We put the en-mnemonic-word-list. TXT file under assets and load it in an Android-appropriate pose. Good, no problem
private fun populateWordList(): List<String> { try { val fis = App.instance.assets? .open("en-mnemonic-word-list.txt")
return readAllLines(fis!!)
} catch (e: IOException) {
e.printStackTrace()
}
return emptyList()
}
Copy the code
Generate mnemonic words
To generate a mnemonic, you need to use the class MnemonicUtils. To generate a mnemonic, you need the following code to generate a mnemonic in the format of 12 words
//average green proud remember advance trick estate oblige trouble when cube person
private val secureRandom = MySecureRandomUtils.secureRandom()
fun makeMnemonic(): String {
val initialEntropy = ByteArray(16)
secureRandom.nextBytes(initialEntropy)
return MyMnemonicUtils.generateMnemonic(initialEntropy)
}
Copy the code
Generating an ETH Address
Based on the generated mnemonic words, a series of categories are generated. BIP32 Deterministic Wallet algorithm is applied.
private fun createETHWalletFromWords(words: String): TianWallet {
val wordsList = Arrays.asList(*words.split("\\s+".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray())
val deterministicSeed = DeterministicSeed(wordsList, null, "", 0)
val deterministicKeyChain = DeterministicKeyChain.builder().seed(deterministicSeed).build()
//DERIVATION_PATH = "m/44'/60'/0'/0/0"
val privateKey = deterministicKeyChain.getKeyByPath(parsePath(DERIVATION_PATH), true).privKey
val ecKeyPair = ECKeyPair.create(privateKey)
val address = Keys.getAddress(ecKeyPair)
return TianWallet("0x$address", Numeric.toHexStringWithPrefix(ecKeyPair.privateKey), Numeric.toHexStringWithPrefix(ecKeyPair.publicKey), words)
}
Copy the code