区块链以太坊 web3j for java 使用 - 为用户创建凭证credentials <2;
Posted 宣之于口
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了区块链以太坊 web3j for java 使用 - 为用户创建凭证credentials <2;相关的知识,希望对你有一定的参考价值。
为用户创建凭证credentials <2>
环境:mac os & android studio
参考:here
一、初始工作
查看上一篇文章:here
二、创建credentials
String filePath = Environment.getExternalStorageDirectory().toString() + "/Pictures";
String fileName = WalletUtils.generateNewWalletFile("password",new File(filePath),false);
Credentials credentials = WalletUtils.loadCredentials("password", filePath+"/"+fileName);
//"password" -> 输入你的密码
//这样就可以创建一个凭证
但是你会遇到问题:
/storage/emulated/0/Pictures/UTC--2017--11-28....json(Permission denied)"
解决办法:
加上权限:
<!-- SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 向SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
但可能还是失败,不要慌,在创建凭证前加上一段:
int REQUEST_EXTERNAL_STORAGE = 1;
String[] PERMISSIONS_STORAGE =
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
;
int permission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
f (permission != PackageManager.PERMISSION_GRANTED)
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
MainActivity.this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
就可以成功啦:
三、主代码
package com.example.pro.blockchain1;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jFactory;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
private Button showButton;
private TextView textview;
public static final int SHOW = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.textview);
showButton = (Button) findViewById(R.id.showView);
showButton.setOnClickListener(this);
@Override
public void onClick(View v)
if(v.getId() == R.id.showView)
show();
private void show()
new Thread(new Runnable()
@Override
public void run()
try
String url = "http://10.0.2.2:8545";
Web3j web3 = Web3jFactory.build(new HttpService(url));
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
// 获取凭证
int REQUEST_EXTERNAL_STORAGE = 1;
String[] PERMISSIONS_STORAGE =
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
;
int permission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED)
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
MainActivity.this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
String filePath = Environment.getExternalStorageDirectory().toString() + "/Pictures";
String fileName = WalletUtils.generateNewWalletFile("123456",new File(filePath),false);
Credentials credentials = WalletUtils.loadCredentials("123456", filePath+"/"+fileName);
String myAddress = credentials.getAddress();
textview.setText(myAddress);
catch (ExecutionException e)
e.printStackTrace();
catch (InterruptedException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (CipherException e)
e.printStackTrace();
catch (NoSuchAlgorithmException e)
e.printStackTrace();
catch (NoSuchProviderException e)
e.printStackTrace();
catch (InvalidAlgorithmParameterException e)
e.printStackTrace();
).start();
四、使用私有链创建的凭证
这里我暂时没有好解决的方法,希望有大佬能解答。以下给出我的方法:
1.打开本地私有链文件夹找到keystore文件
随便选取一个,比如:UTC--2017-11-16T08-22-03.297518621Z--ea7cb6058aaf6369c3a97c4a5203364c8c4ff7fa
2.打开monitor
工具栏: Tools -> Android -> Android Device Monitor
3.把keystore文件放入sdcard/目录下
- 安卓端代码
/* 使用已存在凭证 */
String filePath = "sdcard/";
String fileName = "UTC--2017-11-16T08-22-03.297518621Z--ea7cb6058aaf6369c3a97c4a5203364c8c4ff7fa";
Credentials credentials = WalletUtils.loadCredentials("123456", filePath + "/" + fileName);
String myAddress = credentials.getAddress();
textview.setText(myAddress);
可以查看到ea7cb6058aaf6369c3a97c4a5203364c8c4ff7fa的地址
以上是关于区块链以太坊 web3j for java 使用 - 为用户创建凭证credentials <2;的主要内容,如果未能解决你的问题,请参考以下文章