diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 1c3ac0b357..7fda24b09c 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -20,7 +20,7 @@ rustdoc-args = [ "--cfg", "docsrs" ] # Mandatory dependencies bech32 = { version = "0.9.1", default-features = false } bitflags = { version = "2.3.3", default-features = false } -derive_more = { version = "0.99.17", default-features = false, features = [ "from", "as_ref", "deref", "deref_mut" ] } +derive_more = { version = "0.99.17", default-features = false, features = [ "from", "as_ref", "deref", "deref_mut", "display" ] } getset = { version = "0.1.2", default-features = false } hashbrown = { version = "0.14.0", default-features = false, features = [ "ahash", "inline-more" ] } hex = { version = "0.4.3", default-features = false } diff --git a/sdk/src/client/node_api/core/routes.rs b/sdk/src/client/node_api/core/routes.rs index a0105fe42b..486d4237c6 100644 --- a/sdk/src/client/node_api/core/routes.rs +++ b/sdk/src/client/node_api/core/routes.rs @@ -21,6 +21,7 @@ use crate::{ block::{ output::{dto::OutputMetadataDto, Output, OutputId, OutputMetadata, OutputWithMetadata}, payload::transaction::TransactionId, + slot::{SlotCommitment, SlotCommitmentId, SlotIndex}, Block, BlockDto, BlockId, }, }, @@ -89,7 +90,7 @@ impl ClientInner { .await } - // Tangle routes. + // Blocks routes. /// Returns tips that are ideal for attaching a block. /// GET /api/core/v3/tips @@ -106,8 +107,6 @@ impl ClientInner { Ok(response.tips) } - // Blocks routes. - /// Returns the BlockId of the submitted block. /// POST JSON to /api/core/v3/blocks pub async fn post_block(&self, block: &Block) -> Result { @@ -341,6 +340,56 @@ impl ClientInner { .await } + // Commitments routes. + + /// Gets the slot commitment by the given slot commitment id. + /// GET /api/core/v3/commitments/{commitmentId} + pub async fn get_slot_commitment_by_id(&self, slot_commitment_id: &SlotCommitmentId) -> Result { + let path = &format!("api/core/v3/commitments/{slot_commitment_id}"); + + self.node_manager + .read() + .await + .get_request::(path, None, self.get_timeout().await, false, true) + .await + } + + /// Gets the slot commitment, as raw bytes, by the given slot commitment id. + /// GET /api/core/v3/commitments/{commitmentId} + pub async fn get_slot_commitment_by_id_raw(&self, slot_commitment_id: &SlotCommitmentId) -> Result> { + let path = &format!("api/core/v3/commitments/{slot_commitment_id}"); + + self.node_manager + .read() + .await + .get_request_bytes(path, None, self.get_timeout().await) + .await + } + + /// Gets the slot commitment by the given slot index. + /// GET /api/core/v3/commitments/by-index/{index} + pub async fn get_slot_commitment_by_index(&self, slot_index: &SlotIndex) -> Result { + let path = &format!("api/core/v3/commitments/by-index/{slot_index}"); + + self.node_manager + .read() + .await + .get_request::(path, None, self.get_timeout().await, false, true) + .await + } + + /// Gets the slot commitment, as raw bytes, by the given slot index. + /// GET /api/core/v3/commitments/by-index/{index} + pub async fn get_slot_commitment_by_index_raw(&self, slot_index: &SlotIndex) -> Result> { + let path = &format!("api/core/v3/commitments/by-index/{slot_index}"); + + self.node_manager + .read() + .await + .get_request_bytes(path, None, self.get_timeout().await) + .await + } + // Peers routes. /// GET /api/core/v3/peers diff --git a/sdk/src/types/block/slot/commitment.rs b/sdk/src/types/block/slot/commitment.rs index d4d9b65f98..5d10e56f5e 100644 --- a/sdk/src/types/block/slot/commitment.rs +++ b/sdk/src/types/block/slot/commitment.rs @@ -9,12 +9,17 @@ use crate::types::block::slot::{RootsId, SlotCommitmentId, SlotIndex}; /// Contains a summary of a slot. /// It is linked to the commitment of the previous slot, which forms a commitment chain. #[derive(Clone, Debug, Eq, PartialEq, Hash, derive_more::From, Packable)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(rename_all = "camelCase") +)] pub struct SlotCommitment { /// The slot index of this commitment. /// It is calculated based on genesis timestamp and the duration of a slot. index: SlotIndex, /// The commitment ID of the previous slot. + #[cfg_attr(feature = "serde", serde(rename = "prevId"))] previous_slot_commitment_id: SlotCommitmentId, /// A BLAKE2b-256 hash of concatenating multiple sparse merkle tree roots of a slot. roots_id: RootsId, diff --git a/sdk/src/types/block/slot/index.rs b/sdk/src/types/block/slot/index.rs index 6a4b902c22..b78ed69ad1 100644 --- a/sdk/src/types/block/slot/index.rs +++ b/sdk/src/types/block/slot/index.rs @@ -1,12 +1,12 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -use derive_more::{Deref, From}; +use derive_more::{Deref, Display, From}; /// Timeline is divided into slots, and each slot has a corresponding slot index. /// To calculate the slot index of a timestamp, `genesisTimestamp` and the duration of a slot are needed. /// The slot index of timestamp `ts` is `(ts - genesisTimestamp)/duration + 1`. -#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, From, Deref, packable::Packable)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, From, Deref, Display, packable::Packable)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct SlotIndex(u64);