Making the HtlcHash class implement the JsonSerializable interface

This commit is contained in:
Nelson R. Perez 2019-07-26 17:12:38 -05:00
parent ca30338af6
commit 9312280897

View file

@ -1,13 +1,16 @@
package cy.agorise.graphenej; package cy.agorise.graphenej;
import com.google.common.primitives.Bytes; 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.ByteSerializable;
import cy.agorise.graphenej.interfaces.JsonSerializable;
/** /**
* Class used to represent a HTLC hash. * Class used to represent a HTLC hash.
*/ */
public class HtlcHash implements ByteSerializable { public class HtlcHash implements ByteSerializable, JsonSerializable {
private HtlcHashType hashType; private HtlcHashType hashType;
private byte[] hash; private byte[] hash;
@ -16,9 +19,27 @@ public class HtlcHash implements ByteSerializable {
this.hash = hash; this.hash = hash;
} }
public HtlcHashType getType(){
return this.hashType;
}
@Override @Override
public byte[] toBytes() { public byte[] toBytes() {
byte[] hashTypeBytes = new byte[] { Util.revertInteger(hashType.ordinal())[3] }; byte[] hashTypeBytes = new byte[] { Util.revertInteger(hashType.ordinal())[3] };
return Bytes.concat(hashTypeBytes, hash); 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;
}
} }