From 3e639b0b764d9cf7e74705e4728a1b246712d7ca Mon Sep 17 00:00:00 2001 From: federico Date: Sat, 20 Sep 2025 14:12:18 +0800 Subject: [PATCH 1/2] replace the Exception with TronError --- .../org/tron/common/utils/LocalWitnesses.java | 12 +-- .../core/config/args/LocalWitnessTest.java | 83 ++++++------------- 2 files changed, 32 insertions(+), 63 deletions(-) diff --git a/chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java b/chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java index 812e4d7fa5..7179045ea7 100644 --- a/chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java +++ b/chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java @@ -25,6 +25,7 @@ import org.tron.common.crypto.SignInterface; import org.tron.common.crypto.SignUtils; import org.tron.core.config.Parameter.ChainConstant; +import org.tron.core.exception.TronError; @Slf4j(topic = "app") public class LocalWitnesses { @@ -78,13 +79,14 @@ private void validate(String privateKey) { if (StringUtils.isBlank(privateKey) || privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) { - throw new IllegalArgumentException( - String.format("private key must be %d hex string, actual: %d", - ChainConstant.PRIVATE_KEY_LENGTH, - StringUtils.isBlank(privateKey) ? 0 : privateKey.length())); + throw new TronError(String.format("private key must be %d hex string, actual: %d", + ChainConstant.PRIVATE_KEY_LENGTH, + StringUtils.isBlank(privateKey) ? 0 : privateKey.length()), + TronError.ErrCode.WITNESS_INIT); } if (!StringUtil.isHexadecimal(privateKey)) { - throw new IllegalArgumentException("private key must be hex string"); + throw new TronError("private key must be hex string", + TronError.ErrCode.WITNESS_INIT); } } diff --git a/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java b/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java index 8b9da2c7bc..cb4c07d756 100644 --- a/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java +++ b/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java @@ -15,6 +15,9 @@ package org.tron.core.config.args; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.common.collect.Lists; @@ -28,6 +31,8 @@ import org.tron.common.utils.PublicMethod; import org.tron.common.utils.StringUtil; import org.tron.core.Constant; +import org.tron.core.exception.TronError; +import org.tron.core.exception.TronError.ErrCode; public class LocalWitnessTest { @@ -42,7 +47,7 @@ public void setLocalWitness() { localWitness .setPrivateKeys( Lists.newArrayList( - PRIVATE_KEY)); + PRIVATE_KEY)); } @Test @@ -52,13 +57,13 @@ public void whenSetNullPrivateKey() { Assert.assertNotNull(localWitness.getPublicKey()); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = TronError.class) public void whenSetEmptyPrivateKey() { localWitness.setPrivateKeys(Lists.newArrayList("")); fail("private key must be 64-bits hex string"); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = TronError.class) public void whenSetBadFormatPrivateKey() { localWitness.setPrivateKeys(Lists.newArrayList("a111")); fail("private key must be 64-bits hex string"); @@ -104,62 +109,24 @@ public void testValidPrivateKeyWithPrefix() { @Test public void testInvalidPrivateKey() { LocalWitnesses localWitnesses = new LocalWitnesses(); + String expectedMessage = "private key must be 64 hex string"; + assertTronError(localWitnesses, null, expectedMessage); + assertTronError(localWitnesses, "", expectedMessage); + assertTronError(localWitnesses, " ", expectedMessage); + assertTronError(localWitnesses, "11111", expectedMessage); + String expectedMessage2 = "private key must be hex string"; + final String privateKey = "11111111111111111111111111111111111111111111111111111111111111 "; + assertTronError(localWitnesses, privateKey, expectedMessage2); + final String privateKey2 = "xy11111111111111111111111111111111111111111111111111111111111111"; + assertTronError(localWitnesses, privateKey2, expectedMessage2); + } - try { - localWitnesses.addPrivateKeys(null); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string")); - } catch (Exception e) { - fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName()); - } - - try { - localWitnesses.addPrivateKeys(""); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string")); - } catch (Exception e) { - fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName()); - } - - try { - localWitnesses.addPrivateKeys(" "); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string")); - } catch (Exception e) { - fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName()); - } - - try { - localWitnesses.addPrivateKeys("11111"); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string")); - } catch (Exception e) { - fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName()); - } - - try { - String privateKey = "11111111111111111111111111111111111111111111111111111111111111 "; - localWitnesses.addPrivateKeys(privateKey); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - Assert.assertTrue(e.getMessage().contains("private key must be hex string")); - } catch (Exception e) { - fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName()); - } - - try { - String privateKey = "xy11111111111111111111111111111111111111111111111111111111111111"; - localWitnesses.addPrivateKeys(privateKey); - fail("should throw IllegalArgumentException"); - } catch (IllegalArgumentException e) { - Assert.assertTrue(e.getMessage().contains("private key must be hex string")); - } catch (Exception e) { - fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName()); - } + private void assertTronError(LocalWitnesses localWitnesses, String privateKey, + String expectedMessage) { + TronError thrown = assertThrows(TronError.class, + () -> localWitnesses.addPrivateKeys(privateKey)); + assertEquals(ErrCode.WITNESS_INIT, thrown.getErrCode()); + assertTrue(thrown.getMessage().contains(expectedMessage)); } @Test From 2e6d038a5bd723779d163cfd05016cb9394faa29 Mon Sep 17 00:00:00 2001 From: federico Date: Thu, 25 Sep 2025 16:55:03 +0800 Subject: [PATCH 2/2] remove plaintext key --- .../java/org/tron/core/config/args/LocalWitnessTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java b/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java index cb4c07d756..15d64f3e07 100644 --- a/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java +++ b/framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java @@ -22,11 +22,13 @@ import com.google.common.collect.Lists; import java.io.IOException; +import java.security.SecureRandom; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.tron.common.utils.ByteArray; import org.tron.common.utils.LocalWitnesses; import org.tron.common.utils.PublicMethod; import org.tron.common.utils.StringUtil; @@ -115,9 +117,12 @@ public void testInvalidPrivateKey() { assertTronError(localWitnesses, " ", expectedMessage); assertTronError(localWitnesses, "11111", expectedMessage); String expectedMessage2 = "private key must be hex string"; - final String privateKey = "11111111111111111111111111111111111111111111111111111111111111 "; + SecureRandom secureRandom = new SecureRandom(); + byte[] keyBytes = new byte[31]; + secureRandom.nextBytes(keyBytes); + final String privateKey = ByteArray.toHexString(keyBytes) + " "; assertTronError(localWitnesses, privateKey, expectedMessage2); - final String privateKey2 = "xy11111111111111111111111111111111111111111111111111111111111111"; + final String privateKey2 = "xy" + ByteArray.toHexString(keyBytes); assertTronError(localWitnesses, privateKey2, expectedMessage2); }