From 931228089746605060f42ff9a8c4ac9b49833ec4 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Fri, 26 Jul 2019 17:12:38 -0500 Subject: [PATCH] Making the HtlcHash class implement the JsonSerializable interface --- .../java/cy/agorise/graphenej/HtlcHash.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/graphenej/src/main/java/cy/agorise/graphenej/HtlcHash.java b/graphenej/src/main/java/cy/agorise/graphenej/HtlcHash.java index feffe22..aa0b6d1 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/HtlcHash.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/HtlcHash.java @@ -1,13 +1,16 @@ package cy.agorise.graphenej; import com.google.common.primitives.Bytes; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import cy.agorise.graphenej.interfaces.ByteSerializable; +import cy.agorise.graphenej.interfaces.JsonSerializable; /** * Class used to represent a HTLC hash. */ -public class HtlcHash implements ByteSerializable { +public class HtlcHash implements ByteSerializable, JsonSerializable { private HtlcHashType hashType; private byte[] hash; @@ -16,9 +19,27 @@ public class HtlcHash implements ByteSerializable { this.hash = hash; } + public HtlcHashType getType(){ + return this.hashType; + } + @Override public byte[] toBytes() { byte[] hashTypeBytes = new byte[] { Util.revertInteger(hashType.ordinal())[3] }; return Bytes.concat(hashTypeBytes, hash); } + + @Override + public String toJsonString() { + JsonElement element = toJsonObject(); + return element.toString(); + } + + @Override + public JsonElement toJsonObject() { + JsonArray array = new JsonArray(); + array.add(hashType.ordinal()); + array.add(Util.byteToString(hash)); + return array; + } }