diff --git a/graphenej/src/main/java/cy/agorise/graphenej/Htlc.java b/graphenej/src/main/java/cy/agorise/graphenej/Htlc.java new file mode 100644 index 0000000..6fc462d --- /dev/null +++ b/graphenej/src/main/java/cy/agorise/graphenej/Htlc.java @@ -0,0 +1,43 @@ +package cy.agorise.graphenej; + +import com.google.gson.JsonElement; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.io.IOException; + +import cy.agorise.graphenej.interfaces.ByteSerializable; +import cy.agorise.graphenej.interfaces.JsonSerializable; + +/** + * Class used to represent an existing HTLC contract. + */ +public class Htlc extends GrapheneObject implements ByteSerializable, JsonSerializable { + + public Htlc(String id) { + super(id); + } + + @Override + public byte[] toBytes() { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + DataOutput out = new DataOutputStream(byteArrayOutputStream); + try { + Varint.writeUnsignedVarLong(this.instance, out); + } catch (IOException e) { + e.printStackTrace(); + } + return byteArrayOutputStream.toByteArray(); + } + + @Override + public String toJsonString() { + return this.getObjectId(); + } + + @Override + public JsonElement toJsonObject() { + return null; + } +} diff --git a/graphenej/src/test/java/cy/agorise/graphenej/HtlcTest.java b/graphenej/src/test/java/cy/agorise/graphenej/HtlcTest.java new file mode 100644 index 0000000..c5c02c7 --- /dev/null +++ b/graphenej/src/test/java/cy/agorise/graphenej/HtlcTest.java @@ -0,0 +1,26 @@ +package cy.agorise.graphenej; + +import org.junit.Assert; +import org.junit.Test; + +public class HtlcTest { + private final Htlc htlc = new Htlc("1.16.124"); + + @Test + public void testByteSerialization(){ + Htlc htlc1 = new Htlc("1.16.1"); + Htlc htlc2 = new Htlc("1.16.100"); + Htlc htlc3 = new Htlc("1.16.500"); + Htlc htlc4 = new Htlc("1.16.1000"); + + byte[] expected_1 = Util.hexToBytes("01"); + byte[] expected_2 = Util.hexToBytes("64"); + byte[] expected_3 = Util.hexToBytes("f403"); + byte[] expected_4 = Util.hexToBytes("e807"); + + Assert.assertArrayEquals(expected_1, htlc1.toBytes()); + Assert.assertArrayEquals(expected_2, htlc2.toBytes()); + Assert.assertArrayEquals(expected_3, htlc3.toBytes()); + Assert.assertArrayEquals(expected_4, htlc4.toBytes()); + } +}