Skip to content

Commit

Permalink
Print: Fix typing issues with for issuance box
Browse files Browse the repository at this point in the history
  • Loading branch information
ldgaetano committed May 4, 2023
1 parent 401a6e6 commit 8c9fbc9
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 130 deletions.
78 changes: 78 additions & 0 deletions src/main/scala/app/EIP24IssuerBoxPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,82 @@ case class EIP24IssuerBoxPrinter(ergoClient: ErgoClient, networkType: NetworkTyp

}

private def processCollection(coll: Any)(implicit tag: ClassTag[_]): String = {

(coll, tag.runtimeClass) match {

case (royalties: Coll[_], t) if t == classOf[Coll[(Coll[Byte], Integer)]] =>

val royaltiesTyped = royalties.asInstanceOf[Coll[(Coll[Byte], Integer)]]
val stringColl: Coll[(String, String)] = royaltiesTyped.map(r => {
val address: ErgoTree = ErgoTreeSerializer.DefaultSerializer.deserializeErgoTree(r._1.toArray)
val share: Integer = r._2
(address.bytesHex, share.toString)
})

stringColl.toString()

case (traits: (_, _), t) if t == classOf[(Coll[(Coll[Byte], Coll[Byte])], (Coll[(Coll[Byte], (Integer, Integer))], Coll[(Coll[Byte], (Integer, Integer))]))] =>

val traitsTyped = traits.asInstanceOf[(Coll[(Coll[Byte], Coll[Byte])], (Coll[(Coll[Byte], (Integer, Integer))], Coll[(Coll[Byte], (Integer, Integer))]))]
val properties: Coll[(Coll[Byte], Coll[Byte])] = traitsTyped._1
val levels: Coll[(Coll[Byte], (Integer, Integer))] = traitsTyped._2._1
val stats: Coll[(Coll[Byte], (Integer, Integer))] = traitsTyped._2._2

val propertiesStringColl: Coll[(String, String)] = properties.map(p => {

val keyByteArray: Array[Byte] = JavaHelpers.collToByteArray(p._1)
val valueByteArray: Array[Byte] = JavaHelpers.collToByteArray(p._2)
val key: String = new String(keyByteArray, Charset.defaultCharset())
val value: String = new String(valueByteArray, Charset.defaultCharset())

(key, value)

})

val levelsStringColl: Coll[(String, String)] = stats.map(l => {

val keyByteArray: Array[Byte] = JavaHelpers.collToByteArray(l._1)
val key: String = new String(keyByteArray, Charset.defaultCharset())
val valueMax: String = l._2.toString()

(key, valueMax)

})

val statsStringColl: Coll[(String, String)] = levels.map(s => {

val keyByteArray: Array[Byte] = JavaHelpers.collToByteArray(s._1)
val key: String = new String(keyByteArray, Charset.defaultCharset())
val valueMax: String = s._2.toString()

(key, valueMax)

})

"{ properties: " + propertiesStringColl.toString() + ", levels: " + levelsStringColl.toString() + ", stats: " + statsStringColl.toString() + "}"

case (info: Coll[_], t) if t == classOf[Coll[(Coll[Byte], Coll[Byte])]] =>

val infoTyped = info.asInstanceOf[Coll[(Coll[Byte], Coll[Byte])]]
val infoStringColl: Coll[(String, String)] = infoTyped.map(i => {

val keyByteArray: Array[Byte] = JavaHelpers.collToByteArray(i._1)
val key: String = new String(keyByteArray, Charset.defaultCharset())

val valueByteArray: Array[Byte] = JavaHelpers.collToByteArray(i._2)
val value: String = new String(valueByteArray, Charset.defaultCharset())

(key, value)

})

infoStringColl.toString()

case _ => ""

}

}

}
75 changes: 48 additions & 27 deletions src/main/scala/app/EIP4IssuanceBoxPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import special.collection.Coll

import java.nio.charset.Charset
import scala.collection.JavaConverters._
import scala.reflect._


