Skip to content

Commit 2878de0

Browse files
Merge pull request #828 from tronprotocol/release_v4.8.0
feat(merge): merge release_v4.8.0 into master
2 parents c5382ff + 13d011a commit 2878de0

30 files changed

+1774
-749
lines changed

README.md

Lines changed: 176 additions & 85 deletions
Large diffs are not rendered by default.

src/main/java/org/tron/common/utils/TransactionUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,14 @@ public static Transaction setPermissionId(Transaction transaction, String tipStr
367367
}
368368

369369
System.out.println(tipString);
370-
int permission_id = inputPermissionId();
371-
if (permission_id < 0) {
370+
int permissionId = inputPermissionId();
371+
if (permissionId < 0) {
372372
throw new CancelException("User cancelled");
373373
}
374-
if (permission_id != 0) {
374+
if (permissionId != 0) {
375375
Transaction.raw.Builder raw = transaction.getRawData().toBuilder();
376376
Transaction.Contract.Builder contract =
377-
raw.getContract(0).toBuilder().setPermissionId(permission_id);
377+
raw.getContract(0).toBuilder().setPermissionId(permissionId);
378378
raw.clearContract();
379379
raw.addContract(contract);
380380
transaction = transaction.toBuilder().setRawData(raw).build();

src/main/java/org/tron/common/utils/Utils.java

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,30 @@
1818

1919
package org.tron.common.utils;
2020

21+
import static org.apache.commons.lang3.StringUtils.EMPTY;
22+
import static org.apache.commons.lang3.StringUtils.isEmpty;
23+
import static org.tron.ledger.console.ConsoleColor.ANSI_BLUE;
24+
import static org.tron.ledger.console.ConsoleColor.ANSI_BOLD;
25+
import static org.tron.ledger.console.ConsoleColor.ANSI_GREEN;
26+
import static org.tron.ledger.console.ConsoleColor.ANSI_RED;
27+
import static org.tron.ledger.console.ConsoleColor.ANSI_RESET;
28+
import static org.tron.ledger.console.ConsoleColor.ANSI_YELLOW;
29+
2130
import com.alibaba.fastjson.JSON;
2231
import com.alibaba.fastjson.JSONArray;
2332
import com.alibaba.fastjson.JSONObject;
2433
import com.google.protobuf.Any;
2534
import com.google.protobuf.InvalidProtocolBufferException;
2635
import com.google.protobuf.Message;
2736

37+
import java.io.ByteArrayOutputStream;
2838
import java.io.Console;
2939
import java.io.IOException;
40+
import java.io.InputStream;
3041
import java.nio.ByteBuffer;
3142
import java.nio.CharBuffer;
3243
import java.nio.charset.Charset;
44+
import java.nio.charset.StandardCharsets;
3345
import java.security.SecureRandom;
3446
import java.text.ParsePosition;
3547
import java.text.SimpleDateFormat;
@@ -43,6 +55,7 @@
4355
import org.tron.common.crypto.Sha256Sm3Hash;
4456
import org.tron.keystore.StringUtils;
4557
import org.tron.protos.contract.BalanceContract;
58+
import org.tron.walletcli.Client;
4659
import org.tron.walletserver.WalletApi;
4760
import org.tron.protos.Protocol.Block;
4861
import org.tron.protos.Protocol.Transaction;
@@ -87,6 +100,9 @@ public class Utils {
87100
public static final String VISIBLE = "visible";
88101
public static final String TRANSACTION = "transaction";
89102
public static final String VALUE = "value";
103+
public static final String LOCK_WARNING = "⚠️" + ANSI_YELLOW
104+
+ " Wallet is locked. Transaction not allowed. Please use " + greenBoldHighlight("unlock")
105+
+ ANSI_YELLOW + " to retry" + ANSI_RESET;
90106

91107
private static SecureRandom random = new SecureRandom();
92108

@@ -95,7 +111,7 @@ public static SecureRandom getRandom() {
95111
}
96112

97113
public static byte[] getBytes(char[] chars) {
98-
Charset cs = Charset.forName("UTF-8");
114+
Charset cs = StandardCharsets.UTF_8;
99115
CharBuffer cb = CharBuffer.allocate(chars.length);
100116
cb.put(chars);
101117
cb.flip();
@@ -239,12 +255,13 @@ public static String printTransactionApprovedList(
239255
return JsonFormatUtil.formatJson(jsonObject.toJSONString());
240256
}
241257

242-
public static char[] inputPassword2Twice() throws IOException {
258+
public static char[] inputPassword2Twice(boolean isNew) throws IOException {
243259
char[] password0;
260+
String newStr = isNew ? "new" : EMPTY;
244261
while (true) {
245-
System.out.println("Please input password.");
262+
System.out.println("Please input " + newStr + "password.");
246263
password0 = Utils.inputPassword(true);
247-
System.out.println("Please input password again.");
264+
System.out.println("Please input " + newStr + "password again.");
248265
char[] password1 = Utils.inputPassword(true);
249266
boolean flag = Arrays.equals(password0, password1);
250267
StringUtils.clear(password1);
@@ -679,12 +696,12 @@ public static JSONObject printTransactionToJSON(Transaction transaction, boolean
679696
}
680697

681698
public static JSONObject printTransactionInfoToJSON(TransactionInfo transactioninfo) {
682-
return JSONObject.parseObject(JsonFormat.printToString(transactioninfo, true));
699+
return JSON.parseObject(JsonFormat.printToString(transactioninfo, true));
683700
}
684701

685702
public static boolean confirmEncrption() {
686703
System.out.println(
687-
"Please confirm encryption module,if input y or Y means default Eckey, other means SM2.");
704+
"Please confirm encryption module,if input " + greenBoldHighlight("y/Y") + " means default Eckey, other means SM2.");
688705
Scanner in = new Scanner(System.in);
689706
String input = in.nextLine().trim();
690707
String str = input.split("\\s+")[0];
@@ -710,4 +727,70 @@ public static boolean isHexString(String str) {
710727
}
711728
return bRet;
712729
}
730+
731+
public static String yellowBoldHighlight(String str) {
732+
return ANSI_BOLD + ANSI_YELLOW + str + ANSI_RESET;
733+
}
734+
735+
public static String greenHighlight(String str) {
736+
return ANSI_GREEN + str + ANSI_RESET;
737+
}
738+
739+
public static String greenBoldHighlight(String str) {
740+
return ANSI_BOLD + ANSI_GREEN + str + ANSI_RESET;
741+
}
742+
743+
public static String greenBoldHighlight(int i) {
744+
return ANSI_BOLD + ANSI_GREEN + i + ANSI_RESET;
745+
}
746+
747+
public static String blueBoldHighlight(String str) {
748+
return ANSI_BOLD + ANSI_BLUE + str + ANSI_RESET;
749+
}
750+
751+
public static String redBoldHighlight(String str) {
752+
return ANSI_BOLD + ANSI_RED + str + ANSI_RESET;
753+
}
754+
755+
public static String successfulHighlight() {
756+
return ANSI_BOLD + ANSI_GREEN + " successful" + ANSI_RESET;
757+
}
758+
759+
public static String failedHighlight() {
760+
return ANSI_BOLD + ANSI_RED + " failed" + ANSI_RESET;
761+
}
762+
763+
public static long getLong(String str) {
764+
if (isEmpty(str)) {
765+
return 300;
766+
}
767+
try {
768+
return Long.parseLong(str);
769+
} catch (NumberFormatException e) {
770+
throw new IllegalArgumentException("The parameter is invalid. Please enter an integer.");
771+
}
772+
}
773+
774+
public static void printBanner() {
775+
try (InputStream inputStream = Client.class.getResourceAsStream("/banner.txt")) {
776+
if (inputStream != null) {
777+
String banner = new String(readAllBytes(inputStream), StandardCharsets.UTF_8);
778+
System.out.println(banner);
779+
} else {
780+
System.out.println("No banner.txt found!");
781+
}
782+
} catch (IOException e) {
783+
System.err.println("Failed to load banner: " + e.getMessage());
784+
}
785+
}
786+
787+
private static byte[] readAllBytes(InputStream inputStream) throws IOException {
788+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
789+
byte[] data = new byte[4096];
790+
int bytesRead;
791+
while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
792+
buffer.write(data, 0, bytesRead);
793+
}
794+
return buffer.toByteArray();
795+
}
713796
}

src/main/java/org/tron/core/config/Configuration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public static Config getByPath(final String configurationPath) {
5151
if(configFile.exists()){
5252
try {
5353
config = ConfigFactory.parseReader(new InputStreamReader(new FileInputStream(configurationPath)));
54-
System.out.println("use user defined config file in current dir");
54+
System.out.println("Use user defined config file in current dir");
5555
} catch (FileNotFoundException e) {
56-
System.out.println("load user defined config file exception: " + e.getMessage());
56+
System.out.println("Load user defined config file exception: " + e.getMessage());
5757
}
5858
}else {
5959
config = ConfigFactory.load(configurationPath);
60-
System.out.println("user defined config file doesn't exists, use default config file in jar");
60+
System.out.println("User defined config file doesn't exists, use default config file in jar");
6161
}
6262
}
6363
return config;

src/main/java/org/tron/core/zen/ShieldedTRC20Wrapper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.tron.core.zen;
22

3+
import static org.tron.common.utils.Utils.failedHighlight;
4+
import static org.tron.common.utils.Utils.greenBoldHighlight;
5+
36
import com.google.protobuf.ByteString;
47
import com.typesafe.config.Config;
58
import io.netty.util.internal.StringUtil;
@@ -695,7 +698,7 @@ private byte[] loadSkey() throws IOException, CipherException {
695698
}
696699
}
697700
if (passwd == null) {
698-
System.out.println("Load skey failed, you can not use operation for shieldedTRC20 "
701+
System.out.println("Load skey " + failedHighlight() + ", you can not use operation for shieldedTRC20 "
699702
+ "transaction.");
700703
return null;
701704
}
@@ -708,7 +711,7 @@ private byte[] generateSkey() throws IOException, CipherException {
708711
new SecureRandom().nextBytes(skey);
709712

710713
System.out.println("ShieldedTRC20 wallet does not exist, will build it.");
711-
char[] password = Utils.inputPassword2Twice();
714+
char[] password = Utils.inputPassword2Twice(false);
712715
byte[] passwd = StringUtils.char2Byte(password);
713716

714717
SKeyCapsule sKeyCapsule = SKeyEncryptor.createStandard(passwd, skey);
@@ -765,7 +768,8 @@ public ShieldedAddressInfo backupShieldedTRC20Wallet() throws IOException,
765768
if (shieldedAddressInfoList.size() == 1) {
766769
return shieldedAddressInfoList.get(0);
767770
} else {
768-
System.out.println("Please choose between 1 and " + shieldedAddressInfoList.size());
771+
System.out.println("Please choose between " + greenBoldHighlight(1) + " and "
772+
+ greenBoldHighlight(shieldedAddressInfoList.size()));
769773
Scanner in = new Scanner(System.in);
770774
while (true) {
771775
String input = in.nextLine().trim();

src/main/java/org/tron/core/zen/ShieldedWrapper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.tron.core.zen;
22

3+
import static org.tron.common.utils.Utils.failedHighlight;
4+
import static org.tron.common.utils.Utils.greenBoldHighlight;
5+
36
import com.google.protobuf.ByteString;
47
import io.netty.util.internal.StringUtil;
58
import lombok.Getter;
@@ -556,7 +559,7 @@ private byte[] loadSkey() throws IOException, CipherException {
556559
}
557560
}
558561
if (passwd == null) {
559-
System.out.println("Load skey failed, you can not use operation for shileded transaction.");
562+
System.out.println("Load skey " + failedHighlight() + ", you can not use operation for shileded transaction.");
560563
return null;
561564
}
562565
return SKeyEncryptor.decrypt2PrivateBytes(passwd, skey);
@@ -568,7 +571,7 @@ private byte[] generateSkey() throws IOException, CipherException {
568571
new SecureRandom().nextBytes(skey);
569572

570573
System.out.println("Shielded wallet does not exist, will build it.");
571-
char[] password = Utils.inputPassword2Twice();
574+
char[] password = Utils.inputPassword2Twice(false);
572575
byte[] passwd = StringUtils.char2Byte(password);
573576

574577
SKeyCapsule sKeyCapsule = SKeyEncryptor.createStandard(passwd, skey);
@@ -625,7 +628,8 @@ public ShieldedAddressInfo backupShieldedWallet() throws IOException, CipherExce
625628
if (shieldedAddressInfoList.size() == 1) {
626629
return shieldedAddressInfoList.get(0);
627630
} else {
628-
System.out.println("Please choose between 1 and " + shieldedAddressInfoList.size());
631+
System.out.println("Please choose between " + greenBoldHighlight(1) + " and "
632+
+ greenBoldHighlight(shieldedAddressInfoList.size()));
629633
Scanner in = new Scanner(System.in);
630634
while (true) {
631635
String input = in.nextLine().trim();

src/main/java/org/tron/demo/ShieldedTRC20Demo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.tron.demo;
22

3+
import static org.tron.common.utils.Utils.failedHighlight;
4+
35
import com.google.protobuf.ByteString;
46
import java.math.BigInteger;
57
import java.security.SecureRandom;
@@ -328,7 +330,7 @@ private String triggerContract(byte[] contractAddress, String method, String arg
328330

329331
TransactionExtention transactionExtention = grpcClient.triggerContract(triggerContract);
330332
if (transactionExtention == null || !transactionExtention.getResult().getResult()) {
331-
System.out.println("RPC create call trx failed!");
333+
System.out.println("RPC create call trx " + failedHighlight() + "!");
332334
System.out.println("Code = " + transactionExtention.getResult().getCode());
333335
System.out
334336
.println("Message = " + transactionExtention.getResult().getMessage().toStringUtf8());

src/main/java/org/tron/keystore/ClearWalletUtils.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package org.tron.keystore;
22

3+
import static org.apache.commons.lang3.StringUtils.EMPTY;
4+
import static org.apache.commons.lang3.StringUtils.isEmpty;
5+
import static org.tron.common.utils.Utils.failedHighlight;
6+
import static org.tron.common.utils.Utils.greenBoldHighlight;
7+
import static org.tron.common.utils.Utils.redBoldHighlight;
8+
import static org.tron.common.utils.Utils.successfulHighlight;
9+
310
import org.jline.reader.LineReader;
411
import org.jline.reader.LineReaderBuilder;
512
import org.jline.terminal.Terminal;
@@ -22,24 +29,30 @@ public static boolean confirmAndDeleteWallet(String address, Collection<String>
2229
LineReader lineReader = LineReaderBuilder.builder().terminal(terminal).build();
2330

2431
System.out.println("\n\u001B[31mWarning: Dangerous operation!\u001B[0m");
25-
System.out.println("This operation will permanently delete the Wallet&Mnemonic files of the Address: " + address);
32+
System.out.println("This operation will permanently delete the Wallet&Mnemonic files "+ (isEmpty(address) ? EMPTY : "of the Address: " + address));
2633
System.out.println("\u001B[31mWarning: The private key and mnemonic words will be permanently lost and cannot be recovered!\u001B[0m");
2734

2835
int attempts = 0;
2936
while (attempts < MAX_ATTEMPTS) {
30-
String confirm = lineReader.readLine("Continue? (Y/Yes to proceed): ").trim();
37+
System.out.println("Continue? (" + greenBoldHighlight("y/Y")
38+
+ " to proceed, " + greenBoldHighlight("c/C") + " to cancel): ");
39+
String confirm = lineReader.readLine("").trim();
40+
if ("c".equalsIgnoreCase(confirm)) {
41+
System.out.println("Your operation has been canceled.");
42+
return false;
43+
}
3144
if (isConfirmed(confirm)) {
3245
break;
3346
}
3447
if (++attempts == MAX_ATTEMPTS) {
3548
System.out.println("Maximum retry attempts reached, operation canceled.");
3649
return false;
3750
}
38-
System.out.println("Invalid input, please enter Y or Yes to confirm.");
51+
System.out.println("Invalid input, please enter " + greenBoldHighlight("y/Y") + " to confirm.");
3952
}
4053

4154
System.out.println("\nFinal confirmation:");
42-
System.out.println("Please enter: '" + CONFIRMATION_WORD + "' To confirm the delete operation:");
55+
System.out.println("Please enter: '" + redBoldHighlight(CONFIRMATION_WORD) + "' to confirm the delete operation:");
4356

4457
attempts = 0;
4558
while (attempts < MAX_ATTEMPTS) {
@@ -51,7 +64,7 @@ public static boolean confirmAndDeleteWallet(String address, Collection<String>
5164
System.out.println("Maximum retry attempts reached, operation canceled.");
5265
return false;
5366
}
54-
System.out.println("Input does not match, Please enter: 'DELETE' To confirm the delete operation.");
67+
System.out.println("Input does not match, Please enter: '" + redBoldHighlight(CONFIRMATION_WORD) + "' to confirm the delete operation.");
5568
}
5669

5770
return deleteFiles(filePaths);
@@ -62,17 +75,15 @@ public static boolean confirmAndDeleteWallet(String address, Collection<String>
6275
}
6376

6477
private static boolean isConfirmed(String input) {
65-
return input.equalsIgnoreCase("Y") || input.equalsIgnoreCase("YES");
78+
return input.equalsIgnoreCase("y") || input.equalsIgnoreCase("Y");
6679
}
6780

6881
private static final String BACKUP_SUFFIX = ".bak";
6982

7083
public static boolean deleteFiles(Collection<String> filePaths) {
7184
if (filePaths == null || filePaths.isEmpty()) {
72-
System.err.println("No files specified for deletion");
73-
return false;
85+
return true;
7486
}
75-
7687
List<PathPair> pathPairs = new ArrayList<>();
7788
for (String path : filePaths) {
7889
pathPairs.add(new PathPair(path));
@@ -185,12 +196,12 @@ private static void rollback(List<PathPair> pairs) throws Exception {
185196
}
186197

187198
private static void printSuccess(List<PathPair> pairs) {
188-
System.out.println("\nFile deleted successfully:");
199+
System.out.println("\nDelete File " + successfulHighlight() + ":");
189200
pairs.forEach(pair -> System.out.println("- " + pair.original));
190201
}
191202

192203
private static void printBackupLocations(List<PathPair> pairs) {
193-
System.err.println("\nRecovery failed, backup file is located at:");
204+
System.err.println("\nRecovery " + failedHighlight() + ", backup file is located at:");
194205
pairs.forEach(pair -> {
195206
if (pair.backupCreated) {
196207
System.err.println("- " + pair.backup);

src/main/java/org/tron/keystore/Wallet.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.tron.common.crypto.ECKey;
88
import org.tron.common.crypto.Hash;
99
import org.tron.common.crypto.SignInterface;
10-
import org.tron.common.crypto.SignatureInterface;
1110
import org.tron.common.crypto.sm2.SM2;
1211
import org.tron.common.utils.ByteArray;
1312
import org.tron.core.exception.CipherException;

0 commit comments

Comments
 (0)