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