1

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

2 Answers 2

2

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"));
4
  • Im going to test. but one theorycal question. If in this moment im not going to use smart contrarts its necesarry wallets?? Thk !! Nov 15, 2018 at 9:24
  • If you want to send transactions (to an account or a smart contract), you need a funded wallet. Otherwise for events listening or smart contract calls for instance, you don't need a wallet Nov 15, 2018 at 9:35
  • oquei its work and I can se the output. But the wallet creation is not commited/confirmed and it not appear in keystore folder. I need just one line to confirm the previous wallet. What can I do?? Nov 15, 2018 at 9:58
  • Just edited my answer with how to generate a Wallet file that can be copied the keystore folder. Nov 15, 2018 at 10:20
1

I did this:

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, creating a wallet with path kept throwing errors. Part of my code was copied from: https://github.com/serso/web3a/blob/4dda74db948f8cbd9a79ba4b9ab456316ea52c4d/app/src/main/java/org/solovyev/android/web3a/App.java#L47, as newer versions of web3j throw errors when creating a ECKeyPair.

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.