Skip to content

Commit

Permalink
Check private view key is a valid scalar.
Browse files Browse the repository at this point in the history
  • Loading branch information
yonson2023 committed Mar 26, 2023
1 parent e2b25c1 commit 165f2d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ public String getSubaddressBase58(String privateViewKeyHex, long accountId, long
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,9 @@ public void testWalletAddress() throws WalletAddress.InvalidWalletAddressExcepti
"0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF",
"0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF"),
false);

String nonReducedPrivateKey = "42594aba0e809490dec97b2dfaf64f7ae5bef1c2d19af636eb84544773df5b5f";
assertEquals(WalletAddress.isPrivateKeyValid(nonReducedPrivateKey), false);
assertEquals(WalletAddress.isPrivateKeyValid(privateViewKeyHex), true);
}
}

0 comments on commit 165f2d0

Please sign in to comment.