之前的博客服务器到期了~,望支持~~~
由于已经不从事区块链相关项目,对疑惑的小伙伴提供一些帮助~~
对于离线交易不做过多解释~,
废话不多说 ,直接上代码:
package com.bscoin.coldwallet.cointype.eth;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;
public class EthWallet {
private static Logger logger = LoggerFactory.getLogger(EthWallet.class);
//private static ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
//默认 GAS
public final Long GAS_LIMIT = 25000L;
/**
*生成钱包:
* 公钥、私钥、地址
*/
public static Map createWallet() throws Exception{
ECKeyPair ecKeyPair = Keys.createEcKeyPair();
//WalletFile walletFile = Wallet.createLight(password, ecKeyPair);
//System.out.println("address " + walletFile.getAddress());
String privateKey = ecKeyPair.getPrivateKey().toString(16);
String publicKey = ecKeyPair.getPublicKey().toString(16);
String address = Keys.getAddress(ecKeyPair.getPublicKey());
Map result = new HashMap();
result.put("privateKey", privateKey);
result.put("publicKey", publicKey);
result.put("address", "0x"+address);
return result;
}
/**
* 签名
* from:发送方
* nonce:nonce值
* gasPrice:gas价格 一般以GWei(1 ETH = 1000000000 GWei)
* gasLimit:最多gas总量
* to:接收方
* value:发送的金额
* privateKey: 私钥
*/
public static String signTransaction(String from,BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to,
BigInteger value, String privateKey) throws IOException {
logger.info("=======================signTransaction==========================");
logger.info("nonce:"+nonce);
logger.info("gasPrice:"+gasPrice);
logger.info("gasLimit:"+gasLimit);
logger.info("to:"+to);
logger.info("value:"+value);
logger.info("privateKey:"+privateKey);
logger.info("from:"+from);
if (privateKey.startsWith("0x")) {
privateKey = privateKey.substring(2);
}
ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
Credentials credentials = Credentials.create(ecKeyPair);
String signData = signTransaction(nonce, gasPrice, gasLimit, to, value, "", credentials);
logger.info("=======================signTransaction(end)=======================");
return signData;
}
/**
* 签名获取hex 进行广播交易
*/
public static String signTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to,
BigInteger value, String data, Credentials credentials) throws IOException {
byte[] signedMessage;
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data);
signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
logger.info("signedData : " + hexValue);
return hexValue;
}
public static BigDecimal fromEthtoWei(BigDecimal eth) {
return Convert.toWei(eth, Convert.Unit.ETHER);
}
public static BigDecimal fromGWeitoWei(BigDecimal gwei) {
return Convert.toWei(gwei, Convert.Unit.GWEI);
}
public static void main(String[] args) throws Exception {
Map result = createWallet();
System.out.println(JsonUtil.toJson(result));
}
}
Eth工具类:
import java.math.BigDecimal;
import java.math.BigInteger;
import org.web3j.tx.Contract;
import org.web3j.utils.Convert;
public class EthUtil {
public static String fromWeitoEth(String wei) {
return Convert.fromWei(wei, Convert.Unit.ETHER).toString();
}
/*public static String fromEthtoWei(String eth) {
return Convert.fromWei(eth, Convert.Unit.WEI).toString();
}*/
public static BigDecimal fromEthtoWei(BigDecimal eth) {
return Convert.toWei(eth, Convert.Unit.ETHER);
}
public static String fromWeitoGWei(String wei) {
return Convert.fromWei(wei, Convert.Unit.GWEI).toString();
}
public static BigDecimal fromGWeitoWei(String gwei) {
return Convert.toWei(gwei, Convert.Unit.GWEI);
}
public static boolean isBlankBlockHash(String blockHash) {
boolean result = true;
int i = 0;
while(true) {
char c ;
try {
c = blockHash.charAt(i++);
}catch(StringIndexOutOfBoundsException e) {
break;
}
if(c == '0' || c == 'x') {
continue;
}else {
result = false;
break;
}
}
return result;
}
/**
* 在本价格上加的百分比
* @param i
* @param d
* @return
*/
public static BigInteger addGasPrice(BigInteger i,double d) {
return BigInteger.valueOf(Long.valueOf((long)(i.longValue()*(1+d))));
}
public static boolean validateAddress(String address) {
if(StringUtils.isBlank(address)) {
return false;
}
if(!address.startsWith("0x")) {
return false;
}
if(address.length()!=42) {
return false;
}
return true;
}
public static void main(String[] args) {
//BigDecimal x = EthUtil.fromEthtoWei(BigDecimal.valueOf(1L));
//System.out.println(x);
boolean x = validateAddress("0x7066366975d4abcd079bec98772675e87b0b28e8");
System.out.println(x);
}
}
Duration 帮助类
package com.bscoin.coldwallet.cointype.eth;
public class Duration {
public static final long SECOND_MILLIS = 1000;
public static final long MINUTE_MILLIS = 60*SECOND_MILLIS;
public static final long HOUR_MILLIS=60*MINUTE_MILLIS;
public static final long DAY_MILLIS = 24*HOUR_MILLIS;
private long time =0;
private Duration(long time){
this.time = time;
}
public static Duration ofMillis(long time){
return new Duration(time);
}
public int toDays(){
return (int)(time/DAY_MILLIS);
}
public Duration minusDays(int d){
time = time-d*DAY_MILLIS;
return this;
}
public int toHours(){
return (int)(time/HOUR_MILLIS);
}
public int toMinutes(){
return (int)(time/MINUTE_MILLIS);
}
public int getSeconds(){
return (int)(time/SECOND_MILLIS);
}
public Duration minusHours(int h){
return new Duration(time-h*HOUR_MILLIS);
}
public Duration minusMinutes(int m){
return new Duration(time-m*MINUTE_MILLIS);
}
public long getMillis(){
return time;
}
public static void main(String[] args) {
/* Duration d = Duration.ofMillis((long) (Duration.HOUR_MILLIS * 2 + 50 * Duration.MINUTE_MILLIS));
int h = d.toHours();
System.out.println(d.minusHours(h).getMillis());
int m = d.minusHours(h).toMinutes();
System.out.println(d.minusHours(h).getMillis());
System.out.println(d.minusHours(h).getMillis()); System.out.println(d.minusHours(h).getMillis()); System.out.println(d.minusHours(h).getMillis());
System.out.println(h+"小时"+m+"分");*/
System.out.println(HOUR_MILLIS);
}
}
之前的csdn找不回来了,决定重新注册一个。望支持~~~