研究區(qū)塊鏈技術(shù)和通證經(jīng)濟有大半年了,但是直到最近2周才開始寫代碼,主要研究了ETH開發(fā),后面打算研究一下EOS。今天主要說一下Android Web3j連接Geth私有鏈,然后發(fā)送一筆交易或者獲取一些信息這樣的功能。
Geth創(chuàng)建與設(shè)置
1、創(chuàng)建ETH初始區(qū)塊文件 genesis.json
{
"config": {
"chainId": 100,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x200",
"extraData" : "",
"gasLimit" : "0xffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
2、初始化區(qū)塊節(jié)點
首先進入genesis.json文件所在的路徑,然后初始化
//進入初始化文件所在目錄
cd privatechain
//初始化操作
geth --datadir blockdata init genesis.json
blockdata是創(chuàng)建的文件名,是用來存儲節(jié)點數(shù)據(jù)的

3、啟動geth客戶端節(jié)點
geth --identity "TestNode1" --datadir "blockdata" --rpc --rpcapi "db,eth,net,web3" --rpcaddr "0.0.0.0" --rpcport "8545" --networkid "100" console
使用命令 geth -h 可以查看geth 相關(guān)的幫助文檔。
--Identity : 節(jié)點身份標識,起個名字
--datadir : 指定節(jié)點存在位置,“blockdata”
--rpc : 啟用http-rpc服務(wù)器
--rpcapi : 基于http-rpc提供的api接口。eth,net,web3,db...
--rpcaddr : http-rpc服務(wù)器接口地址:默認“127.0.0.1”,
--rpcport : http-rpc 端口(多節(jié)點時,不要重復(fù)),默認為8545,當然你可以設(shè)置成其他任意值
--port : 節(jié)點端口號(多節(jié)點時,不要重復(fù))
--networkid : 網(wǎng)絡(luò)標識符 隨便指定一個id(確保多節(jié)點是統(tǒng)一網(wǎng)絡(luò),保持一致)
注意:
連接geth,如果你的geth是在服務(wù)器上的話,就能通過客戶端直接訪問的,但是如果你的geth在本地的話,那就只能通過局域網(wǎng)來訪問了,也就是說你電腦的IP(注意手機和電腦需要保持一致)+設(shè)置的端口號(--rpcport "8545",默認的就是8545)
如果android客戶端訪問本地geth,需要設(shè)置成--rpcaddr "0.0.0.0"或者--rpcaddr "本地IP"
Android客戶端訪問
導入web3j相關(guān)依賴
implementation 'org.web3j:core:3.3.1'
implementation 'org.web3j:core:3.3.1-android'
獲取geth版本號
//獲得Web3j對象,URL為geth私鏈的地址
//你需要換成你的地址,如果是本地的話是局域網(wǎng)IP+設(shè)置的端口號,默認是8545
Web3j web3j = Web3j.build(new HttpService("http://192.168.2.181:8545"));
/**
* 獲取版本等信息
* @param web3j
*/
public static void getInfo(Web3j web3j){
Web3ClientVersion web3ClientVersion = null;
try {
web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
Log.i(TAG, "sendRawTransaction: clientVersion " + clientVersion);
} catch (InterruptedException e) {
e.printStackTrace();
Log.i(TAG, "sendRawTransaction: "+e.getMessage());
} catch (ExecutionException e) {
e.printStackTrace();
Log.i(TAG, "sendRawTransaction: "+e.getMessage());
}
}
發(fā)送一筆交易
1、生成交易簽名信息
2、發(fā)送交易
/**
* 發(fā)起一筆交易
*
* @param web3j
* @param signedTransactionData 交易簽名信息
*/
public static void sendRawTransaction(Web3j web3j, String signedTransactionData) {
EthSendTransaction ethSendTransaction = null;
try {
ethSendTransaction = web3j.ethSendRawTransaction(signedTransactionData).send();
String transactionHash = ethSendTransaction.getTransactionHash();
String rawResponse = ethSendTransaction.getRawResponse();
Log.i(TAG, "sendRawTransaction transactionHash : " + transactionHash);
Log.i(TAG, "sendRawTransaction rawResponse : " + rawResponse);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "sendRawTransaction: " + e.getMessage());
}
}