1818
1919package 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+
2130import com .alibaba .fastjson .JSON ;
2231import com .alibaba .fastjson .JSONArray ;
2332import com .alibaba .fastjson .JSONObject ;
2433import com .google .protobuf .Any ;
2534import com .google .protobuf .InvalidProtocolBufferException ;
2635import com .google .protobuf .Message ;
2736
37+ import java .io .ByteArrayOutputStream ;
2838import java .io .Console ;
2939import java .io .IOException ;
40+ import java .io .InputStream ;
3041import java .nio .ByteBuffer ;
3142import java .nio .CharBuffer ;
3243import java .nio .charset .Charset ;
44+ import java .nio .charset .StandardCharsets ;
3345import java .security .SecureRandom ;
3446import java .text .ParsePosition ;
3547import java .text .SimpleDateFormat ;
4355import org .tron .common .crypto .Sha256Sm3Hash ;
4456import org .tron .keystore .StringUtils ;
4557import org .tron .protos .contract .BalanceContract ;
58+ import org .tron .walletcli .Client ;
4659import org .tron .walletserver .WalletApi ;
4760import org .tron .protos .Protocol .Block ;
4861import 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}
0 commit comments