Skip to content

Commit

Permalink
tx-signing-js: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Aug 20, 2023
1 parent b0d6202 commit ace1cdb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ sealed class MapSigmaValidationSettings(private val map: Map[Short, (ValidationR

override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj match {
case that: MapSigmaValidationSettings =>
map.iterator.forall { case (id, v) => that.map.get(id).exists(_ == v) }
map.size == that.map.size &&
map.iterator.forall { case (id, v) => that.map.get(id).exists(_ == v) }
case _ => false
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ trait ObjectGenerators extends TypeGenerators
regs <- Gen.sequence(additionalRegistersGen(regNum))(Buildable.buildableSeq)
} yield
Map(regs.toIndexedSeq:_*)
// mutable.LinkedHashMap(regs.toIndexedSeq.sortBy(_._1.number):_*)

def ergoBoxTokens(availableTokens: Seq[TokenId]): Gen[Coll[Token]] = for {
tokens <-
Expand Down
14 changes: 7 additions & 7 deletions sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ trait JsonCodecs {

implicit val contextExtensionEncoder: Encoder[ContextExtension] = Encoder.instance({ extension =>
Json.obj(extension.values.toSeq.map { case (key, value) =>
key.toString -> evaluatedValueEncoder.apply(value)
key.toString -> evaluatedValueEncoder(value)
}: _*)
})

Expand Down Expand Up @@ -369,7 +369,7 @@ trait JsonCodecs {

implicit val ergoLikeTransactionEncoder: Encoder[ErgoLikeTransaction] = Encoder.instance({ tx =>
Json.obj(
"type" -> "elt".asJson, // ErgoLikeTransaction
"type" -> "ELT".asJson, // ErgoLikeTransaction
"id" -> tx.id.asJson,
"inputs" -> tx.inputs.asJson,
"dataInputs" -> tx.dataInputs.asJson,
Expand All @@ -380,15 +380,15 @@ trait JsonCodecs {
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 <- {require(t == "ELT"); 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
"type" -> "UELT".asJson, // UnsignedErgoLikeTransaction
"id" -> tx.id.asJson,
"inputs" -> tx.inputs.asJson,
"dataInputs" -> tx.dataInputs.asJson,
Expand All @@ -399,7 +399,7 @@ trait JsonCodecs {
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 <- {require(t == "UELT"); 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 @@ -415,8 +415,8 @@ trait JsonCodecs {
for {
t <- cursor.downField("type").as[String]
tx <- t match {
case "elt" => ergoLikeTransactionDecoder(cursor)
case "uelt" => unsignedErgoLikeTransactionDecoder(cursor)
case "ELT" => ergoLikeTransactionDecoder(cursor)
case "UELT" => unsignedErgoLikeTransactionDecoder(cursor)
}
} yield tx
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,49 +113,11 @@ class JsonSerializationSpec extends SerializationSpecification with JsonCodecs {
}

property("ErgoLikeTransaction should be encoded into JSON and decoded back correctly") {
forAll(ergoLikeTransactionGen, MinSuccessful(500)) { v: ErgoLikeTransaction => jsonRoundTrip(v) }
forAll(ergoLikeTransactionGen, MinSuccessful(50)) { v: ErgoLikeTransaction => jsonRoundTrip(v) }
}

property("UnsignedErgoLikeTransaction should be encoded into JSON and decoded back correctly") {
forAll(unsignedErgoLikeTransactionGen) { v: UnsignedErgoLikeTransaction => jsonRoundTrip(v) }
}

private def sortRegisters(box: ErgoBoxCandidate): ErgoBoxCandidate = box match {
case box: ErgoBox =>
new ErgoBox(box.value,
box.ergoTree,
box.additionalTokens,
mutable.LinkedHashMap(box.additionalRegisters.toIndexedSeq.sortBy(_._1.number): _*),
box.transactionId,
box.index,
box.creationHeight
)
case box: ErgoBoxCandidate =>
new ErgoBoxCandidate(box.value,
box.ergoTree,
box.creationHeight,
box.additionalTokens,
mutable.LinkedHashMap(box.additionalRegisters.toIndexedSeq.sortBy(_._1.number): _*)
)
}

private def sortRegisters(tx: ErgoLikeTransactionTemplate[_ <: UnsignedInput]): ErgoLikeTransactionTemplate[_ <: UnsignedInput] = {
tx match {
case tx: ErgoLikeTransaction =>
new ErgoLikeTransaction(tx.inputs,
tx.dataInputs,
tx.outputCandidates.map { out =>
sortRegisters(out)
}
)
case tx: UnsignedErgoLikeTransaction =>
new UnsignedErgoLikeTransaction(tx.inputs,
tx.dataInputs,
tx.outputCandidates.map { out =>
sortRegisters(out)
}
)
}
forAll(unsignedErgoLikeTransactionGen, MinSuccessful(50)) { v: UnsignedErgoLikeTransaction => jsonRoundTrip(v) }
}

property("ErgoLikeTransactionTemplate should be encoded into JSON and decoded back correctly") {
Expand Down

0 comments on commit ace1cdb

Please sign in to comment.