11package org .tron .core .manager ;
22
3+ import static org .apache .commons .lang3 .StringUtils .EMPTY ;
34import static org .tron .common .utils .ByteUtil .hexStringToIntegerList ;
45import static org .tron .common .utils .ByteUtil .integerListToHexString ;
56import static org .tron .common .utils .Utils .greenBoldHighlight ;
1920import java .util .Scanner ;
2021import java .util .Set ;
2122import java .util .stream .Collectors ;
23+ import java .util .stream .IntStream ;
2224import lombok .Getter ;
2325import lombok .Setter ;
2426import org .bouncycastle .util .encoders .Hex ;
@@ -38,8 +40,8 @@ public class UpdateAccountPermissionInteractive {
3840 );
3941 private static final List <Integer > AVAILABLE_OPERATIONS = Arrays .asList (
4042 0 , 1 , 2 , 4 , 5 , 6 , 8 , 9 , 10 , 11 , 12 , 13 ,
41- 14 , 15 , 16 , 17 , 18 , 30 , 31 , 33 ,
42- 41 , 42 , 43 , 44 , 45 , 46 , 48 , 49 , 54 ,
43+ 14 , 15 , 16 , 17 , 18 , 19 , 30 , 31 , 33 ,
44+ 41 , 42 , 43 , 44 , 45 , 46 , 48 , 49 , 52 , 53 , 54 ,
4345 55 , 56 , 57 , 58 , 59
4446 );
4547 private static final Map <String , String > operationsMap = new HashMap <>();
@@ -62,6 +64,7 @@ public class UpdateAccountPermissionInteractive {
6264 operationsMap .put ("16" , "Create Proposal" );
6365 operationsMap .put ("17" , "Approve Proposal" );
6466 operationsMap .put ("18" , "Cancel Proposal" );
67+ operationsMap .put ("19" , "Set Account Id" );
6568 operationsMap .put ("30" , "Create Smart Contract" );
6669 operationsMap .put ("31" , "Trigger Smart Contract" );
6770 operationsMap .put ("33" , "Update Contract Parameters" );
@@ -73,6 +76,8 @@ public class UpdateAccountPermissionInteractive {
7376 operationsMap .put ("46" , "Update Account Permissions" );
7477 operationsMap .put ("48" , "Clear Contract ABI" );
7578 operationsMap .put ("49" , "Update SR Commission Ratio" );
79+ operationsMap .put ("52" , "Market Sell Asset" );
80+ operationsMap .put ("53" , "Market Cancel Order" );
7681 operationsMap .put ("54" , "TRX Stake (2.0)" );
7782 operationsMap .put ("55" , "TRX Unstake (2.0)" );
7883 operationsMap .put ("56" , "Withdraw Unstaked TRX" );
@@ -124,7 +129,11 @@ public String start(String address) {
124129 deleteActivePermission ();
125130 break ;
126131 case "6" :
127- return showFinalSummary ();
132+ String result = showFinalSummary ();
133+ if (!result .isEmpty ()) {
134+ return result ;
135+ }
136+ break ;
128137 case "7" :
129138 System .out .println ("Exiting interactive mode." );
130139 throw new IllegalArgumentException ("Already exited interactive mode." );
@@ -220,6 +229,10 @@ private void editKeys(Permission permission) {
220229 System .out .println ("Each active permission can only add 5 addresses at most." );
221230 continue ;
222231 }
232+ if (permission .getType () == 1 ) {
233+ System .out .println ("Witness permission's key count should be 1, adding keys is not allowed" );
234+ continue ;
235+ }
223236 System .out .print ("Enter key(Authorized To) address (or 'q' to cancel): " );
224237 String addr = scanner .nextLine ().trim ();
225238 if ("q" .equalsIgnoreCase (addr )) continue ;
@@ -248,7 +261,16 @@ private void editKeys(Permission permission) {
248261 continue ;
249262 }
250263
264+ long totalWeight = permission .getKeys ().stream ()
265+ .mapToLong (Key ::getWeight )
266+ .sum ();
267+ if (totalWeight + weight < permission .getThreshold ()) {
268+ System .out .println (redBoldHighlight ("The sum of address weights must be greater than or equal to the threshold" ));
269+ continue ;
270+ }
271+
251272 permission .getKeys ().add (new Key (addr , weight ));
273+
252274 System .out .println ("Added key: " + addr + " (weight=" + weight + ")" );
253275
254276 break ;
@@ -288,8 +310,17 @@ private void editKeys(Permission permission) {
288310 try {
289311 long w = Long .parseLong (newWeight );
290312 long threshold = permission .getThreshold ();
313+ List <Key > keys = permission .getKeys ();
314+ long remainWeight = IntStream .range (0 , keys .size ())
315+ .filter (i -> i != midx )
316+ .mapToLong (i -> keys .get (i ).getWeight ())
317+ .sum ();
318+ if (remainWeight + w < permission .getThreshold ()) {
319+ System .out .println (redBoldHighlight ("The sum of address weights must be greater than or equal to the threshold" ));
320+ continue ;
321+ }
291322 if (w <= 0 || w > threshold ) {
292- System .out .println ("Weight must be >0 and <= threshold(" + threshold + "). Skip changing weight." );
323+ System .out .println ("Weight must be > 0 and <= threshold(" + threshold + "). Skip changing weight." );
293324 } else {
294325 keyToEdit .weight = w ;
295326 }
@@ -305,6 +336,10 @@ private void editKeys(Permission permission) {
305336
306337 break ;
307338 case "3" :
339+ if (permission .getType () == 1 ) {
340+ System .out .println ("Witness permission's key count should be 1, deleting keys is not allowed" );
341+ continue ;
342+ }
308343 if (permission .getKeys ().isEmpty ()) {
309344 System .out .println ("No keys(Authorized To) to delete." );
310345 continue ;
@@ -627,6 +662,10 @@ private void deleteActivePermission() {
627662 printPermissionSummary (actives .get (i ), i + 1 );
628663 System .out .println ("---------------------------------------------------" );
629664 }
665+ if (actives .size () == 1 ) {
666+ System .out .println (redBoldHighlight ("Currently, there is only one active permission, and deletion is not allowed." ));
667+ return ;
668+ }
630669 System .out .print ("Enter index to delete, Enter " + greenBoldHighlight ("b" ) + " to back: " );
631670 String idxStr = scanner .nextLine ().trim ();
632671 if ("b" .equalsIgnoreCase (idxStr ) || "q" .equalsIgnoreCase (idxStr )) {
@@ -674,13 +713,27 @@ private void printPermissionSummary(Permission p, int index) {
674713 }
675714
676715 private String showFinalSummary () {
677- System .out .println ("The preview of your updated account permissions is as follows:" );
678- printPermissionData (data );
679- return JSON .toJSONString (data .toTronJson ());
716+ while (true ) {
717+ printPermissionData (data );
718+
719+ System .out .print ("\n Do you want to proceed with these changes? (Enter " + greenBoldHighlight ("y/n" ) + ", n = return to edit): " );
720+ String input = scanner .nextLine ().trim ().toLowerCase ();
721+
722+ switch (input ) {
723+ case "y" :
724+ System .out .println ("Confirmed. Preparing final JSON..." );
725+ return JSON .toJSONString (data .toTronJson ());
726+ case "n" :
727+ System .out .println ("Returning to main menu for further edits..." );
728+ return EMPTY ;
729+ default :
730+ System .out .println ("Invalid input. Please enter" + greenBoldHighlight ("y/n" ));
731+ }
732+ }
680733 }
681734
682735 private void printPermissionData (PermissionData data ) {
683- System .out .println ("\n ================ Account Permission Info ================ " );
736+ System .out .println ("\n =============== Preview of Updated Account Permissions ===============\n " );
684737
685738 // --- Owner Permission ---
686739 Permission owner = data .getOwnerPermission ();
@@ -711,7 +764,7 @@ private void printPermissionData(PermissionData data) {
711764 printPermissionDetail (actives .get (i ));
712765 }
713766 }
714- System .out .println ("=========================================================" );
767+ System .out .println ("======================================================================= " );
715768 }
716769
717770 private void printPermissionDetail (Permission p ) {
0 commit comments