Ethereumj – How to Create an Account

ethereumj

How to create an external account ( personal.newAccount()) equivalent in the ethereumj ? I know I could use the jsonrpc api to do so ? But I read https://github.com/ethereum/ethereumj/issues/335 , which says it could create an account, but it could not, it just generates the address, but does not include/add it to the keystore. How can I add this to the keystore.

Best Answer

Firstly you will need the following Maven pom.xml dependency:

    <dependency>
        <groupId>org.ethereum</groupId>
        <artifactId>ethereumj-core</artifactId>
        <version>1.2.0-RELEASE</version>
        <!--  <type>zip</type>  -->
    </dependency>

And here is the code to create a private key / account pair:

import org.ethereum.crypto.ECKey;
import org.spongycastle.util.encoders.Hex;

public class CreateAccount {

    public static void main(String[] args) {
        ECKey key = new ECKey();

        byte[] addr = key.getAddress();
        byte[] priv = key.getPrivKeyBytes();

        String addrBase16 = Hex.toHexString(addr);
        String privBase16 = Hex.toHexString(priv);

        System.out.println("Address     : " + addrBase16);
        System.out.println("Private Key : " + privBase16);
    }
}

Running the code twice produces the following output:

Address     : 82d4b1c01afaf7f25bb21fd0b4b4c4a7eb7120ce
Private Key : 133965f412d1362645cbd963619023585abc8765c7372ed238374acb884b2b3a

Address     : 68ccabefc7f4ae21ce0df1d98e50e099d7fc290f
Private Key : 1caeb7f26cb9f3cc7d9d0dbcdd3cf3cb056dbc011ec9013e8f3b8cdb2f193b32

Verifying the information with https://www.myetherwallet.com/#view-wallet-info: enter image description here


enter image description here

And note that the accounts created in EthereumJ are all lowercase while the accounts generated from the private keys using MyEtherWallet are mixed case. This is because MyEtherWallet is using the new checksummed accounts. See Yet another cool checksum address encoding #55.

Related Topic