1.ETH账户之间的转账
public class EthUtil {
private static final String URL = “http://127.0.0.1:8545/”;
public static final String KEYSTOREPATH = “C:\Users\Administrator\AppData\Roaming\Ethereum\keystore”;
public static final String PASSWORD = “syy123456”;
public static String CreateWallet() throws CipherException, IOException {
Bip39Wallet wallet = WalletUtils.generateBip39Wallet(PASSWORD, new File(KEYSTOREPATH));
String fileName = wallet.getFilename();
return getAddress(fileName);
}
public static String getAddress(String fileName) {
String walletAddress = "0x" + fileName.substring(
fileName.lastIndexOf("-") + 1,
fileName.lastIndexOf("-") + 41);
return walletAddress;
}
public static List IterateAllWallet(String path) {
List<File> fileList = new ArrayList<File>();
File file = new File(path);
File[] files = file.listFiles();
if (files == null) {
return fileList;
}
for (File f : files) {
if (f.isFile()) {
fileList.add(f);
} else if (f.isDirectory()) {
System.out.println(f.getAbsolutePath());
IterateAllWallet(f.getAbsolutePath());
}
}
return fileList;
}
public static File getFileFromAddress(String address) {
File file = new File(KEYSTOREPATH);
if (!file.isDirectory() || file.listFiles().length == 0) {
return null;
}
for (File f : file.listFiles()) {
String fileName = f.getName();
String walletAddress = "0x" + fileName.substring(fileName.lastIndexOf("-") + 1,
fileName.lastIndexOf("-") + 41);
if (address.equalsIgnoreCase(walletAddress)) {
return f;
}
}
return null;
}
public static Web3j getConnection() {
Web3j web3 = Web3j.build(new HttpService(URL));
return web3;
}
public static Admin initAdmin() {
return Admin.build(getService());
}
private static HttpService getService() {
return new HttpService(URL);
}
}
Web3j web3j = EthUtil.getConnection();
String txHash = ethService.tranferEth(web3j, fromAddr, toAddr, amount, gasPrice, gasLimit, walletPwd);
if (StringUtils.isEmpty(txHash)) {
throw new Exception(“转账失败”);
}
@Transactional(rollbackFor = Exception.class)
@Override
public String tranferEth(Web3j web3j, String from, String to, String amount, String gasPrice1, String gasLimit1, String walletPwd) throws Exception {
BigInteger gasPrice = new BigInteger(gasPrice1);
BigInteger gasLimit = new BigInteger(gasLimit1);
BigInteger gas = gasPrice.multiply(gasLimit);
BigInteger value = new BigInteger(amount);
BigInteger allFee = value.add(gas);
String transactionHash;
try {
BigInteger balance = web3j.ethGetBalance(from, DefaultBlockParameterName.LATEST).send().getBalance();
if (balance.compareTo(allFee) < 0) {
throw new Exception(“账户余额不足”);
}
Credentials credentials = WalletUtils.loadCredentials(walletPwd, EthUtil.getFileFromAddress(from));
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
from, DefaultBlockParameterName.LATEST).sendAsync().get();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
nonce, gasPrice, gasLimit, to, value);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
transactionHash = ethSendTransaction.getTransactionHash();
} catch (Exception e) {
throw new Exception("转账失败!");
}
return transactionHash;
}
2.指定地址转ETH至合约地址
Result result = new Result();
Web3j web3j = EthUtil.getConnection();
String txHash = ethService.tranferToContractAddr(web3j, account, contractAddr, amount, gasPrice, gasLimit, walletPwd);
if (StringUtils.isEmpty(txHash)) {
throw new Exception(“转账失败”);
}
@Transactional(rollbackFor = Exception.class)
@Override
public String tranferToContractAddr(Web3j web3j, String from, String to, String value, String gasPrice, String gasLimit, String walletPwd) throws Exception {
BigInteger balance = web3j.ethGetBalance(from, DefaultBlockParameterName.LATEST).send().getBalance();
BigInteger gasPrice1 = new BigInteger(gasPrice);
BigInteger gasLimit1 = new BigInteger(gasLimit);
BigInteger gas = gasPrice1.multiply(gasLimit1);
BigInteger amount = new BigInteger(value);
BigInteger allFee = amount.add(gas);
if (balance.compareTo(allFee) < 0) {
throw new Exception(“账户余额不足”);
}
Credentials credentials = WalletUtils.loadCredentials(walletPwd, EthUtil.getFileFromAddress(from));
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
from, DefaultBlockParameterName.LATEST).sendAsync().get();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
Function function = new Function(
"_transfer",
Arrays.asList(new Address(from), new Uint256(amount)),
Arrays.asList(new TypeReference<Address>() {
}, new TypeReference<Uint256>() {
})
);
String encodedFunction = FunctionEncoder.encode(function);
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice1, gasLimit1, to, encodedFunction);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
String transactionHash = ethSendTransaction.getTransactionHash();
return transactionHash;
}