Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromel777 committed Aug 3, 2022
1 parent 073c093 commit 9ee5e64
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package org.ergoplatform.ergo.domain

import derevo.circe.encoder
import derevo.derive
import cats.syntax.either._
import io.circe.Decoder
import org.ergoplatform.common.HexString
import org.ergoplatform.ergo.{PubKey, SErgoTree}
import org.ergoplatform.ergo.PubKey
import org.ergoplatform.ergo.domain.SigmaType.SimpleKindSigmaType._
import org.ergoplatform.ergo.domain.SigmaType._
import scodec.Codec
import scodec.bits._
import shapeless._
import scodec.{Attempt, Codec, Err}
import scodec.codecs._
import scodec.codecs.{Discriminated, int16, short, uint4, uint8}
import scodec.codecs.uint8
import tofu.logging.derivation.loggable

@derive(encoder, loggable)
Expand All @@ -37,9 +36,20 @@ object SConstant {
final case class ByteaConstant(value: HexString) extends SConstant

object ByteaConstant {

implicit val codec: Codec[ByteaConstant] =
scodec.codecs.variableSizeBits(uint16, utf8)
.xmap(str => ByteaConstant(HexString.unsafeFromString(str)), _.value.unwrapped)
scodec.codecs
.variableSizeBits(uint16, utf8)
.exmap(
str =>
Attempt.fromEither(
Either
.catchNonFatal(HexString.unsafeFromString(str))
.map(ByteaConstant(_))
.leftMap(err => Err(err.getMessage))
),
const => Attempt.successful(const.value.unwrapped)
)
}

@derive(encoder, loggable)
Expand All @@ -53,8 +63,10 @@ object SConstant {
final case class UnresolvedConstant(raw: String) extends SConstant

object UnresolvedConstant {

implicit val codec: Codec[UnresolvedConstant] =
scodec.codecs.variableSizeBits(uint16, utf8)
scodec.codecs
.variableSizeBits(uint16, utf8)
.xmap(UnresolvedConstant(_), _.raw)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import io.estatico.newtype.ops._
import org.ergoplatform.common.HexString
import org.ergoplatform.common.errors.RefinementFailed
import org.ergoplatform.ergo.CurrencyId
import org.ergoplatform.ergo.domain.SConstant.ByteaConstant
import org.ergoplatform.ergo.syntax.PubKeyOps
import pureconfig.ConfigReader
import pureconfig.error.CannotConvert
import scodec.{Attempt, Err}
import scodec.bits.ByteVector
import scorex.crypto.hash.Sha256
import scorex.util.encode.Base16
Expand Down Expand Up @@ -242,7 +244,18 @@ package object ergo {
def unsafeFromString(s: String): SErgoTree = SErgoTree(HexString.unsafeFromString(s))

implicit def codec: scodec.Codec[SErgoTree] =
scodec.codecs.variableSizeBits(uint16, utf8).xmap(str => SErgoTree(HexString.unsafeFromString(str)), _.unwrapped)
scodec.codecs
.variableSizeBits(uint16, utf8)
.exmap(
str =>
Attempt.fromEither(
Either
.catchNonFatal(HexString.unsafeFromString(str))
.map(SErgoTree(_))
.leftMap(err => Err(err.getMessage))
),
tree => Attempt.successful(tree.value.unwrapped)
)
}

@newtype case class ErgoTreeTemplate(value: HexString) {
Expand Down Expand Up @@ -298,6 +311,17 @@ package object ergo {
def unsafeFromString(s: String): PubKey = PubKey(HexString.unsafeFromString(s))

implicit val codec: scodec.Codec[PubKey] =
scodec.codecs.variableSizeBits(uint16, utf8).xmap(str => PubKey(HexString.unsafeFromString(str)), _.unwrapped)
scodec.codecs
.variableSizeBits(uint16, utf8)
.exmap(
str =>
Attempt.fromEither(
Either
.catchNonFatal(HexString.unsafeFromString(str))
.map(PubKey(_))
.leftMap(err => Err(err.getMessage))
),
tree => Attempt.successful(tree.value.unwrapped)
)
}
}

0 comments on commit 9ee5e64

Please sign in to comment.