赞
踩
Web3j 是一个高度模块化、反应式、类型安全的 Java 和 Android 库,用于处理智能合约并与以太坊网络上的客户端(节点)集成
引入依赖
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.3.0</version>
</dependency>
2、工具类
/** * @author XiangYu * @since 2024/1/7 */ @Slf4j public class Web3Client { private static final Web3j web3j = Web3j.build(new HttpService("主网/测试网rpc")); private Web3Client(){ } /** * 发送交易、只能发送主网默认代币 * @param privateKey 私钥 * @param to to * @param value 金额 * @return hash */ public static String sendRawTransaction(String privateKey,String to,String value) { try { Credentials credentials = getCredentials(privateKey); BigDecimal amount = new BigDecimal(value); String signed = signed(web3j, to, credentials, amount); return web3j.ethSendRawTransaction(signed).send().getTransactionHash(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 发送交易 * 发送token代币 * @param privateKey 私钥 * @param to 接收地址 * @param amt 转账金额 * @param contractAddress 合约地址 * @return hash */ public static String sendRawTransaction(String privateKey,String to,String amt,String contractAddress){ try { Credentials credentials = getCredentials(privateKey); Function function = getFunction(to, amt); String encodedFunction = FunctionEncoder.encode(function); String hexSigned = signed(web3j,contractAddress, credentials, encodedFunction); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexSigned).send(); return ethSendTransaction.getTransactionHash(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 签名交易 * @param web3j web3 * @param to to * @param credentials 私钥凭证 * @param value 金额 * @return sign */ private static String signed(String to, Credentials credentials,BigDecimal value) { BigInteger nonce = getNonce(web3j, credentials.getAddress()); RawTransaction rawTransaction = RawTransaction.createTransaction( nonce, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT, to, Convert.toWei(value, Convert.Unit.ETHER).toBigInteger(), "" ); return signed(credentials, rawTransaction); } /** * 签名 * @param web3j web3 * @param to to * @param credentials 凭证 * @param encodedFunction 合约函数调用编码 * @return 签名 */ private static String signed(String to, Credentials credentials, String encodedFunction) { BigInteger nonce = getNonce(web3j, credentials.getAddress()); RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT, to, encodedFunction); return signed(credentials, rawTransaction); } /** * 签名 * @param credentials 私钥凭证 * @param rawTransaction 包装交易 * @return hex sign */ private static String signed(Credentials credentials, RawTransaction rawTransaction) { byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, Web3Manage.INSTANCE.getChainId(), credentials); return Numeric.toHexString(signedMessage); } /** * 包装智能合约的transfer函数方法 * @param to 接收地址 * @param amt 金额 * @return transfer function */ private static Function getFunction(String to, String amt) { Address address = new Address(to); Uint256 value = new Uint256(new BigInteger(Convert.toWei(amt, Convert.Unit.ETHER).toString())); List<Type> parametersList = new ArrayList<>(); parametersList.add(address); parametersList.add(value); return new Function("transfer", parametersList, new ArrayList<>()); } /** * 获取交易账户确认的nonce * @param web3j web3j * @param address 地址 * @return nonce */ public static BigInteger getNonce(String address) { try { EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount( address, DefaultBlockParameterName.LATEST).send(); return ethGetTransactionCount.getTransactionCount(); } catch (Exception e) { e.printStackTrace(); } throw new BscRpcException("nonce too low"); } /** * 通过私钥获取账户凭证 * @param privateKey 私钥 * @return 凭证 */ private static Credentials getCredentials(String privateKey) { BigInteger bigInteger = new BigInteger(privateKey, 16); ECKeyPair ecKeyPair = ECKeyPair.create(bigInteger); return Credentials.create(ecKeyPair); } /** * 钱包生成 * @param walletFilePath 钱包生成路径 * @return 凭证 */ public static Credentials genWallet(String walletFilePath){ try { // 钱包密码 String password = ""; //生成钱包,对应目录下会创建对应的私钥文件。 String walletFileName = WalletUtils.generateNewWalletFile(password, new File(walletFilePath), false); // 加载指定位置的钱包 return WalletUtils.loadCredentials(password, walletFilePath + "/" + walletFileName); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 校验hash是否符合条件 * @param hash 金额 * @return bool */ public static Transaction getTransactionHash(String hash){ Web3j web3j = Web3Manage.INSTANCE.getWeb3j(); try { // 获取交易信息 EthTransaction ethTransaction = web3j.ethGetTransactionByHash(hash).send(); return ethTransaction.getTransaction().orElse(null); } catch (IOException e) { e.printStackTrace(); } return null; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。