From 0e2b13924bfbd457fde3e564a1aed0ab7b7ac949 Mon Sep 17 00:00:00 2001 From: yonson2023 <122909161+yonson2023@users.noreply.github.com> Date: Wed, 29 Mar 2023 10:37:43 -0500 Subject: [PATCH] Code review changes. --- .../knaccc/monero/address/WalletAddress.java | 13 +++++-- .../knaccc/monero/address/CryptoUtilTest.java | 2 +- .../monero/address/WalletAddressTest.java | 37 +++++++++---------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/bisq/core/xmr/knaccc/monero/address/WalletAddress.java b/core/src/main/java/bisq/core/xmr/knaccc/monero/address/WalletAddress.java index c86f12226e..c2665b17f6 100755 --- a/core/src/main/java/bisq/core/xmr/knaccc/monero/address/WalletAddress.java +++ b/core/src/main/java/bisq/core/xmr/knaccc/monero/address/WalletAddress.java @@ -7,6 +7,8 @@ import bisq.core.xmr.org.nem.core.crypto.ed25519.arithmetic.Ed25519EncodedGroupE import bisq.core.xmr.org.nem.core.crypto.ed25519.arithmetic.Ed25519Group; import bisq.core.xmr.org.nem.core.crypto.ed25519.arithmetic.Ed25519GroupElement; +import com.google.common.annotations.VisibleForTesting; + import java.util.Arrays; import static bisq.core.xmr.knaccc.monero.address.ByteUtil.concat; @@ -130,17 +132,20 @@ public class WalletAddress { return getSubaddressBase58(new Scalar(privateViewKeyHex), hexToBytes(getPublicSpendKeyHex()), accountId, subaddressId); } - public boolean checkPrivateViewKey(String privateViewKey) { - return isPrivateKeyValid(privateViewKey) && arePubPrivKeysRelated(this.publicViewKeyHex, privateViewKey); + @VisibleForTesting + boolean checkPrivateViewKey(String privateViewKey) { + return isPrivateKeyReduced(privateViewKey) && doesPrivateKeyResolveToPublicKey(privateViewKey, this.publicViewKeyHex); } - public static boolean isPrivateKeyValid(String privateKey) { + @VisibleForTesting + static boolean isPrivateKeyReduced(String privateKey) { byte[] input = hexToBytes(privateKey); byte[] reduced = CryptoUtil.scReduce32(input); return Arrays.equals(input, reduced); } - public static boolean arePubPrivKeysRelated(String publicKey, String privateKey) { + @VisibleForTesting + static boolean doesPrivateKeyResolveToPublicKey(String privateKey, String publicKey) { Scalar m = new Scalar(privateKey); Ed25519GroupElement M = G.scalarMultiply(new Ed25519EncodedFieldElement(m.bytes)); byte[] generatedPubKey = M.encode().getRaw(); diff --git a/core/src/test/java/bisq/core/xmr/knaccc/monero/address/CryptoUtilTest.java b/core/src/test/java/bisq/core/xmr/knaccc/monero/address/CryptoUtilTest.java index f1d34cd6d0..07e82d3413 100644 --- a/core/src/test/java/bisq/core/xmr/knaccc/monero/address/CryptoUtilTest.java +++ b/core/src/test/java/bisq/core/xmr/knaccc/monero/address/CryptoUtilTest.java @@ -15,7 +15,7 @@ * along with Bisq. If not, see . */ -package knaccc.monero.address; +package bisq.core.xmr.knaccc.monero.address; import bisq.core.xmr.knaccc.monero.crypto.CryptoUtil; diff --git a/core/src/test/java/bisq/core/xmr/knaccc/monero/address/WalletAddressTest.java b/core/src/test/java/bisq/core/xmr/knaccc/monero/address/WalletAddressTest.java index 196fc3335e..dd447cdc0d 100644 --- a/core/src/test/java/bisq/core/xmr/knaccc/monero/address/WalletAddressTest.java +++ b/core/src/test/java/bisq/core/xmr/knaccc/monero/address/WalletAddressTest.java @@ -15,13 +15,13 @@ * along with Bisq. If not, see . */ -package knaccc.monero.address; - -import bisq.core.xmr.knaccc.monero.address.WalletAddress; +package bisq.core.xmr.knaccc.monero.address; import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; public class WalletAddressTest { @Test @@ -48,27 +48,24 @@ public class WalletAddressTest { assertEquals(walletAddress.getSubaddressBase58(privateViewKeyHex, 1, 0), addr10); assertEquals(walletAddress.getSubaddressBase58(privateViewKeyHex, 1, 1), addr11); - assertEquals(walletAddress.checkPrivateViewKey(privateViewKeyHex), true); - assertEquals(WalletAddress.arePubPrivKeysRelated(publicViewKeyHex, privateViewKeyHex), true); - assertEquals(WalletAddress.arePubPrivKeysRelated(privateViewKeyHex, privateViewKeyHex), false); + assertTrue(walletAddress.checkPrivateViewKey(privateViewKeyHex)); + assertTrue(WalletAddress.doesPrivateKeyResolveToPublicKey(privateViewKeyHex, publicViewKeyHex)); + assertFalse(WalletAddress.doesPrivateKeyResolveToPublicKey(privateViewKeyHex, privateViewKeyHex)); - assertEquals(WalletAddress.arePubPrivKeysRelated( - "bdc158199c8933353627d54edb4bbae547dbbde3130860d7940313210edca0a6", - "a82a9017a1d259c71f5392ad9091b743b86dac7a21f5e402ea0a55e5c8a6750f"), - true); + assertTrue(WalletAddress.doesPrivateKeyResolveToPublicKey( + "a82a9017a1d259c71f5392ad9091b743b86dac7a21f5e402ea0a55e5c8a6750f", + "bdc158199c8933353627d54edb4bbae547dbbde3130860d7940313210edca0a6")); - assertEquals(WalletAddress.arePubPrivKeysRelated( - "d17698d07fe9edbc41552299b90a93de73bb1bd4b94b8083af0bbe3a1931e2ec", - "dae1bceeb2563b8c376f8e0456e5fe7aa3d6291b38ace18c6ad5647424a3b104"), - true); + assertTrue(WalletAddress.doesPrivateKeyResolveToPublicKey( + "dae1bceeb2563b8c376f8e0456e5fe7aa3d6291b38ace18c6ad5647424a3b104", + "d17698d07fe9edbc41552299b90a93de73bb1bd4b94b8083af0bbe3a1931e2ec")); - assertEquals(WalletAddress.arePubPrivKeysRelated( + assertFalse(WalletAddress.doesPrivateKeyResolveToPublicKey( "0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF", - "0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF"), - false); + "0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF")); - String nonReducedPrivateKey = "42594aba0e809490dec97b2dfaf64f7ae5bef1c2d19af636eb84544773df5b5f"; - assertEquals(WalletAddress.isPrivateKeyValid(nonReducedPrivateKey), false); - assertEquals(WalletAddress.isPrivateKeyValid(privateViewKeyHex), true); + String nonReducedPrivateKey = "680bceef3ca8b2ca1a9a29283c184f6f590a9bd2881825f0542ad99cdaba091a"; + assertFalse(WalletAddress.isPrivateKeyReduced(nonReducedPrivateKey)); + assertTrue(WalletAddress.isPrivateKeyReduced(privateViewKeyHex)); } }