ETH 离线签名交易 java

版权声明:望支持~~~ https://blog.csdn.net/u011663149/article/details/85295855

之前的csdn找不回来了,决定重新注册一个。望支持~~~

由于已经不从事区块链相关项目,对疑惑的小伙伴提供一些帮助~~

对于离线交易不做过多解释~,

废话不多说 ,直接上代码:

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();
	
	public final Long GAS_LIMIT = 25000L; 
	
	
	public static Map<String,String> 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<String,String> result = new HashMap<String,String>();
			result.put("privateKey", privateKey);
			result.put("publicKey", publicKey);
			result.put("address", "0x"+address);
			
			return result;
	}
	
	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;
	}

	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<String,String> result = createWallet();
		System.out.println(JsonUtil.toJson(result));
		
	}

}
 
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找不回来了,决定重新注册一个。望支持~~~

猜你喜欢

转载自blog.csdn.net/u011663149/article/details/85295855
ETH