Web3j documentation explains most of the things. I found this android ethereum wallet created using web3j. Might help you. https://github.com/matthiaszimmermann/ethereum-paper-wallet
Alternate:
I would recommend cross-compiled go-ethereum for Android. This is a light client for mobile devices and does not require you to download the entire blockchain. After importing maven dependency for the same you will get an API to interact with the blockchain (example program). You can create new account, check balances and send transactions.
The only thing is that Android API is not documented hence it is difficult to work with it. I hope the developers would do something about it in the future.
Updated:
Ethereum don't have have an Android API till now, but auto complete tools in IntelliJ or Android Studio will help you. You can refer to the following code to get a little understanding of the API.
Ethereum creates a keystore file whenever you create a new account which is something like this:
{"address":"bab565b65fede.....7a98ab7c330c","crypto":{"cipher":"aes-128-ctr","ciphertext":"ab4913efa82.......d3d5fe335025596","cipherparams":{"iv":"ece5ac32........4942b58"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"bcd244b8ee3def42........0d4c78758d858d3502"},"mac":"1c618efa10d54......3f5a3e4377"},"id":"0e0416d0-2a58-43c9-86cc-2c1eaa11957b","version":3}
Where "address" is your wallet address and "crypto" constains your private key enctypted with a passphase. So ethereum will help you manage user accounts, you just have to create a mapping between usernames and wallet addresses.
Here is a more complete program than the one you provided. The only undefined part is Contract_sol_Contract
, which you did not define. Notice how privateKey
and publicKey
are computed.
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
import java.math.BigInteger;
public class SO3 {
public SO3() throws IOException {
Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));
String account = web3.ethAccounts().send().getAccounts().get(0);
Credentials credentials = Credentials.create(account);
ECKeyPair keyPair = credentials.getEcKeyPair();
BigInteger privateKey = keyPair.getPrivateKey();
BigInteger publicKey = keyPair.getPublicKey();
Contract_sol_Contract contract = Contract_sol_Contract.deploy(web3, credentials, new BigInteger("240000"), new BigInteger("2400000"), props).send();
}
}
I think that the reason that web3j
deviated from web3
in requiring just the public or private key is because providing less information is a better security practice. The web3 accounts docs has this warning: The accounts private key. This should never be shared or stored unencrypted in localstorage! Also make sure to null the memory after usage.
This means passing around the entire accounts object is discouraged.
Seems the web3
authors knew they created a security issue because earlier on that same page they give this big fat warning: This package has NOT been audited and might potentially be unsafe. Take precautions to clear memory properly, store the private keys safely, and test transaction receiving and sending functionality properly before using in production!
.
Best Answer
I wrote a medium article just on this topic (Getting started with web3j on Android), I hope it helps: https://medium.com/@nschapeler/an-introduction-to-ethereum-development-on-android-using-web3j-and-infura-763940719997