Skip to content

Commit

Permalink
tx-signing-js: implemented fromPointHex for js.SigmaProp
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Aug 18, 2023
1 parent 58140c1 commit 9917c2b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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))
}
}
17 changes: 16 additions & 1 deletion sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala
Original file line number Diff line number Diff line change
@@ -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))
}
}
22 changes: 17 additions & 5 deletions sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions sigma-js/sigmastate-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -63,6 +70,8 @@ declare module "sigmastate-js/main" {
static ofInt(value: number): Value<number>;
static ofLong(value: bigint): Value<bigint>;
static ofBigInt(value: bigint): Value<bigint>;
static ofGroupElement(pointHex: string): Value<GroupElement>;
static ofSigmaProp(pointHex: string): Value<SigmaProp>;
static pairOf<R, L>(left: Value<R>, right: Value<L>): Value<[R, L]>;
static collOf<T>(items: T[], type: Type): Value<T[]>;
static fromHex<T>(hex: HexString): Value<T>;
Expand Down
11 changes: 7 additions & 4 deletions sigma-js/tests/js/GroupElement.spec.js
Original file line number Diff line number Diff line change
@@ -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)
});
});
14 changes: 14 additions & 0 deletions sigma-js/tests/js/SigmaProp.spec.js
Original file line number Diff line number Diff line change
@@ -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)
});
});

0 comments on commit 9917c2b

Please sign in to comment.