Skip to content

Commit

Permalink
Update Javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
sappenin committed Oct 3, 2023
1 parent 5734bb8 commit d700785
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,21 @@ public static UnsignedByteArray of(UnsignedByte first, UnsignedByte... rest) {
}

/**
* Creates an {@link UnsignedByteArray} from a {@link BigInteger} with a minimum number of prefix padding bytes.
* 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 prefix padded with `0x00` bytes until
* the final array length is {@code minFinalByteLength}.
* <p>
* This function primarily exists to ensure that transforming a secp256k1 private key (as a {@link BigInteger}) to an
* instance of {@link UnsignedByteArray} is always done in a consistent fashion 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 occasionally be unexpectedly (though correctly) truncated. For example,
* 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. Thus, this function can be used to
* normalize the bytes 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). For more details, see
* <a href="https://github.com/XRPLF/xrpl4j/issues/486">Github issue #486</a>.
* </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
* one might expect. For example, 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
Expand All @@ -102,6 +103,8 @@ public static UnsignedByteArray of(UnsignedByte first, UnsignedByte... rest) {
*
* @return An {@link UnsignedByteArray}with a length of at least {@code minFinalByteLength} (possibly via zero-byte
* prefix padding).
*
* @see "https://github.com/XRPLF/xrpl4j/issues/486"
*/
public static UnsignedByteArray from(final BigInteger amount, int minFinalByteLength) {
Objects.requireNonNull(amount);
Expand Down

0 comments on commit d700785

Please sign in to comment.