Skip to content

Commit

Permalink
Update PrivateKey to support natural and prefixed bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
sappenin committed Sep 29, 2023
1 parent 4885d58 commit 5088272
Show file tree
Hide file tree
Showing 11 changed files with 540 additions and 114 deletions.
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.
*
* @return An {@link UnsignedByteArray} containing the decoded public key.
*
* @see "https://xrpl.org/base58-encodings.html"
*/
public UnsignedByteArray decodeNodePrivateKey(final String privateKeyBytes) {
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.
*
* @return An {@link UnsignedByteArray} containing the decoded public key.
*
* @see "https://xrpl.org/base58-encodings.html"
*/
public UnsignedByteArray decodeAccountPrivateKey(final String publicKey) {
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 @@ -23,10 +23,12 @@
public enum Version {

ED25519_SEED(new int[] {0x01, 0xE1, 0x4B}),
FAMILY_SEED(new int[] {0x21}),
FAMILY_SEED(new int[] {0x21}), // 33 in decimal
ACCOUNT_ID(new int[] {0}),
NODE_PUBLIC(new int[] {0x1C}),
ACCOUNT_PUBLIC_KEY(new int[] {0x23});
NODE_PUBLIC(new int[] {0x1C}), // 28 in decimal
NODE_PRIVATE(new int[] {0x20}), // 32 in decimal
ACCOUNT_SECRET_KEY(new int[] {0x22}), // 34 in decimal
ACCOUNT_PUBLIC_KEY(new int[] {0x23}); // 35 in decimal

private final int[] values;

Expand Down
Loading

0 comments on commit 5088272

Please sign in to comment.