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

Support for NetworkID #461

Merged
merged 6 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
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 @@ -37,6 +37,7 @@
import org.immutables.value.Value;
import org.xrpl.xrpl4j.model.client.common.LedgerIndex;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.NetworkId;
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;

import java.io.IOException;
Expand Down Expand Up @@ -177,6 +178,14 @@ default boolean isLedgerInCompleteLedgers(final UnsignedLong ledgerIndex) {
@JsonProperty("validation_quorum")
Optional<UnsignedInteger> validationQuorum();

/**
* The {@link NetworkId} of the network that this server is connected to.
*
* @return An optionally-present {@link NetworkId}.
*/
@JsonProperty("network_id")
Optional<NetworkId> networkId();

/**
* Deserializes complete_ledgers field in the server_info response from hyphen-separated ledger indices to list of
* range of UnsignedLong values.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.xrpl.xrpl4j.model.jackson.modules;

/*-
* ========================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.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.google.common.primitives.UnsignedInteger;
import org.xrpl.xrpl4j.model.transactions.NetworkId;

import java.io.IOException;

/**
* Custom Jackson deserializer for {@link NetworkId}s.
*/
public class NetworkIdDeserializer extends StdDeserializer<NetworkId> {

/**
* No-args constructor.
*/
public NetworkIdDeserializer() {
super(NetworkId.class);
}

@Override
public NetworkId deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
return NetworkId.of(UnsignedInteger.valueOf(jsonParser.getValueAsLong()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.xrpl.xrpl4j.model.jackson.modules;

/*-
* ========================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.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import org.xrpl.xrpl4j.model.transactions.NetworkId;

import java.io.IOException;

/**
* Custom Jackson serializer for {@link NetworkId}s.
*/
public class NetworkIdSerializer extends StdScalarSerializer<NetworkId> {

/**
* No-args constructor.
*/
public NetworkIdSerializer() {
super(NetworkId.class, false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
super(NetworkId.class, false);
super(NetworkId.class);

}

@Override
public void serialize(NetworkId networkId, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumber(networkId.value().longValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,7 @@ default PublicKey signingPublicKey() {
@JsonProperty("TxnSignature")
Optional<Signature> transactionSignature();

@JsonProperty("NetworkID")
Optional<NetworkId> networkId();

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.xrpl.xrpl4j.model.jackson.modules.Hash256Serializer;
import org.xrpl.xrpl4j.model.jackson.modules.MarkerDeserializer;
import org.xrpl.xrpl4j.model.jackson.modules.MarkerSerializer;
import org.xrpl.xrpl4j.model.jackson.modules.NetworkIdDeserializer;
import org.xrpl.xrpl4j.model.jackson.modules.NetworkIdSerializer;
import org.xrpl.xrpl4j.model.jackson.modules.NfTokenIdDeserializer;
import org.xrpl.xrpl4j.model.jackson.modules.NfTokenIdSerializer;
import org.xrpl.xrpl4j.model.jackson.modules.NfTokenUriSerializer;
Expand Down Expand Up @@ -350,6 +352,7 @@ public String toString() {
* Construct {@link TransferFee} as a percentage value.
*
* @param percent of type {@link BigDecimal}
*
* @return {@link TransferFee}
*/
static TransferFee ofPercent(BigDecimal percent) {
Expand All @@ -374,4 +377,30 @@ public void validateBounds() {

}

/**
* A wrapped {@link com.google.common.primitives.UnsignedInteger} containing a Network ID.
*/
@Value.Immutable
@Wrapped
@JsonSerialize(as = NetworkId.class, using = NetworkIdSerializer.class)
@JsonDeserialize(as = NetworkId.class, using = NetworkIdDeserializer.class)
abstract static class _NetworkId extends Wrapper<UnsignedInteger> implements Serializable {

@Override
public String toString() {
return this.value().toString();
}

/**
* Construct a {@link NetworkId} from a {@code long}. The supplied value must be less than or equal to
* 4,294,967,295, the largest unsigned 32-bit integer.
*
* @param networkId A {@code long}.
*
* @return A {@link NetworkId}.
*/
public static NetworkId of(long networkId) {
return NetworkId.of(UnsignedInteger.valueOf(networkId));
}
}
}
Loading
Loading