Skip to content

Commit

Permalink
Move OutputIdProof to block types
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Dec 8, 2023
1 parent 427dcc4 commit 8ca4054
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 92 deletions.
77 changes: 1 addition & 76 deletions sdk/src/types/api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
use alloc::{
boxed::Box,
collections::{BTreeMap, BTreeSet},
format,
string::String,
vec::Vec,
};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{
types::block::{
Expand All @@ -23,7 +21,7 @@ use crate::{
slot::{EpochIndex, SlotCommitment, SlotCommitmentId, SlotIndex},
BlockDto, BlockId,
},
utils::serde::{option_string, prefix_hex_bytes, string},
utils::serde::{option_string, string},
};

/// Response of GET /api/core/v3/info.
Expand Down Expand Up @@ -539,76 +537,3 @@ pub struct UtxoChangesResponse {
// pub output: Output,
// pub output_id_proof: OutputIdProof,
// }

/// The proof of the output identifier.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OutputIdProof {
pub slot: SlotIndex,
pub output_index: u16,
pub transaction_commitment: String,
pub output_commitment_proof: OutputCommitmentProof,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum OutputCommitmentProof {
HashableNode(HashableNode),
LeafHash(LeafHash),
ValueHash(ValueHash),
}

impl<'de> Deserialize<'de> for OutputCommitmentProof {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let value = Value::deserialize(d)?;
Ok(
match value
.get("type")
.and_then(Value::as_u64)
.ok_or_else(|| serde::de::Error::custom("invalid output commitment proof type"))?
as u8
{
0 => Self::HashableNode(
serde_json::from_value::<HashableNode>(value)
.map_err(|e| serde::de::Error::custom(format!("cannot deserialize hashable node: {e}")))?,
),
1 => Self::LeafHash(
serde_json::from_value::<LeafHash>(value)
.map_err(|e| serde::de::Error::custom(format!("cannot deserialize leaf hash: {e}")))?,
),
2 => Self::ValueHash(
serde_json::from_value::<ValueHash>(value)
.map_err(|e| serde::de::Error::custom(format!("cannot deserialize value hash: {e}")))?,
),
_ => return Err(serde::de::Error::custom("invalid output commitment proof")),
},
)
}
}

/// Node contains the hashes of the left and right children of a node in the tree.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct HashableNode {
#[serde(rename = "type")]
pub kind: u8,
pub l: Box<OutputCommitmentProof>,
pub r: Box<OutputCommitmentProof>,
}

/// Leaf Hash contains the hash of a leaf in the tree.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct LeafHash {
#[serde(rename = "type")]
pub kind: u8,
#[serde(with = "prefix_hex_bytes")]
pub hash: [u8; 32],
}

