Skip to content

Commit

Permalink
Merge pull request #917 from ScorexFoundation/core-serializers2
Browse files Browse the repository at this point in the history
Move TypeSerializer and DataSerializer to core module (part 2)
  • Loading branch information
aslesarenko authored Oct 21, 2023
2 parents 0b9279e + dc23717 commit 3435531
Show file tree
Hide file tree
Showing 271 changed files with 3,476 additions and 3,089 deletions.
52 changes: 20 additions & 32 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import scala.language.postfixOps
import scala.sys.process._
import org.scalajs.linker.interface.CheckedBehavior
import scala.sys.process.*
import org.scalajs.linker.interface.{CheckedBehavior, ModuleSplitStyle}

organization := "org.scorexfoundation"

Expand Down Expand Up @@ -196,7 +196,12 @@ lazy val core = crossProject(JVMPlatform, JSPlatform)
scorexUtilDependency,
publish / skip := true
)
.jvmSettings( crossScalaSettings )
.jvmSettings(
crossScalaSettings,
libraryDependencies ++= Seq(
bouncycastleBcprov
)
)
.jsSettings(
crossScalaSettingsJS,
scalacOptions ++= Seq(
Expand All @@ -212,6 +217,18 @@ lazy val core = crossProject(JVMPlatform, JSPlatform)
)
lazy val coreJS = core.js
.enablePlugins(ScalaJSBundlerPlugin)
.enablePlugins(ScalablyTypedConverterGenSourcePlugin)
.settings(
stOutputPackage := "sigmastate",
scalaJSLinkerConfig ~= { conf =>
conf.withSourceMap(false)
.withModuleKind(ModuleKind.CommonJSModule)
},
Compile / npmDependencies ++= Seq(
"sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion,
"@fleet-sdk/common" -> "0.1.3"
)
)

lazy val interpreter = crossProject(JVMPlatform, JSPlatform)
.in(file("interpreter"))
Expand All @@ -226,24 +243,10 @@ lazy val interpreter = crossProject(JVMPlatform, JSPlatform)
.jvmSettings( crossScalaSettings )
.jsSettings(
crossScalaSettingsJS,
libraryDependencies ++= Seq (
"org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0"
),
useYarn := true
)
lazy val interpreterJS = interpreter.js
.enablePlugins(ScalaJSBundlerPlugin)
.enablePlugins(ScalablyTypedConverterGenSourcePlugin)
.settings(
stOutputPackage := "sigmastate",
scalaJSLinkerConfig ~= { conf =>
conf.withSourceMap(false)
},
Compile / npmDependencies ++= Seq(
"sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion,
"@fleet-sdk/common" -> "0.1.3"
)
)

lazy val parsers = crossProject(JVMPlatform, JSPlatform)
.in(file("parsers"))
Expand All @@ -260,9 +263,6 @@ lazy val parsers = crossProject(JVMPlatform, JSPlatform)
)
.jsSettings(
crossScalaSettingsJS,
libraryDependencies ++= Seq(
"org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0"
),
useYarn := true
)
lazy val parsersJS = parsers.js
Expand All @@ -271,9 +271,6 @@ lazy val parsersJS = parsers.js
scalaJSLinkerConfig ~= { conf =>
conf.withSourceMap(false)
},
Compile / npmDependencies ++= Seq(
"sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion
)
)

lazy val sdk = crossProject(JVMPlatform, JSPlatform)
Expand All @@ -290,9 +287,6 @@ lazy val sdk = crossProject(JVMPlatform, JSPlatform)
)
.jsSettings(
crossScalaSettingsJS,
libraryDependencies ++= Seq(
"org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0"
),
useYarn := true
)
lazy val sdkJS = sdk.js
Expand All @@ -302,9 +296,6 @@ lazy val sdkJS = sdk.js
conf.withSourceMap(false)
.withModuleKind(ModuleKind.CommonJSModule)
},
Compile / npmDependencies ++= Seq(
"sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion
)
)

lazy val sc = crossProject(JVMPlatform, JSPlatform)
Expand Down Expand Up @@ -348,9 +339,6 @@ lazy val scJS = sc.js
.withArrayIndexOutOfBounds(CheckedBehavior.Compliant)
)
},
Compile / npmDependencies ++= Seq(
"sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion
)
)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sigmastate.crypto
package sigma.crypto

