diff --git a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/XrplBinaryCodec.java b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/XrplBinaryCodec.java index faa272554..e3dfc2a33 100644 --- a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/XrplBinaryCodec.java +++ b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/XrplBinaryCodec.java @@ -146,7 +146,7 @@ public String encodeForSigningClaim(String json) throws JsonProcessingException } UnsignedByteArray channel = UnsignedByteArray.fromHex(node.get(CHANNEL_FIELD_NAME).asText()); UnsignedByteArray amount = UnsignedByteArray.of( - new UInt64Type(UnsignedLong.valueOf(node.get(AMOUNT_FIELD_NAME).asText()), 16).toBytes() + new UInt64Type(UnsignedLong.valueOf(node.get(AMOUNT_FIELD_NAME).asText())).toBytes() ); UnsignedByteArray byteArray = UnsignedByteArray.empty(); diff --git a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/serdes/BinaryParser.java b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/serdes/BinaryParser.java index 2293c9540..16b8cb998 100644 --- a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/serdes/BinaryParser.java +++ b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/serdes/BinaryParser.java @@ -195,7 +195,7 @@ public > T readType(Class type) { * @return The type associated with the given field. */ public SerializedType typeForField(FieldInstance field) { - return SerializedType.getTypeByName(field.type(), field.name()); + return SerializedType.getTypeByName(field.type()); } /** diff --git a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/serdes/BinarySerializer.java b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/serdes/BinarySerializer.java index 05a31df73..8a53d50b3 100644 --- a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/serdes/BinarySerializer.java +++ b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/serdes/BinarySerializer.java @@ -107,12 +107,8 @@ public void writeFieldAndValue(final FieldInstance field, final SerializedType v public void writeFieldAndValue(final FieldInstance field, final JsonNode value) throws JsonProcessingException { Objects.requireNonNull(field); Objects.requireNonNull(value); - SerializedType typedValue; - if (field.name().equals("BaseFee")) { - typedValue = SerializedType.getTypeByName(field.type(), field.name()).fromHex(value.asText()); - } else { - typedValue = SerializedType.getTypeByName(field.type(), field.name()).fromJson(value); - } + SerializedType typedValue = SerializedType.getTypeByName(field.type()).fromJson(value, field); + writeFieldAndValue(field, typedValue); } diff --git a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/SerializedType.java b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/SerializedType.java index 6dd101a45..ebcc7dc46 100644 --- a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/SerializedType.java +++ b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/SerializedType.java @@ -24,15 +24,15 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.TextNode; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Sets; import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray; import org.xrpl.xrpl4j.codec.binary.BinaryCodecObjectMapperFactory; +import org.xrpl.xrpl4j.codec.binary.definitions.FieldInstance; import org.xrpl.xrpl4j.codec.binary.serdes.BinaryParser; import java.util.Map; import java.util.Objects; -import java.util.Set; import java.util.function.Function; +import java.util.function.Supplier; /** * Defines an abstract type serialization parent-class for all XRPL serialized type definitions. @@ -41,36 +41,28 @@ */ public abstract class SerializedType> { - private static final Set BASE_10_UINT64_FIELD_NAMES = Sets.newHashSet( - "MaximumAmount", "OutstandingAmount", "MPTAmount" - ); + @SuppressWarnings("all") - private static final Map>> typeMap = - new ImmutableMap.Builder>>() - .put("AccountID", fieldName -> new AccountIdType()) - .put("Amount", fieldName -> new AmountType()) - .put("Blob", fieldName -> new BlobType()) - .put("Currency", fieldName -> new CurrencyType()) - .put("Hash128", fieldName -> new Hash128Type()) - .put("Hash160", fieldName -> new Hash160Type()) - .put("Hash192", fieldName -> new Hash192Type()) - .put("Hash256", fieldName -> new Hash256Type()) - .put("PathSet", fieldName -> new PathSetType()) - .put("STArray", fieldName -> new STArrayType()) - .put("STObject", fieldName -> new STObjectType()) - .put("UInt8", fieldName -> new UInt8Type()) - .put("UInt16", fieldName -> new UInt16Type()) - .put("UInt32", fieldName -> new UInt32Type()) - .put("UInt64", fieldName -> { - if (BASE_10_UINT64_FIELD_NAMES.contains(fieldName)) { - return new UInt64Type(10); - } else { - return new UInt64Type(16); - } - }) - .put("Vector256", fieldName -> new Vector256Type()) - .put("Issue", fieldName -> new IssueType()) - .put("XChainBridge", fieldName -> new XChainBridgeType()) + private static final Map>> typeMap = + new ImmutableMap.Builder>>() + .put("AccountID", () -> new AccountIdType()) + .put("Amount", () -> new AmountType()) + .put("Blob", () -> new BlobType()) + .put("Currency", () -> new CurrencyType()) + .put("Hash128", () -> new Hash128Type()) + .put("Hash160", () -> new Hash160Type()) + .put("Hash192", () -> new Hash192Type()) + .put("Hash256", () -> new Hash256Type()) + .put("PathSet", () -> new PathSetType()) + .put("STArray", () -> new STArrayType()) + .put("STObject", () -> new STObjectType()) + .put("UInt8", () -> new UInt8Type()) + .put("UInt16", () -> new UInt16Type()) + .put("UInt32", () -> new UInt32Type()) + .put("UInt64", () -> new UInt64Type()) + .put("Vector256", () -> new Vector256Type()) + .put("Issue", () -> new IssueType()) + .put("XChainBridge", () -> new XChainBridgeType()) .build(); private final UnsignedByteArray bytes; @@ -81,13 +73,12 @@ public SerializedType(UnsignedByteArray bytes) { /** * Get the {@link SerializedType} for the supplied {@code name}. * - * @param name A {@link String} representing the name of a {@link SerializedType}. - * @param fieldName The name of the field that is being serialized. + * @param name A {@link String} representing the name of a {@link SerializedType}. * * @return A {@link SerializedType} for the supplied {@code name}. */ - public static SerializedType getTypeByName(String name, String fieldName) { - return typeMap.get(name).apply(fieldName); + public static SerializedType getTypeByName(String name) { + return typeMap.get(name).get(); } /** @@ -102,7 +93,7 @@ public static String getNameByType(SerializedType type) { .stream() // We only care about the class name, and the String passed to .apply is only used to figure // out the radix of the JSON string for UInt64Types. Plus this method is only used in a test. - .filter(entry -> entry.getValue().apply("").getClass().equals(type.getClass())) + .filter(entry -> entry.getValue().get().getClass().equals(type.getClass())) .map(Map.Entry::getKey) .findAny() .orElse(null); @@ -141,6 +132,10 @@ public T fromParser(BinaryParser parser, int lengthHint) { */ public abstract T fromJson(JsonNode node) throws JsonProcessingException; + public T fromJson(JsonNode node, FieldInstance fieldInstance) throws JsonProcessingException { + return fromJson(node); + } + /** * Construct a concrete instance of {@link SerializedType} from the supplied {@code json}. * @@ -212,6 +207,10 @@ public JsonNode toJson() { return new TextNode(toHex()); } + public JsonNode toJson(FieldInstance fieldInstance) { + return toJson(); + } + /** * Convert this {@link SerializedType} to a hex-encoded {@link String}. * diff --git a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/UInt64Type.java b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/UInt64Type.java index 018561adb..b5957fe06 100644 --- a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/UInt64Type.java +++ b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/UInt64Type.java @@ -22,38 +22,65 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.TextNode; +import com.google.common.collect.Sets; import com.google.common.primitives.UnsignedLong; +import org.xrpl.xrpl4j.codec.binary.definitions.FieldInstance; import org.xrpl.xrpl4j.codec.binary.serdes.BinaryParser; +import java.util.Set; + /** * Codec for XRPL UInt64 type. */ public class UInt64Type extends UIntType { - private final int radix; + private static final Set BASE_10_UINT64_FIELD_NAMES = Sets.newHashSet( + "MaximumAmount", "OutstandingAmount", "MPTAmount" + ); - public UInt64Type(int radix) { - this(UnsignedLong.ZERO, radix); + public UInt64Type() { + this(UnsignedLong.ZERO); } - public UInt64Type(UnsignedLong value, int radix) { + public UInt64Type(UnsignedLong value) { super(value, 64); - this.radix = radix; } @Override public UInt64Type fromParser(BinaryParser parser) { - return new UInt64Type(parser.readUInt64(), radix); + return new UInt64Type(parser.readUInt64()); } @Override public UInt64Type fromJson(JsonNode value) { - // STUInt64s are represented as hex-encoded Strings in JSON. - return new UInt64Type(UnsignedLong.valueOf(value.asText(), radix), radix); + throw new UnsupportedOperationException("Cannot construct UInt64Type from JSON without a FieldInstance. Call " + + "the overload of this method that accepts a FieldInstance instead."); } @Override public JsonNode toJson() { + throw new UnsupportedOperationException("Cannot convert UInt64Type to JSON without a FieldInstance. Call " + + "the overload of this method that accepts a FieldInstance instead."); + } + + @Override + public UInt64Type fromJson(JsonNode value, FieldInstance fieldInstance) { + int radix = getRadix(fieldInstance); + // STUInt64s are represented as hex-encoded Strings in JSON. + return new UInt64Type(UnsignedLong.valueOf(value.asText(), radix)); + } + + @Override + public JsonNode toJson(FieldInstance fieldInstance) { + int radix = getRadix(fieldInstance); return new TextNode(UnsignedLong.valueOf(toHex(), 16).toString(radix).toUpperCase()); } + + private static int getRadix(FieldInstance fieldInstance) { + int radix = 16; + if (BASE_10_UINT64_FIELD_NAMES.contains(fieldInstance.name())) { + radix = 10; + } + return radix; + } } diff --git a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/UIntType.java b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/UIntType.java index 915dcc678..99ba88722 100644 --- a/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/UIntType.java +++ b/xrpl4j-core/src/main/java/org/xrpl/xrpl4j/codec/binary/types/UIntType.java @@ -25,6 +25,7 @@ import com.google.common.primitives.UnsignedLong; import org.xrpl.xrpl4j.codec.addresses.ByteUtils; import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray; +import org.xrpl.xrpl4j.codec.binary.definitions.FieldInstance; /** * Base codec for XRPL UInt types. @@ -50,4 +51,5 @@ UnsignedLong valueOf() { public JsonNode toJson() { return new TextNode(UnsignedLong.valueOf(toHex(), 16).toString()); } + } diff --git a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/codec/binary/types/UInt64TypeUnitTest.java b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/codec/binary/types/UInt64TypeUnitTest.java index 295e3e57d..590b35089 100644 --- a/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/codec/binary/types/UInt64TypeUnitTest.java +++ b/xrpl4j-core/src/test/java/org/xrpl/xrpl4j/codec/binary/types/UInt64TypeUnitTest.java @@ -29,8 +29,7 @@ import org.junit.jupiter.params.provider.ValueSource; public class UInt64TypeUnitTest { - private final UInt64Type base16Type = new UInt64Type(16); - private final UInt64Type base10Type = new UInt64Type(10); + private final UInt64Type base16Type = new UInt64Type(); private static UnsignedLong maxUint64 = UnsignedLong.valueOf("FFFFFFFFFFFFFFFF", 16); @Test @@ -50,28 +49,6 @@ void encodeBase16() { assertThat(base16Type.fromJson("\"FFFFFFFFFFFFFFFF\"").toHex()).isEqualTo("FFFFFFFFFFFFFFFF"); } - @Test - void decodeBase10() { - assertThat(base10Type.fromHex("0000000000000000").valueOf()).isEqualTo(UnsignedLong.valueOf(0)); - assertThat(base10Type.fromHex("000000000000000F").valueOf()).isEqualTo(UnsignedLong.valueOf(15)); - assertThat(base10Type.fromHex("00000000FFFFFFFF").valueOf()).isEqualTo(UnsignedLong.valueOf(4294967295L)); - assertThat(base10Type.fromHex("FFFFFFFFFFFFFFFF").valueOf()).isEqualTo(maxUint64); - } - - @Test - void encodeBase10() { - assertThat(base10Type.fromJson("\"0\"").toHex()).isEqualTo("0000000000000000"); - assertThat(base10Type.fromJson("\"15\"").toHex()).isEqualTo("000000000000000F"); - assertThat(base10Type.fromJson("\"65535\"").toHex()).isEqualTo("000000000000FFFF"); - assertThat(base10Type.fromJson("\"4294967295\"").toHex()).isEqualTo("00000000FFFFFFFF"); - assertThat(base10Type.fromJson("\"18446744073709551615\"").toHex()).isEqualTo("FFFFFFFFFFFFFFFF"); - } - - @ParameterizedTest - @ValueSource(strings = {"\"0\"", "\"15\"", "\"65535\"", "\"4294967295\"", "\"18446744073709551615\""}) - void toFromJsonBase10(String json) { - assertThat(base10Type.fromJson(json).toJson().toString()).isEqualTo(json); - } @ParameterizedTest @ValueSource(strings = {"\"0\"", "\"F\"", "\"FFFF\"", "\"FFFFFFFF\"", "\"FFFFFFFFFFFFFFFF\""})