Blockchain Using Java’s Web3j and Ethereum Part II: Restore/Create or Open an account
By: Aniket Tikone
Tags: Blockchain Technology, Smart Contracts, Web3j, Blockchain, Java

ref:google.com

Interaction of the private key with an etherium wallet

A private key is a very large random number that is used to generate a public key. This public key is your address on the Ethereum blockchain. The private key allows you to sign transactions. These transactions can be verified by other participants on the chain against your Public key.

Private keys are difficult to remember, so wallets allow you to store your private key in them secured with a password (using symmetric cryptography). What this means is, you can sign your transaction on that same wallet instance using an easy to remember the password. It is very important to understand that this password is not your private key, and if you lose access to this wallet, your password will not allow you to retrieve the private key from other wallets.

Mnemonic phrases:

Mnemonic phrases, also known as ‘seed words’ or ‘recovery phrases’, are ordered lists of 12–24 words which bring you to specific wallet addresses. These are typically used for recovery, and they are not meant to be used as the main method of access for anyone’s wallet. These phrases offer direct access to one’s wallet, so they should be treated carefully.

If you want to give the functionality of creating an account using java,then Web3j’s WalletUtil’s offers a method to create a JSON encrypted keystore.

Step 1:

Create an maven project in Eclipse and change java compiler to version 1.8

Step 2:

add Web3j library/dependency from https://mvnrepository.com/artifact/org.web3j/core/4.5.5 into your pom.xml file

here’s the dependency to:



<dependency>

<groupId>org.web3j</groupId>

<artifactId>core</artifactId>

<version>4.5.5</version>

</dependency> https://mvnrepository.com/artifact/org.web3j/core org.web3j core 4.5.5

pom.xml

Step 3:

WalletUtils has some Utility functions for working with Wallet files.

Define a location to store wallet file

and provide password and file location to below method which will provide Mnemonic phrases and will generate a UTC Json file,which stores credentials of user in Encrypted format

Bip39Wallet walletName = WalletUtils.generateBip39Wallet(walletPassword, walletDirectory);

UTC Json File:

Step 4:

load Credentials from Bip39Wallet which will provide private ,public keys for an account

Credentials credentials = WalletUtils.loadBip39Credentials(walletPassword, walletName.getMnemonic());

Step 5:

Get Private and public keys:

ECKeyPair privateKey = credentials.getEcKeyPair();

ECKeyPair will provide private key and public key,which are essentials for handling transactions and events

Full reference code for creating account using Password and Mnemonic phrases

// ************ — — — — — — create account by seed phrase ****************** System.out.println(“Creating New Account”); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter New Password”); String walletPassword = br.readLine(); /* Define Wallet File Location */ File walletDirectory = new File(“/home/anikett”); Bip39Wallet walletName = WalletUtils.generateBip39Wallet(walletPassword, walletDirectory); System.out.println(“wallet location: “ + walletDirectory + “/” + walletName); Credentials credentials = WalletUtils.loadBip39Credentials(walletPassword, walletName.getMnemonic()); String accountAddress = credentials.getAddress(); System.out.println(“Account address: “ + credentials.getAddress()); ECKeyPair privateKey = credentials.getEcKeyPair(); String seedPhrase = walletName.getMnemonic(); System.out.println(“Account Details:”); System.out.println(“Your New Account : “ + credentials.getAddress()); System.out.println(“Mneminic Code: “ + walletName.getMnemonic()); System.out.println(“Private Key: “ + privateKey.getPrivateKey().toString(16)); System.out.println(“Public Key: “ + privateKey.getPublicKey().toString(16));

createAccountWithSeeds.java

Output of this code:

Creating New Account Enter New Password Anny1234 wallet location: /home/anikett/Bip39Wallet{filename=’UTC — 2020–03–17T09–44–25.272000000Z — 967a1e84a5a35eeac81b3cad03b9c4d473282471.json’, mnemonic=’veteran piano hotel three echo hire mosquito dutch despair emotion crunch neck’} Account address: 0x967a1e84a5a35eeac81b3cad03b9c4d473282471 Account Details: Your New Account : 0x967a1e84a5a35eeac81b3cad03b9c4d473282471 Mneminic Code: veteran piano hotel three echo hire mosquito dutch despair emotion crunch neck Private Key: 7c75421dbfbce3ab3f0d4d68df68cb9b40a45eca78decbaa304338662d9c1493 Public Key: 22d48659060d75b300c7f6ba8229fa33ed676df57f46da6f0dbe33f8487030bc68c6143b42085b99678e5ea96cdd45b04fbee868aa0fbd93047ca851426e1b34