import sigma.data.RType
import scorex.util.encode.Base16
Expand Down Expand Up @@ -129,7 +129,7 @@ object Platform {
class Curve

// TODO JS: Use JS library for secure source of randomness
type SecureRandom = sigmastate.crypto.SecureRandomJS
type SecureRandom = sigma.crypto.SecureRandomJS

/** Opaque point type. */
@js.native
Expand Down Expand Up @@ -185,28 +185,28 @@ object Platform {
private val ctx = new CryptoContextJs

/** The underlying elliptic curve descriptor. */
override def curve: crypto.Curve = ???
override def curve: Curve = ???

override def fieldCharacteristic: BigInteger = Convert.bigIntToBigInteger(ctx.getModulus())

override def order: BigInteger = Convert.bigIntToBigInteger(ctx.getOrder())

override def validatePoint(x: BigInteger, y: BigInteger): crypto.Ecp = {
override def validatePoint(x: BigInteger, y: BigInteger): Ecp = {
val point = ctx.validatePoint(Convert.bigIntegerToBigInt(x), Convert.bigIntegerToBigInt(y))
new Ecp(point)
}

override def infinity(): crypto.Ecp =
override def infinity(): Ecp =
new Ecp(ctx.getInfinity())

override def decodePoint(encoded: Array[Byte]): crypto.Ecp = {
override def decodePoint(encoded: Array[Byte]): Ecp = {
if (encoded(0) == 0) {
return infinity()
}
new Ecp(ctx.decodePoint(Base16.encode(encoded)))
}

override def generator: crypto.Ecp =
override def generator: Ecp =
new Ecp(ctx.getGenerator())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sigmastate.crypto
package sigma.crypto

import debox.cfor

Expand Down
50 changes: 50 additions & 0 deletions core/js/src/main/scala/sigma/js/AvlTree.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sigma.js

import sigma.Extensions.ArrayOps
import sigma.data.Iso.{isoStringToArray, isoStringToColl}
import sigma.data.{AvlTreeData, AvlTreeFlags, CAvlTree, Iso}

import scala.scalajs.js
import scala.scalajs.js.UndefOr
import scala.scalajs.js.annotation.JSExportTopLevel

/** Equivalent of [[sigma.AvlTree]] available from JS. */
@JSExportTopLevel("AvlTree")
class AvlTree(
val digest: String,
val insertAllowed: Boolean,
val updateAllowed: Boolean,
val removeAllowed: Boolean,
val keyLength: Int,
val valueLengthOpt: UndefOr[Int]
) extends js.Object

object AvlTree {

implicit val isoAvlTree: Iso[AvlTree, sigma.AvlTree] = new Iso[AvlTree, sigma.AvlTree] {
override def to(x: AvlTree): sigma.AvlTree = {
CAvlTree(
AvlTreeData(
digest = isoStringToArray.to(x.digest).toColl,
treeFlags = AvlTreeFlags(x.insertAllowed, x.updateAllowed, x.removeAllowed),
x.keyLength,
valueLengthOpt = sigma.js.Isos.isoUndefOr(Iso.identityIso[Int]).to(x.valueLengthOpt),
),
)
}

override def from(x: sigma.AvlTree): AvlTree = {
val tree = x.asInstanceOf[CAvlTree]
val data = tree.treeData
new AvlTree(
digest = isoStringToColl.from(tree.digest),
insertAllowed = data.treeFlags.insertAllowed,
updateAllowed = data.treeFlags.updateAllowed,
removeAllowed = data.treeFlags.removeAllowed,
keyLength = data.keyLength,
valueLengthOpt = sigma.js.Isos.isoUndefOr(Iso.identityIso[Int]).from(data.valueLengthOpt),
)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.ergoplatform.sdk.js
package sigma.js

import sigmastate.crypto.{CryptoFacade, CryptoFacadeJs, Ecp, Platform}
import sigmastate.eval.Extensions.ArrayByteOps
import sigma.Extensions.CoreArrayByteOps
import sigma.crypto.{CryptoFacade, CryptoFacadeJs, Ecp, Platform}

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportTopLevel
Expand Down
29 changes: 29 additions & 0 deletions core/js/src/main/scala/sigma/js/Isos.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sigma.js

import sigma.{Coll, Colls}
import sigma.data.{Iso, RType}

import scala.reflect.ClassTag
import scala.scalajs.js
import scala.scalajs.js.JSConverters.JSRichOption

object Isos {

implicit def isoUndefOr[A, B](implicit iso: Iso[A, B]): Iso[js.UndefOr[A], Option[B]] = new Iso[js.UndefOr[A], Option[B]] {
override def to(x: js.UndefOr[A]): Option[B] = x.toOption.map(iso.to)
override def from(x: Option[B]): js.UndefOr[A] = x.map(iso.from).orUndefined
}

implicit def isoArrayToColl[A, B](iso: Iso[A, B])
(implicit ctA: ClassTag[A], tB: RType[B]): Iso[js.Array[A], Coll[B]] = new Iso[js.Array[A], Coll[B]] {
override def to(x: js.Array[A]): Coll[B] = Colls.fromArray(x.map(iso.to).toArray(tB.classTag))
override def from(x: Coll[B]): js.Array[A] = js.Array(x.toArray.map(iso.from): _*)
}

implicit def isoArrayToIndexed[A, B](iso: Iso[A, B])
(implicit cB: ClassTag[B]): Iso[js.Array[A], IndexedSeq[B]] = new Iso[js.Array[A], IndexedSeq[B]] {
override def to(x: js.Array[A]): IndexedSeq[B] = x.map(iso.to).toArray(cB).toIndexedSeq
override def from(x: IndexedSeq[B]): js.Array[A] = js.Array(x.map(iso.from): _*)
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.ergoplatform.sdk.js
package sigma.js

import sigmastate.Values.SigmaBoolean
import sigmastate.crypto.DLogProtocol.ProveDlog
import sigma.data.{ProveDlog, SigmaBoolean}

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportTopLevel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.ergoplatform.sdk.js
package sigma.js

import sigma.data.RType

Expand All @@ -10,7 +10,7 @@ import scala.scalajs.js.annotation.JSExportTopLevel
* wrapper around {@link RType} type descriptor.
*/
@JSExportTopLevel("Type")
class Type(private[js] final val rtype: RType[_]) extends js.Object {
class Type(final val rtype: RType[_]) extends js.Object {
/** Syntactically correct type name (type expression as String) */
def name: String = rtype.name

Expand Down
Loading

0 comments on commit 3435531

Please sign in to comment.