Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #486 #487

Merged
merged 41 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
cc042ee
Fixes #486
sappenin Sep 28, 2023
b6e0407
Stop using deprecated constant
sappenin Sep 28, 2023
77094c4
Fix formatting and warnings
sappenin Sep 28, 2023
4885d58
Update comment
sappenin Sep 28, 2023
5088272
Update PrivateKey to support natural and prefixed bytes
sappenin Sep 29, 2023
82f3c66
Fix broken unit tests
sappenin Oct 2, 2023
57a2ffe
Fix checkstyle
sappenin Oct 2, 2023
ffe3f1d
Add more unit tests
sappenin Oct 2, 2023
0d4730d
More unit tests
sappenin Oct 2, 2023
6786d8d
Add byte length to error emission
sappenin Oct 2, 2023
0505aac
Stop using deprecated methods
sappenin Oct 3, 2023
5734bb8
Update padding and add unit tests
sappenin Oct 3, 2023
d700785
Update Javadoc
sappenin Oct 3, 2023
2b4c7f6
Fix checkstyle
sappenin Oct 3, 2023
0cd3b2b
Fix checkstyle
sappenin Oct 3, 2023
92b93ae
Update Javadoc
sappenin Oct 4, 2023
209cda8
Improve Javadoc
sappenin Oct 4, 2023
10e70ec
Fix TODO from PR comments
sappenin Oct 4, 2023
41d271b
Add comment
sappenin Oct 4, 2023
ad21765
Formatting
sappenin Oct 4, 2023
f890042
Update xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/addresses/Unsi…
sappenin Oct 4, 2023
901847c
Update logging emission
sappenin Oct 4, 2023
92fb265
Update comment per PR feedback
sappenin Oct 4, 2023
68b3292
Remove redundant code
sappenin Oct 4, 2023
fedf466
Fix checkstyle
sappenin Oct 4, 2023
efb49a8
Remove redundant array copy
sappenin Oct 4, 2023
381ff1c
Fix checkstyle
sappenin Oct 4, 2023
fd38efc
Add new unit tests
sappenin Oct 4, 2023
3da9e08
Refactor into Secp256k1 util class
sappenin Oct 5, 2023
1403a17
Fix checkstyle
sappenin Oct 5, 2023
b5c042e
Create ED25519_PREFIX constant in PublicKey
sappenin Oct 5, 2023
01f713d
Update comment
sappenin Oct 5, 2023
9cf65c4
Rename accessors on PrivateKey
sappenin Oct 5, 2023
979ba29
Fix Javadoc
sappenin Oct 5, 2023
7e13d81
Update xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/addresses/Priv…
nkramer44 Oct 5, 2023
9c516ef
Update xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/addresses/Priv…
nkramer44 Oct 5, 2023
14f69a3
Update xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/addresses/Priv…
nkramer44 Oct 5, 2023
8c6b6bc
Update xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/addresses/Priv…
nkramer44 Oct 5, 2023
46db60c
Add test for immutability
sappenin Oct 5, 2023
c4b5c89
Fix compile issue
sappenin Oct 5, 2023
408012b
Update per PR
sappenin Oct 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@
<version>3.3.0</version>
<inherited>true</inherited>
<configuration>
<encoding>UTF-8</encoding>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
<consoleOutput>true</consoleOutput>
<linkXRef>false</linkXRef>
<failOnViolation>true</failOnViolation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -56,7 +56,7 @@ public static String encode(
Objects.requireNonNull(expectedLength);

if (expectedLength.intValue() != bytes.getUnsignedBytes().size()) {
throw new EncodeException("Length of bytes does not match expectedLength.");
throw new EncodeException(String.format("Length of bytes does not match expectedLength of %s.", expectedLength));
}

return encodeChecked(bytes.toByteArray(), versions);
Expand Down Expand Up @@ -99,6 +99,7 @@ public static String encodeChecked(final byte[] bytes, final List<Version> versi
* @param version The {@link Version} to try decoding with.
*
* @return A {@link Decoded} containing the decoded value and version.
*
* @throws EncodingFormatException If the version bytes of the Base58 value are invalid.
*/
public static Decoded decode(
Expand Down Expand Up @@ -136,14 +137,14 @@ public static Decoded decode(
* Decode a Base58Check {@link String}.
*
* @param base58Value The Base58Check encoded {@link String} to be decoded.
* @param keyTypes A {@link List} of {@link KeyType}s which can be associated with the result of this
* method.
* @param keyTypes A {@link List} of {@link KeyType}s which can be associated with the result of this method.
* @param versions A {@link List} of {@link Version}s to try decoding with.
* @param expectedLength The expected length of the decoded value.
*
* @return A {@link Decoded} containing the decoded value, version, and type.
* @throws EncodingFormatException If more than one version is supplied without an expectedLength value present,
* or if the version bytes of the Base58 value are invalid.
*
* @throws EncodingFormatException If more than one version is supplied without an expectedLength value present, or if
* the version bytes of the Base58 value are invalid.
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public static Decoded decode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -86,7 +86,10 @@ public UnsignedByteArray decodeAccountId(final Address accountId) {
* @param publicKey An {@link UnsignedByteArray} containing the public key to be encoded.
*
* @return The Base58 representation of publicKey.
*
* @deprecated Prefer {@link PublicKeyCodec#encodeNodePublicKey(UnsignedByteArray)}.
*/
@Deprecated
public String encodeNodePublicKey(final UnsignedByteArray publicKey) {
Objects.requireNonNull(publicKey);

Expand All @@ -101,7 +104,9 @@ public String encodeNodePublicKey(final UnsignedByteArray publicKey) {
* @return An {@link UnsignedByteArray} containing the decoded public key.
*
* @see "https://xrpl.org/base58-encodings.html"
* @deprecated Prefer {@link PublicKeyCodec#decodeNodePublicKey(String)}.
*/
@Deprecated
public UnsignedByteArray decodeNodePublicKey(final String publicKey) {
Objects.requireNonNull(publicKey);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.xrpl.xrpl4j.codec.addresses;

/*-
* ========================LICENSE_START=================================
* xrpl4j :: core
* %%
* Copyright (C) 2020 - 2023 XRPL Foundation and its contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/

import com.google.common.collect.Lists;
import com.google.common.primitives.UnsignedInteger;

import java.util.Objects;

/**
* A Codec for encoding/decoding various seed primitives.
*/
public class PrivateKeyCodec {

private static final PrivateKeyCodec INSTANCE = new PrivateKeyCodec();

public static PrivateKeyCodec getInstance() {
return INSTANCE;
}

/**
* Encode an XRPL Node Private Key to a Base58Check encoded {@link String}.
*
* @param privateKeyBytes An {@link UnsignedByteArray} containing the public key to be encoded.
*
* @return The Base58 representation of privateKeyBytes.
*/
public String encodeNodePrivateKey(final UnsignedByteArray privateKeyBytes) {
Objects.requireNonNull(privateKeyBytes);

return AddressBase58.encode(
privateKeyBytes,
Lists.newArrayList(Version.NODE_PRIVATE),
UnsignedInteger.valueOf(32)
);
}

/**
* Decode a Base58Check encoded XRPL Node Private Key.
*
* @param privateKeyBytes The Base58 encoded public key to be decoded.
nkramer44 marked this conversation as resolved.
Show resolved Hide resolved
*
* @return An {@link UnsignedByteArray} containing the decoded public key.
*
* @see "https://xrpl.org/base58-encodings.html"
*/
public UnsignedByteArray decodeNodePrivateKey(final String privateKeyBytes) {
nkramer44 marked this conversation as resolved.
Show resolved Hide resolved
Objects.requireNonNull(privateKeyBytes);

return AddressBase58.decode(
privateKeyBytes,
Lists.newArrayList(Version.NODE_PRIVATE),
UnsignedInteger.valueOf(32)
).bytes();
}

/**
* Encode an XRPL Account Private Key to a Base58Check encoded {@link String}.
*
* @param privateKeyBytes An {@link UnsignedByteArray} containing the public key to be encoded.
*
* @return The Base58 representation of privateKeyBytes.
*/
public String encodeAccountPrivateKey(final UnsignedByteArray privateKeyBytes) {
Objects.requireNonNull(privateKeyBytes);

return AddressBase58.encode(
privateKeyBytes,
Lists.newArrayList(Version.ACCOUNT_SECRET_KEY),
UnsignedInteger.valueOf(32)
);
}

/**
* Decode a Base58Check encoded XRPL Account Private Key.
*
* @param publicKey The Base58 encoded public key to be decoded.
nkramer44 marked this conversation as resolved.
Show resolved Hide resolved
*
* @return An {@link UnsignedByteArray} containing the decoded public key.
*
* @see "https://xrpl.org/base58-encodings.html"
*/
public UnsignedByteArray decodeAccountPrivateKey(final String publicKey) {
nkramer44 marked this conversation as resolved.
Show resolved Hide resolved
Objects.requireNonNull(publicKey);

return AddressBase58.decode(
publicKey,
Lists.newArrayList(Version.ACCOUNT_SECRET_KEY),
UnsignedInteger.valueOf(32)
).bytes();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -28,7 +28,6 @@
/**
* A Codec for encoding/decoding various seed primitives.
*/
@SuppressWarnings( {"OptionalUsedAsFieldOrParameterType", "ParameterName", "MethodName"})
public class PublicKeyCodec {

private static final PublicKeyCodec INSTANCE = new PublicKeyCodec();
Expand All @@ -46,7 +45,12 @@ public static PublicKeyCodec getInstance() {
*/
public String encodeNodePublicKey(final UnsignedByteArray publicKey) {
Objects.requireNonNull(publicKey);
return AddressBase58.encode(publicKey, Lists.newArrayList(Version.NODE_PUBLIC), UnsignedInteger.valueOf(33));

return AddressBase58.encode(
publicKey,
Lists.newArrayList(Version.NODE_PUBLIC),
UnsignedInteger.valueOf(33)
);
}

/**
Expand Down Expand Up @@ -78,7 +82,11 @@ public UnsignedByteArray decodeNodePublicKey(final String publicKey) {
public String encodeAccountPublicKey(final UnsignedByteArray publicKey) {
Objects.requireNonNull(publicKey);

return AddressBase58.encode(publicKey, Lists.newArrayList(Version.ACCOUNT_PUBLIC_KEY), UnsignedInteger.valueOf(33));
return AddressBase58.encode(
publicKey,
Lists.newArrayList(Version.ACCOUNT_PUBLIC_KEY),
UnsignedInteger.valueOf(33)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -42,6 +42,17 @@ private UnsignedByte(final int value) {
this.value = value;
}

/**
* Copy constructor. Constructs an {@link UnsignedByte} from an {@code UnsignedByte}.
*
* @param value An {@code int} value.
*
* @return An {@link UnsignedByte}.
*/
public static UnsignedByte of(final UnsignedByte value) {
return new UnsignedByte(value.asInt());
}

/**
* Construct an {@link UnsignedByte} from an {@code int}.
*
Expand Down Expand Up @@ -141,8 +152,8 @@ public boolean isNthBitSet(final int nth) {
}

/**
* Does a bitwise OR on this {@link UnsignedByte} and the given {@link UnsignedByte}, and returns a new {@link
* UnsignedByte} as the result.
* Does a bitwise OR on this {@link UnsignedByte} and the given {@link UnsignedByte}, and returns a new
* {@link UnsignedByte} as the result.
*
* @param unsignedByte The {@link UnsignedByte} to perform a bitwise OR on.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,6 +20,9 @@
* =========================LICENSE_END==================================
*/

import com.google.common.base.Preconditions;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -75,6 +78,82 @@ public static UnsignedByteArray of(UnsignedByte first, UnsignedByte... rest) {
return new UnsignedByteArray(unsignedBytes);
}

/**
* Creates an {@link UnsignedByteArray} from the bytes of a supplied {@link BigInteger}. If the length of the
* resulting array is not at least {@code minFinalByteLength}, then the result is prefix padded with `0x00` bytes
* until the final array length is {@code minFinalByteLength}.
*
* <p>This function primarily exists to ensure that transformation of secp256k1 private keys from one form to another
* (e.g., from {@link BigInteger} to a byte array) are done in a consistent manner, always yielding the desired number
* of bytes. For example, secp256k1 private keys are 32-bytes long naturally. However, when transformed to a byte
* array via `BigInteger.toByteArray()`, the result will not always have the same number of leading zero bytes that
sappenin marked this conversation as resolved.
Show resolved Hide resolved
* one might expect. Sometimes the returned array will have 33 bytes, one of which is a zero-byte prefix pad that is
* meant to ensure the underlying number is not represented as a negative number. Other times, the array will have
* fewer than 32 bytes, for example 31 or even 30, if the byte array has redundant leading zero bytes.
*
* <p>Thus, this function can be used to normalize a byte array with a desired number of 0-byte padding to ensure
* that the resulting byte array is always the desired {@code minFinalByteLength} (e.g., in this library, secp256k1
* private keys should always be comprised of a 32-byte natural private key with a one-byte `0x00` prefix pad).
*
* @param amount A {@link BigInteger} to convert into an {@link UnsignedByteArray}.
* @param minFinalByteLength The minimum length, in bytes, that the final result must be. If the final byte length is
* less than this number, the resulting array will be prefix padded to increase its length
* to this number.
*
* @return An {@link UnsignedByteArray} with a length of at least {@code minFinalByteLength}.
*
* @see "https://github.com/XRPLF/xrpl4j/issues/486"
*/
public static UnsignedByteArray from(final BigInteger amount, int minFinalByteLength) {
Objects.requireNonNull(amount);
Preconditions.checkArgument(amount.signum() >= 0, "amount must not be negative");
sappenin marked this conversation as resolved.
Show resolved Hide resolved
Preconditions.checkArgument(minFinalByteLength >= 0, "minFinalByteLength must not be negative");

// Return the `amount` as an UnsignedByteArray, but with the proper zero-byte prefix padding.
return withPrefixPadding(amount.toByteArray(), minFinalByteLength);
}

/**
* Construct a new {@link UnsignedByteArray} that contains this byte array's bytes, but with enough `0x00` prefix
* padding bytes such that the final length of the returned value is {@code minFinalByteLength}.
*
* @param minFinalByteLength The minimum length, in bytes, that the final result must be zero-byte prefix-padded to.
* If this number is greater-than {@code #length}, then this value will be reduced to
* {@code #length}.
*
* @return A copy of this {@link UnsignedByteArray} that has been zero-byte prefix-padded such that its final length
* is at least {@code minFinalByteLength}.
*/
public UnsignedByteArray withPrefixPadding(int minFinalByteLength) {
Preconditions.checkArgument(minFinalByteLength >= 0, "minFinalByteLength must not be negative");

return withPrefixPadding(this.toByteArray(), minFinalByteLength);
}

/**
* Creates a new {@link UnsignedByteArray} from a byte array by prefixing enough zero-pad bytes to ensure that the
* result has at least {@code minFinalByteLength} bytes.
*
* @param bytes A byte array to convert into an {@link UnsignedByteArray}.
* @param minFinalByteLength The minimum length, in bytes, that the final result must be prefix-padded to. If this
* number is greater-than {@code amount#length}, then this value will be reduced to
* {@code amount#length}.
*
* @return A zero-byte prefix-padded {@link UnsignedByteArray} with a length of at least {@code minFinalByteLength}.
*/
private static UnsignedByteArray withPrefixPadding(final byte[] bytes, int minFinalByteLength) {
Preconditions.checkArgument(minFinalByteLength >= 0, "minFinalByteLength must not be negative");

if (bytes.length > minFinalByteLength) { // <-- Increase `minFinalByteLength` to be at least bytes.length
minFinalByteLength = bytes.length;
}

final int numPadBytes = minFinalByteLength - bytes.length; // <-- numPadBytes will never be negative
byte[] resultBytes = new byte[minFinalByteLength];
System.arraycopy(bytes, 0, resultBytes, numPadBytes, bytes.length);
return UnsignedByteArray.of(resultBytes);
}

/**
* Creates an empty {@link UnsignedByteArray}.
*
Expand Down
Loading
Loading