iota-sdk 2.0.0-beta.1

The IOTA SDK provides developers with a seamless experience to develop on IOTA by providing account abstractions and clients to interact with node APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

/// Convenience macro to implement and derive base features of identifiers.
#[macro_export(local_inner_macros)]
macro_rules! impl_id {
    (
        $(#[$hash_meta:meta])*
        $hash_vis:vis $hash_name:ident {
            $len_vis:vis const LENGTH: usize = $length:literal;
        }
        $(
            $(#[$id_meta:meta])*
            $id_vis:vis $id_name:ident;
        )?
    ) => {
        $(#[$hash_meta])*
        #[derive(
            Clone,
            Copy,
            Eq,
            Hash,
            PartialEq,
            Ord,
            PartialOrd,
            derive_more::From,
            derive_more::AsRef,
            packable::Packable,
        )]
        #[as_ref(forward)]
        #[repr(transparent)]
        $hash_vis struct $hash_name([u8; Self::LENGTH]);

        impl $hash_name {
            #[doc = core::concat!("The length of a [`", core::stringify!($hash_name), "`].")]
            $len_vis const LENGTH: usize = $length;

            #[doc = core::concat!("Creates a new [`", core::stringify!($hash_name), "`].")]
            $hash_vis const fn new(bytes: [u8; Self::LENGTH]) -> Self {
                Self(bytes)
            }

            #[doc = core::concat!("Creates a null [`", core::stringify!($hash_name), "`].")]
            $hash_vis const fn null() -> Self {
                Self([0u8; Self::LENGTH])
            }

            #[doc = core::concat!("Checks if the [`", core::stringify!($hash_name), "`] is null.")]
            $hash_vis fn is_null(&self) -> bool {
                self.0.iter().all(|&b| b == 0)
            }
        }

        impl core::str::FromStr for $hash_name {
            type Err = $crate::types::block::IdentifierError;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok(Self::new(prefix_hex::decode(s).map_err($crate::types::block::IdentifierError)?))
            }
        }

        impl TryFrom<&alloc::string::String> for $hash_name {
            type Error = $crate::types::block::IdentifierError;

            fn try_from(s: &alloc::string::String) -> Result<Self, Self::Error> {
                core::str::FromStr::from_str(s.as_str())
            }
        }

        impl TryFrom<&str> for $hash_name {
            type Error = $crate::types::block::IdentifierError;

            fn try_from(s: &str) -> Result<Self, Self::Error> {
                core::str::FromStr::from_str(s)
            }
        }

        impl $crate::utils::ConvertTo<$hash_name> for &alloc::string::String {
            fn convert(self) -> Result<$hash_name, $crate::utils::ConversionError> {
                self.try_into().map_err($crate::utils::ConversionError::new)
            }
        }

        impl $crate::utils::ConvertTo<$hash_name> for &str {
            fn convert(self) -> Result<$hash_name, $crate::utils::ConversionError> {
                self.try_into().map_err($crate::utils::ConversionError::new)
            }
        }

        impl core::fmt::Display for $hash_name {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::write!(f, "{}", prefix_hex::encode(self.0))
            }
        }

        impl core::fmt::Debug for $hash_name {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::write!(f, "{}({})", core::stringify!($hash_name), self)
            }
        }

        impl core::ops::Deref for $hash_name {
            type Target = [u8; Self::LENGTH];

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        #[cfg(feature = "serde")]
        string_serde_impl!($hash_name);

        $(
            paste::paste!{
                impl $hash_name {
                    pub fn [<into_ $id_name:snake>](self, slot_index: impl Into<$crate::types::block::slot::SlotIndex>) -> $id_name {
                        $id_name {
                            hash: self,
                            slot_index: slot_index.into().to_le_bytes(),
                        }
                    }

                    pub const fn [<const_into_ $id_name:snake>](self, slot_index: $crate::types::block::slot::SlotIndex) -> $id_name {
                        $id_name {
                            hash: self,
                            slot_index: slot_index.0.to_le_bytes(),
                        }
                    }
                }
            }

            $(#[$id_meta])*
            #[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, packable::Packable)]
            #[repr(C)]
            $id_vis struct $id_name {
                pub(crate) hash: $hash_name,
                slot_index: [u8; core::mem::size_of::<$crate::types::block::slot::SlotIndex>()],
            }

            impl $id_name {
                #[doc = core::concat!("The length of a [`", core::stringify!($id_name), "`].")]
                pub const LENGTH: usize = $hash_name::LENGTH + core::mem::size_of::<$crate::types::block::slot::SlotIndex>();

                pub fn new(bytes: [u8; Self::LENGTH]) -> Self {
                    unsafe { core::mem::transmute(bytes) }
                }

                #[doc = core::concat!("Returns the [`", core::stringify!($id_name), "`]'s hash part.")]
                pub fn hash(&self) -> &$hash_name {
                    &self.hash
                }

                #[doc = core::concat!("Returns the [`", core::stringify!($id_name), "`]'s slot index part.")]
                pub fn slot_index(&self) -> $crate::types::block::slot::SlotIndex {
                    unsafe {
                        #[cfg(target_endian = "little")]
                        {
                            core::mem::transmute(self.slot_index)
                        }

                        #[cfg(target_endian = "big")]
                        {
                            core::mem::transmute(self.slot_index.to_le())
                        }
                    }
                }
            }

            impl AsRef<[u8]> for $id_name {
                fn as_ref(&self) -> &[u8] {
                    unsafe { core::mem::transmute::<_, &[u8; Self::LENGTH]>(self) }
                }
            }

            impl core::str::FromStr for $id_name {
                type Err = $crate::types::block::IdentifierError;

                fn from_str(s: &str) -> Result<Self, Self::Err> {
                    Ok(Self::new(prefix_hex::decode(s).map_err($crate::types::block::IdentifierError)?))
                }
            }

            impl core::fmt::Debug for $id_name {
                fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                    f.debug_struct(core::stringify!($id_name))
                        .field("id", &alloc::string::ToString::to_string(self))
                        .field("slot_index", &self.slot_index())
                        .finish()
                }
            }

            impl core::fmt::Display for $id_name {
                fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                    prefix_hex::encode(self.as_ref()).fmt(f)
                }
            }

            impl TryFrom<&alloc::string::String> for $id_name {
                type Error = $crate::types::block::IdentifierError;

                fn try_from(s: &alloc::string::String) -> Result<Self, Self::Error> {
                    core::str::FromStr::from_str(s.as_str())
                }
            }

            impl TryFrom<&str> for $id_name {
                type Error = $crate::types::block::IdentifierError;

                fn try_from(s: &str) -> Result<Self, Self::Error> {
                    core::str::FromStr::from_str(s)
                }
            }

            impl $crate::utils::ConvertTo<$id_name> for &alloc::string::String {
                fn convert(self) -> Result<$id_name, $crate::utils::ConversionError> {
                    self.try_into().map_err($crate::utils::ConversionError::new)
                }
            }

            impl $crate::utils::ConvertTo<$id_name> for &str {
                fn convert(self) -> Result<$id_name, $crate::utils::ConversionError> {
                    self.try_into().map_err($crate::utils::ConversionError::new)
                }
            }

            impl core::ops::Deref for $id_name {
                type Target = [u8; Self::LENGTH];

                fn deref(&self) -> &Self::Target {
                    unsafe { core::mem::transmute::<_, &[u8; Self::LENGTH]>(self) }
                }
            }
            #[cfg(feature = "serde")]
            string_serde_impl!($id_name);
        )?
    };
}

