【区块链】以太坊 web3j for java 使用 - 为用户创建凭证credentials <2>

区块链 同时被 2 个专栏收录
27 篇文章 0 订阅

为用户创建凭证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/目录下

  1. 安卓端代码
/* 使用已存在凭证 */
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的地址

  • 1
    点赞
  • 3
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值