/// Value Hash contains the hash of the value for which the proof is being computed.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ValueHash {
#[serde(rename = "type")]
pub kind: u8,
#[serde(with = "prefix_hex_bytes")]
pub hash: [u8; 32],
}
17 changes: 8 additions & 9 deletions sdk/src/types/block/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod delegation;
mod metadata;
mod native_token;
mod output_id;
mod output_id_proof;
mod state_transition;
mod storage_score;
mod token_scheme;
Expand Down Expand Up @@ -50,17 +51,15 @@ pub(crate) use self::{
feature::{MetadataFeatureLength, TagFeatureLength},
native_token::NativeTokenCount,
output_id::OutputIndex,
output_id_proof::OutputIdProof,
unlock_condition::AddressUnlockCondition,
};
use crate::types::{
api::core::OutputIdProof,
block::{
address::Address,
protocol::{CommittableAgeRange, ProtocolParameters, WorkScore, WorkScoreParameters},
semantic::SemanticValidationContext,
slot::SlotIndex,
Error,
},
use crate::types::block::{
address::Address,
protocol::{CommittableAgeRange, ProtocolParameters, WorkScore, WorkScoreParameters},
semantic::SemanticValidationContext,
slot::SlotIndex,
Error,
};

/// The maximum number of outputs of a transaction.
Expand Down
90 changes: 90 additions & 0 deletions sdk/src/types/block/output/output_id_proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use alloc::{boxed::Box, string::String};

#[cfg(feature = "serde")]
use {crate::utils::serde::prefix_hex_bytes, alloc::format, serde::de::Deserialize, serde_json::Value};

use crate::types::block::slot::SlotIndex;

/// The proof of the output identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct OutputIdProof {
pub slot: SlotIndex,
pub output_index: u16,
pub transaction_commitment: String,
pub output_commitment_proof: OutputCommitmentProof,
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(untagged))]
pub enum OutputCommitmentProof {
HashableNode(HashableNode),
LeafHash(LeafHash),
ValueHash(ValueHash),
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for OutputCommitmentProof {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let value = Value::deserialize(d)?;
Ok(
match value
.get("type")
.and_then(Value::as_u64)
.ok_or_else(|| serde::de::Error::custom("invalid output commitment proof type"))?
as u8
{
0 => Self::HashableNode(
serde_json::from_value::<HashableNode>(value)
.map_err(|e| serde::de::Error::custom(format!("cannot deserialize hashable node: {e}")))?,
),
1 => Self::LeafHash(
serde_json::from_value::<LeafHash>(value)
.map_err(|e| serde::de::Error::custom(format!("cannot deserialize leaf hash: {e}")))?,
),
2 => Self::ValueHash(
serde_json::from_value::<ValueHash>(value)
.map_err(|e| serde::de::Error::custom(format!("cannot deserialize value hash: {e}")))?,
),
_ => return Err(serde::de::Error::custom("invalid output commitment proof")),
},
)
}
}

/// Node contains the hashes of the left and right children of a node in the tree.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HashableNode {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
pub kind: u8,
pub l: Box<OutputCommitmentProof>,
pub r: Box<OutputCommitmentProof>,
}

/// Leaf Hash contains the hash of a leaf in the tree.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LeafHash {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
pub kind: u8,
#[cfg_attr(feature = "serde", serde(with = "prefix_hex_bytes"))]
pub hash: [u8; 32],
}

/// Value Hash contains the hash of the value for which the proof is being computed.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ValueHash {
#[cfg_attr(feature = "serde", serde(rename = "type"))]
pub kind: u8,
#[cfg_attr(feature = "serde", serde(with = "prefix_hex_bytes"))]
pub hash: [u8; 32],
}
5 changes: 1 addition & 4 deletions sdk/src/wallet/operations/syncing/addresses/outputs.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;

use instant::Instant;

use crate::{
client::secret::SecretManage,
types::block::address::Address,
wallet::{
constants::PARALLEL_REQUESTS_AMOUNT,
task,
Expand All @@ -25,7 +22,7 @@ where
pub(crate) async fn get_outputs_from_address_output_ids(
&self,
addresses_with_unspent_outputs: Vec<AddressWithUnspentOutputs>,
) -> crate::wallet::Result<(Vec<(AddressWithUnspentOutputs, Vec<OutputData>)>)> {
) -> crate::wallet::Result<Vec<(AddressWithUnspentOutputs, Vec<OutputData>)>> {
log::debug!("[SYNC] start get_outputs_from_address_output_ids");
let address_outputs_start_time = Instant::now();

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/wallet/operations/syncing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use self::options::SyncOptions;
use crate::{
client::secret::SecretManage,
types::block::{
address::{AccountAddress, Address, Bech32Address, NftAddress, ToBech32Ext},
address::{AccountAddress, Address, Bech32Address, NftAddress},
output::{FoundryId, Output, OutputId, OutputMetadata},
},
wallet::{
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/wallet/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub use self::{
use crate::{
client::secret::types::InputSigningData,
types::{
api::core::{OutputIdProof, OutputWithMetadataResponse},
api::core::OutputWithMetadataResponse,
block::{
output::{Output, OutputId, OutputMetadata},
output::{Output, OutputId, OutputIdProof, OutputMetadata},
payload::signed_transaction::{dto::SignedTransactionPayloadDto, SignedTransactionPayload, TransactionId},
protocol::{CommittableAgeRange, ProtocolParameters},
slot::SlotIndex,
Expand Down

0 comments on commit 8ca4054

Please sign in to comment.