@@ -2,17 +2,29 @@ package org.ergoplatform.kiosk
22
33import org .ergoplatform .kiosk .encoding .ScalaErgoConverters
44import org .bouncycastle .util .encoders .Hex
5- import org .ergoplatform .appkit .{BlockchainContext , ErgoType , ErgoValue , InputBox , OutBox }
5+ import org .ergoplatform .appkit .{
6+ BlockchainContext ,
7+ ErgoType ,
8+ ErgoValue ,
9+ InputBox ,
10+ OutBox
11+ }
612import org .ergoplatform .sdk .ErgoToken
713import sigmastate .SGroupElement
8- import sigmastate .Values .{ByteArrayConstant , CollectionConstant , ErgoTree }
14+ import sigmastate .Values .{
15+ ByteArrayConstant ,
16+ CollectionConstant ,
17+ ErgoTree ,
18+ LongConstant
19+ }
920import sigmastate .crypto .SecP256K1Group
1021import sigmastate .eval .SigmaDsl
1122import sigmastate .serialization .ErgoTreeSerializer .DefaultSerializer
1223import sigmastate .serialization .ValueSerializer
1324import sigma .Coll
1425import sigma .Colls
1526import sigma .GroupElement
27+ import sigmastate .lang .StdSigmaBuilder .mkTuple
1628
1729import scala .io .BufferedSource
1830import scala .util .Try
@@ -22,13 +34,15 @@ package object ergo {
2234 def decodeHex = Hex .decode(string)
2335 }
2436
25- implicit def ByteArrayToBetterByteArray (bytes : Array [Byte ]): BetterByteArray = new BetterByteArray (bytes)
37+ implicit def ByteArrayToBetterByteArray (bytes : Array [Byte ]): BetterByteArray =
38+ new BetterByteArray (bytes)
2639
2740 class BetterByteArray (bytes : Seq [Byte ]) {
2841 def encodeHex : String = Hex .toHexString(bytes.toArray).toLowerCase
2942 }
3043
31- implicit def StringToBetterString (string : String ): BetterString = new BetterString (string)
44+ implicit def StringToBetterString (string : String ): BetterString =
45+ new BetterString (string)
3246
3347 sealed trait KioskType [T ] {
3448 val serialize : Array [Byte ]
@@ -40,71 +54,107 @@ package object ergo {
4054 override def toString = value.toString
4155 }
4256
43- case class KioskCollByte (arrayBytes : Array [Byte ]) extends KioskType [Coll [Byte ]] {
57+ case class KioskCollByte (arrayBytes : Array [Byte ])
58+ extends KioskType [Coll [Byte ]] {
4459 override val value : Coll [Byte ] = Colls .fromArray(arrayBytes)
45- override val serialize : Array [Byte ] = ValueSerializer .serialize(ByteArrayConstant (value))
60+ override val serialize : Array [Byte ] =
61+ ValueSerializer .serialize(ByteArrayConstant (value))
4662 override def toString : String = arrayBytes.encodeHex
4763 override val typeName : String = " Coll[Byte]"
48- override def getErgoValue = ErgoValue .of(arrayBytes)
64+ override def getErgoValue = ErgoValue .of(arrayBytes)
4965 }
5066
51- case class KioskCollGroupElement (groupElements : Array [GroupElement ]) extends KioskType [Coll [GroupElement ]] {
67+ case class KioskCollGroupElement (groupElements : Array [GroupElement ])
68+ extends KioskType [Coll [GroupElement ]] {
5269 override val value : Coll [GroupElement ] = Colls .fromArray(groupElements)
53- override val serialize : Array [Byte ] = ValueSerializer .serialize(CollectionConstant [SGroupElement .type ](value, SGroupElement ))
54- override def toString : String = " [" + groupElements.map(_.getEncoded.toArray.encodeHex).reduceLeft(_ + " ," + _) + " ]"
70+ override val serialize : Array [Byte ] = ValueSerializer .serialize(
71+ CollectionConstant [SGroupElement .type ](value, SGroupElement )
72+ )
73+ override def toString : String = " [" + groupElements
74+ .map(_.getEncoded.toArray.encodeHex)
75+ .reduceLeft(_ + " ," + _) + " ]"
5576 override val typeName : String = " Coll[GroupElement]"
56- override def getErgoValue = ErgoValue .of(groupElements, ErgoType .groupElementType)
77+ override def getErgoValue =
78+ ErgoValue .of(groupElements, ErgoType .groupElementType)
5779 }
5880
5981 case class KioskInt (value : Int ) extends KioskType [Int ] {
6082 override val serialize : Array [Byte ] = ValueSerializer .serialize(value)
61- override val typeName : String = " Int"
62- override def getErgoValue = ErgoValue .of(value)
83+ override val typeName : String = " Int"
84+ override def getErgoValue = ErgoValue .of(value)
6385 }
6486
6587 case class KioskLong (value : Long ) extends KioskType [Long ] {
6688 override val serialize : Array [Byte ] = ValueSerializer .serialize(value)
67- override val typeName : String = " Long"
68- override def getErgoValue = ErgoValue .of(value)
89+ override val typeName : String = " Long"
90+ override def getErgoValue = ErgoValue .of(value)
6991 }
7092
7193 case class KioskBigInt (bigInt : BigInt ) extends KioskType [sigma.BigInt ] {
72- override val value : sigma.BigInt = SigmaDsl .BigInt (bigInt.bigInteger)
94+ override val value : sigma.BigInt = SigmaDsl .BigInt (bigInt.bigInteger)
7395 override val serialize : Array [Byte ] = ValueSerializer .serialize(value)
74- override val typeName : String = " BigInt"
75- override def toString : String = bigInt.toString(10 )
76- override def getErgoValue = ErgoValue .of(bigInt.bigInteger)
96+ override val typeName : String = " BigInt"
97+ override def toString : String = bigInt.toString(10 )
98+ override def getErgoValue = ErgoValue .of(bigInt.bigInteger)
7799 }
78100
79- case class KioskGroupElement (value : GroupElement ) extends KioskType [GroupElement ] {
101+ case class KioskGroupElement (value : GroupElement )
102+ extends KioskType [GroupElement ] {
80103 override val serialize : Array [Byte ] = ValueSerializer .serialize(value)
104+
81105 override def toString : String = value.getEncoded.toArray.encodeHex
82106 override val typeName : String = " GroupElement"
83- override def getErgoValue = ErgoValue .of(value)
84- def + (that : KioskGroupElement ) = KioskGroupElement (value.multiply(that.value))
107+ override def getErgoValue = ErgoValue .of(value)
108+ def + (that : KioskGroupElement ) = KioskGroupElement (
109+ value.multiply(that.value)
110+ )
111+ }
112+
113+ case class KioskTupleLong (value : (Long , Long ))
114+ extends KioskType [Tuple2 [Long , Long ]] {
115+ override val serialize : Array [Byte ] = ValueSerializer .serialize(
116+ mkTuple(Seq (LongConstant (value._1), LongConstant (value._2)))
117+ )
118+ override def toString : String = value.toString()
119+ override val typeName : String = " Tuple[Long, Long]"
120+ override def getErgoValue =
121+ ErgoValue .pairOf(ErgoValue .of(value._1), ErgoValue .of(value._2))
85122 }
86123
87- lazy val PointAtInfinity = KioskGroupElement (SigmaDsl .GroupElement (SecP256K1Group .identity))
124+ lazy val PointAtInfinity = KioskGroupElement (
125+ SigmaDsl .GroupElement (SecP256K1Group .identity)
126+ )
88127
89128 case class KioskErgoTree (value : ErgoTree ) extends KioskType [ErgoTree ] {
90- override val serialize : Array [Byte ] = DefaultSerializer .serializeErgoTree(value)
129+ override val serialize : Array [Byte ] =
130+ DefaultSerializer .serializeErgoTree(value)
91131 override val typeName : String = " ErgoTree"
92132
93133 override def getErgoValue = ??? // should never be needed
94134 override def toString : ID = " <ergo tree>"
95135 }
96136
97- implicit def groupElementToKioskGroupElement (g : GroupElement ): KioskGroupElement = KioskGroupElement (g)
137+ implicit def groupElementToKioskGroupElement (
138+ g : GroupElement
139+ ): KioskGroupElement = KioskGroupElement (g)
98140
99- case class DhtData (g : GroupElement , h : GroupElement , u : GroupElement , v : GroupElement , x : BigInt )
141+ case class DhtData (
142+ g : GroupElement ,
143+ h : GroupElement ,
144+ u : GroupElement ,
145+ v : GroupElement ,
146+ x : BigInt
147+ )
100148
101- type ID = String
149+ type ID = String
102150 type Amount = Long
103151
104- type Token = (ID , Amount )
152+ type Token = (ID , Amount )
105153 type Tokens = Array [Token ]
106154
107- def decodeBigInt (encoded : String ): BigInt = Try (BigInt (encoded, 10 )).recover { case ex => BigInt (encoded, 16 ) }.get
155+ def decodeBigInt (encoded : String ): BigInt = Try (BigInt (encoded, 10 )).recover {
156+ case ex => BigInt (encoded, 16 )
157+ }.get
108158
109159 case class KioskBox (
110160 address : String ,
@@ -120,11 +170,17 @@ package object ergo {
120170 .outBoxBuilder
121171 .value(value)
122172 .tokens(tokens.map(token => new ErgoToken (token._1, token._2)): _* )
123- .contract(ctx.newContract(ScalaErgoConverters .getAddressFromString(address).script))
173+ .contract(
174+ ctx.newContract(
175+ ScalaErgoConverters .getAddressFromString(address).script
176+ )
177+ )
124178 .registers(registers.map(register => register.getErgoValue): _* )
125179 .build()
126180 }
127- def toInBox (txId : String , txIndex : Short )(implicit ctx : BlockchainContext ): InputBox = {
181+ def toInBox (txId : String , txIndex : Short )(implicit
182+ ctx : BlockchainContext
183+ ): InputBox = {
128184 val outBox = toOutBox
129185 outBox.convertToInputWith(txId, txIndex)
130186 }
@@ -138,7 +194,11 @@ package object ergo {
138194 def fromString (str : String ): Value =
139195 values
140196 .find(value => value.toString.equalsIgnoreCase(str))
141- .getOrElse(throw new Exception (s " Invalid op $str. Permitted options are ${values.map(_.toString).reduceLeft(_ + " , " + _)}" ))
197+ .getOrElse(
198+ throw new Exception (
199+ s " Invalid op $str. Permitted options are ${values.map(_.toString).reduceLeft(_ + " , " + _)}"
200+ )
201+ )
142202 def toString (op : Value ): String = op.toString
143203 }
144204}
0 commit comments