web3j 4.x ETF Mobile Wallet Development Tutorial and Source Code

Posted by tsiedsma on Wed, 11 Sep 2019 05:09:25 +0200

This tutorial explains how to use the Ethernet node provided by Web3j 4.x and Infura to develop the Ethernet wallet application of Android mobile phone. The tutorial includes project dependency configuration, Ethernet node selection, Ethernet wallet address creation, testing the acquisition of Ethernet currency, executing Ethernet currency transfer transactions, and provides a complete reference source for implementation. Code download.

1. Project configuration

The first step in using Web3j is to add Web3j dependencies to the Android project. Because Web3j has a maven plug-in, it's easy: just add Maven central to your project's build.gradle file, and then add web3j as a dependency to the build.gradle file (make sure you use the Android version).

repositories {
    mavenCentral()
    google()
    jcenter()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'org.web3j:core:4.1.0-android'
}

To systematically and efficiently learn web3j, recommend online interactive tutorials for Huizhi.com Detailed Explanation of Web 3j EtherWorks Development The course covers the core concepts of ETF, such as account management, status and transaction, intelligent contract development and interaction, filters and events. It also details how to use web3j to interact with ETF block chains. It is the best choice for java engineers to learn ETF application development.

2. Determining the type of node to use

Wallet applications can communicate with Taifang block chains only through an Ethernet node. We can deploy our own nodes or use cloud nodes provided by third parties, such as Infura's open nodes. The reason I decided to use Infura was that I didn't need to synchronize the block chain data myself, because the synchronization process took a lot of time and was cumbersome, so I wanted to avoid building my own nodes as much as possible.

You can register in Infura and get an API Key to create a Web3j object to access the Rinkeby test network as follows:

// FIXME: Add your own API key here
web3 = Web3j.build(new HttpService("https://rinkeby.infura.io/v3/YOURKEY"));
try {
      Web3ClientVersion clientVersion = web3.web3ClientVersion().sendAsync().get();
      if(!clientVersion.hasError()){
      //Connected
      }
       else {
         //Show Error
       }
}
catch (Exception e) {
  //Show Error
}

Note the url in the code above - https://rinkeby.infura.io/v3/YOURKEY. YOURKEY needs to be replaced by your API KEY. The rinkeby in this url indicates that you can use this url to access the rinkeby test chain of ETF. It is easy to understand. By replacing rinkeby with mainnet, you can access the main network of ETF, for example:

https://mainnet.infura.io/v3/YOURKEY

If all is well, the above code can be connected to the Linkeby test chain of ETF.

3. Creating a Wallet

Now let's create a wallet to send or receive some ether coins from the test chain. To do this, we need to first create a wallet file in the user's device:

//FIXME: Use your own password here
private final String password = "medium";
private String walletPath = getFilesDir().getAbsolutePath();
private File walletDir  = new File(walletPath);

try{
   WalletUtils.generateNewWalletFile(password, walletDir);
 }
catch (Exception e){
  //Display an Error
}

4. Get the address and load it into your wallet

Well, now we have a wallet. Now we can get the address of the wallet, and then we can get some test ET coins from Rinkeby Faucet for that address:

try {
  Credentials credentials = WalletUtils.loadCredentials(password, walletDir);
  Toast.makeText(this, "Your address is " + credentials.getAddress(), Toast.LENGTH_LONG).show();

}
catch (Exception e){ 
  //Show Error
}

5. Sending Transactions

Now that there are some ET coins in our wallet, let's turn these test coins back:

try{
  Credentials credentials = WalletUtils.loadCredentials(password, walletDir); TransactionReceipt receipt = Transfer.sendFunds(web3,credentials,"0x31B98D14007bDEe637298086988A0bBd31184523",new BigDecimal(1),Convert.Unit.ETHER).sendAsync().get(); 
  Toast.makeText(this, "Transaction complete: " +receipt.getTransactionHash(), Toast.LENGTH_LONG).show();
} 
catch (Exception e){ 
  //Show Error
}

6. Conclusion

In this tutorial, we created a simple Android mobile phone application to send and receive Ethernet coins, which you can use in Here Download the complete implementation code.

Links to the original text: Web 3j 4.x Android ETF Wallet Implementation Tutorial-Huizhi Network

Topics: Android network Mobile Maven