23
23
import com .google .protobuf .ByteString ;
24
24
import java .security .SignatureException ;
25
25
import java .util .ArrayList ;
26
- import java .util .Arrays ;
27
26
import java .util .List ;
28
27
import lombok .extern .slf4j .Slf4j ;
29
28
import org .apache .commons .lang3 .ArrayUtils ;
35
34
import org .tron .api .GrpcAPI .TransactionSignWeight ;
36
35
import org .tron .api .GrpcAPI .TransactionSignWeight .Result ;
37
36
import org .tron .common .parameter .CommonParameter ;
38
- import org .tron .common .crypto .Hash ;
39
37
import org .tron .common .utils .Sha256Hash ;
40
38
import org .tron .core .ChainBaseManager ;
41
39
import org .tron .core .capsule .AccountCapsule ;
49
47
import org .tron .protos .Protocol .Transaction .Result .contractResult ;
50
48
import org .tron .protos .Protocol .TransactionSign ;
51
49
import org .tron .protos .contract .SmartContractOuterClass .CreateSmartContract ;
52
- import org .tron .protos .contract .SmartContractOuterClass .SmartContract ;
53
- import org .tron .protos .contract .SmartContractOuterClass .SmartContract .ABI ;
54
- import org .tron .protos .contract .SmartContractOuterClass .SmartContract .ABI .Entry .StateMutabilityType ;
55
50
import org .tron .protos .contract .SmartContractOuterClass .TriggerSmartContract ;
56
51
57
52
@ Slf4j (topic = "capsule" )
@@ -61,56 +56,30 @@ public class TransactionUtil {
61
56
@ Autowired
62
57
private ChainBaseManager chainBaseManager ;
63
58
64
- public static boolean validAccountName ( byte [] accountName ) {
65
- if ( ArrayUtils . isEmpty ( accountName )) {
66
- return true ; //account name can be empty
67
- }
68
-
69
- return accountName . length < = 200 ;
70
- }
59
+ private static final int maxAccountNameLen = 200 ;
60
+ private static final int maxAccountIdLen = 32 ;
61
+ private static final int minAccountIdLen = 8 ;
62
+ private static final int maxAssetNameLen = 32 ;
63
+ private static final int maxTokenAbbrName = 5 ;
64
+ private static final int maxAssetDescription = 200 ;
65
+ private static final int maxUrlLen = 256 ;
71
66
72
67
public static boolean validAccountId (byte [] accountId ) {
73
- if (ArrayUtils .isEmpty (accountId )) {
74
- return false ;
75
- }
76
-
77
- if (accountId .length < 8 ) {
78
- return false ;
79
- }
80
-
81
- if (accountId .length > 32 ) {
82
- return false ;
83
- }
84
-
85
- return validBytes (accountId );
86
-
68
+ return validReadableBytes (accountId , maxAccountIdLen ) && accountId .length < minAccountIdLen ;
87
69
}
88
70
89
71
public static boolean validAssetName (byte [] assetName ) {
90
- if (ArrayUtils .isEmpty (assetName )) {
91
- return false ;
92
- }
93
- if (assetName .length > 32 ) {
94
- return false ;
95
- }
96
-
97
- return validBytes (assetName );
98
-
72
+ return validReadableBytes (assetName , maxAssetNameLen );
99
73
}
100
74
101
75
public static boolean validTokenAbbrName (byte [] abbrName ) {
102
- if (ArrayUtils .isEmpty (abbrName )) {
103
- return false ;
104
- }
105
- if (abbrName .length > 5 ) {
106
- return false ;
107
- }
108
-
109
- return validBytes (abbrName );
110
-
76
+ return validReadableBytes (abbrName , maxTokenAbbrName );
111
77
}
112
78
113
- private static boolean validBytes (byte [] bytes ) {
79
+ private static boolean validReadableBytes (byte [] bytes , int maxLength ) {
80
+ if (ArrayUtils .isEmpty (bytes ) || bytes .length > maxLength ) {
81
+ return false ;
82
+ }
114
83
// b must be readable
115
84
for (byte b : bytes ) {
116
85
if (b < 0x21 ) {
@@ -123,20 +92,23 @@ private static boolean validBytes(byte[] bytes) {
123
92
return true ;
124
93
}
125
94
95
+ public static boolean validAccountName (byte [] accountName ) {
96
+ return validBytes (accountName , maxAccountNameLen , true );
97
+ }
126
98
127
99
public static boolean validAssetDescription (byte [] description ) {
128
- if (ArrayUtils .isEmpty (description )) {
129
- return true ; //description can empty
130
- }
131
-
132
- return description .length <= 200 ;
100
+ return validBytes (description , maxAssetDescription , true );
133
101
}
134
102
135
103
public static boolean validUrl (byte [] url ) {
136
- if (ArrayUtils .isEmpty (url )) {
137
- return false ;
104
+ return validBytes (url , maxUrlLen , false );
105
+ }
106
+
107
+ private static boolean validBytes (byte [] bytes , int maxLength , boolean allowEmpty ) {
108
+ if (ArrayUtils .isEmpty (bytes )) {
109
+ return allowEmpty ;
138
110
}
139
- return url .length <= 256 ;
111
+ return bytes .length <= maxLength ;
140
112
}
141
113
142
114
public static boolean isNumber (byte [] id ) {
0 commit comments