-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTransaction.java
More file actions
131 lines (108 loc) · 3.83 KB
/
Transaction.java
File metadata and controls
131 lines (108 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package cn.chain33.javasdk.model.protobuf;
import cn.chain33.javasdk.model.enums.SignType;
import cn.chain33.javasdk.utils.HashUtil;
import cn.chain33.javasdk.utils.HexUtil;
import cn.chain33.javasdk.utils.TransactionUtil;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
/**
* @authoer lhl
* @date 2022/6/30 下午8:20
*/
public class Transaction {
private static final long MAXTXSIZE = 100000;
private TransactionAllProtobuf.Transaction tx;
public Transaction(TransactionAllProtobuf.Transaction tx) {
this.tx = tx;
}
public Transaction(String hexTx) throws Exception {
try {
this.tx = TransactionAllProtobuf.Transaction.parseFrom(HexUtil.fromHexString(hexTx));
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
throw e;
}
}
public void sign(SignType signType, String privateKey) {
this.tx = TransactionUtil.signedProtobufTx(tx, privateKey, signType);
}
public TransactionAllProtobuf.Transaction getTx() {
return this.tx;
}
public String hexString() {
return HexUtil.toHexString(tx.toByteArray());
}
public byte[] hash() throws Exception {
return HashUtil.sha256(clone().getTx().toBuilder().clearSignature().clearHeader().build().toByteArray());
}
@Override
public Transaction clone() throws CloneNotSupportedException {
try {
return new Transaction(TransactionAllProtobuf.Transaction.parseFrom(this.tx.toByteArray()));
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
return null;
}
//暂时只支持设置时间
public void setExpire(long expire) {
this.tx = tx.toBuilder().setExpire(TransactionUtil.getExpire(expire)).build();
}
public void setGroupCount(int groupCount) {
this.tx = tx.toBuilder().setGroupCount(groupCount).build();
}
public void setHeader(byte[] header) {
this.tx = tx.toBuilder().setHeader(ByteString.copyFrom(header)).build();
}
public void setNext(byte[] next) {
this.tx = tx.toBuilder().setNext(ByteString.copyFrom(next)).build();
}
public void setChainId(int chainId) {
this.tx = tx.toBuilder().setChainID(chainId).build();
}
public byte[] getExecer() {
return tx.getExecer().toByteArray();
}
public Transactions toTransactions() throws Exception {
if (tx.getGroupCount() < 0 || tx.getGroupCount() == 1 || tx.getGroupCount() > 20) {
throw new Exception("ErrTxGroupCount");
}
if (tx.getGroupCount() > 0) {
return new Transactions(TransactionAllProtobuf.Transactions.parseFrom(tx.getHeader()));
}
if (!tx.getNext().isEmpty() || !tx.getHeader().isEmpty()) {
throw new Exception("ErrNomalTx");
}
return null;
}
public int size() {
return tx.toByteArray().length;
}
public long getFee() {
return tx.getFee();
}
//获取真实fee
public long getRealFee(long feeRate) throws Exception {
int txSize = tx.toByteArray().length;
//如果签名为空,那么加上签名的空间
if (tx.getSignature() == null) {
txSize += 300;
}
if (size() > MAXTXSIZE) {
throw new Exception("ErrTxMsgSizeTooBig");
}
// 检查交易费是否小于最低值
long realFee = (txSize / 1000 + 1) * feeRate;
return realFee;
}
//通过链上费率直接设置手续费
public void setFeeRate(long feeRate) throws Exception {
long realFee = getRealFee(feeRate);
if (tx.getFee() < realFee) {
setFee(realFee);
}
}
public void setFee(long fee) {
this.tx = tx.toBuilder().setFee(fee).build();
}
}