Java 以太坊開(kāi)發(fā) Dapp(三)

Java接入SDK

畢竟都是一個(gè)體系的 其實(shí)Java Android API差別不大

Java Version 8+

Maven
<dependency>
  <groupId>org.web3j</groupId>
  <artifactId>core</artifactId>
  <version>3.4.0</version>
</dependency>

Gradle
compile ('org.web3j:core:3.4.0')

Android 接入SDK

Maven
<dependency>
  <groupId>org.web3j</groupId>
  <artifactId>core</artifactId>
  <version>3.3.1-android</version>
</dependency>

Gradle
compile ('org.web3j:core:3.3.1-android')

//引入RxAndroid
compile 'io.reactivex:rxandroid:1.2.1'

初始化項(xiàng)目

無(wú)論是Java Android 都有一個(gè)Web3j的接口
先初始化這個(gè)接口

//SERVICE_IP 這個(gè)是測(cè)試網(wǎng)地址  或者你的私有鏈地址 
//私有鏈的話 一般帶上端口號(hào)比如我的私有鏈?zhǔn)?http://127.0.0.1:8545
//如果是從infura.io注冊(cè)的測(cè)試網(wǎng)的話  不需要加端口號(hào) 例如 https://ropsten.infura.io/your-token

Java
Web3j web3j = Web3j.build(new HttpService(SERVICE_IP));

Android
Web3j web3j = JsonRpc2_0Web3j(new HttpService(SERVICE_IP));

同步調(diào)用API

這里舉例獲取ETH 客戶端版本API

同步調(diào)用的話 Java Android都一樣

//兩種方式  都是同步  具體細(xì)節(jié)沒(méi)有去研究過(guò)
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();

String version = web3ClientVersion.getWeb3ClientVersion();

異步調(diào)用

得益于RxJava 讓我們異步調(diào)用更簡(jiǎn)潔 更方便

因?yàn)锳ndroid的特性 子線程不可更改UI 所以 Android方面 有點(diǎn)不一樣

Java:
web3j.web3ClientVersion()
            .observable()
            .observeOn(Schedulers.io())
            .subscribe(new Subscriber<Web3ClientVersion>() {
                @Override
                public void onStart() {
                    super.onStart();
                    System.out.println("onStart:" + Thread.currentThread());
                }

                @Override
                public void onCompleted() {
                    System.out.println("onCompleted:" + Thread.currentThread());
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    System.out.println("onError:" + Thread.currentThread());
                }

                @Override
                public void onNext(Web3ClientVersion web3ClientVersion) {
                    System.out.println("onNext:" + Thread.currentThread());
                    System.out.println("web3ClientVersion:" + web3ClientVersion.getWeb3ClientVersion());
                }
            });
    //最后輸出
    onStart:Thread[main,5,main] //主線程
    onNext:Thread[RxIoScheduler-2,5,main]   //IO線程
    web3ClientVersion:Geth/v1.8.3-stable/linux-amd64/go1.10 //ETH客戶端版本
    onCompleted:Thread[RxIoScheduler-2,5,main]  //IO線程

Android:
web3j.web3ClientVersion()
            .observable()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<Web3ClientVersion>() {
                @Override
                public void onStart() {
                    super.onStart();
                    System.out.println("onStart:" + Thread.currentThread());
                }

                @Override
                public void onCompleted() {
                    System.out.println("onCompleted:" + Thread.currentThread());
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    System.out.println("onError:" + Thread.currentThread());
                }

                @Override
                public void onNext(Web3ClientVersion web3ClientVersion) {
                    System.out.println("onNext:" + Thread.currentThread());
                    System.out.println("web3ClientVersion:" + web3ClientVersion.getWeb3ClientVersion());
                }
            });

//subscribeOn(Schedulers.io()) 用IO線程執(zhí)行耗時(shí)操作
//observeOn(AndroidSchedulers.mainThread()) 在觀察者回調(diào)的時(shí)候 使用Android 主線程回調(diào) 

獲取賬戶余額

//address 賬戶地址
EthGetBalance send = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
// 默認(rèn)獲取到的單位是WEI  轉(zhuǎn)換為ETH
BigDecimal balance = Convert.fromWei(send.getBalance().toString(), Convert.Unit.ETHER);

區(qū)塊鏈高度

 BigInteger blockNumber = web3j.ethBlockNumber().send().getBlockNumber();

區(qū)塊詳情

DefaultBlockParameterNumber defaultBlockParameterNumber = new DefaultBlockParameterNumber(blockNumber);
EthBlock send = web3j.ethGetBlockByNumber(defaultBlockParameterNumber, true).send();
EthBlock.Block block = send.getBlock();

創(chuàng)建以太坊地址

String newWalletFile = WalletUtils.generateNewWalletFile(password, walletFolder, false);
//password 錢包文件密碼
//walletFolder 錢包文件存儲(chǔ)目錄
//false 不進(jìn)行加密  (如果加密  內(nèi)存開(kāi)銷很大)

加載憑證

憑證是指 一個(gè)賬戶地址的憑證
可以通過(guò)私鑰直接加載 或者通過(guò)密碼 和私鑰文件加載 (私鑰文件是指keystore目錄下的加密文件)

私鑰加載憑證

 Credentials credentials = Credentials.create(privateKey);

私鑰文件加載憑證

//password 密碼  filePath私鑰路徑
Credentials credentials = WalletUtils.loadCredentials(password, filePath);

通過(guò)憑證 取出地址和私鑰

ECKeyPair ecKeyPair = credentials.getEcKeyPair();
String address = "0x" + ecKeyPair.getPublicKey().toString(16);
String privateKey = "0x"+ecKeyPair.getPrivateKey().toString(16);

//address 以太坊地址
//privateKey 私鑰

其他的API 自己摸索吧

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容