abstract class EIP4IssuanceBoxPrinter(ergoClient: ErgoClient, networkType: NetworkType, issuanceBoxId: String) extends ErgoBoxPrinter {
Expand Down Expand Up @@ -42,51 +43,43 @@ abstract class EIP4IssuanceBoxPrinter(ergoClient: ErgoClient, networkType: Netwo
private def printTokenVerboseName(): Unit = {

val reg: ErgoValue[_] = registers(0)
val decodedTokenName: String = reg.getValue match {
case name: Coll[Byte] =>
val nameByteArray: Array[Byte] = JavaHelpers.collToByteArray(name)
val nameString: String = new String(nameByteArray, Charset.defaultCharset())
nameString
}

val regValue = reg.getValue
val classTag = reg.getType.getRType.classTag
val decodedTokenName: String = processStringCollection(regValue)(classTag)
println("Register 4 (token name): " + decodedTokenName)

}

private def printTokenDescription(): Unit = {

val reg: ErgoValue[_] = registers(1)
val decodedDescrptionName: String = reg.getValue match {
case description: Coll[Byte] =>
val descriptionByteArray: Array[Byte] = JavaHelpers.collToByteArray(description)
val descriptionString: String = new String(descriptionByteArray, Charset.defaultCharset())
descriptionString
}

println("Register 5 (token description): " + decodedDescrptionName)
val reg = registers(1)
val regValue = reg.getValue
val classTag = reg.getType.getRType.classTag
val decodedDescriptionName: String = processStringCollection(regValue)(classTag)
println("Register 5 (token description): " + decodedDescriptionName)

}

private def printNumberOfDecimals(): Unit = {

val reg: ErgoValue[_] = registers(2)
val decodedDecimals: String = reg.getValue match {
case decimals: Coll[Byte] =>
val decimalsByteArray: Array[Byte] = JavaHelpers.collToByteArray(decimals)
val decimalsString: String = new String(decimalsByteArray, Charset.defaultCharset())
decimalsString
}
val reg = registers(2)
val regValue = reg.getValue
val classTag = reg.getType.getRType.classTag
val decodedDecimals: String = processStringCollection(regValue)(classTag)

println("Register 6 (number of decimals): " + decodedDecimals)

}

private def printAssetType(): Unit = {
private def printAssetType(): Unit = {

val reg = registers(3)
val decodedAssetType: String = reg.getValue match {
case assetType: Coll[Byte] =>
val assetTypeByteArray: Array[Byte] = JavaHelpers.collToByteArray(assetType)
val regValue = reg.getValue
val runtime = reg.getType.getRType.classTag.runtimeClass
val decodedAssetType: String = (regValue, runtime) match {
case (assetType: Coll[_], t) if t == classOf[Coll[Byte]] =>
val assetTypeTyped = assetType.asInstanceOf[Coll[Byte]]
val assetTypeByteArray: Array[Byte] = JavaHelpers.collToByteArray(assetTypeTyped)
assetTypeByteArray.mkString("Coll(", ", ", ")")
}

Expand All @@ -97,4 +90,32 @@ abstract class EIP4IssuanceBoxPrinter(ergoClient: ErgoClient, networkType: Netwo
protected def printRegister8(): Unit
protected def printRegister9(): Unit

protected def processStringCollection(coll: Any)(implicit tag: ClassTag[_]): String = {
(coll, tag.runtimeClass) match {
case (string: Coll[_], t) if t == classOf[Coll[Byte]] =>
val stringTyped = string.asInstanceOf[Coll[Byte]]
val stringByteArray: Array[Byte] = JavaHelpers.collToByteArray(stringTyped)
new String(stringByteArray, Charset.defaultCharset())
}
}

protected def processHash(coll: Any)(implicit tag: ClassTag[_]): String = {
(coll, tag.runtimeClass) match {

case (hash: Coll[_], t) if t == classOf[Coll[Byte]] =>

val hashTyped = hash.asInstanceOf[Coll[Byte]]
val hashByteArray: Array[Byte] = JavaHelpers.collToByteArray(hashTyped)
val hashString: String = hashByteArray.map("%02x".format(_)).mkString
hashString

case (hashColl: Coll[_], t) if t == classOf[Coll[Coll[Byte]]] =>

val hashCollTyped = hashColl.asInstanceOf[Coll[Coll[Byte]]]
val hashStringColl: Coll[String] = hashCollTyped.map(h => processHash(h)(ClassTag(h.getClass)))
hashStringColl.toString()
}

}

}
27 changes: 9 additions & 18 deletions src/main/scala/app/EIP4PictureNFTPrinter.scala
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
package app

import org.ergoplatform.appkit.{ErgoClient, Iso, JavaHelpers, NetworkType}
import special.collection.Coll

import java.nio.charset.Charset
import org.ergoplatform.appkit.{ErgoClient, NetworkType}

case class EIP4PictureNFTPrinter(ergoClient: ErgoClient, networkType: NetworkType, issuanceBoxId: String) extends EIP4IssuanceBoxPrinter(ergoClient, networkType, issuanceBoxId) {


override protected def printRegister8(): Unit = {

val reg8 = registers(4)
val decodedHashString: String = reg8.getValue match {
case hash: Coll[Byte] =>
val hashByteArray: Array[Byte] = JavaHelpers.collToByteArray(hash)
val hashString: String = hashByteArray.map("%02x".format(_)).mkString
hashString
}
val reg = registers(4)
val regValue = reg.getValue
val classTag = reg.getType.getRType.classTag
val decodedHashString: String = processHash(regValue)(classTag)

println("Register 8 (sha256 picture hash): " + decodedHashString)

}

override protected def printRegister9(): Unit = {

val reg9 = registers(5)
val decodedLink: String = reg9.getValue match {
case link: Coll[Byte] =>
val linkByteArray: Array[Byte] = JavaHelpers.collToByteArray(link)
val linkString: String = new String(linkByteArray, Charset.defaultCharset())
linkString
}
val reg = registers(5)
val regValue = reg.getValue
val classTag = reg.getType.getRType.classTag
val decodedLink: String = processStringCollection(regValue)(classTag)

println("Register 9 (picture link): " + decodedLink)

Expand Down
86 changes: 1 addition & 85 deletions src/main/scala/app/ErgoBoxPrinter.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package app

import org.ergoplatform.appkit.{ErgoValue, InputBox, JavaHelpers}
import sigmastate.Values.ErgoTree
import sigmastate.serialization.ErgoTreeSerializer
import special.collection.Coll

import java.nio.charset.Charset
import org.ergoplatform.appkit.{ErgoValue, InputBox}
import scala.collection.JavaConverters._
import scala.reflect.ClassTag

trait ErgoBoxPrinter {

Expand Down Expand Up @@ -45,82 +39,4 @@ trait ErgoBoxPrinter {
}
def printDecodedRegisters(): Unit

protected def processCollection(coll: Any)(implicit tag: ClassTag[_]): String = {

(coll, tag.runtimeClass) match {

case (royalties: Coll[_], t) if t == classOf[Coll[(Coll[Byte], Integer)]] =>

val royaltiesTyped = royalties.asInstanceOf[Coll[(Coll[Byte], Integer)]]
val stringColl: Coll[(String, String)] = royaltiesTyped.map(r => {
val address: ErgoTree = ErgoTreeSerializer.DefaultSerializer.deserializeErgoTree(r._1.toArray)
val share: Integer = r._2
(address.bytesHex, share.toString)
})

stringColl.toString()

case (traits: (_, _), t) if t == classOf[(Coll[(Coll[Byte], Coll[Byte])], (Coll[(Coll[Byte], (Integer, Integer))], Coll[(Coll[Byte], (Integer, Integer))]))] =>

val traitsTyped = traits.asInstanceOf[(Coll[(Coll[Byte], Coll[Byte])], (Coll[(Coll[Byte], (Integer, Integer))], Coll[(Coll[Byte], (Integer, Integer))]))]
val properties: Coll[(Coll[Byte], Coll[Byte])] = traitsTyped._1
val levels: Coll[(Coll[Byte], (Integer, Integer))] = traitsTyped._2._1
val stats: Coll[(Coll[Byte], (Integer, Integer))] = traitsTyped._2._2

val propertiesStringColl: Coll[(String, String)] = properties.map(p => {

val keyByteArray: Array[Byte] = JavaHelpers.collToByteArray(p._1)
val valueByteArray: Array[Byte] = JavaHelpers.collToByteArray(p._2)
val key: String = new String(keyByteArray, Charset.defaultCharset())
val value: String = new String(valueByteArray, Charset.defaultCharset())

(key, value)

})

val levelsStringColl: Coll[(String, String)] = stats.map(l => {

val keyByteArray: Array[Byte] = JavaHelpers.collToByteArray(l._1)
val key: String = new String(keyByteArray, Charset.defaultCharset())
val valueMax: String = l._2.toString()

(key, valueMax)

})

val statsStringColl: Coll[(String, String)] = levels.map(s => {

val keyByteArray: Array[Byte] = JavaHelpers.collToByteArray(s._1)
val key: String = new String(keyByteArray, Charset.defaultCharset())
val valueMax: String = s._2.toString()

(key, valueMax)

})

"{ properties: " + propertiesStringColl.toString() + ", levels: " + levelsStringColl.toString() + ", stats: " + statsStringColl.toString() + "}"

case (info: Coll[_], t) if t == classOf[Coll[(Coll[Byte], Coll[Byte])]] =>

val infoTyped = info.asInstanceOf[Coll[(Coll[Byte], Coll[Byte])]]
val infoStringColl: Coll[(String, String)] = infoTyped.map(i => {

val keyByteArray: Array[Byte] = JavaHelpers.collToByteArray(i._1)
val key: String = new String(keyByteArray, Charset.defaultCharset())

val valueByteArray: Array[Byte] = JavaHelpers.collToByteArray(i._2)
val value: String = new String(valueByteArray, Charset.defaultCharset())

(key, value)

})

infoStringColl.toString()

case _ => ""

}

}

}

0 comments on commit 8c9fbc9

Please sign in to comment.