Skip to content

Commit 25cd607

Browse files
authored
add genBroadcastTx (#52)
Co-authored-by: ylsGit <>
1 parent d4aa078 commit 25cd607

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/main/java/com/okexchain/msg/common/Data2Sign.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
66
import com.google.gson.annotations.SerializedName;
7+
import com.okexchain.msg.tx.BoardcastTx;
78
import com.okexchain.utils.Utils;
89
import org.apache.commons.lang3.builder.ToStringBuilder;
910
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -88,16 +89,20 @@ public void setData(Fee fee) {
8889
@Override
8990
public String toString() {
9091
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
91-
.append("account_number", accountNumber)
92-
.append("chain_id", chainId)
93-
.append("fee", fee)
94-
.append("memo", memo)
95-
.append("msgs", msgs)
96-
.append("sequence", sequence)
97-
.toString();
92+
.append("account_number", accountNumber)
93+
.append("chain_id", chainId)
94+
.append("fee", fee)
95+
.append("memo", memo)
96+
.append("msgs", msgs)
97+
.append("sequence", sequence)
98+
.toString();
9899
}
99100

100101
public String toJson() {
101102
return Utils.serializer.toJson(this);
102103
}
104+
105+
public static Data2Sign fromJson(String json) {
106+
return Utils.serializer.fromJson(json, Data2Sign.class);
107+
}
103108
}

src/main/java/com/okexchain/msg/tx/UnsignedTx.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.okexchain.msg.common.Signature;
44
import com.okexchain.msg.common.TxValue;
5+
import com.okexchain.msg.common.Data2Sign;
56

67
import java.util.ArrayList;
78
import java.util.List;
@@ -30,6 +31,7 @@ public BoardcastTx signed(Signature signature) {
3031
boardcastTx.getTx().setSignatures(signatureList);
3132
return boardcastTx;
3233
}
34+
3335
public BoardcastValue sign4gentx(Signature signature) {
3436
List<Signature> signatureList = new ArrayList<>();
3537
signatureList.add(signature);
@@ -40,4 +42,18 @@ public BoardcastValue sign4gentx(Signature signature) {
4042
public String toString() {
4143
return unsignedTxJson;
4244
}
45+
46+
47+
public static BoardcastTx genBroadcastTx(String unsignedTxStr, Signature signature) {
48+
Data2Sign data = Data2Sign.fromJson(unsignedTxStr);
49+
50+
TxValue txValue = new TxValue();
51+
txValue.setMsgs(data.getMsgs());
52+
txValue.setFee(data.getFee());
53+
txValue.setMemo(data.getMemo());
54+
55+
UnsignedTx unsignedTx = new UnsignedTx(txValue, unsignedTxStr);
56+
57+
return unsignedTx.signed(signature);
58+
}
4359
}

src/main/java/com/okexchain/sample/ColdSign.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,27 @@ public static void main(String[] args) {
1919
PrivateKey key = new PrivateKey("8145bfb1d3acc216c54490952c994d5e3bce09dd65ae73d0c79f892284f721e7");
2020

2121
MsgSend msg = new MsgSend();
22-
msg.init(key.getAddress(), key.getPubKey());
22+
// msg.init(key.getAddress(), key.getPubKey());
2323

2424
// or init by account number and sequence number
25-
// msg.init(key.getPubKey(), "0", "10");
25+
msg.init(key.getPubKey(), "0", "10");
2626

2727
Message messages = msg.produceSendMsg(
2828
"okt",
2929
"6.00000000",
3030
"okexchain1v853tq96n9ghvyxlvqyxyj97589clccrufrkz9");
3131

3232
try {
33-
UnsignedTx unsignedTx = msg.getUnsignedTx(messages,"0.01000000", "200000", "okexchain transfer!");
33+
UnsignedTx unsignedTx = msg.getUnsignedTx(messages, "0.01000000", "200000", "okexchain transfer!");
3434

3535
Signature signature = MsgBase.signTx(unsignedTx.toString(), key.getPriKey());
3636

37-
BoardcastTx signedTx = unsignedTx.signed(signature);
37+
// BoardcastTx signedTx = unsignedTx.signed(signature);
38+
BoardcastTx signedTx = UnsignedTx.genBroadcastTx(unsignedTx.toString(), signature);
39+
System.out.println(signedTx.toJson());
3840

39-
MsgBase.boardcast(signedTx.toJson(), EnvInstance.getEnv().GetRestServerUrl());
41+
42+
// MsgBase.boardcast(signedTx.toJson(), EnvInstance.getEnv().GetRestServerUrl());
4043

4144
} catch (Exception e) {
4245
System.out.println("serialize transfer msg failed");

0 commit comments

Comments
 (0)