【區塊鏈】以太坊 web3j for java 使用 - 為使用者建立憑證credentials <2>__MYSQL

來源:互聯網
上載者:User
為使用者建立憑證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的地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.