Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
web3jdemo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
duanjinfei
web3jdemo
Commits
5e86fe41
Commit
5e86fe41
authored
Aug 23, 2022
by
duanjinfei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
format public code
parent
2b90b298
Pipeline
#540
failed with stages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
65 deletions
+49
-65
SignAndSubmit.java
src/main/java/com/wuban/signDemo/SignAndSubmit.java
+49
-52
SSLSocketClient.java
src/main/java/com/wuban/util/SSLSocketClient.java
+0
-13
No files found.
src/main/java/com/wuban/signDemo/SignAndSubmit.java
View file @
5e86fe41
...
...
@@ -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
();
}
}
src/main/java/com/wuban/util/SSLSocketClient.java
View file @
5e86fe41
...
...
@@ -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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment