diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala index 3e09608ba9..cc20e704bb 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala @@ -12,7 +12,7 @@ class GroupElement(val point: Ecp) extends js.Object { /** Returns the point encoded as hex string (ASN.1 encoding). * @see CryptoFacade.getASN1Encoding */ - def toHex(): String = { + def toPointHex(): String = { CryptoFacade.getASN1Encoding(point, true).toHex } } @@ -22,8 +22,8 @@ object GroupElement extends js.Object { /** Creates a new [[GroupElement]] from the given hex string (ASN.1 encoding) * representation of the underlying [[sigmastate.crypto.Platform.Point]]. */ - def fromHex(hex: String): GroupElement = { - val point = CryptoFacadeJs.createCryptoContext().decodePoint(hex) + def fromPointHex(pointHex: String): GroupElement = { + val point = CryptoFacadeJs.createCryptoContext().decodePoint(pointHex) new GroupElement(new Platform.Ecp(point)) } } \ No newline at end of file diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala index 0722b68e5a..bc2da23561 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala @@ -1,9 +1,24 @@ package org.ergoplatform.sdk.js import sigmastate.Values.SigmaBoolean +import sigmastate.basics.DLogProtocol.ProveDlog + import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel /** Equivalent of [[special.sigma.SigmaProp]] available from JS. */ @JSExportTopLevel("SigmaProp") -class SigmaProp(val sigmaBoolean: SigmaBoolean) extends js.Object +class SigmaProp(val sigmaBoolean: SigmaBoolean) extends js.Object { +} + +@JSExportTopLevel("SigmaPropObj") +object SigmaProp extends js.Object { + /** Creates a new [[SigmaProp]] from the given hex string of public key. + * @param pointHex hex representation of elliptic curve point (ASN.1 encoded) + * @see CryptoFacade.getASN1Encoding, GroupElement.fromPointHex, Point + */ + def fromPointHex(pointHex: String): SigmaProp = { + val point = GroupElement.fromPointHex(pointHex).point + new SigmaProp(ProveDlog(point)) + } +} diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala index 2ae73a781d..0c30216dbc 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala @@ -89,8 +89,8 @@ object Value extends js.Object { val v = data.asInstanceOf[js.BigInt] SigmaDsl.BigInt(new BigInteger(v.toString(16), 16)) case special.sigma.GroupElementRType => - val point = data.asInstanceOf[Platform.Point] - SigmaDsl.GroupElement(new Platform.Ecp(point)) + val ge = data.asInstanceOf[GroupElement] + SigmaDsl.GroupElement(ge.point) case special.sigma.SigmaPropRType => val p = data.asInstanceOf[SigmaProp] SigmaDsl.SigmaProp(p.sigmaBoolean) @@ -129,8 +129,8 @@ object Value extends js.Object { val hex = SigmaDsl.toBigInteger(value.asInstanceOf[special.sigma.BigInt]).toString(10) js.BigInt(hex) case special.sigma.GroupElementRType => - val point: Platform.Point = value.asInstanceOf[CGroupElement].wrappedValue.asInstanceOf[Platform.Ecp].point - point + val point = value.asInstanceOf[CGroupElement].wrappedValue.asInstanceOf[Platform.Ecp] + new GroupElement(point) case special.sigma.SigmaPropRType => new SigmaProp(value.asInstanceOf[CSigmaProp].wrappedValue) case special.sigma.AvlTreeRType => @@ -164,6 +164,10 @@ object Value extends js.Object { n case special.sigma.BigIntRType => data.asInstanceOf[js.BigInt] + case special.sigma.GroupElementRType => + data.asInstanceOf[GroupElement] + case special.sigma.SigmaPropRType => + data.asInstanceOf[SigmaProp] case PairType(l, r) => data match { case arr: js.Array[Any @unchecked] => checkJsData(arr(0), l) @@ -217,10 +221,18 @@ object Value extends js.Object { * @param pointHex hex of ASN representation of [[sigmastate.crypto.Platform.Point]] */ def ofGroupElement(pointHex: String): Value = { - val ge = GroupElement.fromHex(pointHex) + val ge = GroupElement.fromPointHex(pointHex) new Value(ge, Type.GroupElement) } + /** Creates a Value of SigmaProp type from [[sigmastate.crypto.Platform.Point]] hex. + * @param pointHex hex of ASN representation of [[sigmastate.crypto.Platform.Point]] + */ + def ofSigmaProp(pointHex: String): Value = { + val sp = SigmaProp.fromPointHex(pointHex) + new Value(sp, Type.SigmaProp) + } + /** Create Pair value from two values. */ def pairOf(l: Value, r: Value): Value = { val data = js.Array(l.data, r.data) // the l and r data have been validated diff --git a/sigma-js/sigmastate-js.d.ts b/sigma-js/sigmastate-js.d.ts index 0ee5e572db..3914841695 100644 --- a/sigma-js/sigmastate-js.d.ts +++ b/sigma-js/sigmastate-js.d.ts @@ -21,11 +21,18 @@ declare module "sigmastate-js/main" { } export declare class GroupElement { - toHex(): HexString; + toPointHex(): HexString; } export declare class GroupElementObj { - static fromHex(value: HexString): GroupElement; + static fromPointHex(value: HexString): GroupElement; + } + + export declare class SigmaProp { + } + + export declare class SigmaPropObj { + static fromPointHex(value: HexString): SigmaProp; } export declare class Type { @@ -63,6 +70,8 @@ declare module "sigmastate-js/main" { static ofInt(value: number): Value; static ofLong(value: bigint): Value; static ofBigInt(value: bigint): Value; + static ofGroupElement(pointHex: string): Value; + static ofSigmaProp(pointHex: string): Value; static pairOf(left: Value, right: Value): Value<[R, L]>; static collOf(items: T[], type: Type): Value; static fromHex(hex: HexString): Value; diff --git a/sigma-js/tests/js/GroupElement.spec.js b/sigma-js/tests/js/GroupElement.spec.js index c80cdbaa24..6d860691be 100644 --- a/sigma-js/tests/js/GroupElement.spec.js +++ b/sigma-js/tests/js/GroupElement.spec.js @@ -1,10 +1,13 @@ -const { GroupElementObj, ErgoTree} = require("sigmastate-js/main"); +const { GroupElementObj, ValueObj } = require("sigmastate-js/main"); let pointAsn1Hex = "02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5"; describe("GroupElement", () => { - it("should implement toHex/fromHex", () => { - let ge = GroupElementObj.fromHex(pointAsn1Hex) - expect(ge.toHex()).toEqual(pointAsn1Hex) + it("should implement toPointHex/fromPointHex", () => { + let ge = GroupElementObj.fromPointHex(pointAsn1Hex) + expect(ge.toPointHex()).toEqual(pointAsn1Hex) + + let v = ValueObj.ofGroupElement(pointAsn1Hex) + expect(v.toHex()).toEqual("07"/* GroupElement type id */ + pointAsn1Hex) }); }); \ No newline at end of file diff --git a/sigma-js/tests/js/SigmaProp.spec.js b/sigma-js/tests/js/SigmaProp.spec.js new file mode 100644 index 0000000000..89568120c7 --- /dev/null +++ b/sigma-js/tests/js/SigmaProp.spec.js @@ -0,0 +1,14 @@ +const { SigmaPropObj, ValueObj } = require("sigmastate-js/main"); + +let pointAsn1Hex = "02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5"; + +describe("SigmaProp", () => { + it("should implement fromPointHex", () => { + let ge = SigmaPropObj.fromPointHex(pointAsn1Hex) + expect(ge).not.toBeUndefined() + + let v = ValueObj.ofSigmaProp(pointAsn1Hex) + expect(v.toHex()) + .toEqual("08"/* SigmaProp type id */ + "cd"/* ProveDlog.opCode */ + pointAsn1Hex) + }); +}); \ No newline at end of file