開發以太坊prc客戶端:
本次使用的是《MetaMask》錢包

1,安裝node.js
2,安裝ganache-cli,開啟本地web3j的測驗服務
安裝命令:npm install -g ganache-cli
啟動本地服務命令:ganache-cli -i 1 -h 0.0.0.0 -p 8545
啟動成功同時生成是個測驗賬號,每個賬號100eth,如圖

然后再MetaMask錢包可以通過輸入秘鑰的方式添加賬號,如圖

3.springboot部分:
匯入pom <dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>5.0.0</version>
</dependency>
如果運行報錯 (okhttp3.RequestBody.create(Ljava/lang/String;Lokhttp3/MediaType;)Lokhttp3/RequestBody)則添加pom
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.3.1</version>
</dependency>
創建web3j物件:![]()
Web3j web3 = Web3j.build(new HttpService("http://localhost:8545/"));
4,已有metamask錢包的情況,沒有metamask錢包,請先申請賬號

public static void trade(Web3j web3) {
try {
//根據私鑰創建身份資訊
Credentials credentials = Credentials.create("0xa91514e0c5f1d5929b3052b756d07a0123ec5cf7b86188f4410033bdc99a85e7");
//轉賬
TransactionReceipt transactionReceipt = Transfer.sendFunds(
web3, credentials, "0xb425cEA579C1c2F7A902ec1aFB5d2bEE04A611eA",
BigDecimal.valueOf(1.0), Convert.Unit.ETHER).send();
System.out.println(transactionReceipt.getBlockHash());
} catch (IOException | CipherException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TransactionException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
5,使用代碼創建錢包
創建:

public static void createWallet() {
//存放錢包的位置,不需要檔案名,會自動生成
String filePath = "G:/eth_wallet";
File f = new File(filePath);
try {//"l.1061326670"你錢包的密碼
String fileName = WalletUtils.generateNewWalletFile("l.1061326670", f , false);
System.out.println(fileName);
Credentials credentials = WalletUtils.loadCredentials("l.1061326670", f);
System.out.println(credentials.getAddress());//錢包地址
System.out.println(credentials.getEcKeyPair().getPrivateKey());//私鑰
System.out.println(credentials.getEcKeyPair().getPublicKey());//公鑰
} catch (CipherException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
轉賬:

public static void tradeByOnlineWallet(Web3j web3) {
try {
String filePath = "G:/eth_wallet/***.json";
File f = new File(filePath);
Credentials credentials = WalletUtils.loadCredentials("l.1061326670", f);
TransactionReceipt transactionReceipt = Transfer.sendFunds(
web3, credentials, "0xb425cEA579C1c2F3A024ec1aFB5d2bEE04A611eA",
BigDecimal.valueOf(1.0), Convert.Unit.ETHER).send();
System.out.println(transactionReceipt.getBlockHash());
} catch (IOException | CipherException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TransactionException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/337772.html
標籤:區塊鏈
