diff --git a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/crypto/keys/PrivateKey.java b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/crypto/keys/PrivateKey.java index 00bfd34a5..32ab47cbe 100644 --- a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/crypto/keys/PrivateKey.java +++ b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/crypto/keys/PrivateKey.java @@ -119,6 +119,10 @@ public static PrivateKey fromPrefixedBytes(final UnsignedByteArray value) { */ @Deprecated public static PrivateKey of(final UnsignedByteArray value) { + // Assumption: Any developer using this method before this method was deprecated (i.e., v3.3) will likely have + // expected `value` to have been prefixed. That said, this assumption is technically invalid because it's ambiguous + // what any particular developer would have meant (see #486) because prior to #486, there was no byte-length check + // on these bytes. return fromPrefixedBytes(value); } @@ -128,16 +132,17 @@ public static PrivateKey of(final UnsignedByteArray value) { * @param value An {@link UnsignedByteArray} for this key's value. */ private PrivateKey(final UnsignedByteArray value, final KeyType keyType) { - this.value = Objects.requireNonNull(value); + Objects.requireNonNull(value); // <-- Check not-null first. this.keyType = Objects.requireNonNull(keyType); - // We assert this precondition here instead of allowing 33 bytes because this is a private constructor that can be - // fully tested via unit test, so this precondition should never be violated, and if it is, then it's a bug in - // xrpl4j. + // We assert this precondition here because this is a private constructor that can be fully tested via unit test, + // so this precondition should never be violated, and if it is, then it's a bug in xrpl4j. Preconditions.checkArgument( value.length() == 32, "Byte values passed to this constructor must be 32 bytes long, with no prefix." ); + + this.value = UnsignedByteArray.of(value.toByteArray()); // <- Always copy to ensure immutability } /** @@ -149,12 +154,12 @@ private PrivateKey(final UnsignedByteArray value, final KeyType keyType) { */ @Deprecated public UnsignedByteArray value() { - // This is technically wrong (because `value()` had an ambiguous meaning prior to fixing #486), but this mirrors - // what's in v3 prior to fixing #486, and will be fixed in v4 once the deprecated `.value()` is removed. + // Check for empty value, which can occur if this PrivateKey is "destroyed" but still in memory. if (value.length() == 0) { return UnsignedByteArray.empty(); } else { - + // This is technically wrong (because `value()` had an ambiguous meaning prior to fixing #486), but this mirrors + // what's in v3 prior to fixing #486, and will be fixed in v4 once the deprecated `.value()` is removed. return this.valueWithPrefixedBytes(); } } @@ -168,9 +173,14 @@ public UnsignedByteArray value() { * @return An instance of {@link UnsignedByteArray}. */ public UnsignedByteArray valueWithNaturalBytes() { - // Note: `toByteArray()` will perform a copy, which is what we want in order to enforce immutability of this - // PrivateKey (because Java 8 doesn't support immutable byte arrays). - return UnsignedByteArray.of(value.toByteArray()); + // Check for empty value, which can occur if this PrivateKey is "destroyed" but still in memory. + if (value.length() == 0) { + return UnsignedByteArray.empty(); + } else { + // Note: `toByteArray()` will perform a copy, which is what we want in order to enforce immutability of this + // PrivateKey (because Java 8 doesn't support immutable byte arrays). + return UnsignedByteArray.of(value.slice(0, 32).toByteArray()); + } } /** @@ -182,18 +192,23 @@ public UnsignedByteArray valueWithNaturalBytes() { * @return An instance of {@link UnsignedByteArray}. */ public UnsignedByteArray valueWithPrefixedBytes() { - // Note: value.slice() will take a view of the existing UBA, then `.toByteArray()` will perform a copy, which is - // what we want in order to enforce immutability of this PrivateKey (because Java 8 doesn't support immutable byte - // arrays). - switch (keyType) { - case ED25519: { - return UnsignedByteArray.of(ED2559_PREFIX).append(value); - } - case SECP256K1: { - return UnsignedByteArray.of(SECP256K1_PREFIX).append(value); - } - default: { - throw new IllegalStateException(String.format("Invalid keyType=%s", keyType)); + // Check for empty value, which can occur if this PrivateKey is "destroyed" but still in memory. + if (value.length() == 0) { + return UnsignedByteArray.empty(); + } else { + // Note: value.slice() will take a view of the existing UBA, then `.toByteArray()` will perform a copy, which is + // what we want in order to enforce immutability of this PrivateKey (because Java 8 doesn't support immutable byte + // arrays). + switch (keyType) { + case ED25519: { + return UnsignedByteArray.of(ED2559_PREFIX).append(value); + } + case SECP256K1: { + return UnsignedByteArray.of(SECP256K1_PREFIX).append(value); + } + default: { + throw new IllegalStateException(String.format("Invalid keyType=%s", keyType)); + } } } } diff --git a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/codec/addresses/PrivateKeyCodecTest.java b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/codec/addresses/PrivateKeyCodecTest.java index a925c330f..63ae99118 100644 --- a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/codec/addresses/PrivateKeyCodecTest.java +++ b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/codec/addresses/PrivateKeyCodecTest.java @@ -23,7 +23,7 @@ public void encodeDecodeEdNodePrivate() { testEncodeDecode( prefixedNodePrivateKey -> privateKeyCodec.encodeNodePrivateKey(prefixedNodePrivateKey), prefixedNodePrivateKey -> privateKeyCodec.decodeNodePrivateKey(prefixedNodePrivateKey), - TestConstants.ED_PRIVATE_KEY.valueWithNaturalBytes(), + TestConstants.getEdPrivateKey().valueWithNaturalBytes(), "paZHnTCvwm4GsxZ7qiA2nUBKE2DLnCoDWYqyocVZfVEZx3kvA4u" ); } @@ -48,7 +48,7 @@ public void encodeDecodeEdAccountPrivateKey() { testEncodeDecode( prefixedAccountPrivateKey -> privateKeyCodec.encodeAccountPrivateKey(prefixedAccountPrivateKey), prefixedAccountPrivateKey -> privateKeyCodec.decodeAccountPrivateKey(prefixedAccountPrivateKey), - TestConstants.ED_PRIVATE_KEY.valueWithNaturalBytes(), + TestConstants.getEdPrivateKey().valueWithNaturalBytes(), "pwSmRvZy1c55Kb5tCpBZyq41noSmPn7ynFzUHu1MaoGLAP1VfrT" ); } @@ -73,7 +73,7 @@ public void encodeDecodeEcNodePrivate() { testEncodeDecode( prefixedNodePrivate -> privateKeyCodec.encodeNodePrivateKey(prefixedNodePrivate), prefixedNodePrivate -> privateKeyCodec.decodeNodePrivateKey(prefixedNodePrivate), - TestConstants.EC_PRIVATE_KEY.valueWithNaturalBytes(), + TestConstants.getEcPrivateKey().valueWithNaturalBytes(), "pa1UHARsPMiuDqrJLwFhzcJokoHgyiuaxgPhUGYhkG5ArCfG2vt" ); } @@ -83,7 +83,7 @@ public void encodeDecodeEcAccountPrivateKey() { testEncodeDecode( prefixedAccountPrivateKey -> privateKeyCodec.encodeAccountPrivateKey(prefixedAccountPrivateKey), prefixedNodePrivateKey -> privateKeyCodec.decodeAccountPrivateKey(prefixedNodePrivateKey), - TestConstants.EC_PRIVATE_KEY.valueWithNaturalBytes(), + TestConstants.getEcPrivateKey().valueWithNaturalBytes(), "pwkgeQKfaDDMV7w59LhhuEWMbpX3HG2iXxXGgZuij2j6z1RYY7n" ); } diff --git a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/TestConstants.java b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/TestConstants.java index 746b856e6..0fa398728 100644 --- a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/TestConstants.java +++ b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/TestConstants.java @@ -39,11 +39,13 @@ public interface TestConstants { String ED_PRIVATE_KEY_HEX = "B224AFDCCEC7AA4E245E35452585D4FBBE37519BCA3929578BFC5BBD4640E163"; String ED_PRIVATE_KEY_WITH_PREFIX_HEX = "ED" + ED_PRIVATE_KEY_HEX; - String ED_PRIVATE_KEY_B58 = "UzQrAxCr8oeMRzzm6FFVJao8xkFc3g2ZQBs5GNBWRhZg"; - PrivateKey ED_PRIVATE_KEY = PrivateKey.fromNaturalBytes( - UnsignedByteArray.of(BaseEncoding.base16().decode(ED_PRIVATE_KEY_HEX)), - KeyType.ED25519 - ); + + static PrivateKey getEdPrivateKey() { + return PrivateKey.fromNaturalBytes( + UnsignedByteArray.of(BaseEncoding.base16().decode(ED_PRIVATE_KEY_HEX)), + KeyType.ED25519 + ); + } // Secp256k1 Public Key String EC_PUBLIC_KEY_HEX = "027535A4E90B2189CF9885563F45C4F454B3BFAB21930089C3878A9427B4D648D9"; @@ -52,11 +54,12 @@ public interface TestConstants { String EC_PRIVATE_KEY_HEX = "DAD3C2B4BF921398932C889DE5335F89D90249355FC6FFB73F1256D2957F9F17"; String EC_PRIVATE_KEY_WITH_PREFIX_HEX = "00" + EC_PRIVATE_KEY_HEX; - String EC_PRIVATE_KEY_WITH_PREFIX_B58 = "rEjDwJp2Pm3NrUtcf8v17jWopvqPJxyi5RTrDfhcJcWSi"; - PrivateKey EC_PRIVATE_KEY = PrivateKey.fromNaturalBytes( - UnsignedByteArray.of(BaseEncoding.base16().decode(EC_PRIVATE_KEY_HEX)), KeyType.SECP256K1 - ); + static PrivateKey getEcPrivateKey() { + return PrivateKey.fromNaturalBytes( + UnsignedByteArray.of(BaseEncoding.base16().decode(EC_PRIVATE_KEY_HEX)), KeyType.SECP256K1 + ); + } // Both generated from Passphrase.of("hello") Address ED_ADDRESS = Address.of("rwGWYtRR6jJJJq7FKQg74YwtkiPyUqJ466"); diff --git a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/keys/PrivateKeyTest.java b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/keys/PrivateKeyTest.java index d0f51a8a0..75b4c53b5 100644 --- a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/keys/PrivateKeyTest.java +++ b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/keys/PrivateKeyTest.java @@ -22,10 +22,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.xrpl.xrpl4j.crypto.TestConstants.EC_PRIVATE_KEY; import static org.xrpl.xrpl4j.crypto.TestConstants.EC_PRIVATE_KEY_HEX; import static org.xrpl.xrpl4j.crypto.TestConstants.EC_PRIVATE_KEY_WITH_PREFIX_HEX; -import static org.xrpl.xrpl4j.crypto.TestConstants.ED_PRIVATE_KEY; import static org.xrpl.xrpl4j.crypto.TestConstants.ED_PRIVATE_KEY_HEX; import static org.xrpl.xrpl4j.crypto.TestConstants.ED_PRIVATE_KEY_WITH_PREFIX_HEX; @@ -33,6 +31,7 @@ import org.junit.jupiter.api.Test; import org.xrpl.xrpl4j.codec.addresses.KeyType; import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray; +import org.xrpl.xrpl4j.crypto.TestConstants; /** * Unit tests for {@link PrivateKey}. @@ -58,7 +57,7 @@ void testOfWithNull() { void testEcOf() { UnsignedByteArray thirtyThreeBytes = UnsignedByteArray.of( BaseEncoding.base16().decode(EC_PRIVATE_KEY_WITH_PREFIX_HEX)); - assertThat(PrivateKey.of(thirtyThreeBytes)).isEqualTo(EC_PRIVATE_KEY); + assertThat(PrivateKey.of(thirtyThreeBytes)).isEqualTo(TestConstants.getEcPrivateKey()); UnsignedByteArray thirtyTwoBytes = UnsignedByteArray.of(thirtyThreeBytes.slice(1, 33).toByteArray()); IllegalArgumentException exception = assertThrows( @@ -71,7 +70,7 @@ void testEcOf() { void testEdOf() { UnsignedByteArray thirtyThreeBytes = UnsignedByteArray.of( BaseEncoding.base16().decode(ED_PRIVATE_KEY_WITH_PREFIX_HEX)); - assertThat(PrivateKey.of(thirtyThreeBytes)).isEqualTo(ED_PRIVATE_KEY); + assertThat(PrivateKey.of(thirtyThreeBytes)).isEqualTo(TestConstants.getEdPrivateKey()); UnsignedByteArray thirtyTwoBytes = UnsignedByteArray.of(thirtyThreeBytes.slice(1, 33).toByteArray()); IllegalArgumentException exception = assertThrows( @@ -80,11 +79,19 @@ void testEdOf() { assertThat(exception.getMessage()).isEqualTo(EXPECTED_ERROR); } + @Test + void testOfWithLessWithEmpty() { + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, () -> PrivateKey.of(UnsignedByteArray.empty()) + ); + assertThat(exception.getMessage()).isEqualTo(EXPECTED_ERROR); + } + @Test void testEdOfWithLessThan32Bytes() { UnsignedByteArray twoBytes = UnsignedByteArray.of(new byte[] {(byte) 0xED, (byte) 0xFF}); IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, () -> PrivateKey.of(twoBytes) + IllegalArgumentException.class, () -> PrivateKey.of(UnsignedByteArray.empty()) ); assertThat(exception.getMessage()).isEqualTo(EXPECTED_ERROR); } @@ -108,10 +115,29 @@ void testFromNaturalBytesWithNull() { assertThrows(NullPointerException.class, () -> PrivateKey.fromNaturalBytes(UnsignedByteArray.empty(), null)); } + @Test + void testEcFromNaturalBytesWithEmpty() { + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, () -> PrivateKey.fromNaturalBytes(UnsignedByteArray.empty(), KeyType.SECP256K1) + ); + assertThat(exception.getMessage()) + .isEqualTo("Byte values passed to this constructor must be 32 bytes long, with no prefix."); + } + + @Test + void testEdFromNaturalBytesWithEmpty() { + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, () -> PrivateKey.fromNaturalBytes(UnsignedByteArray.empty(), KeyType.ED25519) + ); + assertThat(exception.getMessage()) + .isEqualTo("Byte values passed to this constructor must be 32 bytes long, with no prefix."); + } + @Test void testEcFromNaturalBytes() { UnsignedByteArray thirtyThreeBytes = UnsignedByteArray.of( - BaseEncoding.base16().decode(EC_PRIVATE_KEY_WITH_PREFIX_HEX)); + BaseEncoding.base16().decode(EC_PRIVATE_KEY_WITH_PREFIX_HEX) + ); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, () -> PrivateKey.fromNaturalBytes(thirtyThreeBytes, KeyType.SECP256K1) ); @@ -119,7 +145,9 @@ void testEcFromNaturalBytes() { .isEqualTo("Byte values passed to this constructor must be 32 bytes long, with no prefix."); UnsignedByteArray thirtyTwoBytes = UnsignedByteArray.of(thirtyThreeBytes.slice(1, 33).toByteArray()); - assertThat(PrivateKey.fromNaturalBytes(thirtyTwoBytes, KeyType.SECP256K1)).isEqualTo(EC_PRIVATE_KEY); + assertThat(PrivateKey.fromNaturalBytes(thirtyTwoBytes, KeyType.SECP256K1)).isEqualTo( + TestConstants.getEcPrivateKey() + ); } @Test @@ -133,7 +161,7 @@ void testEdFromNaturalBytes() { .isEqualTo("Byte values passed to this constructor must be 32 bytes long, with no prefix."); UnsignedByteArray thirtyTwoBytes = UnsignedByteArray.of(thirtyThreeBytes.slice(1, 33).toByteArray()); - assertThat(PrivateKey.fromNaturalBytes(thirtyTwoBytes, KeyType.ED25519)).isEqualTo(ED_PRIVATE_KEY); + assertThat(PrivateKey.fromNaturalBytes(thirtyTwoBytes, KeyType.ED25519)).isEqualTo(TestConstants.getEdPrivateKey()); } @Test @@ -170,7 +198,7 @@ void testEcFromPrefixedBytes() { UnsignedByteArray thirtyThreeBytes = UnsignedByteArray.of( BaseEncoding.base16().decode(EC_PRIVATE_KEY_WITH_PREFIX_HEX) ); - assertThat(PrivateKey.fromPrefixedBytes(thirtyThreeBytes)).isEqualTo(EC_PRIVATE_KEY); + assertThat(PrivateKey.fromPrefixedBytes(thirtyThreeBytes)).isEqualTo(TestConstants.getEcPrivateKey()); UnsignedByteArray thirtyTwoBytes = UnsignedByteArray.of(thirtyThreeBytes.slice(1, 33).toByteArray()); IllegalArgumentException exception = assertThrows( @@ -184,7 +212,7 @@ void testEdFromPrefixedBytes() { UnsignedByteArray thirtyThreeBytes = UnsignedByteArray.of( BaseEncoding.base16().decode(ED_PRIVATE_KEY_WITH_PREFIX_HEX) ); - assertThat(PrivateKey.fromPrefixedBytes(thirtyThreeBytes)).isEqualTo(ED_PRIVATE_KEY); + assertThat(PrivateKey.fromPrefixedBytes(thirtyThreeBytes)).isEqualTo(TestConstants.getEdPrivateKey()); UnsignedByteArray thirtyTwoBytes = UnsignedByteArray.of(thirtyThreeBytes.slice(1, 33).toByteArray()); IllegalArgumentException exception = assertThrows( @@ -230,16 +258,18 @@ void testConstants() { @Test void valueForEd25519() { - assertThat(ED_PRIVATE_KEY.value().hexValue()).isEqualTo(ED_PRIVATE_KEY_WITH_PREFIX_HEX); - assertThat(ED_PRIVATE_KEY.valueWithPrefixedBytes().hexValue()).isEqualTo(ED_PRIVATE_KEY_WITH_PREFIX_HEX); - assertThat(ED_PRIVATE_KEY.valueWithNaturalBytes().hexValue()).isEqualTo(ED_PRIVATE_KEY_HEX); + assertThat(TestConstants.getEdPrivateKey().value().hexValue()).isEqualTo(ED_PRIVATE_KEY_WITH_PREFIX_HEX); + assertThat(TestConstants.getEdPrivateKey().valueWithPrefixedBytes().hexValue()).isEqualTo( + ED_PRIVATE_KEY_WITH_PREFIX_HEX); + assertThat(TestConstants.getEdPrivateKey().valueWithNaturalBytes().hexValue()).isEqualTo(ED_PRIVATE_KEY_HEX); } @Test void valueForSecp256k1() { - assertThat(EC_PRIVATE_KEY.value().hexValue()).isEqualTo(EC_PRIVATE_KEY_WITH_PREFIX_HEX); - assertThat(EC_PRIVATE_KEY.valueWithPrefixedBytes().hexValue()).isEqualTo(EC_PRIVATE_KEY_WITH_PREFIX_HEX); - assertThat(EC_PRIVATE_KEY.valueWithNaturalBytes().hexValue()).isEqualTo(EC_PRIVATE_KEY_HEX); + assertThat(TestConstants.getEcPrivateKey().value().hexValue()).isEqualTo(EC_PRIVATE_KEY_WITH_PREFIX_HEX); + assertThat(TestConstants.getEcPrivateKey().valueWithPrefixedBytes().hexValue()).isEqualTo( + EC_PRIVATE_KEY_WITH_PREFIX_HEX); + assertThat(TestConstants.getEcPrivateKey().valueWithNaturalBytes().hexValue()).isEqualTo(EC_PRIVATE_KEY_HEX); } /////////////////// @@ -248,17 +278,17 @@ void valueForSecp256k1() { @Test void keyTypeEd25519() { - assertThat(ED_PRIVATE_KEY.keyType()).isEqualTo(KeyType.ED25519); + assertThat(TestConstants.getEdPrivateKey().keyType()).isEqualTo(KeyType.ED25519); } @Test void keyTypeSecp256k1() { - assertThat(EC_PRIVATE_KEY.keyType()).isEqualTo(KeyType.SECP256K1); + assertThat(TestConstants.getEcPrivateKey().keyType()).isEqualTo(KeyType.SECP256K1); } @Test void destroy() { - PrivateKey privateKey = PrivateKey.of(ED_PRIVATE_KEY.value()); + PrivateKey privateKey = PrivateKey.of(TestConstants.getEdPrivateKey().value()); assertThat(privateKey.isDestroyed()).isFalse(); privateKey.destroy(); assertThat(privateKey.isDestroyed()).isTrue(); @@ -266,8 +296,11 @@ void destroy() { privateKey.destroy(); assertThat(privateKey.isDestroyed()).isTrue(); assertThat(privateKey.value().hexValue()).isEqualTo(""); + assertThat(privateKey.valueWithNaturalBytes()).isEqualTo(UnsignedByteArray.empty()); + assertThat(privateKey.valueWithPrefixedBytes()).isEqualTo(UnsignedByteArray.empty()); + assertThat(privateKey.value()).isEqualTo(UnsignedByteArray.empty()); - privateKey = PrivateKey.of(EC_PRIVATE_KEY.value()); + privateKey = PrivateKey.of(TestConstants.getEcPrivateKey().value()); assertThat(privateKey.isDestroyed()).isFalse(); privateKey.destroy(); assertThat(privateKey.isDestroyed()).isTrue(); @@ -275,33 +308,36 @@ void destroy() { privateKey.destroy(); assertThat(privateKey.isDestroyed()).isTrue(); assertThat(privateKey.value().hexValue()).isEqualTo(""); + assertThat(privateKey.valueWithNaturalBytes()).isEqualTo(UnsignedByteArray.empty()); + assertThat(privateKey.valueWithPrefixedBytes()).isEqualTo(UnsignedByteArray.empty()); + assertThat(privateKey.value()).isEqualTo(UnsignedByteArray.empty()); } @Test void equals() { - assertThat(ED_PRIVATE_KEY).isEqualTo(ED_PRIVATE_KEY); - assertThat(ED_PRIVATE_KEY).isEqualTo(PrivateKey.of(ED_PRIVATE_KEY.value())); - assertThat(ED_PRIVATE_KEY).isNotEqualTo(EC_PRIVATE_KEY); - assertThat(EC_PRIVATE_KEY).isNotEqualTo(new Object()); - - assertThat(EC_PRIVATE_KEY).isEqualTo(EC_PRIVATE_KEY); - assertThat(EC_PRIVATE_KEY).isEqualTo(PrivateKey.of(EC_PRIVATE_KEY.value())); - assertThat(EC_PRIVATE_KEY).isNotEqualTo(ED_PRIVATE_KEY); - assertThat(EC_PRIVATE_KEY).isNotEqualTo(new Object()); + assertThat(TestConstants.getEdPrivateKey()).isEqualTo(TestConstants.getEdPrivateKey()); + assertThat(TestConstants.getEdPrivateKey()).isEqualTo(PrivateKey.of(TestConstants.getEdPrivateKey().value())); + assertThat(TestConstants.getEdPrivateKey()).isNotEqualTo(TestConstants.getEcPrivateKey()); + assertThat(TestConstants.getEcPrivateKey()).isNotEqualTo(new Object()); + + assertThat(TestConstants.getEcPrivateKey()).isEqualTo(TestConstants.getEcPrivateKey()); + assertThat(TestConstants.getEcPrivateKey()).isEqualTo(PrivateKey.of(TestConstants.getEcPrivateKey().value())); + assertThat(TestConstants.getEcPrivateKey()).isNotEqualTo(TestConstants.getEdPrivateKey()); + assertThat(TestConstants.getEcPrivateKey()).isNotEqualTo(new Object()); } @Test void testHashcode() { - assertThat(ED_PRIVATE_KEY.hashCode()).isEqualTo(ED_PRIVATE_KEY.hashCode()); - assertThat(ED_PRIVATE_KEY.hashCode()).isNotEqualTo(EC_PRIVATE_KEY.hashCode()); + assertThat(TestConstants.getEdPrivateKey().hashCode()).isEqualTo(TestConstants.getEdPrivateKey().hashCode()); + assertThat(TestConstants.getEdPrivateKey().hashCode()).isNotEqualTo(TestConstants.getEcPrivateKey().hashCode()); - assertThat(EC_PRIVATE_KEY.hashCode()).isEqualTo(EC_PRIVATE_KEY.hashCode()); - assertThat(EC_PRIVATE_KEY.hashCode()).isNotEqualTo(ED_PRIVATE_KEY.hashCode()); + assertThat(TestConstants.getEcPrivateKey().hashCode()).isEqualTo(TestConstants.getEcPrivateKey().hashCode()); + assertThat(TestConstants.getEcPrivateKey().hashCode()).isNotEqualTo(TestConstants.getEdPrivateKey().hashCode()); } @Test void testToString() { - assertThat(ED_PRIVATE_KEY.toString()).isEqualTo( + assertThat(TestConstants.getEdPrivateKey().toString()).isEqualTo( "PrivateKey{" + "value=[redacted]," + "keyType=ED25519," + @@ -309,7 +345,7 @@ void testToString() { "}" ); - assertThat(EC_PRIVATE_KEY.toString()).isEqualTo( + assertThat(TestConstants.getEcPrivateKey().toString()).isEqualTo( "PrivateKey{" + "value=[redacted]," + "keyType=SECP256K1," + diff --git a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/signing/AbstractSignatureServiceTest.java b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/signing/AbstractSignatureServiceTest.java index 6abab24a9..03abb1692 100644 --- a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/signing/AbstractSignatureServiceTest.java +++ b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/crypto/signing/AbstractSignatureServiceTest.java @@ -193,12 +193,12 @@ public void signWithNullPrivateKey() { @Test public void signWithNullTransaction() { assertThrows(NullPointerException.class, - () -> signatureService.sign(TestConstants.ED_PRIVATE_KEY, (Transaction) null)); + () -> signatureService.sign(TestConstants.getEdPrivateKey(), (Transaction) null)); } @Test public void signEd25519() { - signatureService.sign(TestConstants.ED_PRIVATE_KEY, transactionMock); + signatureService.sign(TestConstants.getEdPrivateKey(), transactionMock); verify(signatureUtilsMock, times(0)).toMultiSignableBytes(any(), any()); verify(signatureUtilsMock).toSignableBytes(transactionMock); @@ -208,7 +208,7 @@ public void signEd25519() { @Test public void signSecp256k1() { - signatureService.sign(TestConstants.EC_PRIVATE_KEY, transactionMock); + signatureService.sign(TestConstants.getEcPrivateKey(), transactionMock); verify(signatureUtilsMock, times(0)).toMultiSignableBytes(any(), any()); verify(signatureUtilsMock).toSignableBytes(transactionMock); @@ -220,7 +220,7 @@ public void signSecp256k1() { void multiSignEd25519() { when(signedTransactionMock.signature()).thenReturn(ed25519SignatureMock); - final Signature signature = signatureService.multiSign(TestConstants.ED_PRIVATE_KEY, transactionMock); + final Signature signature = signatureService.multiSign(TestConstants.getEdPrivateKey(), transactionMock); assertThat(signature).isEqualTo(ed25519SignatureMock); verify(signatureUtilsMock).toMultiSignableBytes(transactionMock, TestConstants.ED_ADDRESS); @@ -232,7 +232,7 @@ void multiSignEd25519() { void multiSignSecp256k1() { when(signedTransactionMock.signature()).thenReturn(secp256k1SignatureMock); - final Signature signature = signatureService.multiSign(TestConstants.EC_PRIVATE_KEY, transactionMock); + final Signature signature = signatureService.multiSign(TestConstants.getEcPrivateKey(), transactionMock); assertThat(signature).isEqualTo(secp256k1SignatureMock); verify(signatureUtilsMock).toMultiSignableBytes(transactionMock, TestConstants.EC_ADDRESS); @@ -252,7 +252,7 @@ public void signUnsignedClaimWithNullPrivateKey() { @Test public void signUnsignedClaimWithNullUnsignedClaim() { assertThrows(NullPointerException.class, - () -> signatureService.sign(TestConstants.ED_PRIVATE_KEY, (UnsignedClaim) null)); + () -> signatureService.sign(TestConstants.getEdPrivateKey(), (UnsignedClaim) null)); } @Test @@ -260,7 +260,7 @@ public void signUnsignedClaimEd25519() { UnsignedClaim unsignedClaimMock = mock(UnsignedClaim.class); when(signatureUtilsMock.toSignableBytes(unsignedClaimMock)).thenReturn(UnsignedByteArray.empty()); - Signature actualSignature = signatureService.sign(TestConstants.ED_PRIVATE_KEY, unsignedClaimMock); + Signature actualSignature = signatureService.sign(TestConstants.getEdPrivateKey(), unsignedClaimMock); assertThat(actualSignature).isEqualTo(ed25519SignatureMock); verify(signatureUtilsMock, times(0)).toMultiSignableBytes(any(), any()); @@ -350,7 +350,7 @@ public void verifyMultiSecp256k1() { @Test public void edDsaSign() { - Signature actual = signatureService.edDsaSign(TestConstants.ED_PRIVATE_KEY, UnsignedByteArray.empty()); + Signature actual = signatureService.edDsaSign(TestConstants.getEdPrivateKey(), UnsignedByteArray.empty()); assertThat(actual).isEqualTo(ed25519SignatureMock); assertThat(ed25519VerifyCalled.get()).isFalse(); @@ -364,7 +364,7 @@ public void edDsaSign() { @Test public void ecDsaSign() { - Signature actual = signatureService.ecDsaSign(TestConstants.EC_PRIVATE_KEY, UnsignedByteArray.empty()); + Signature actual = signatureService.ecDsaSign(TestConstants.getEcPrivateKey(), UnsignedByteArray.empty()); assertThat(actual).isEqualTo(secp256k1SignatureMock);