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 all 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 privateKeyBase58 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 privateKeyBase58) {
Objects.requireNonNull(privateKeyBase58);

return AddressBase58.decode(
privateKeyBase58,
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 privateKeyBase58 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 privateKeyBase58) {
Objects.requireNonNull(privateKeyBase58);

return AddressBase58.decode(
privateKeyBase58,
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 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
Loading