/// Convenience macro to serialize types to string via serde.
#[cfg(feature = "serde")]
#[macro_export]
macro_rules! string_serde_impl {
    ($type:ty) => {
        impl serde::Serialize for $type {
            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                use alloc::string::ToString;

                s.serialize_str(&self.to_string())
            }
        }

        impl<'de> serde::Deserialize<'de> for $type {
            fn deserialize<D>(deserializer: D) -> Result<$type, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                struct StringVisitor;

                impl<'de> serde::de::Visitor<'de> for StringVisitor {
                    type Value = $type;

                    fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                        formatter.write_str("a string representing the value")
                    }

                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                    where
                        E: serde::de::Error,
                    {
                        let value = core::str::FromStr::from_str(v).map_err(serde::de::Error::custom)?;
                        Ok(value)
                    }
                }

                deserializer.deserialize_str(StringVisitor)
            }
        }
    };
}

/// Convenience macro to work around the fact the `[bitflags]` crate does not yet support iterating over the
/// individual flags. This macro essentially creates the `[bitflags]` and puts the individual flags into an associated
/// constant `pub const ALL_FLAGS: &'static []`.
#[macro_export]
macro_rules! create_bitflags {
    ($(#[$meta:meta])* $vis:vis $Name:ident, $TagType:ty, [$(($FlagName:ident, $TypeName:ident),)+]) => {
        bitflags! {
            $(#[$meta])*
            #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
            $vis struct $Name: $TagType {
                $(
                    #[doc = concat!("Signals the presence of a [`", stringify!($TypeName), "`].")]
                    const $FlagName = 1 << $TypeName::KIND;
                )*
            }
        }

        impl $Name {
            #[allow(dead_code)]
            /// Returns a slice of all possible base flags.
            $vis const ALL_FLAGS: &'static [$Name] = &[$($Name::$FlagName),*];
        }
    };
}

#[macro_export]
macro_rules! impl_serde_typed_dto {
    ($base:ty, $dto:ty, $type_str:literal) => {
        impl<'de> Deserialize<'de> for $base {
            fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                let dto = <$dto>::deserialize(d)?;
                if dto.kind != Self::KIND {
                    return Err(serde::de::Error::custom(alloc::format!(
                        "invalid {} type: expected {}, found {}",
                        $type_str,
                        Self::KIND,
                        dto.kind
                    )));
                }
                dto.try_into().map_err(serde::de::Error::custom)
            }
        }

        impl Serialize for $base {
            fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                <$dto>::from(self).serialize(s)
            }
        }
    };
}

