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.
1 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:
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.
-
I am not using Wallet. And I am running on a private network. My intention was to let the users access the java code to create their external accounts in the private network. Commented May 24, 2016 at 19:15
-
You don't need MyEtherWallet. I included the images just to confirm that the private key/account pairs are valid. Commented May 24, 2016 at 19:23
-
2If you are asking how the Java generated accounts can be imported into
geth
, you will have to use the commandgeth account import private-key-file-to-delete-when-completed
where 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 yourgeth
keystore through the Java code. You can use thepersonal.newAccount()
API as listed in github.com/ethereum/go-ethereum/wiki/… but you will have to use your own code to access the IPC or RPC API Commented May 24, 2016 at 19:43 -
2@The Neo Noir Developer Just a note: when you create a new account using tools you are unfamiliar with, you want to cross-check using a different client/language the private key -> address derivation to insure everything is good. In this example, you create w/ Java & you verify with Javascript. You could also verify with python or go or rust or any of the other clients/languages available. It's making sure the private key unlocks the address you saved. Otherwise you might send to an address you don't actually have access to.– tayvanoCommented May 25, 2016 at 0:47
-
1And, here is a link to importing a plain private key into geth: ethereum.stackexchange.com/questions/465/…– tayvanoCommented May 25, 2016 at 0:47