:2026-02-28 20:15 点击:6
在区块链技术快速发展的今天,以太坊作为智能合约平台的代表,其生态应用日益丰富,无论是DeFi、NFT还是DAO,都离不开用户与以太坊网络的交互——而钱包正是这种交互的核心入口,本文将详细介绍如何使用Java生态中的主流工具Web3j,从零开始开发一个功能完善的以太坊钱包,涵盖钱包创建、地址生成、私钥管理、以太坊转账等核心功能,帮助开发者快速掌握以太坊钱包开发的核心技术。
Web3j是一个轻量级的、响应式的Java库,用于与以太坊节点进行交互,它基于以太坊JSON-RPC API封装,提供了丰富的Java API,支持以太坊的核心功能,包括:
相较于其他工具(如web3.py、ethers.js),Web3j的优势在于:完全兼容Java生态,可与Spring Boot、Android等Java框架无缝集成;轻量级,无需运行完整以太坊节点,通过连接远程节点(如Infura、Alchemy)即可使用;功能全面,覆盖了以太坊开发的大部分场景。
对于Java开发者而言,Web3j是开发以太坊应用(尤其是钱包)的理想选择。
在开始开发前,需确保以下环境已配置完成:
钱包需要与以太坊网络交互,因此需连接以太坊节点,开发者可选择以下方式:
本文以Infura的Ropsten测试网为例(测试网ETH免费,适合开发测试)。
mvn archetype:generate命令)。 pom.xml中添加Web3j依赖(最新版本可参考Web3j官网): <dependencies>
<!-- Web3j核心依赖 -->
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.9.8</version>
</dependency>
<!-- 工具类依赖(包含加密、格式转换等) -->
<dependency>
<groupId>org.web3j</groupId>
<artifactId>crypto</artifactId>
<version>4.9.8</version>
</dependency>
<!-- 单元测试依赖(可选) -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
钱包的核心是管理账户的密钥对(公钥+私钥),并通过公钥生成以太坊地址,Web3j的crypto模块提供了完整的密钥管理工具。
Web3j支持多种方式生成钱包:
import org.web3j.crypto.WalletUtils;
import java.io.File;
public class WalletGenerator {
public static void main(String[] args) throws Exception {
// 设置钱包文件存储目录(当前项目的wallets目录)
String walletDir = "wallets";
File directory = new File(walletDir);
if (!directory.exists()) {
directory.mkdirs();
}
// 生成钱包(密码可自定义,用于加密钱包文件)
String password = "your-secure-password";
String walletFile = WalletUtils.generateNewWalletFile(password, directory, false);
System.out.println("钱包文件已生成: " + walletFile);
System.out.println("请妥善保存钱包文件和密码!");
}
}
运行后,wallets目录下会生成一个以UTC--开头的JSON文件(如UTC--2023-10-01T12-00-00.000000000Z--0x1234...),这是加密后的钱包文件,包含私钥、地址等信息。
import org.web3j.crypto.Bip39Wallet;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.core.methods.response.EthAccount;
import java.io.File;
public class WalletFromMnemonic {
public static void main(String[] args) throws Exception {
// 生成12位助记词(也可手动输入已有的助记词)
String mnemonic = WalletUtils.generateBip39 mnemonic();
System.out.println("助记词: " + mnemonic);
// 从助记词和密码生成钱包
String password = "your-secure-password";
File walletDir = new File("wallets");
if (!walletDir.exists()) walletDir.mkdirs();
Bip39Wallet wallet = WalletUtils.generateBip39Wallet(password, walletDir);
System.out.println("钱包文件: " + wallet.getFilename());
System.out.println("地址: " + wallet.getAddress());
}
}
助记词是钱包的“备份”,必须妥善保存,一旦丢失将无法恢复钱包。
生成钱包后,需通过钱包文件和密码加载账户,获取地址和私钥:
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import java.io.File;
public class WalletLoader {
public static void main(String[] args) throws Exception {
// 钱包文件路径(需替换为实际路径)
String walletFile = "wallets/UTC--2023-10-01T12-00-00.000000000Z--0x1234...json";
String password = "your-secure-password";
// 加载钱包(Credentials包含地址、私钥等信息)
Credentials credentials = WalletUtils.loadCredentials(password, walletFile);
System.out.println("钱包地址: " + credentials.getAddress());
System.out.println("私钥: " + credentials.getEcKeyPair().getPrivateKey().toString(16));
}
}
Credentials是Web3j中账户的核心类,后续转账、查询等操作均依赖它。
加载钱包后,可通过Web3j连接以太坊节点,查询账户的ETH余额:
import org.web3j.protocol.Web3j; import org.web3j.protocol.http.HttpService; import org.web3j.protocol.core.methods.response.EthGetBalance; import java.io.IOException; import java.math.BigInteger; public class BalanceChecker { public static void main(String[] args) throws IOException { // 连接Infura Ropsten测试网(替换为你的Infura URL) String infuraUrl = "https://ropsten.infura.io/v3/YOUR_INFURA_PROJECT_ID"; Web3j web3j = Web3j.build(new HttpService(infuraUrl)); // 要查询的地址(替换为实际钱包地址) String address = "0xYourWalletAddress"; // 查询余额(单位:Wei,1 ETH = 10^18 Wei) EthGetBalance balance = web3j.ethGetBalance(address, org.web3j.protocol.core.DefaultBlockParameterName.LATEST).send(); BigInteger balanceInWei = balance.getBalance(); // 转换为ETH(除以10^18) double balanceInEth = balanceInWei.doubleValue() / Math.pow(10, 18); System.out.println("地址 " + address + " 的余额: " + balanceInEth + " ETH"); // 关闭Web3j连接 web3j.shutdown
本文由用户投稿上传,若侵权请提供版权资料并联系删除!