-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.0' into api-tips-example-test
- Loading branch information
Showing
12 changed files
with
205 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Find an output with its metadata, by its identifier by querying the `/api/core/v3/outputs/{outputId}/full` node | ||
//! endpoint. | ||
//! | ||
//! Make sure to provide a somewhat recent output id to make this example run successfully! | ||
//! | ||
//! Rename `.env.example` to `.env` first, then run the command: | ||
//! ```sh | ||
//! cargo run --release --example node_api_core_get_output_full <OUTPUT ID> [NODE URL] | ||
//! ``` | ||
|
||
use iota_sdk::{ | ||
client::{Client, Result}, | ||
types::block::output::OutputId, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// If not provided we use the default node from the `.env` file. | ||
dotenvy::dotenv().ok(); | ||
|
||
// Take the node URL from command line argument or use one from env as default. | ||
let node_url = std::env::args() | ||
.nth(2) | ||
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set")); | ||
|
||
// Create a node client. | ||
let client = Client::builder().with_node(&node_url)?.finish().await?; | ||
|
||
// Take the output id from the command line, or panic. | ||
let output_id = std::env::args() | ||
.nth(1) | ||
.expect("missing example argument: OUTPUT ID") | ||
.parse::<OutputId>()?; | ||
|
||
// Get the output with its metadata. | ||
let output_with_metadata = client.get_output_with_metadata(&output_id).await?; | ||
|
||
println!("{output_with_metadata:?}"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters