Commit 5e86fe41 authored by duanjinfei's avatar duanjinfei

format public code

parent 2b90b298
Pipeline #540 failed with stages
......@@ -19,51 +19,45 @@ import java.util.concurrent.ExecutionException;
* @Version 1.0
*/
public class SignAndSubmit {
private static Web3j web3j;
public SignAndSubmit(String rpcUrl) {
web3j = Web3j.build(new CustomizeHttpService(rpcUrl));
private SignAndSubmit(String rpcUrl) {
if (Objects.isNull(web3j)){
web3j = Web3j.build(new CustomizeHttpService(rpcUrl));
}
}
/**
* 提交交易
* @param signedMessage 交易签名
*/
public void submitSign(byte[] signedMessage) {
private void submitSign(byte[] signedMessage) {
try {
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
// 交易Hash
String transactionHash = ethSendTransaction.getTransactionHash();
System.out.println("transactionHash:"+transactionHash);
System.out.println("transactionHash:" + transactionHash);
if (ethSendTransaction.hasError()) {
String message = ethSendTransaction.getError().getMessage();
System.out.println("transaction failed,info:" + message);
} else {
EthGetTransactionReceipt send = web3j.ethGetTransactionReceipt(transactionHash).send();
if (send != null) {
System.out.println("交易成功");
System.out.println("transaction success");
}
}
} catch (Exception e) {
if (e instanceof ExecutionException || e instanceof InterruptedException) {
System.out.println("----------获取Nonce异常-----------");
System.out.println("----------get Nonce exception-----------");
}
e.printStackTrace();
}
}
/**
* 获取账户交易Nonce
* @param privateKey 账户地址
* @return 返回Nonce
*/
public BigInteger getAddressNonce(String privateKey) {
private BigInteger getAddressNonce(String privateKey) {
try {
String fromAddress = Keys.toChecksumAddress(
Keys.getAddress(
ECKeyPair.create(
Numeric.toBigInt(privateKey))));
System.out.println("fromAddress:"+fromAddress);
System.out.println("fromAddress:" + fromAddress);
return web3j.ethGetTransactionCount(
fromAddress, DefaultBlockParameterName.LATEST).send().getTransactionCount();
} catch (Exception e) {
......@@ -71,23 +65,17 @@ public class SignAndSubmit {
return BigInteger.valueOf(-1);
}
}
/**
* 私钥交易
* @param fromPk 发送方私钥
* @param toAddress 接收方地址
* @param value 数量 单位wei
*/
public byte[] offlineSign(String fromPk, String toAddress, BigInteger value) {
private byte[] offlineSign(String fromPk, String toAddress, BigInteger value) {
try {
BigInteger nonce = getAddressNonce(fromPk);
System.out.println("Nonce:"+nonce);
System.out.println("Nonce:" + nonce);
BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
BigInteger gasLimit = new BigInteger("900000");
if (fromPk.startsWith("0x")){
if (fromPk.startsWith("0x")) {
fromPk = fromPk.substring(2);
}
Credentials credentials = Credentials.create(fromPk);
//生成RawTransaction交易对象
RawTransaction rawTransaction = RawTransaction.createTransaction(
nonce,
gasPrice,
......@@ -95,29 +83,21 @@ public class SignAndSubmit {
toAddress,
value,
"");
//使用私钥生成Credentials对象
System.out.println("toAddress:"+toAddress);
return TransactionEncoder.signMessage(rawTransaction,512512, credentials);
System.out.println("toAddress:" + toAddress);
return TransactionEncoder.signMessage(rawTransaction, 512512, credentials);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 签名且提交
* @param fromPk 发送方私钥
* @param toAddress 接收方地址
* @param value 数量 单位wei
*/
public boolean signAndSend(String fromPk, String toAddress, BigInteger value) {
try {
BigInteger nonce = getAddressNonce(fromPk);
System.out.println("Nonce:"+nonce);
System.out.println("Nonce:" + nonce);
BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
BigInteger gasLimit = new BigInteger("900000");
Credentials credentials = Credentials.create(fromPk);
//生成RawTransaction交易对象
RawTransactionManager transactionManager = new RawTransactionManager(web3j,
credentials,
512512);
......@@ -128,8 +108,7 @@ public class SignAndSubmit {
toAddress,
value,
"");
//使用私钥生成Credentials对象
System.out.println("toAddress:"+toAddress);
System.out.println("toAddress:" + toAddress);
EthSendTransaction ethSendTransaction = transactionManager.signAndSend(rawTransaction);
String transactionHash = ethSendTransaction.getTransactionHash();
if (ethSendTransaction.hasError()) {
......@@ -138,7 +117,7 @@ public class SignAndSubmit {
} else {
EthGetTransactionReceipt send = web3j.ethGetTransactionReceipt(transactionHash).send();
if (send != null) {
System.out.println("交易成功");
System.out.println("transaction success");
}
}
return true;
......@@ -148,16 +127,34 @@ public class SignAndSubmit {
}
}
public static void main(String[] args) {
SignAndSubmit signAndSubmit = new SignAndSubmit("https://galaxy.block.caduceus.foundation");
byte[] signMessage = signAndSubmit.offlineSign(
"私钥",
"地址",
new BigInteger("1000000000000000000"));
if (Objects.isNull(signMessage)){
System.exit(0);
public static void signAfterSubmit(String rpcUrl, String fromPk, String toAddress, String amount) {
try {
SignAndSubmit signAndSubmit = new SignAndSubmit(rpcUrl);
// sign transaction
byte[] signMessage = signAndSubmit.offlineSign(fromPk, toAddress, new BigInteger(amount));
if (Objects.isNull(signMessage)) {
return;
}
// submit sign message
signAndSubmit.submitSign(signMessage);
} catch (Exception e) {
System.out.println("transaction failed,exception:" + e);
web3j.shutdown();
}finally {
web3j.shutdown();
}
}
public static boolean signAndSubmit(String rpcUrl, String fromPk, String toAddress, String amount) {
try {
SignAndSubmit signAndSubmit = new SignAndSubmit(rpcUrl);
return signAndSubmit.signAndSend(fromPk, toAddress, new BigInteger(amount));
} catch (Exception e) {
web3j.shutdown();
System.out.println("transaction failed,exception:" + e);
return false;
} finally {
web3j.shutdown();
}
signAndSubmit.submitSign(signMessage);
web3j.shutdown();
}
}
......@@ -5,10 +5,6 @@ import java.security.SecureRandom;
import java.security.cert.X509Certificate;
public class SSLSocketClient {
/**
* 获取这个SSLSocketFactory
* @return SocketFactory
*/
public static SSLSocketFactory getSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
......@@ -18,10 +14,6 @@ public class SSLSocketClient {
throw new RuntimeException(e);
}
}
/**
* 获取TrustManager
*/
private static TrustManager[] getTrustManager() {
return new TrustManager[]{
new X509TrustManager() {
......@@ -40,11 +32,6 @@ public class SSLSocketClient {
}
};
}
/**
* 获取HostnameVerifier
* @return true
*/
public static HostnameVerifier getHostnameVerifier() {
return (s, sslSession) -> true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment