Skip to content

Commit

Permalink
tx-signing-js: ReducedTransaction exported to JS and can be serialise…
Browse files Browse the repository at this point in the history
…d and deserialized
  • Loading branch information
aslesarenko committed Aug 17, 2023
1 parent b948a31 commit 4ec1b49
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import debox.{cfor, Buffer => DBuffer}
import org.ergoplatform.ErgoBox
import org.ergoplatform.ErgoBox.TokenId
import scalan.{Nullable, RType}
import scorex.util.encode.Base16
import sigmastate.SType.AnyOps
import sigmastate.Values.{Constant, ConstantNode}
import sigmastate.crypto.{CryptoFacade, Ecp}
Expand Down Expand Up @@ -41,6 +42,8 @@ object Extensions {
implicit class ArrayByteOps(val arr: Array[Byte]) extends AnyVal {
/** Wraps array into TokenId instance. The source array in not cloned. */
@inline def toTokenId: TokenId = Digest32Coll @@ Colls.fromArray(arr)
/** Encodes array into hex string */
@inline def toHex: String = Base16.encode(arr)
}

implicit class EvalIterableOps[T: RType](seq: Iterable[T]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ergoplatform.sdk.js

import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix
import org.ergoplatform.sdk
import org.ergoplatform.sdk.SecretString

Expand All @@ -9,9 +8,15 @@ import scala.scalajs.js.annotation.JSExportTopLevel
import Isos._
import sigmastate.eval.SigmaDsl

/** Equivalent of [[sdk.ProverBuilder]] available from JS. */
/** Equivalent of [[sdk.ProverBuilder]] available from JS.
*
* @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.
*/
@JSExportTopLevel("ProverBuilder")
class ProverBuilder(parameters: BlockchainParameters, network: NetworkPrefix) extends js.Object {
class ProverBuilder(parameters: BlockchainParameters, network: Byte) extends js.Object {
val _builder = new sdk.ProverBuilder(parameters, network)

/** Configure this builder to use the given seed when building a new prover.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.ergoplatform.sdk.js

import org.ergoplatform.sdk
import scala.scalajs.js.annotation.JSExportTopLevel

/** Equivalent of [[sdk.ReducedTransaction]] available from JS. */
@JSExportTopLevel("ReducedTransaction")
class ReducedTransaction(private[js] val _tx: sdk.ReducedTransaction) {
/** Serialized bytes of this transaction in hex format. */
def toHex: String = _tx.toHex
}

@JSExportTopLevel("ReducedTransactionObj")
object ReducedTransaction {
/** Creates a [[ReducedTransaction]] from serialized bytes in hex format. */
def fromHex(hex: String): ReducedTransaction = {
val tx = sdk.ReducedTransaction.fromHex(hex)
new ReducedTransaction(tx)
}
}
28 changes: 19 additions & 9 deletions sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ import scala.scalajs.js.annotation.JSExportTopLevel
class SigmaProver(_prover: sdk.SigmaProver) extends js.Object {
import Isos._

/** Returns the Pay-to-Public-Key (P2PK) address associated with the prover's public key.
* The returned address corresponds to the master secret derived from the mnemonic
* phrase configured in the [[ProverBuilder]].
*/
def getP2PKAddress: String = {
val addr = _prover.getP2PKAddress
addr.toString
}

/** Returns the prover's secret key. */
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] = {
val addresses = _prover.getEip3Addresses
js.Array(addresses.map(_.toString): _*)
}

/** Reduces the transaction to the reduced form, which is ready to be signed.
* @param stateCtx blockchain state context
* @param unsignedTx unsigned transaction to be reduced (created by Fleet builders)
Expand Down Expand Up @@ -49,12 +68,3 @@ class SigmaProver(_prover: sdk.SigmaProver) extends js.Object {
isoSignedTransaction.from(signed.ergoTx)
}
}

//TODO finish implementation
@JSExportTopLevel("ReducedTransaction")
class ReducedTransaction(private [js] val _tx: sdk.ReducedTransaction)

//TODO finish implementation
@JSExportTopLevel("SigmaProverObj")
object SigmaProver {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import special.sigma.GroupElement
import java.math.BigInteger
import scala.collection.mutable.ArrayBuffer

/** A builder class for constructing a `Prover` with specified secrets. */
/** A builder class for constructing a `Prover` with specified secrets.
*
* @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 networkPrefix Network prefix to use for addresses.
*/
class ProverBuilder(parameters: BlockchainParameters, networkPrefix: NetworkPrefix) {
private var _masterKey: Option[ExtendedSecretKey] = None

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.ergoplatform.sdk

import org.ergoplatform.sdk.JavaHelpers.StringExtensions
import org.ergoplatform.{ErgoBox, ErgoLikeTransaction, UnsignedErgoLikeTransaction}
import sigmastate.eval.Extensions.ArrayByteOps
import sigmastate.serialization.SigmaSerializer

import java.util

Expand Down Expand Up @@ -45,7 +48,23 @@ case class UnreducedTransaction(
}

/** Represents results for transaction reduction by [[ReducingInterpreter]]. */
case class ReducedTransaction(ergoTx: ReducedErgoLikeTransaction)
case class ReducedTransaction(ergoTx: ReducedErgoLikeTransaction) {
/** Serialized bytes of this transaction in hex format. */
def toHex: String = {
val w = SigmaSerializer.startWriter()
ReducedErgoLikeTransactionSerializer.serialize(ergoTx, w)
w.toBytes.toHex
}
}

object ReducedTransaction {
/** Creates a [[ReducedTransaction]] from serialized bytes in hex format. */
def fromHex(hex: String): ReducedTransaction = {
val r = SigmaSerializer.startReader(hex.toBytes)
val tx = ReducedErgoLikeTransactionSerializer.parse(r)
ReducedTransaction(tx)
}
}

/** Represents results for transaction signing by a prover like [[SigmaProver]]. */
case class SignedTransaction(ergoTx: ErgoLikeTransaction, cost: Int)
Expand Down

0 comments on commit 4ec1b49

Please sign in to comment.