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

ErgoValue with Java types #160

Merged
merged 2 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -6,17 +6,14 @@

import java.math.BigInteger;

import scala.Byte;
import scala.Int;
import scala.Long;
import special.collection.Coll;
import special.sigma.BigInt;

public class ErgoValueTest {

@Test
public void testTypeDeclarations() {
ErgoValue<Int> intErgoValue = ErgoValue.of(1);
ErgoValue<Integer> intErgoValue = ErgoValue.of(1);
ErgoValue<Long> longErgoValue = ErgoValue.of(0L);
ErgoValue<Byte> byteErgoValue = ErgoValue.of((byte) 1);
ErgoValue<BigInt> bigIntErgoValue = ErgoValue.of(BigInteger.ZERO);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ergoplatform.appkit;

import scala.Byte;
import scala.Tuple2;
import special.collection.Coll;

Expand All @@ -21,7 +20,7 @@ public interface Eip29Attachment {
/**
* @return returns full ErgoValue for this attachment, to use for register 9 of out boxes
*/
ErgoValue<Tuple2<Coll<Byte>, Tuple2<scala.Int, Coll<Byte>>>> getErgoValue();
ErgoValue<Tuple2<Coll<Byte>, Tuple2<Integer, Coll<Byte>>>> getErgoValue();

/**
* @return array R4-R9 to use with OutboxBuilder#registers
Expand Down
6 changes: 3 additions & 3 deletions common/src/main/java/org/ergoplatform/appkit/Eip4Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,23 +210,23 @@ public String getNftCoverImageLink() {
* @return value of register 4 of token minting box
*/
@Nonnull
public ErgoValue<Coll<scala.Byte>> getMintingBoxR4() {
public ErgoValue<Coll<Byte>> getMintingBoxR4() {
return ErgoValue.of(name.getBytes(StandardCharsets.UTF_8));
}

/**
* @return value of register 5 of token minting box
*/
@Nonnull
public ErgoValue<Coll<scala.Byte>> getMintingBoxR5() {
public ErgoValue<Coll<Byte>> getMintingBoxR5() {
return ErgoValue.of(description.getBytes(StandardCharsets.UTF_8));
}

/**
* @return value of register 6 of token minting box
*/
@Nonnull
public ErgoValue<Coll<scala.Byte>> getMintingBoxR6() {
public ErgoValue<Coll<Byte>> getMintingBoxR6() {
return ErgoValue.of(Integer.toString(decimals).getBytes(StandardCharsets.UTF_8));
}

Expand Down
20 changes: 10 additions & 10 deletions common/src/main/java/org/ergoplatform/appkit/ErgoType.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* wrapper around {@link RType} type descriptor.
*/
public class ErgoType<T> {
private static ErgoType<scala.Byte> _byte = new ErgoType(RType.ByteType());
private static ErgoType<scala.Short> _short = new ErgoType(RType.ShortType());
private static ErgoType<scala.Int> _integer = new ErgoType(RType.IntType());
private static ErgoType<scala.Long> _long = new ErgoType(RType.LongType());
private static ErgoType<scala.Boolean> _boolean = new ErgoType(RType.BooleanType());
private static ErgoType<Byte> _byte = new ErgoType(RType.ByteType());
private static ErgoType<Short> _short = new ErgoType(RType.ShortType());
private static ErgoType<Integer> _integer = new ErgoType(RType.IntType());
private static ErgoType<Long> _long = new ErgoType(RType.LongType());
private static ErgoType<Boolean> _boolean = new ErgoType(RType.BooleanType());
private static ErgoType<BigInt> _bigInt = new ErgoType(JavaHelpers.BigIntRType());
private static ErgoType<Unit> _unit = new ErgoType(RType.UnitType());
private static ErgoType<GroupElement> _groupElement = new ErgoType(JavaHelpers.GroupElementRType());
Expand Down Expand Up @@ -51,15 +51,15 @@ public boolean equals(Object obj) {
return (obj instanceof ErgoType<?>) && Objects.equals(_rtype, ((ErgoType<?>)obj)._rtype);
}

static public ErgoType<scala.Byte> byteType() { return _byte; }
static public ErgoType<Byte> byteType() { return _byte; }

static public ErgoType<scala.Short> shortType() { return _short; }
static public ErgoType<Short> shortType() { return _short; }

static public ErgoType<scala.Int> integerType() { return _integer; }
static public ErgoType<Integer> integerType() { return _integer; }

static public ErgoType<scala.Long> longType() { return _long; }
static public ErgoType<Long> longType() { return _long; }

static public ErgoType<scala.Boolean> booleanType() { return _boolean; }
static public ErgoType<Boolean> booleanType() { return _boolean; }

static public ErgoType<BigInt> bigIntType() { return _bigInt; }

Expand Down
16 changes: 8 additions & 8 deletions common/src/main/java/org/ergoplatform/appkit/ErgoValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,23 @@ public boolean equals(Object obj) {
return false;
}

static public ErgoValue<scala.Byte> of(byte value) {
static public ErgoValue<Byte> of(byte value) {
return new ErgoValue(Iso.jbyteToByte().to(Byte.valueOf(value)), ErgoType.byteType());
}

static public ErgoValue<scala.Short> of(short value) {
static public ErgoValue<Short> of(short value) {
return new ErgoValue(Iso.jshortToShort().to(Short.valueOf(value)), ErgoType.shortType());
}

static public ErgoValue<scala.Int> of(int value) {
static public ErgoValue<Integer> of(int value) {
return new ErgoValue(Iso.jintToInt().to(value), ErgoType.integerType());
}

static public ErgoValue<scala.Long> of(long value) {
static public ErgoValue<Long> of(long value) {
return new ErgoValue(Iso.jlongToLong().to(Long.valueOf(value)), ErgoType.longType());
}

static public ErgoValue<scala.Boolean> of(boolean value) {
static public ErgoValue<Boolean> of(boolean value) {
return new ErgoValue(Iso.jboolToBool().to(Boolean.valueOf(value)), ErgoType.booleanType());
}

Expand Down Expand Up @@ -117,10 +117,10 @@ static public ErgoValue<Box> of(ErgoBox value) {
return new ErgoValue<>(JavaHelpers.SigmaDsl().Box(value), ErgoType.boxType());
}

static public ErgoValue<Coll<scala.Byte>> of(byte[] arr) {
static public ErgoValue<Coll<Byte>> of(byte[] arr) {
Coll value = JavaHelpers.collFrom(arr);
ErgoType<Coll<scala.Byte>> type = ErgoType.collType(ErgoType.byteType());
return new ErgoValue<Coll<scala.Byte>>(value, type);
ErgoType<Coll<Byte>> type = ErgoType.collType(ErgoType.byteType());
return new ErgoValue<Coll<Byte>>(value, type);
}

static public <A, B> ErgoValue<Tuple2<A, B>> pairOf(ErgoValue<A> val1, ErgoValue<B> val2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Arrays;
import java.util.List;

import scala.Byte;
import scala.Tuple2;
import special.collection.Coll;

Expand Down Expand Up @@ -34,8 +33,8 @@ public int getTypeRawValue() {
}

@Override
public ErgoValue<Tuple2<Coll<Byte>, Tuple2<scala.Int, Coll<Byte>>>> getErgoValue() {
ErgoValue<Tuple2<scala.Int, Coll<scala.Byte>>> contentPair = ErgoValue.pairOf(
public ErgoValue<Tuple2<Coll<Byte>, Tuple2<Integer, Coll<Byte>>>> getErgoValue() {
ErgoValue<Tuple2<Integer, Coll<Byte>>> contentPair = ErgoValue.pairOf(
ErgoValue.of(getTypeRawValue()),
ErgoValue.of(attachmentContent));
return ErgoValue.pairOf(ErgoValue.of(MAGIC_BYTES), contentPair);
Expand Down Expand Up @@ -78,11 +77,11 @@ public static Eip29Attachment createFromErgoValue(ErgoValue<?> r9) {
throw new IllegalArgumentException(illegalArgumentException + r9.toHex());
}

return createFromAttachmentTuple((Tuple2<scala.Int, Coll<Byte>>) attachmentValue);
return createFromAttachmentTuple((Tuple2<Integer, Coll<Byte>>) attachmentValue);
}

private static Eip29Attachment createFromAttachmentTuple(Tuple2<scala.Int, Coll<Byte>> attachmentTuple) {
int typeConstant = attachmentTuple._1.toInt();
private static Eip29Attachment createFromAttachmentTuple(Tuple2<Integer, Coll<Byte>> attachmentTuple) {
Integer typeConstant = attachmentTuple._1;
GenericEip29Attachment.Type attachmentType = GenericEip29Attachment.Type.fromTypeRawValue(typeConstant);
byte[] attachmentContent = ScalaHelpers.collByteToByteArray(attachmentTuple._2);

Expand Down Expand Up @@ -124,7 +123,7 @@ public static GenericEip29Attachment.PlainTextAttachment buildForText(String tex
* Attachment containing list of attachments
*/
public static class MultiAttachment extends GenericEip29Attachment {
private final Tuple2<scala.Int, Coll<Byte>>[] attachmentList;
private final Tuple2<Integer, Coll<Byte>>[] attachmentList;

private MultiAttachment(byte[] attachmentContent) {
super(Type.MULTI_ATTACHMENT.toTypeRawValue(), attachmentContent);
Expand All @@ -137,7 +136,7 @@ private MultiAttachment(byte[] attachmentContent) {
attachmentList = (Tuple2[]) ((Coll<?>) attachmentTuples.getValue()).toArray();
}

private MultiAttachment(byte[] attachmentContent, Tuple2<scala.Int, Coll<Byte>>[] attachmentList) {
private MultiAttachment(byte[] attachmentContent, Tuple2<Integer, Coll<Byte>>[] attachmentList) {
super(Type.MULTI_ATTACHMENT.toTypeRawValue(), attachmentContent);
this.attachmentList = attachmentList;
}
Expand All @@ -161,12 +160,12 @@ public int getAttachmentCount() {
* @return object representing a multi attachment
*/
public static GenericEip29Attachment.MultiAttachment buildForList(List<Eip29Attachment> attachments) {
List<Tuple2<scala.Int, Coll<Byte>>> attachmentTuples = new ArrayList<>(attachments.size());
List<Tuple2<Integer, Coll<Byte>>> attachmentTuples = new ArrayList<>(attachments.size());
for (Eip29Attachment attachment : attachments) {
attachmentTuples.add(attachment.getErgoValue().getValue()._2);
}
Tuple2<scala.Int, Coll<Byte>>[] tupleArray = attachmentTuples.toArray(new Tuple2[]{});
ErgoValue<Coll<Tuple2<scala.Int, Coll<Byte>>>> ergoValue = ErgoValue.of(tupleArray, ErgoType.pairType(ErgoType.integerType(), ErgoType.collType(ErgoType.byteType())));
Tuple2<Integer, Coll<Byte>>[] tupleArray = attachmentTuples.toArray(new Tuple2[]{});
ErgoValue<Coll<Tuple2<Integer, Coll<Byte>>>> ergoValue = ErgoValue.of(tupleArray, ErgoType.pairType(ErgoType.integerType(), ErgoType.collType(ErgoType.byteType())));

return new MultiAttachment(ergoValue.toHex().getBytes(StandardCharsets.UTF_8), tupleArray);
}
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/org/ergoplatform/appkit/JavaHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ object Iso extends LowPriorityIsos {
override def from(a: Boolean): JBoolean = a
}

implicit def collToColl[A: RType, B: RType](implicit iso: Iso[A, B]): Iso[Coll[A], Coll[B]] = new Iso[Coll[A], Coll[B]] {
override def to(as: Coll[A]): Coll[B] = as.map(iso.to)
override def from(bs: Coll[B]): Coll[A] = bs.map(iso.from)
}

implicit val isoErgoTokenToPair: Iso[ErgoToken, (TokenId, Long)] = new Iso[ErgoToken, (TokenId, Long)] {
override def to(a: ErgoToken) = (Digest32 @@ a.getId.getBytes, a.getValue)
override def from(t: (TokenId, Long)): ErgoToken = new ErgoToken(t._1, t._2)
Expand Down Expand Up @@ -255,6 +260,11 @@ object JavaHelpers {
}

implicit val TokenIdRType: RType[TokenId] = RType.arrayRType[Byte].asInstanceOf[RType[TokenId]]
implicit val JByteRType: RType[JByte] = RType.ByteType.asInstanceOf[RType[JByte]]
implicit val JShortRType: RType[JShort] = RType.ShortType.asInstanceOf[RType[JShort]]
implicit val JIntRType: RType[JInt] = RType.IntType.asInstanceOf[RType[JInt]]
implicit val JLongRType: RType[JLong] = RType.LongType.asInstanceOf[RType[JLong]]
implicit val JBooleanRType: RType[JBoolean] = RType.BooleanType.asInstanceOf[RType[JBoolean]]

val HeaderRType: RType[Header] = special.sigma.HeaderRType
val PreHeaderRType: RType[special.sigma.PreHeader] = special.sigma.PreHeaderRType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ergoplatform.appkit;

import scala.Byte;
import special.collection.Coll;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.ergoplatform.appkit

import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks
import org.scalatest.{PropSpec, Matchers}
import org.scalatest.{Matchers, PropSpec}
import scalan.RType
import scorex.util.encode.Base16
import sigmastate._
import sigmastate.Values.Constant
import sigmastate.eval.SigmaDsl
import sigmastate.serialization.ValueSerializer
import sigmastate.serialization.generators.ObjectGenerators
import JavaHelpers._
import special.collection.Coll

class ErgoValueSpec extends PropSpec with Matchers with ScalaCheckDrivenPropertyChecks
with AppkitTestingCommon with ObjectGenerators {
Expand Down Expand Up @@ -39,7 +41,7 @@ class ErgoValueSpec extends PropSpec with Matchers with ScalaCheckDrivenProperty
hex shouldBe "1a0203010203020a14"

val t = ErgoType.collType(ErgoType.byteType)
val collV = ErgoValue.of(coll, t)
val collV = ErgoValue.of(coll.convertTo[Coll[Coll[java.lang.Byte]]], t)
collV.toHex shouldBe hex

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ private static Eip4Token buildNftToken(@Nonnull String id, long amount, @Nonnull
@Nullable String linkToCoverImage) {


ErgoValue<Coll<scala.Byte>> r7 = ErgoValue.of(type.getR7ByteArrayForType());
ErgoValue<Coll<scala.Byte>> r8 = ErgoValue.of(sha256ContentHash);
ErgoValue<Coll<Byte>> r7 = ErgoValue.of(type.getR7ByteArrayForType());
ErgoValue<Coll<Byte>> r8 = ErgoValue.of(sha256ContentHash);
ErgoValue<?> r9;

if (linkToContent != null && linkToCoverImage == null)
Expand Down