4

How can i generate private key and address using web3j instead of creating keystore json file

1
  • it is a good question
    – Nulik
    Feb 27, 2018 at 15:10

2 Answers 2

14

Below is my approach, verified by importing the result privatekey into MetaMask and get the same address as expected.

private static JSONObject process(String seed){

         JSONObject processJson = new JSONObject();

         try {
            ECKeyPair ecKeyPair = Keys.createEcKeyPair();
            BigInteger privateKeyInDec = ecKeyPair.getPrivateKey();

            String sPrivatekeyInHex = privateKeyInDec.toString(16);

            WalletFile aWallet = Wallet.createLight(seed, ecKeyPair);
            String sAddress = aWallet.getAddress();


            processJson.put("address", "0x" + sAddress);
            processJson.put("privatekey", sPrivatekeyInHex);


        } catch (CipherException e) {
            //
        } catch (InvalidAlgorithmParameterException e) {
            //
        } catch (NoSuchAlgorithmException e) {
            //
        } catch (NoSuchProviderException e) {
            //
        } 

        return processJson;
}


main(){  // unit test 
    String seed = UUID.randomUUID().toString();
    JSONObject result = process(seed); // get a json containing private key and address
}
3
  • String sPrivatekeyInHex = privateKeyInDec.toString(16); What this line does ? Could you explian ?
    – Ajit Soman
    Feb 28, 2018 at 5:12
  • 1
    @Ajit Soman getPrivateKey() is returning decimal, usually we need to use hex string private key.
    – pyang
    Feb 28, 2018 at 5:42
  • I have posted a question . could you provide me some information regarding this: ethereum.stackexchange.com/questions/41194/…
    – Ajit Soman
    Feb 28, 2018 at 7:54
0

This is what I did:

public class MainActivity extends AppCompatActivity {
    private WalletFile wallet;
    private String password = "PASSWORD";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupBouncyCastle();


        try {
            wallet = createWallet();
        } catch (Exception e) {
            System.out.println("BIG RIP");
        }
    }


    public WalletFile createWallet() throws Exception {
        ECKeyPair keyPair = Keys.createEcKeyPair();
        return Wallet.createLight(password, keyPair);

    }


    private void setupBouncyCastle() {
        final Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
        if (provider == null) {
            // Web3j will set up the provider lazily when it's first used.
            return;
        }
        if (provider.getClass().equals(BouncyCastleProvider.class)) {
            // BC with same package name, shouldn't happen in real life.
            return;
        }
        // Android registers its own BC provider. As it might be outdated and might not include
        // all needed ciphers, we substitute it with a known BC bundled in the app.
        // Android's BC has its package rewritten to "com.android.org.bouncycastle" and because
        // of that it's possible to have another BC implementation loaded in VM.
        Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
        Security.insertProviderAt(new BouncyCastleProvider(), 1);
    }
}

For some reason, in newer versions of web3j, Keys.createEcKeyPair() throws an error, so I copied some code from here: https://github.com/serso/web3a/blob/4dda74db948f8cbd9a79ba4b9ab456316ea52c4d/app/src/main/java/org/solovyev/android/web3a/App.java#L47 to set up bouncy castle before running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.