Skip to content

Commit 2f840d3

Browse files
authored
Merge pull request #836 from Alesfatalis/json-base16-sigma-boolean
serialize sigmaboolean as base-16 string in ergo-lib-wasm bindings
2 parents ae808fb + 072f858 commit 2f840d3

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

ergo-lib/src/chain/transaction/reduced.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use ergotree_ir::serialization::SigmaSerializable;
1111
use ergotree_ir::serialization::SigmaSerializeResult;
1212
use ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;
1313

14+
use super::unsigned::UnsignedTransaction;
15+
use super::TxIoVec;
1416
use crate::chain::ergo_state_context::ErgoStateContext;
1517
use crate::chain::transaction::Transaction;
1618
use crate::chain::transaction::UnsignedInput;
@@ -20,17 +22,20 @@ use crate::wallet::signing::TransactionContext;
2022
use crate::wallet::signing::TxSigningError;
2123
use crate::wallet::tx_context::TransactionContextError;
2224

23-
use super::unsigned::UnsignedTransaction;
24-
use super::TxIoVec;
25-
2625
/// Input box script reduced to SigmaBoolean
2726
/// see EIP-19 for more details -
2827
/// <https://github.com/ergoplatform/eips/blob/f280890a4163f2f2e988a0091c078e36912fc531/eip-0019.md>
2928
#[derive(PartialEq, Eq, Debug, Clone)]
3029
#[cfg_attr(feature = "json", derive(serde::Serialize, serde::Deserialize))]
3130
pub struct ReducedInput {
3231
/// value of SigmaProp type which represents a statement verifiable via sigma protocol.
33-
#[cfg_attr(feature = "json", serde(rename = "sigmaProp"))]
32+
#[cfg_attr(
33+
feature = "json",
34+
serde(
35+
rename = "sigmaProp",
36+
with = "ergotree_ir::chain::json::sigma_protocol"
37+
)
38+
)]
3439
pub sigma_prop: SigmaBoolean,
3540
/// estimated cost of expression evaluation
3641
pub cost: u64,

ergotree-ir/src/chain/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use serde::Serializer;
1414
pub(crate) mod box_value;
1515
pub(crate) mod ergo_box;
1616
pub mod ergo_tree;
17-
pub(crate) mod sigma_protocol;
17+
pub mod sigma_protocol;
1818
pub(crate) mod token;
1919

2020
/// Serialize bytes ([u8]) as base16 encoded string

ergotree-ir/src/chain/json/sigma_protocol.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
//! SigmaBoolean JSON encoding
2+
13
use core::convert::TryFrom;
24
use core::convert::TryInto;
35

4-
use alloc::vec::Vec;
5-
use bounded_vec::BoundedVecOutOfBounds;
6-
use ergo_chain_types::EcPoint;
7-
use serde::Deserialize;
8-
use serde::Serialize;
9-
6+
use super::serialize_bytes;
7+
use crate::serialization::SigmaSerializable;
108
use crate::sigma_protocol::sigma_boolean::cand::Cand;
119
use crate::sigma_protocol::sigma_boolean::cor::Cor;
1210
use crate::sigma_protocol::sigma_boolean::cthreshold::Cthreshold;
@@ -15,11 +13,16 @@ use crate::sigma_protocol::sigma_boolean::ProveDlog;
1513
use crate::sigma_protocol::sigma_boolean::SigmaBoolean;
1614
use crate::sigma_protocol::sigma_boolean::SigmaConjecture;
1715
use crate::sigma_protocol::sigma_boolean::SigmaProofOfKnowledgeTree;
16+
use alloc::vec::Vec;
17+
use bounded_vec::BoundedVecOutOfBounds;
18+
use ergo_chain_types::EcPoint;
19+
use serde::Serialize;
20+
use serde::{Deserialize, Deserializer, Serializer};
1821

1922
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
2023
#[serde(tag = "op")]
2124
#[allow(clippy::large_enum_variant)]
22-
pub enum SigmaBooleanJson {
25+
pub(crate) enum SigmaBooleanJson {
2326
#[serde(rename = "205")] // OpCode::PROVE_DLOG
2427
ProveDlog { h: EcPoint },
2528
#[serde(rename = "206")] // OpCode::PROVE_DIFFIE_HELLMAN_TUPLE
@@ -132,3 +135,29 @@ impl TryFrom<SigmaBooleanJson> for SigmaBoolean {
132135
})
133136
}
134137
}
138+
139+
/// Serializer (used in Wasm bindings)
140+
pub fn serialize<S>(sb: &SigmaBoolean, serializer: S) -> Result<S::Ok, S::Error>
141+
where
142+
S: Serializer,
143+
{
144+
use serde::ser::Error;
145+
146+
let bytes = sb
147+
.sigma_serialize_bytes()
148+
.map_err(|err| Error::custom(err.to_string()))?;
149+
serialize_bytes(&bytes[..], serializer)
150+
}
151+
152+
/// Deserializer (used in Wasm bindings)
153+
pub fn deserialize<'de, D>(deserializer: D) -> Result<SigmaBoolean, D::Error>
154+
where
155+
D: Deserializer<'de>,
156+
{
157+
use serde::de::Error;
158+
String::deserialize(deserializer)
159+
.and_then(|str| base16::decode(&str).map_err(|err| Error::custom(err.to_string())))
160+
.and_then(|bytes| {
161+
SigmaBoolean::sigma_parse_bytes(&bytes).map_err(|err| Error::custom(err.to_string()))
162+
})
163+
}

0 commit comments

Comments
 (0)