Android应用程序以太坊钱包生成,要做的工作不少,不过如果我们一步一步来应该也比较清楚:

1.在app/build.gradle中集成以下依赖项:

compile ('org.web3j:core-android:2.2.1')

web3j核心是用于从服务器下载以太坊区块链数据的核心类库。它通常用于以太坊开发。

2.我们将设计一个Android UI示例,屏幕上将有文本编辑和按钮。在EditText中,将要求用户输入钱包的密码。然后在按钮的单击事件上,我们将开始发送密码的过程。以下是layout.xml文件:

android:id="@+id/content"

android:layout_height="match_parent"

android:layout_width="match_parent"

android:orientation="vertical">

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"/>

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"/>

3.我们将创建一个FileOutputStream路径,将创建的钱包文件保存在存储中,这需要读写存储权限。

4.对于Android用户Api>26,需要拥有运行时权限以执行上述步骤。

5.然后有一个名为WalletUtils的类。在web3jcore中。在该类中,有一个方法generateWalletNewFile(password, path),它将接受密码参数和钱包文件的路径。 将可以创建钱包文件。

6.web3jcore中还有一个类凭据Credentials,它将使用WalletUtils.loadCredentials(password,path)方法加载文件的所有凭据。以下是用于生成钱包文件的一个类和接口:

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 {

void showGeneratedWallet(String walletAddress);

}

interface Presenter extends BasePresenter {

void generateWallet(String password);

}

}

public interface BasePresenter {

void start();

}

public interface BaseView {

void setPresenter(T presenter);

}

7.现在Credentials类将保存以太坊的钱包地址以及该文件的更多信息。

8.现在可以使用下面的函数获取地址:

credentials.getAddress()->

公钥

credentials.getPublicKey()

私钥

credentials.getEcKeyPair()

9.钱包生成类Activity如下:

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;

}

}

}

}

10.具有textview的活动类,用于显示钱包地址。

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"));

}

}

如果希望快速进行java以太坊开发,那请看我们精心打造的教程:

java以太坊开发教程,主要是针对java和android程序员进行区块链以太坊开发的web3j详解。

其他以太坊教程如下:

以太坊入门教程,主要介绍智能合约与dapp应用开发,适合入门。

以太坊开发进阶教程,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。

python以太坊,主要是针对python工程师使用web3.py进行区块链以太坊开发的详解。

php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。

C#以太坊,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和事件等。

php比特币开发教程,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在Php代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Php工程师不可多得的比特币开发学习课程。

EOS入门教程,本课程帮助你快速入门EOS区块链去中心化应用的开发,内容涵盖EOS工具链、账户与钱包、发行代币、智能合约开发与部署、使用代码与智能合约交互等核心知识点,最后综合运用各知识点完成一个便签DApp的开发。

汇智网原创翻译,转载请标明出处。这里是原文

更多相关文章

  1. 推荐阅读:Android开发者的博客
  2. 安卓开发艺术探索!带你全面解析Android框架体系架构view篇,年薪50W
  3. GitHub最火的android 项目
  4. Android开发(46) 使用 textview实现文字的阴影效果,浮雕效果
  5. android底层开发!双非渣本Android四年磨一剑,offer拿到手软
  6. 分享自学Java,Web,Android视频教程资源(自学Android,有这些就够了)
  7. 干货 | 聊聊这些年总结的一些学习方法
  8. Android专家/经理——北京招聘
  9. Android应用开发提高系列(4)――Android动态加载(上)――加载未安装A

随机推荐

  1. Android-- gravity、layout_gravity、pad
  2. Android系统自带样式(@android:style/)
  3. Android(安卓)ImageView类
  4. Android(安卓)OpenGL ES 开发教程 从入门
  5. [android]控件CheckBox/RadioGroup/Radio
  6. Android(安卓)(layout_toLeftOf,layout_t
  7. ANDROID:控件属性(很全)
  8. 详解Android中Drawable方法
  9. Eclipse导入Android项目 Eclipse常见错误
  10. Android(安卓)EditText inputType同时设