Skip to content

Commit

Permalink
add serializer and deserializer for Signature (#305)
Browse files Browse the repository at this point in the history
* add serializer and deserializer for `Signature`
* WrappedSignature example interface and test
* fix checkstyle
* add `as = ImmutableSignature.class` back

Co-authored-by: David Fuelling <[email protected]>
  • Loading branch information
mukulljangid and sappenin authored Oct 7, 2022
1 parent e9aa7c4 commit 3a48818
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* Represents a digital signature for a transaction that can be submitted to the XRP Ledger.
*/
@Value.Immutable
@JsonSerialize(as = ImmutableSignature.class)
@JsonDeserialize(as = ImmutableSignature.class)
@JsonSerialize(as = ImmutableSignature.class, using = SignatureSerializer.class)
@JsonDeserialize(as = ImmutableSignature.class, using = SignatureDeserializer.class)
public interface Signature {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.xrpl.xrpl4j.crypto.core.signing;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray;

import java.io.IOException;

/**
* Deserializes signature string value to an object of type {@link Signature}.
*/
class SignatureDeserializer extends JsonDeserializer<Signature> {

@Override
public Signature deserialize(JsonParser jsonParser, DeserializationContext ctxt)
throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
return Signature.builder().value(UnsignedByteArray.fromHex(node.asText())).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.xrpl.xrpl4j.crypto.core.signing;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
* Custom Jackson serializer for {@link Signature}es.
*/
class SignatureSerializer extends JsonSerializer<Signature> {
@Override
public void serialize(Signature signature, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeString(signature.base16Value());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.io.IOException;

/**
* A FasterXML serializer for {@link UnsignedByteArray}.
* A FasterXML deserializer for {@link UnsignedByteArray}.
*
* @deprecated This class will go away once {@link UnsignedByteArray} is moved into the core module.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import static org.hamcrest.Matchers.is;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.jayway.jsonassert.JsonAssert;
import org.immutables.value.Value;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray;
Expand Down Expand Up @@ -46,7 +50,8 @@ void hexValue() {
@Test
void jsonSerializeAndDeserialize() throws JsonProcessingException {
String json = ObjectMapperFactory.create().writeValueAsString(signature);
JsonAssert.with(json).assertThat("$.value", is(HEX_32_BYTES));
JsonAssert.with(json).assertThat("$", is(HEX_32_BYTES));
assertThat(json).isEqualTo("\"" + HEX_32_BYTES + "\"");

Signature actual = ObjectMapperFactory.create().readValue(json, Signature.class);
assertThat(actual).isEqualTo(signature);
Expand All @@ -61,4 +66,36 @@ void of() {
void fromBase16() {
assertThat(Signature.fromBase16(HEX_32_BYTES)).isEqualTo(signature);
}

@Test
void serializeSignature() throws JsonProcessingException {

ObjectMapper objectMapper = ObjectMapperFactory.create();

WrappedSignature wrappedSignature = WrappedSignature.builder()
.signature(signature)
.build();
String serializedWrappedSignature = objectMapper.writeValueAsString(wrappedSignature);

String serialized = "{\"signature\":\"" + HEX_32_BYTES + "\"}";
assertThat(serializedWrappedSignature).isEqualTo(serialized);

WrappedSignature deserializedWrappedSignature = objectMapper.readValue(
serializedWrappedSignature, WrappedSignature.class
);
assertThat(deserializedWrappedSignature).isEqualTo(wrappedSignature);

}

@Value.Immutable
@JsonSerialize(as = ImmutableWrappedSignature.class)
@JsonDeserialize(as = ImmutableWrappedSignature.class)
interface WrappedSignature {

static ImmutableWrappedSignature.Builder builder() {
return ImmutableWrappedSignature.builder();
}

Signature signature();
}
}

0 comments on commit 3a48818

Please sign in to comment.