@@ -200,10 +200,6 @@ private void editPermission(Permission permission, String name) {
200200 editKeys (permission );
201201 }
202202
203- /**
204- * editKeys: 支持 add/modify/delete,包含 addressValid 与 weight 校验,
205- * 修改和添加时都可用 'q' 退出回上一层。
206- */
207203 private void editKeys (Permission permission ) {
208204 if (permission == null ) return ;
209205 while (true ) {
@@ -261,16 +257,7 @@ private void editKeys(Permission permission) {
261257 continue ;
262258 }
263259
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-
272260 permission .getKeys ().add (new Key (addr , weight ));
273-
274261 System .out .println ("Added key: " + addr + " (weight=" + weight + ")" );
275262
276263 break ;
@@ -310,15 +297,6 @@ private void editKeys(Permission permission) {
310297 try {
311298 long w = Long .parseLong (newWeight );
312299 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- }
322300 if (w <= 0 || w > threshold ) {
323301 System .out .println ("Weight must be > 0 and <= threshold(" + threshold + "). Skip changing weight." );
324302 } else {
@@ -721,6 +699,9 @@ private String showFinalSummary() {
721699
722700 switch (input ) {
723701 case "y" :
702+ if (!validateAllPermissionWeights (data )) {
703+ return EMPTY ;
704+ }
724705 System .out .println ("Confirmed. Preparing final JSON..." );
725706 return JSON .toJSONString (data .toTronJson ());
726707 case "n" :
@@ -732,6 +713,35 @@ private String showFinalSummary() {
732713 }
733714 }
734715
716+ private boolean validateAllPermissionWeights (PermissionData data ) {
717+ // valid owner permission
718+ if (!validateSinglePermission (data .getOwnerPermission ())) {
719+ System .out .println (redBoldHighlight ("Owner permission key weight sum must >= threshold!" ));
720+ return false ;
721+ }
722+
723+ // valid active permissions
724+ for (Permission p : data .getActivePermissions ()) {
725+ if (!validateSinglePermission (p )) {
726+ System .out .println (redBoldHighlight ("Active permission '" + p .getPermissionName ()
727+ + "' key weight sum must >= threshold!" ));
728+ return false ;
729+ }
730+ }
731+
732+ return true ;
733+ }
734+
735+ private boolean validateSinglePermission (Permission p ) {
736+ if (p == null ) return false ;
737+ int totalWeight = 0 ;
738+ for (Key k : p .getKeys ()) {
739+ totalWeight += k .getWeight ();
740+ }
741+ return totalWeight >= p .getThreshold ();
742+ }
743+
744+
735745 private void printPermissionData (PermissionData data ) {
736746 System .out .println ("\n =============== Preview of Updated Account Permissions ===============\n " );
737747
0 commit comments