Android application ethereum wallet generation, to do a lot of work, but if we step by step should also be relatively clear:

1. Integrate the following dependencies in app/build.gradle:

The compile (' org. Web3j: core - android: 2.2.1 ')Copy the code

Web3j core is the core class library for downloading Ethereum blockchain data from the server. It is commonly used for Ethereum development.

2. We will design a sample Android UI with text editing and buttons on the screen. In the EditText, the user is asked to enter the password for the wallet. Then on the button click event, we will begin the process of sending the password. Here is the layout.xml file:

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="@string/textview_password" android:padding="10dp"/> <Button android:id="@+id/generate_wallet_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/textview_generate_wallet"/> </LinearLayout>Copy the code

3. We will create a FileOutputStream path to save the created wallet file to the store, which requires read/write storage permission.

4. For Android user Api>26, you need to have runtime permissions to perform the above steps.

5. Then there is a class called WalletUtils. In web3jcore. In this class, there is a method generateWalletNewFile(Password, path), which will accept password arguments and the path to the wallet file. You can create wallet files.

6. Have a class in the web3jcore Credentials Credentials, it will use WalletUtils. LoadCredentials (password, path) method to load the file all the Credentials. Here is a class and interface for generating a wallet file:

public class EthereumGenerationPresenter implements EthereumGenerationContract.Presenter { private final EthereumGenerationContract.View mWalletGenerationView; private String mPassword; public EthereumGenerationPresenter(EthereumGenerationContract.View walletGenerationView, String password) { mWalletGenerationView = walletGenerationView; mPassword = password; } @Override public void generateWallet(final String password) { String fileName; try { File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); if (! path.exists()) { path.mkdir(); } fileName = WalletUtils.generateLightNewWalletFile(password, new File(String.valueOf(path))); Log.e("TAG", "generateWallet: " + path+ "/" + fileName); Credentials credentials = WalletUtils.loadCredentials( password, path + "/" + fileName); mWalletGenerationView.showGeneratedWallet(credentials.getAddress()); Log.e("TAG", "generateWallet: " + credentials.getAddress() + " " + credentials.getEcKeyPair().getPublicKey()); } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException | IOException | CipherException e) { e.printStackTrace(); } } @Override public void start() { generateWallet(mPassword); } } public interface EthereumGenerationContract { interface View extends BaseView<Presenter> { void showGeneratedWallet(String walletAddress); } interface Presenter extends BasePresenter { void generateWallet(String password); } } public interface BasePresenter { void start(); } public interface BaseView<T> { void setPresenter(T presenter); }Copy the code

7. The Credentials class now holds ethereum’s wallet address and more information about the file.

8. Now you can use the following function to get the address:

credentials.getAddress()->
Copy the code

The public key

credentials.getPublicKey()
Copy the code

The private key

credentials.getEcKeyPair()
Copy the code

9. The wallet generating Activity class is as follows:

public class WalletGenerationActivity extends AppCompatActivity implements EthereumGenerationContract.View { private static final int REQUEST_PERMISSION_WRITE_STORAGE = 0; private EthereumGenerationContract.Presenter mWalletPresenter; private Button mGenerateWalletButton; private String mWalletAddress; private EditText mPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_generation); mGenerateWalletButton = (Button) findViewById(R.id.generate_wallet_button); mPassword = (EditText) findViewById(R.id.password); mGenerateWalletButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int permissionCheck = ContextCompat.checkSelfPermission(WalletGenerationActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permissionCheck ! = PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions( WalletGenerationActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_WRITE_STORAGE); } else { mWalletPresenter = new EthereumGenerationPresenter(WalletGenerationActivity.this, mPassword.getText().toString()); mWalletPresenter.generateWallet(mPassword.getText().toString()); Intent intent = new Intent(WalletGenerationActivity.this, WalletActivity.class); intent.putExtra("WalletAddress", mWalletAddress); startActivity(intent); }}}); } @Override public void setPresenter(EthereumGenerationContract.Presenter presenter) { mWalletPresenter = presenter; } @Override public void showGeneratedWallet(String address) { mWalletAddress = address; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case REQUEST_PERMISSION_WRITE_STORAGE: { if (grantResults.length == 0 || grantResults[0] ! = PackageManager.PERMISSION_GRANTED) { finish(); } else { mWalletPresenter.generateWallet(mPassword.getText().toString()); } break; }}}}Copy the code

10. An active class with a TextView to display the wallet address.

public class WalletActivity extends AppCompatActivity { private TextView mWalletAddress; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wallet); mWalletAddress = (TextView) findViewById(R.id.account_address); Bundle extras = getIntent().getExtras(); mWalletAddress.setText(extras.getString("WalletAddress")); }}Copy the code

If you want to quickly develop Java Ethereum, please take a look at our carefully designed tutorial: Java Ethereum Development Tutorial, mainly for Java and Android programmers to develop blockchain Ethereum web3J details.

Other Ethereum tutorials are as follows:

  • Ethereum introductory course, mainly introduces smart contract and DAPP application development, suitable for entry.
  • Ethereum development advanced tutorial, mainly introduces the use of Node.js, mongodb, blockchain, IPFS to achieve decentralized e-commerce DApp combat, suitable for advanced.
  • Python Ethereum is a detailed explanation of blockchain ethereum development by Python engineers using Web3.py.
  • PHP Ethereum mainly introduces the use of PHP for smart contract development interaction, account creation, transactions, transfers, token development, filters and events and other content.
  • C# ethereum, mainly explains how to use C# based development. Net ethereum applications, including account management, status and transactions, smart contract development and interaction, filters and events, etc.
  • PHP currency development tutorial, this course for beginners, content covers the core concepts of COINS, such as block mechanism, key chain store, decentralized consensus and script, trading and UTXO etc, also explained how to integrated the currency support functions in PHP code, such as creating address wallet, tectonic naked trading, management, This is a rare bitcoin development course for Php engineers.
  • This course will help you quickly get started on the development of EOS blockchain decentralized applications, covering core knowledge points such as EOS tool chain, account and wallet, issuing tokens, development and deployment of smart contracts, using codes to interact with smart contracts, and finally completing the development of a notepad DApp using all knowledge points comprehensively.

Huizhi net original translation, reprint please indicate the source. Here is the original text