Skip to content

Commit 4ada90e

Browse files
luraismorgan.peng
andauthored
feat(sign): optimize sign exception log (#5446)
Co-authored-by: morgan.peng <morgan.p@qq.com>
1 parent 1536bc9 commit 4ada90e

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

crypto/src/main/java/org/tron/common/crypto/SignUtils.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ public static SignInterface fromPrivate(byte[] privKeyBytes, boolean isECKeyCryp
2626
public static byte[] signatureToAddress(
2727
byte[] messageHash, String signatureBase64, boolean isECKeyCryptoEngine)
2828
throws SignatureException {
29-
if (isECKeyCryptoEngine) {
30-
return ECKey.signatureToAddress(messageHash, signatureBase64);
29+
try {
30+
if (isECKeyCryptoEngine) {
31+
return ECKey.signatureToAddress(messageHash, signatureBase64);
32+
}
33+
return SM2.signatureToAddress(messageHash, signatureBase64);
34+
} catch (Exception e) {
35+
throw new SignatureException(e);
3136
}
32-
return SM2.signatureToAddress(messageHash, signatureBase64);
3337
}
3438

3539
public static SignatureInterface fromComponents(

framework/src/test/java/org/tron/common/crypto/BouncyCastleTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Arrays;
99
import org.bouncycastle.crypto.digests.SM3Digest;
1010
import org.bouncycastle.util.encoders.Hex;
11+
import org.junit.Assert;
1112
import org.junit.Test;
1213
import org.tron.common.crypto.sm2.SM2;
1314
import org.tron.common.utils.Sha256Hash;
@@ -131,4 +132,21 @@ public void testSM2SpongySignature() throws SignatureException {
131132
byte[] address = SignUtils.signatureToAddress(hash, spongySig, false);
132133
assertEquals(spongyAddress, Hex.toHexString(Arrays.copyOfRange(address, 1, 21)));
133134
}
135+
136+
@Test
137+
public void testSignToAddress() {
138+
String messageHash = "818e0e76976123b9b78b6076cc2b5d53e61b49ff9cf78304de688a860ce7cb95";
139+
String base64Sign = "G1y76mVO6TRpFwp3qOiLVzHA8uFsrDiOL7hbC2uN9qTHHiLypaW4vnQkfkoUygjo5qBd"
140+
+ "+NlYQ/mAPVWKu6K00co=";
141+
try {
142+
SignUtils.signatureToAddress(Hex.decode(messageHash), base64Sign, Boolean.TRUE);
143+
} catch (Exception e) {
144+
Assert.assertTrue(e instanceof SignatureException);
145+
}
146+
try {
147+
SignUtils.signatureToAddress(Hex.decode(messageHash), base64Sign, Boolean.FALSE);
148+
} catch (Exception e) {
149+
Assert.assertTrue(e instanceof SignatureException);
150+
}
151+
}
134152
}

0 commit comments

Comments
 (0)