Ethereum Android Wallet Development Series 2: Importing and Managing Accounts

·

Developing a secure and user-friendly Ethereum wallet for Android requires careful handling of account creation, import methods, and long-term management. In this second installment of the series, we dive into the essential functionality of importing existing Ethereum accounts—via private key, Keystore file, or mnemonic phrase—and explore how to securely store and manage multiple wallets within your app.

Whether you're building a decentralized finance (DeFi) tool, NFT marketplace, or general-purpose crypto wallet, understanding these core mechanisms is crucial for delivering a seamless user experience while maintaining top-tier security standards.

👉 Discover how to securely integrate wallet features in your Android app

Understanding Account Import Methods

Before diving into code, it's important to understand why account import functionality matters. Users often already have wallets from other platforms. To ensure continuity and trust, your app must support common import formats without compromising security.

However, always consider security implications:

The safest practice is to recommend that users:

  1. Create a new wallet inside your app and back it up securely
  2. Transfer funds from the old wallet to the newly created one

This minimizes exposure to potentially compromised credentials.

There are three standard ways to import an Ethereum account:

  1. Private Key Import
  2. Keystore File Import
  3. Mnemonic Phrase Import

Each method reconstructs an elliptic curve key pair, which is then used to instantiate a functional wallet.

Importing via Private Key

The most direct method involves using a raw private key. While simple, this approach carries high risk if mishandled—private keys should never be stored in plaintext or exposed to network requests.

Here’s how to implement private key-based wallet import:

public static ETHWallet loadWalletByPrivateKey(String privateKey, String pwd) {
    ECKeyPair ecKeyPair = ECKeyPair.create(Numeric.toBigInt(privateKey));
    return generateWallet(generateNewWalletName(), pwd, ecKeyPair);
}

Note: The pwd parameter isn’t used during wallet derivation but serves to encrypt the private key when saving it as a Keystore file locally. This adds an extra layer of protection even if device storage is compromised.

Importing via Keystore File

A Keystore file (typically a JSON) is an encrypted representation of a user’s private key. It's more secure than raw private keys because it requires a password to decrypt.

Key steps:

  1. Parse the JSON content into a WalletFile object
  2. Decrypt using the provided password to retrieve the ECKeyPair
  3. Generate the wallet instance

Implementation:

public static ETHWallet loadWalletByKeystore(String keystore, String pwd) {
    try {
        WalletFile walletFile = objectMapper.readValue(keystore, WalletFile.class);
        ECKeyPair keyPair = Wallet.decrypt(pwd, walletFile);
        return generateWallet(generateNewWalletName(), pwd, keyPair);
    } catch (IOException | CipherException e) {
        return null;
    }
}

This method ensures sensitive data remains encrypted at rest and only decrypted in memory during use.

Importing via Mnemonic Phrase

Mnemonic phrases (often 12 or 24 words) are human-readable representations of seed phrases defined by BIP39. They allow deterministic derivation of multiple accounts using BIP44 paths.

Steps:

  1. Convert mnemonic words into a deterministic seed
  2. Derive private key using BIP44 path (e.g., m/44'/60'/0'/0/0)
  3. Generate wallet from derived key

Code example:

public static ETHWallet importMnemonic(String path, String mnemonic, String pwd) {
    List<String> wordList = Arrays.asList(mnemonic.split(" "));
    if (!path.startsWith("m") && !path.startsWith("M")) return null;

    String[] pathArray = path.split("/");
    if (pathArray.length <= 1) return null;

    DeterministicSeed seed = new DeterministicSeed(wordList, null, "", System.currentTimeMillis() / 1000);
    return generateWalletByMnemonic(generateNewWalletName(), seed, pathArray, pwd);
}

Users should be prompted to select or input a derivation path, ensuring compatibility with other wallets like MetaMask.

👉 Learn how to build robust Ethereum wallet integrations

Storing Accounts Securely with Room or greenDAO

In-memory wallet objects vanish when the app closes. To preserve accounts across sessions, you need persistent local storage.

Using greenDAO for SQLite Mapping

We use greenDAO, a lightweight ORM that maps Java objects to SQLite efficiently.

Our ETHWallet entity looks like this:

@Entity
public class ETHWallet {
    @Id(autoincrement = true)
    private Long id;
    public String address;
    private String name;
    private String password; // Encrypted
    private String keystorePath;
    private String mnemonic;
    private boolean isCurrent;
    private boolean isBackup;
}

Initialization and Database Setup

Initialize greenDAO in your Application class:

protected void init() {
    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "wallet", null);
    SQLiteDatabase db = helper.getWritableDatabase();
    DaoSession session = new DaoMaster(db).newSession();
    ETHWalletDao ethWalletDao = session.getETHWalletDao();
}