#[macro_export]
macro_rules! def_is_as_opt {
    ($type:ty: $($name:ident),+$(,)?) => {
        paste::paste! {
            $(
                #[doc = "Checks whether the " [<$type:snake>] " is a(n) [`" [<$name $type>] "`]."]
                pub fn [<is_ $name:snake>](&self) -> bool {
                    matches!(self, Self::$name(_))
                }

                #[doc = "Gets the " [<$type:snake>] " as an actual [`" [<$name $type>] "`]."]
                #[doc = "PANIC: do not call on a non-" [<$name>] " " [<$type:snake>] "."]
                pub fn [<as_ $name:snake>](&self) -> &[<$name $type>] {
                    #[allow(irrefutable_let_patterns)]
                    if let Self::$name(v) = self {
                        v
                    } else {
                        panic!("{} called on a non-{} {}", stringify!([<as_ $name:snake>]), stringify!([<$name>]), stringify!([<$type:snake>]));
                    }
                }

                #[doc = "Gets the " [<$type:snake>] " as an actual [`" [<$name $type>] "`], if it is one."]
                pub fn [<as_ $name:snake _opt>](&self) -> Option<&[<$name $type>]> {
                    #[allow(irrefutable_let_patterns)]
                    if let Self::$name(v) = self {
                        Some(v)
                    } else {
                        None
                    }
                }
            )+
        }
    };
}

#[macro_export]
macro_rules! impl_deserialize_untagged {
    ($type:ty: $($name:ident),+$(,)?) => {
        paste::paste!{
            impl<'de> serde::Deserialize<'de> for $type {
                fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                    let value = serde_json::Value::deserialize(d)?;
                    Ok(
                        match value
                            .get("type")
                            .and_then(serde_json::Value::as_u64)
                            .ok_or_else(|| serde::de::Error::custom(core::concat!("invalid ", core::stringify!($type), " type")))? as u8
                        {
                            $(
                            [<$name $type>]::KIND => {
                                Self::from([<$name $type>]::deserialize(value).map_err(|e| {
                                    serde::de::Error::custom(alloc::format!(core::concat!("cannot deserialize ", core::stringify!($name), core::stringify!($type), ": {}"), e))
                                })?)
                            }
                            )+
                            _ => return Err(serde::de::Error::custom(core::concat!("invalid ", core::stringify!($type), " type"))),
                        },
                    )
                }
            }
        }
    }
}

#[macro_export]
macro_rules! impl_from_error_via {
    ($self:ident via $via:ident: $($err:ident),+$(,)?) => {
        $(
        impl From<$err> for $self {
            fn from(error: $err) -> Self {
                Self::from($via::from(error))
            }
        }
        )+
    };
}