So far we have successfully created an account in our local machine !

But what if we don’t remember our private key in future ?

but only do remember our own password and Mnemonic phrases ?

In that situation we will provide password and Mnemonic phrases to below method which will provide us credentials which contains private key,public key and user address

Credentials restoreCredentials = WalletUtils.loadBip39Credentials(password, seedPhrase2);

After providing password and Mnemonic phrases,it will generate your wallet credentials

here’s an reference to my restore account code

/************* — — — — — — Restore Account ************ — — — — — — */ System.out.println(“Restoring Account by password and Seedphrase”); System.out.println(“Enter Wallet Password”); String password=br.readLine(); System.out.println(“Enter Wallet SeedPhrase”); String seedPhrase2=br.readLine(); Credentials restoreCredentials = WalletUtils.loadBip39Credentials(password, seedPhrase2); ECKeyPair restoredPrivateKey = restoreCredentials.getEcKeyPair(); String restoredAccountAddress = restoreCredentials.getAddress(); System.out.println(“Account address: “ + restoreCredentials.getAddress()); System.out.println(“Account Details:”); System.out.println(“Your New Account : “ + restoreCredentials.getAddress()); System.out.println(“Mneminic Code: “ + seedPhrase2); System.out.println(“Private Key: “ + restoredPrivateKey.getPrivateKey().toString(16)); System.out.println(“Public Key: “ + restoredPrivateKey.getPublicKey().toString(16));

output of this code:

Creating New Account Enter New Password anny123 wallet location: /home/anikett/Bip39Wallet{filename='UTC--2020-03-17T10-20-49.24000000Z--27f6178d2d02cb910cace19b181d3421ad410239.json', mnemonic='industry avocado negative obscure merry version will trial sting again hole promote'} Account address: 0x27f6178d2d02cb910cace19b181d3421ad410239 Account Details: Your New Account : 0x27f6178d2d02cb910cace19b181d3421ad410239 Mneminic Code: industry avocado negative obscure merry version will trial sting again hole promote Private Key: 373b8812db52ae1e774483f6db35fab5e31ab7526504987834bbc118a0c4ffc3 Public Key: a48b69bae03835e91dc43ca99b927dc0fc3520d1bf0a3c158e7d008e63dcd26135cdba3a34c31c8083bb0f5b1bbaabf571e55ec1bff300882227239416665061 Restoring Account by password and Seedphrase Enter Wallet Password anny123 Enter Wallet SeedPhrase industry avocado negative obscure merry version will trial sting again hole promote Account address: 0x27f6178d2d02cb910cace19b181d3421ad410239 Account Details: Your New Account : 0x27f6178d2d02cb910cace19b181d3421ad410239 Mneminic Code: industry avocado negative obscure merry version will trial sting again hole promote Private Key: 373b8812db52ae1e774483f6db35fab5e31ab7526504987834bbc118a0c4ffc3 Public Key: a48b69bae03835e91dc43ca99b927dc0fc3520d1bf0a3c158e7d008e63dcd26135cdba3a34c31c8083bb0f5b1bbaabf571e55ec1bff300882227239416665061

You can notice that,after creating and restoring an account we still achieved same private key’s and address.

You can find the repository of this article on my github account

https://github.com/annytikone/java-web3j-medium

Till now we have successfully created and restored account.

Thanks for reading,& suggestions are always welcome.

In the next article we will explore more about transaction of ether’s and erc20 tokens

Please feel free to contact me on:

Email: Anikettikone9@gmail.com

LinkedIn: https://www.linkedin.com/in/aniket-tikone-27808a138

Thanks for Reading…

Connecting to Ethereum network using Java and Ganache: