From fff394ff28ec5530a6535effedd927f2eb297fc0 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Tue, 5 Sep 2023 14:49:25 +0200 Subject: [PATCH] v5.0.11: added TypeScript definitions --- .../ergoplatform/sdk/js/ProverBuilder.scala | 14 ++ .../sdk/js/ReducedTransaction.scala | 2 +- .../org/ergoplatform/sdk/js/SigmaProver.scala | 8 +- sigma-js/sigmastate-js.d.ts | 211 ++++++++++++++++++ 4 files changed, 230 insertions(+), 5 deletions(-) diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala index 8d3b3cd2e3..2c27a177d4 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala @@ -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) +} \ No newline at end of file diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ReducedTransaction.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ReducedTransaction.scala index 66c5ce7f38..a27c61d261 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ReducedTransaction.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ReducedTransaction.scala @@ -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") diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala index b7ca33b1de..6aedecf2e9 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala @@ -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): _*) } diff --git a/sigma-js/sigmastate-js.d.ts b/sigma-js/sigmastate-js.d.ts index 34eef4df12..775f585570 100644 --- a/sigma-js/sigmastate-js.d.ts +++ b/sigma-js/sigmastate-js.d.ts @@ -1,4 +1,12 @@ declare module "sigmastate-js/main" { + import { + Amount, + Box, + EIP12UnsignedInput, + NonMandatoryRegisters, SignedTransaction, TokenAmount, + UnsignedTransaction + } from "@fleet-sdk/common"; + type SigmaCompilerNamedConstantsMap = { [key: string]: Value }; type HexString = string; type ByteArray = { u: Int8Array }; @@ -35,6 +43,95 @@ declare module "sigmastate-js/main" { static fromPointHex(value: HexString): SigmaProp; } + export declare class AvlTree { + digest: HexString; + insertAllowed: Boolean; + updateAllowed: Boolean; + removeAllowed: Boolean; + keyLength: number; + valueLengthOpt: number | undefined; + } + + export declare class PreHeader { + /** Block version, to be increased on every soft and hardfork. */ + version: number; + /** Hex of id of parent block */ + parentId: HexString; + /** Block timestamp (in milliseconds since beginning of Unix Epoch) */ + timestamp: bigint; + /** Current difficulty in a compressed view. + * NOTE: actually it is unsigned integer */ + nBits: bigint; + /** Block height */ + height: number; + /** Miner public key (hex of EC Point). Should be used to collect block rewards. */ + minerPk: GroupElement; + /** Hex of miner votes bytes for changing system parameters. */ + votes: HexString; + } + + export declare class Header { + /** Hex representation of ModifierId of this Header */ + id: HexString; + /** Block version, to be increased on every soft and hardfork. */ + version: number; + /** Hex representation of ModifierId of the parent block */ + parentId: HexString; + /** Hex hash of ADProofs for transactions in a block */ + ADProofsRoot: HexString; + /** AvlTree of a state after block application */ + stateRoot: AvlTree; + /** Hex of root hash (for a Merkle tree) of transactions in a block. */ + transactionsRoot: HexString; + /** Block timestamp (in milliseconds since beginning of Unix Epoch) */ + timestamp: bigint; + /** Current difficulty in a compressed view. + * NOTE: actually it is unsigned Int */ + nBits: bigint; + /** Block height */ + height: number; + /** Hex of root hash of extension section */ + extensionRoot: HexString; + + /** Miner public key (hex of EC Point). Should be used to collect block rewards. + * Part of Autolykos solution. + */ + minerPk: GroupElement; + + /** One-time public key (hex of EC Point). Prevents revealing of miners secret. */ + powOnetimePk: GroupElement; + + /** Hex of nonce bytes */ + powNonce: HexString; + + /** Distance between pseudo-random number, corresponding to nonce `powNonce` and a secret, + * corresponding to `minerPk`. The lower `powDistance` is, the harder it was to find this solution. */ + powDistance: bigint; + + /** Miner votes for changing system parameters. */ + votes: HexString; + } + + export declare class BlockchainParameters { + storageFeeFactor: number; + minValuePerByte: number; + maxBlockSize: number; + tokenAccessCost: number; + inputCost: number; + dataInputCost: number; + outputCost: number; + maxBlockCost: number; + softForkStartingHeight: number | undefined; + softForkVotesCollected: number | undefined; + blockVersion: number; + } + + export declare class BlockchainStateContext { + sigmaLastHeaders: Header[]; + previousStateDigest: HexString; + sigmaPreHeader: PreHeader; + } + export declare class Type { name: string; toString(): string; @@ -90,4 +187,118 @@ declare module "sigmastate-js/main" { static forMainnet(): SigmaCompiler; static forTestnet(): SigmaCompiler; } + + /** Represents results for transaction reduction by {@link SigmaProver}. */ + export declare class ReducedTransaction { + /** Serialized bytes of this transaction in hex format. */ + toHex(): HexString; + } + + export declare class ReducedTransactionObj { + /** Creates a {@link ReducedTransaction} from serialized bytes in hex format. */ + fromHex(hex: HexString): ReducedTransaction; + } + + /** Represents a prover for signing Ergo transactions and messages. + * + * Equivalent of [[org.ergoplatform.sdk.SigmaProver]] available from JS. + */ + export declare class SigmaProver { + /** 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]]. + */ + getP2PKAddress(): HexString; + + /** Returns the prover's secret key. */ + getSecretKey(): bigint; + + /** Returns an array of EIP-3 addresses associated with the prover's secret keys. */ + getEip3Addresses(): HexString[]; + + /** 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) + * @param boxesToSpend boxes to be spent by the transaction + * @param dataInputs data inputs to be used by the transaction + * @param tokensToBurn tokens to be burned by the transaction + * @param baseCost base cost of the transaction + * @return reduced transaction + */ + reduce( + stateCtx: BlockchainStateContext, + unsignedTx: UnsignedTransaction, + boxesToSpend: EIP12UnsignedInput[], + dataInputs: Box[], + tokensToBurn: TokenAmount[], + baseCost: number): ReducedTransaction; + + /** Signs the reduced transaction. + * @param reducedTx reduced transaction to be signed + * @return signed transaction containting all the required proofs (signatures) + */ + signReduced(reducedTx: ReducedTransaction): SignedTransaction; + } + + /** 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. + */ + export declare class ProverBuilder { + /** Configure this builder to use the given seed when building a new prover. + * + * @param mnemonicPhrase secret seed phrase to be used in prover for generating proofs. + * @param mnemonicPass password to protect secret seed phrase. + */ + withMnemonic(mnemonicPhrase: HexString, mnemonicPass: HexString): ProverBuilder; + /** Configure this builder to derive the new EIP-3 secret key with the given index. + * The derivation uses master key derived from the mnemonic configured using + * [[ErgoProverBuilder.withMnemonic]]. + * + * @param index last index in the EIP-3 derivation path. + */ + withEip3Secret(index: number): ProverBuilder; + + /** Configures this builder to use group elements (g, h, u, v) and secret x for a + * ProveDHTuple statement when building a new prover. + * + * ProveDHTuple is a statement consisting of 4 group elements (g, h, u, v) and + * requires the prover to prove knowledge of secret integer x such that. + * + * u = g^x + * and + * y = h^x + * + * @param g [[GroupElement]] instance defining g + * @param h [[GroupElement]] instance defining h + * @param u [[GroupElement]] instance defining u + * @param v [[GroupElement]] instance defining v + * @param x [[BigInteger]] instance defining x + * @see + * example + * @see + * implementation + */ + withDHTSecret(g: HexString, h: HexString, u: HexString, v: HexString, x: bigint): ProverBuilder; + + /** This allows adding additional secret for use in proveDlog, when the secret is not + * part of the wallet. + * + * Multiple secrets can be added by calling this method multiple times. + * + * Multiple secrets are necessary for statements that need multiple proveDlogs, such + * as proveDlog(a) && proveDlog(b), where a and b are two group elements. + */ + withDLogSecret(x: bigint): ProverBuilder; + + /** Builds a new prover using provided configuration. */ + build(): SigmaProver; + } + + export declare class ProverBuilderObj { + static create(parameters: BlockchainParameters, network: number): ProverBuilder; + } }