内容简介:Android应用程序以太坊钱包生成,要做的工作不少,不过如果我们一步一步来应该也比较清楚:1.在web3j核心是用于从服务器下载以太坊区块链数据的核心类库。它通常用于以太坊开发。
Android应用程序以太坊钱包生成,要做的工作不少,不过如果我们一步一步来应该也比较清楚:
1.在 app/build.gradle
中集成以下依赖项:
compile ('org.web3j:core-android:2.2.1')
web3j核心是用于从服务器下载以太坊区块链数据的核心类库。它通常用于以太坊开发。
2.我们将设计一个Android UI示例,屏幕上将有文本编辑和按钮。在EditText中,将要求用户输入钱包的密码。然后在按钮的单击事件上,我们将开始发送密码的过程。以下是layout.xml文件:
<?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>
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<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);
}
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的开发。
汇智网原创翻译,转载请标明出处。这里是 原文
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 比特币钱包开发:钱包项目整体架构设计
- EOS钱包开发:钱包项目整体架构设计
- YOYOW 团队开放桌面版钱包,移动版钱包,信息销售模块以及用户注册模块源代码
- 比特币钱包rpc
- Coinomi钱包解决了漏洞问题
- 再议Wannacry的比特币钱包
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Practical Algorithms for Programmers
Andrew Binstock、John Rex / Addison-Wesley Professional / 1995-06-29 / USD 39.99
Most algorithm books today are either academic textbooks or rehashes of the same tired set of algorithms. Practical Algorithms for Programmers is the first book to give complete code implementations o......一起来看看 《Practical Algorithms for Programmers》 这本书的介绍吧!
图片转BASE64编码
在线图片转Base64编码工具
HEX HSV 转换工具
HEX HSV 互换工具