Then save wallets with a single line:

ethWalletDao.insert(ethWallet);

This ensures all imported or newly created accounts persist after app restart.

Managing Multiple Wallet Accounts

Users may manage several wallets. You must designate one as active (“current”) and update selection states accordingly.

Helper methods:

public static void insertNewWallet(ETHWallet ethWallet) {
    updateCurrent(-1); // Deselect others
    ethWallet.setCurrent(true);
    ethWalletDao.insert(ethWallet);
}

public static ETHWallet updateCurrent(long id) {
    List<ETHWallet> wallets = ethWalletDao.loadAll();
    ETHWallet current = null;
    for (ETHWallet w : wallets) {
        boolean isSelected = (id != -1 && w.getId() == id);
        w.setCurrent(isSelected);
        if (isSelected) current = w;
        ethWalletDao.update(w);
    }
    return current;
}

This logic ensures only one wallet is active at a time—a critical UX and security pattern.

Asynchronous Operations with RxJava

Cryptographic operations and disk I/O are blocking tasks. Never run them on the main thread.

Using RxJava/RxAndroid, we offload work safely:

public Single<ETHWallet> loadWalletByPrivateKey(String privateKey, String pwd) {
    return Single.fromCallable(() -> {
        ETHWallet wallet = ETHWalletUtils.loadWalletByPrivateKey(privateKey, pwd);
        if (wallet != null) WalletDaoUtils.insertNewWallet(wallet);
        return wallet;
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread());
}

This prevents UI jank and improves responsiveness—essential for production apps.

Why Use Reactive Programming?


Frequently Asked Questions

Q: Is it safe to import wallets using private keys?
A: Only if done in a trusted, open-source environment. Always warn users against entering private keys on untrusted apps or websites.

Q: Can I recover a wallet without a mnemonic or Keystore?
A: No. Without the private key, mnemonic phrase, or Keystore file—and its password—recovery is impossible due to blockchain’s design.

Q: Should I store mnemonics in the database?
A: Never store mnemonics or private keys in plaintext. If absolutely necessary, encrypt them using strong algorithms and hardware-backed keystores.

Q: How does BIP44 affect wallet compatibility?
A: BIP44 defines hierarchical deterministic (HD) wallet structures. Using standard paths (like m/44'/60'/0'/0/0) ensures interoperability with MetaMask, Trust Wallet, etc.

Q: What happens if I lose my Keystore password?
A: The account becomes irrecoverable. Emphasize strong password management and backup practices during onboarding.

Q: Can I switch between multiple blockchains using one mnemonic?
A: Yes—HD wallets derive different keys per chain (e.g., Ethereum vs Bitcoin) using unique derivation paths under the same seed.

👉 Explore secure blockchain integration tools for developers

Core Keywords

ethereum android wallet, import ethereum account, private key import, keystore file android, mnemonic phrase recovery, HD wallet derivation, greenDAO ethereum storage, multi-account wallet management

By mastering these concepts, you lay the foundation for building reliable, enterprise-grade cryptocurrency wallets on Android—secure by design and intuitive by nature.