Introduction to Ethereum
What is Ethereum?
Ethereum is an open-source, blockchain-based platform that enables smart contracts and decentralized applications (dApps). Its native cryptocurrency, Ether (ETH), powers the Ethereum Virtual Machine (EVM), which executes peer-to-peer agreements.
Smart Contracts
A smart contract is a self-executing agent residing on the Ethereum blockchain. It has its own ETH address and activates when users send transactions to it. These contracts process embedded data and return results, such as initiating new transactions.
Development Preparation
Web3j: A Java/Android Library for Ethereum
Web3j is a modular, type-safe library for interacting with Ethereum nodes and smart contracts.
Key Features:
- Full JSON-RPC API support (HTTP/IPC).
- Ethereum wallet integration.
- Auto-generated Java wrappers for smart contracts (Solidity/Truffle).
- Reactive API for event filters.
- ERC20/ERC721 token compatibility.
Dependencies:
Java 8:
compile 'org.web3j:core:4.5.12'Android:
implementation 'org.web3j:core:4.2.0-android'
Basic Usage Examples:
Synchronous Call:
Web3j web3 = Web3j.build(new HttpService("NODE_URL")); String version = web3.web3ClientVersion().send().getWeb3ClientVersion();Asynchronous Call:
web3.web3ClientVersion().sendAsync().thenAccept(version -> { /* ... */ });
Hands-On Development
Account Generation
1. Random Account Creation:
String mnemonic = ChainUtil.genMnemonic();
ECKeyPair keyPair = ChainUtil.genECKey(mnemonic, "PATH", "").toECKeyPair();
String address = WalletUtils.generateWalletFile("PASSWORD", keyPair, dir, false); 2. Private Key Import:
Credentials creds = Credentials.create("PRIVATE_KEY");
String address = creds.getAddress(); 3. Keystore Import:
WalletFile wallet = objectMapper.readValue(keystoreJSON, WalletFile.class);
ECKeyPair keyPair = EthWallet.decrypt("PASSWORD", wallet); 4. Mnemonic Recovery:
ECKeyPair keyPair = ChainUtil.genECKey("MNEMONIC", "PATH", "").toECKeyPair(); Transactions
ETH Transfer:
RawTransaction tx = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, value);
byte[] signed = TransactionEncoder.signMessage(tx, creds);
EthSendTransaction receipt = web3.ethSendRawTransaction(hex(signed)).send(); Token Transfer (ERC20):
Function func = new Function("transfer", List.of(new Address(to), new Uint256(value)), List.of());
String encoded = FunctionEncoder.encode(func);
RawTransaction tx = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, CONTRACT, encoded); Balance Queries
ETH Balance:
BigInteger balance = web3.ethGetBalance(address, DefaultBlockParameterName.LATEST).send().getBalance(); Token Balance:
String data = "0x70a08231" + "000000000000000000000000" + address.substring(2);
String balanceHex = web3.ethCall(Transaction.createEthCallTransaction(null, CONTRACT, data), LATEST).send().getValue(); FAQs
1. How do I resolve OOM errors when parsing keystores in Android?
👉 Solution: Use com.lambdaworks:scrypt:1.4.0 to replace the default SCrypt implementation.
2. What’s the recommended gas limit for token transfers?
Answer: Typically 60,000 (vs. 21,000 for ETH transfers).
3. Can I recover a mnemonic from a private key?
Answer: No—mnemonics are one-way derived from seeds.
Conclusion
Building an ETH wallet is streamlined with tools like Web3j, but challenges like keystore compatibility and gas optimization require attention. Mastery of smart contract interactions and secure key management is essential.
References
👉 **Pro Tip**: For advanced wallet features, explore [Web3j’s reactive APIs](https://www.okx.com/join/BLOCKSTAR) to handle real-time event monitoring.
👉 **Security Note**: Always test **transaction signing** on a testnet before deploying to mainnet. Learn more [here](https://www.okx.com/join/BLOCKSTAR).