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.
Asked
Active
Viewed 3,086 times
1 Answers
4
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:

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.
BokkyPooBah
- 40,274
- 14
- 123
- 193

geth, you will have to use the commandgeth account import private-key-file-to-delete-when-completedwhere the contents of the file is your generated private key. Once imported intogeth, your accounts will be available ineth.accounts. EthereumJ does not interact with yourgethkeystore through the Java code. You can use thepersonal.newAccount()API as listed in https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console#personalnewaccount but you will have to use your own code to access the IPC or RPC API – BokkyPooBah May 24 '16 at 19:43