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

Issue:
Add Constant::as_base16_str() for serialized bytes encoded as base16



Discussion / hints:

Fix patch:
diff --git a/sigma-tree/src/ast/constant.rs b/sigma-tree/src/ast/constant.rs
index a9f045598..9d08cd9cd 100644
--- a/sigma-tree/src/ast/constant.rs
+++ b/sigma-tree/src/ast/constant.rs
@@ -1,4 +1,3 @@
-#[cfg(feature = "with-serde")]
 use crate::chain::{Base16DecodedBytes, Base16EncodedBytes};
 use crate::{
     chain::ErgoBox,
@@ -7,9 +6,8 @@ use crate::{
 };
 #[cfg(feature = "with-serde")]
 use serde::{Deserialize, Serialize};
-#[cfg(feature = "with-serde")]
-use sigma_ser::serializer::{SerializationError, SigmaSerializable};
-#[cfg(feature = "with-serde")]
+use sigma_ser::serializer::SerializationError;
+use sigma_ser::serializer::SigmaSerializable;
 use std::convert::TryFrom;
 
 #[derive(PartialEq, Eq, Debug, Clone)]
@@ -102,14 +100,12 @@ pub struct Constant {
     pub v: ConstantVal,
 }
 
-#[cfg(feature = "with-serde")]
 impl Into<Base16EncodedBytes> for Constant {
     fn into(self) -> Base16EncodedBytes {
         Base16EncodedBytes::new(&self.sigma_serialise_bytes())
     }
 }
 
-#[cfg(feature = "with-serde")]
 impl TryFrom<Base16DecodedBytes> for Constant {
     type Error = SerializationError;
     fn try_from(bytes: Base16DecodedBytes) -> Result<Self, Self::Error> {
@@ -118,12 +114,10 @@ impl TryFrom<Base16DecodedBytes> for Constant {
 }
 
 impl Constant {
-    /// Create Sigma property constant
-    pub fn sigma_prop(prop: SigmaProp) -> Constant {
-        Constant {
-            tpe: SType::SSigmaProp,
-            v: ConstantVal::sigma_prop(prop),
-        }
+    /// Serialized bytes encoded as Base16
+    pub fn base16_str(&self) -> String {
+        let base16_bytes: Base16EncodedBytes = self.clone().into();
+        base16_bytes.into()
     }
 }
 
@@ -202,6 +196,21 @@ impl Into<Constant> for i64 {
     }
 }
 
+impl Into<ConstantVal> for SigmaProp {
+    fn into(self) -> ConstantVal {
+        ConstantVal::SigmaProp(Box::new(self))
+    }
+}
+
+impl Into<Constant> for SigmaProp {
+    fn into(self) -> Constant {
+        Constant {
+            tpe: SType::SSigmaProp,
+            v: self.into(),
+        }
+    }
+}
+
 /// Marker trait to select types for which CollElems::NonPrimitive is used to store elements as Vec<ConstantVal>
 pub trait StoredNonPrimitive {}
 
diff --git a/sigma-tree/src/chain.rs b/sigma-tree/src/chain.rs
index 1d1ebaba3..178ff3538 100644
--- a/sigma-tree/src/chain.rs
+++ b/sigma-tree/src/chain.rs
@@ -1,6 +1,7 @@
 //! Ergo chain types
 
 mod address;
+mod base16_bytes;
 mod box_id;
 mod context_extension;
 mod contract;
@@ -15,13 +16,11 @@ mod token;
 mod transaction;
 
 pub use address::*;
+pub use base16_bytes::Base16DecodedBytes;
+pub use base16_bytes::Base16EncodedBytes;
 pub use box_id::*;
 pub use contract::*;
 pub use ergo_box::*;
 pub use input::*;
-#[cfg(feature = "with-serde")]
-pub use json::Base16DecodedBytes;
-#[cfg(feature = "with-serde")]
-pub use json::Base16EncodedBytes;
 pub use token::*;
 pub use transaction::*;
diff --git a/sigma-tree/src/chain/address.rs b/sigma-tree/src/chain/address.rs
index db917d424..33ac6c6d1 100644
--- a/sigma-tree/src/chain/address.rs
+++ b/sigma-tree/src/chain/address.rs
@@ -1,6 +1,6 @@
 use super::digest32;
 use crate::{
-    ast::{Constant, Expr},
+    ast::Expr,
     ecpoint::EcPoint,
     sigma_protocol::{ProveDlog, SigmaBoolean, SigmaProofOfKnowledgeTree, SigmaProp},
     ErgoTree,
@@ -86,9 +86,10 @@ impl Address {
     pub fn script(&self) -> Result<ErgoTree, SerializationError> {
         match self {
             Address::P2PK(prove_dlog) => Ok(ErgoTree::from(Rc::new(Expr::Const(
-                Constant::sigma_prop(SigmaProp::new(SigmaBoolean::ProofOfKnowledge(
+                SigmaProp::new(SigmaBoolean::ProofOfKnowledge(
                     SigmaProofOfKnowledgeTree::ProveDlog(prove_dlog.clone()),
-                ))),
+                ))
+                .into(),
             )))),
             Address::P2S(bytes) => ErgoTree::sigma_parse_bytes(bytes.to_vec()),
         }
diff --git a/sigma-tree/src/chain/json/base16_bytes.rs b/sigma-tree/src/chain/base16_bytes.rs
similarity index 97%
rename from sigma-tree/src/chain/json/base16_bytes.rs
rename to sigma-tree/src/chain/base16_bytes.rs
index 7d665c617..f99b7371e 100644
--- a/sigma-tree/src/chain/json/base16_bytes.rs
+++ b/sigma-tree/src/chain/base16_bytes.rs
@@ -1,5 +1,6 @@
 //! Transitioning type for Base16 encoded bytes in JSON serialization
 
+#[cfg(feature = "with-serde")]
 use serde::{Deserialize, Serialize};
 use std::convert::TryFrom;
 
diff --git a/sigma-tree/src/chain/digest32.rs b/sigma-tree/src/chain/digest32.rs
index 270f56bc6..0604d676d 100644
--- a/sigma-tree/src/chain/digest32.rs
+++ b/sigma-tree/src/chain/digest32.rs
@@ -1,5 +1,4 @@
-#[cfg(feature = "with-serde")]
-use crate::chain::json::{Base16DecodedBytes, Base16EncodedBytes};
+use crate::chain::{Base16DecodedBytes, Base16EncodedBytes};
 use blake2::digest::{Update, VariableOutput};
 use blake2::VarBlake2b;
 #[cfg(test)]
@@ -10,7 +9,6 @@ use sigma_ser::{
     serializer::{SerializationError, SigmaSerializable},
     vlq_encode,
 };
-#[cfg(feature = "with-serde")]
 use std::convert::TryFrom;
 use std::convert::TryInto;
 use std::io;
@@ -50,14 +48,12 @@ impl From<[u8; Digest32::SIZE]> for Digest32 {
     }
 }
 
-#[cfg(feature = "with-serde")]
 impl Into<Base16EncodedBytes> for Digest32 {
     fn into(self) -> Base16EncodedBytes {
         Base16EncodedBytes::new(self.0.as_ref())
     }
 }
 
-#[cfg(feature = "with-serde")]
 impl TryFrom<Base16DecodedBytes> for Digest32 {
     type Error = Digest32Error;
     fn try_from(bytes: Base16DecodedBytes) -> Result<Self, Self::Error> {
@@ -66,7 +62,6 @@ impl TryFrom<Base16DecodedBytes> for Digest32 {
     }
 }
 
-#[cfg(feature = "with-serde")]
 impl Into<String> for Digest32 {
     fn into(self) -> String {
         let bytes: Base16EncodedBytes = self.into();
diff --git a/sigma-tree/src/chain/json.rs b/sigma-tree/src/chain/json.rs
index 0535af8de..78c26ad56 100644
--- a/sigma-tree/src/chain/json.rs
+++ b/sigma-tree/src/chain/json.rs
@@ -1,10 +1,5 @@
 //! JSON serialization
 
-mod base16_bytes;
-
-pub use base16_bytes::Base16DecodedBytes;
-pub use base16_bytes::Base16EncodedBytes;
-
 use serde::Serializer;
 
 pub fn serialize_bytes<S, T>(bytes: T, serializer: S) -> Result<S::Ok, S::Error>


Test patch: