Repository: ergoplatform/sigma-rust (language: rust)

Issue:
Wasm API for creating a ‘Constant‘ from a serialized ‘EcPoint‘ 



Discussion / hints:

Fix patch:
diff --git a/bindings/ergo-lib-wasm/src/ast.rs b/bindings/ergo-lib-wasm/src/ast.rs
index 03b66a2c8..a16890cae 100644
--- a/bindings/ergo-lib-wasm/src/ast.rs
+++ b/bindings/ergo-lib-wasm/src/ast.rs
@@ -3,6 +3,9 @@
 use crate::utils::I64;
 use ergo_lib::chain::Base16Str;
 use ergo_lib::ergotree_ir::mir::constant::{TryExtractFrom, TryExtractInto};
+use ergo_lib::ergotree_ir::serialization::SigmaSerializable;
+use ergo_lib::ergotree_ir::sigma_protocol::dlog_group::EcPoint;
+use ergo_lib::ergotree_ir::sigma_protocol::sigma_boolean::ProveDlog;
 use js_sys::Uint8Array;
 use std::convert::TryFrom;
 use wasm_bindgen::prelude::*;
@@ -109,4 +112,12 @@ impl Constant {
             .map(|it| JsValue::from_str(&it.to_string()))
             .collect())
     }
+
+    /// Parse raw [`EcPoint`] value from bytes and make [`ProveDlog`] constant
+    pub fn from_ecpoint_bytes(bytes: &[u8]) -> Result<Constant, JsValue> {
+        let ecp = EcPoint::sigma_parse_bytes(bytes)
+            .map_err(|e| JsValue::from_str(&format!("{:?}", e)))?;
+        let c: ergo_lib::ergotree_ir::mir::constant::Constant = ProveDlog::new(ecp).into();
+        Ok(c.into())
+    }
 }
diff --git a/ergo-lib/CHANGELOG.md b/ergo-lib/CHANGELOG.md
index 877779d61..339c5ba05 100644
--- a/ergo-lib/CHANGELOG.md
+++ b/ergo-lib/CHANGELOG.md
@@ -14,7 +14,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
 - `Coll.append` [#308](https://github.com/ergoplatform/sigma-rust/pull/308);
 - `MultiplyGroup` IR node for GroupElement [#316](https://github.com/ergoplatform/sigma-rust/pull/316);
 - `SubstConst` IR node and serialization [#318] (https://github.com/ergoplatform/sigma-rust/pull/318);
--  Better Debug print for EC point [#319](https://github.com/ergoplatform/sigma-rust/pull/319);
+- Better Debug print for EC point [#319](https://github.com/ergoplatform/sigma-rust/pull/319);
+- `Constant::from_ecpoint_bytes` to Wasm API [#324](https://github.com/ergoplatform/sigma-rust/pull/324);
 
 ### Changed
 - `ergotree-ir::mir::constant::constant_placeholder` module is public;


Test patch:
diff --git a/bindings/ergo-lib-wasm/tests/test_constant.js b/bindings/ergo-lib-wasm/tests/test_constant.js
index 49b078412..fe926eb7f 100644
--- a/bindings/ergo-lib-wasm/tests/test_constant.js
+++ b/bindings/ergo-lib-wasm/tests/test_constant.js
@@ -48,4 +48,8 @@ it('roundtrip Constant array of i64', async () => {
   expect(decoded_c_value.toString()).equal(value_str.toString());
 });
 
-
+it('Constant from EcPoint bytes', async () => {
+  let base16_bytes_str = `02d6b2141c21e4f337e9b065a031a6269fb5a49253094fc6243d38662eb765db00`;
+  let c = Constant.from_ecpoint_bytes(Uint8Array.from(Buffer.from(base16_bytes_str, 'hex')));
+  expect(c != null);
+});