Check private view key is a valid scalar.

This commit is contained in:
yonson2023 2023-03-26 18:36:12 -05:00
parent e2b25c12e1
commit 165f2d000a
No known key found for this signature in database
GPG Key ID: 653F31AF4087E2F2
2 changed files with 11 additions and 1 deletions

View File

@ -131,7 +131,13 @@ public class WalletAddress {
}
public boolean checkPrivateViewKey(String privateViewKey) {
return arePubPrivKeysRelated(this.publicViewKeyHex, privateViewKey);
return isPrivateKeyValid(privateViewKey) && arePubPrivKeysRelated(this.publicViewKeyHex, privateViewKey);
}
public static boolean isPrivateKeyValid(String privateKey) {
byte[] input = hexToBytes(privateKey);
byte[] reduced = CryptoUtil.scReduce32(input);
return Arrays.equals(input, reduced);
}
public static boolean arePubPrivKeysRelated(String publicKey, String privateKey) {

View File

@ -66,5 +66,9 @@ public class WalletAddressTest {
"0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF",
"0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF"),
false);
String nonReducedPrivateKey = "42594aba0e809490dec97b2dfaf64f7ae5bef1c2d19af636eb84544773df5b5f";
assertEquals(WalletAddress.isPrivateKeyValid(nonReducedPrivateKey), false);
assertEquals(WalletAddress.isPrivateKeyValid(privateViewKeyHex), true);
}
}