Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v5.0.11-fix1' into v5.0.12-RC
Browse files Browse the repository at this point in the history
# Conflicts:
#	sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/protocol/context/BlockchainStateContext.scala
  • Loading branch information
aslesarenko committed Sep 28, 2023
2 parents c21fd9c + 4c53864 commit 0845e0b
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 31 deletions.
15 changes: 15 additions & 0 deletions docs/LangSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,21 @@ trait SigmaProp {
* 2. new ErgoTree created with with ErgoTree.DefaultHeader, EmptyConstant and SigmaPropConstant as the root
*
* Thus obtained ErgoTree is serialized using DefaultSerializer and compared with `box.propositionBytes`.
* Here, propBytes uses very specific ErgoTree with header == 0, and hence ErgoTree.version == 0.
* However, `box.propositionBytes`, contain bytes of ErgoTree from `box`, and version of that
* tree depends on how the `box` was created.
* A better way to compare `box.propositionBytes` and `prop.propBytes` is to use the following code:
* ```
* val propBytes = prop.propBytes
* val treeBytes = box.propositionBytes
* if (treeBytes(0) == 0) {
* treeBytes == propBytes
* } else {
* // offset = 1 + <number of VLQ encoded bytes to store propositionBytes.size>
* val offset = if (propositionBytes.size > 127) 3 else 2
* propBytes.slice(1, propBytes.size) == propositionBytes.slice(offset, propositionBytes.size)
* }
* ```
*/
def propBytes: Coll[Byte]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,22 @@ object ExtractCreationInfo extends SimpleTransformerCompanion {

trait Deserialize[V <: SType] extends NotReadyValue[V]

/** Extracts context variable as Coll[Byte], deserializes it to script and then executes this script in the current context.
* The original `Coll[Byte]` of the script is available as `getVar[Coll[Byte]](id)`
/** This ErgoTree operation work as macros (i.e. executed before ErgoTree reduction) and
* allows to inline arbitrary expression at the point where it is used in ErgoTree.
*
* This operation is executed before ErgoTree reduction as the following:
* 1) the context variable `id` is checked to have type Coll[Byte], if not, then this
* node remains in the ErgoTree which means the reduction will fail later.
* 2) the bytes collection is deserialized to `expr: Value[V]` using ValueSerializer.deserialize
* 3) `this` node is replaced with the deserialized `expr`
*
* This step are performed for each [[DeserializeContext]] node via single traverse of
* ErgoTree. The resulting ErgoTree is passed to reduction.
*
* NOTE, the original `Coll[Byte]` from context variables is available as `getVar[Coll[Byte]](id)`
*
* @param id identifier of the context variable
* @tparam V result type of the deserialized script.
* @throws InterpreterException if the actual script type doesn't conform to T
* @return result of the script execution in the current context
* @since 2.0
* @param tpe expected type of the deserialized script (i.e. expected type of `expr`).
*/
case class DeserializeContext[V <: SType](id: Byte, tpe: V) extends Deserialize[V] {
override def companion = DeserializeContext
Expand Down
14 changes: 14 additions & 0 deletions sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,17 @@ class ProverBuilder(parameters: BlockchainParameters, network: Byte) extends js.
new SigmaProver(p)
}
}

@JSExportTopLevel("ProverBuilderObj")
object ProverBuilder extends js.Object {
/** Creates a new [[ProverBuilder]] instance with the given blockchain parameters
* and network prefix.
*
* @param parameters Blockchain parameters re-adjustable via miners voting and
* voting-related data. All of them are included into extension
* section of a first block of a voting epoch.
* @param network Network prefix to use for addresses.
*/
def create(parameters: BlockchainParameters, network: Byte): ProverBuilder =
new ProverBuilder(parameters, network)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.scalajs.js.annotation.JSExportTopLevel
@JSExportTopLevel("ReducedTransaction")
class ReducedTransaction(val _tx: sdk.ReducedTransaction) extends js.Object {
/** Serialized bytes of this transaction in hex format. */
def toHex: String = _tx.toHex
def toHex(): String = _tx.toHex
}

@JSExportTopLevel("ReducedTransactionObj")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class SigmaProver(_prover: sdk.SigmaProver) extends js.Object {
* The returned address corresponds to the master secret derived from the mnemonic
* phrase configured in the [[ProverBuilder]].
*/
def getP2PKAddress: String = {
def getP2PKAddress(): String = {
val addr = _prover.getP2PKAddress
addr.toString
}

/** Returns the prover's secret key. */
def getSecretKey: js.BigInt =
def getSecretKey(): js.BigInt =
isoBigInt.from(_prover.getSecretKey)

/** Returns a sequence of EIP-3 addresses associated with the prover's secret keys. */
def getEip3Addresses: js.Array[String] = {
/** Returns an array of EIP-3 addresses associated with the prover's secret keys. */
def getEip3Addresses(): js.Array[String] = {
val addresses = _prover.getEip3Addresses
js.Array(addresses.map(_.toString): _*)
}
Expand Down
25 changes: 8 additions & 17 deletions sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -368,37 +368,33 @@ trait JsonCodecs {

implicit val ergoLikeTransactionEncoder: Encoder[ErgoLikeTransaction] = Encoder.instance({ tx =>
Json.obj(
"type" -> "ELT".asJson, // ErgoLikeTransaction
"id" -> tx.id.asJson,
"inputs" -> tx.inputs.asJson,
"dataInputs" -> tx.dataInputs.asJson,
"outputs" -> tx.outputCandidates.asJson
"outputs" -> tx.outputs.asJson
)
})

implicit val ergoLikeTransactionDecoder: Decoder[ErgoLikeTransaction] = Decoder.instance({ implicit cursor =>
for {
t <- cursor.downField("type").as[String]
inputs <- {require(t == "ELT"); cursor.downField("inputs").as[IndexedSeq[Input]] }
inputs <- cursor.downField("inputs").as[IndexedSeq[Input]]
dataInputs <- cursor.downField("dataInputs").as[IndexedSeq[DataInput]]
outputs <- cursor.downField("outputs").as[IndexedSeq[ErgoBoxCandidate]]
} yield new ErgoLikeTransaction(inputs, dataInputs, outputs)
})

implicit val unsignedErgoLikeTransactionEncoder: Encoder[UnsignedErgoLikeTransaction] = Encoder.instance({ tx =>
Json.obj(
"type" -> "UELT".asJson, // UnsignedErgoLikeTransaction
"id" -> tx.id.asJson,
"inputs" -> tx.inputs.asJson,
"dataInputs" -> tx.dataInputs.asJson,
"outputs" -> tx.outputCandidates.asJson
"outputs" -> tx.outputs.asJson
)
})

implicit val unsignedErgoLikeTransactionDecoder: Decoder[UnsignedErgoLikeTransaction] = Decoder.instance({ implicit cursor =>
for {
t <- cursor.downField("type").as[String]
inputs <- {require(t == "UELT"); cursor.downField("inputs").as[IndexedSeq[UnsignedInput]] }
inputs <- cursor.downField("inputs").as[IndexedSeq[UnsignedInput]]
dataInputs <- cursor.downField("dataInputs").as[IndexedSeq[DataInput]]
outputs <- cursor.downField("outputs").as[IndexedSeq[ErgoBoxCandidate]]
} yield new UnsignedErgoLikeTransaction(inputs, dataInputs, outputs)
Expand All @@ -410,15 +406,10 @@ trait JsonCodecs {
case t => throw new SigmaException(s"Don't know how to encode transaction $t")
})

implicit val ergoLikeTransactionTemplateDecoder: Decoder[ErgoLikeTransactionTemplate[_ <: UnsignedInput]] = Decoder.instance({ implicit cursor =>
for {
t <- cursor.downField("type").as[String]
tx <- t match {
case "ELT" => ergoLikeTransactionDecoder(cursor)
case "UELT" => unsignedErgoLikeTransactionDecoder(cursor)
}
} yield tx
})
implicit val ergoLikeTransactionTemplateDecoder: Decoder[ErgoLikeTransactionTemplate[_ <: UnsignedInput]] = {
ergoLikeTransactionDecoder.asInstanceOf[Decoder[ErgoLikeTransactionTemplate[_ <: UnsignedInput]]] or
unsignedErgoLikeTransactionDecoder.asInstanceOf[Decoder[ErgoLikeTransactionTemplate[_ <: UnsignedInput]]]
}

implicit val sigmaValidationSettingsEncoder: Encoder[SigmaValidationSettings] = Encoder.instance({ v =>
SigmaValidationSettingsSerializer.toBytes(v).asJson
Expand Down
4 changes: 2 additions & 2 deletions sigma-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sigma-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sigmastate-js",
"version": "0.2.2",
"version": "0.3.0",
"description": "Sigma.js library",
"files": [
"dist/",
Expand Down
Loading

0 comments on commit 0845e0b

Please sign in to comment.