Web3j Account Creation – How to Create an Account

accountsgo-ethereumweb3j

Im trying to create a account using the web3j library. But i can´t find the method that create accounts. My idea is to make this judgment but not in the Geth console, but yes in the Java application using web3j:

geth account new

Any idea?

Im new in ethereum and till the moment im not generating wallet because im not using smart contracts. Mi idea is just create accounts with de Java application and make simple transacion of eth betwen them in a private chain.

Thank you

Best Answer

Using Web3j (3.4.0), this can be achieved in two steps:

  1. Generate a key-pair Keys.createEcKeyPair()
  2. Create the wallet Wallet.createStandard(seed, keyPair)

Example:

public static void main(String[] args) {

    try {
        String password = "secr3t";
        ECKeyPair keyPair = Keys.createEcKeyPair();
        WalletFile wallet = Wallet.createStandard(password, keyPair);

        System.out.println("Priate key: " + keyPair.getPrivateKey().toString(16));
        System.out.println("Account: " + wallet.getAddress());

    } catch(Exception e) {
        System.err.println("Error: " + e.getMessage());
    }

}

EDIT: create a wallet file

String fileName = WalletUtils.generateNewWalletFile(
        "secr3t",
        new File("/path/to/destination"));