sia_core 0.4.1

Low-level SDK for interacting with the Sia decentralized storage network
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Macro to implement types used as identifiers which are 32 byte hashes and are
// serialized with a prefix
macro_rules! impl_hash_id {
    ($name:ident) => {
        #[derive(
            Debug,
            Clone,
            Copy,
            PartialEq,
            Eq,
            Hash,
            $crate::encoding_async::AsyncSiaDecode,
            $crate::encoding::SiaEncode,
            $crate::encoding::SiaDecode,
            $crate::encoding::V1SiaEncode,
            $crate::encoding::V1SiaDecode,
        )]
        pub struct $name([u8; 32]);

        impl serde::Serialize for $name {
            fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                if serializer.is_human_readable() {
                    String::serialize(&self.to_string(), serializer)
                } else {
                    <[u8; 32]>::serialize(&self.0, serializer)
                }
            }
        }

        impl<'de> serde::Deserialize<'de> for $name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                let s = String::deserialize(deserializer)?;
                s.parse()
                    .map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
            }
        }

        impl $name {
            pub const fn new(b: [u8; 32]) -> Self {
                Self(b)
            }
        }

        impl core::fmt::Display for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                write!(f, "{}", hex::encode(self.0))
            }
        }

        impl From<blake2b_simd::Hash> for $name {
            fn from(hash: blake2b_simd::Hash) -> Self {
                let mut h = [0; 32];
                h.copy_from_slice(&hash.as_bytes()[..32]);
                Self(h)
            }
        }

        impl From<crate::blake2::Output> for $name {
            fn from(output: crate::blake2::Output) -> Self {
                let mut h = [0; 32];
                h.copy_from_slice(&output[..32]);
                Self(h)
            }
        }

        impl From<[u8; 32]> for $name {
            fn from(data: [u8; 32]) -> Self {
                $name(data)
            }
        }

        impl From<$name> for [u8; 32] {
            fn from(hash: $name) -> [u8; 32] {
                hash.0
            }
        }

        impl std::str::FromStr for $name {
            type Err = $crate::types::HexParseError;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                let s = match s.split_once(':') {
                    Some((_prefix, suffix)) => suffix,
                    None => s,
                };

                if s.len() != 64 {
                    return Err($crate::types::HexParseError::InvalidLength(s.len()));
                }

                let mut data = [0u8; 32];
                hex::decode_to_slice(s, &mut data)
                    .map_err($crate::types::HexParseError::HexError)?;
                Ok($name(data))
            }
        }

        impl AsRef<[u8; 32]> for $name {
            fn as_ref(&self) -> &[u8; 32] {
                &self.0
            }
        }

        impl AsRef<[u8]> for $name {
            fn as_ref(&self) -> &[u8] {
                &self.0
            }
        }

        impl Default for $name {
            fn default() -> Self {
                $name([0; 32])
            }
        }
    };
}
pub(crate) use impl_hash_id;

#[inline]
pub(crate) const fn decode_hex_char(c: u8) -> Option<u8> {
    match c {
        b'0'..=b'9' => Some(c - b'0'),
        b'a'..=b'f' => Some(c - b'a' + 10),
        b'A'..=b'F' => Some(c - b'A' + 10),
        _ => None,
    }
}

#[inline]
pub(crate) const fn decode_hex_pair(hi: u8, lo: u8) -> Option<u8> {
    let hi = decode_hex_char(hi);
    let lo = decode_hex_char(lo);
    match (hi, lo) {
        (Some(hi), Some(lo)) => Some((hi << 4) | lo),
        _ => None,
    }
}

/// Decode a hex string of length 64 into a 32-byte array.
///
/// # Panics
/// if the input is not valid hex.
///
/// # Safety
/// This function is intended to be used in const contexts, such as macros.
#[inline]
#[doc(hidden)]
pub const fn decode_hex_256(input: &[u8]) -> [u8; 32] {
    // note: this must be public to be used in macros
    let mut result = [0u8; 32];
    let mut i = 0;
    while i < 64 {
        match decode_hex_pair(input[i], input[i + 1]) {
            Some(byte) => result[i / 2] = byte,
            _ => panic!("invalid hex char"),
        }
        i += 2;
    }
    result
}

/// Decode a hex string of length 128 into a 64-byte array.
///
/// # Panics
/// if the input is not valid hex.
///
/// # Safety
/// This function is intended to be used in const contexts, such as macros.
#[inline]
#[doc(hidden)]
pub const fn decode_hex_512(input: &[u8]) -> [u8; 64] {
    // note: this must be public to be used in macros
    let mut result = [0u8; 64];
    let mut i = 0;
    while i < 128 {
        match decode_hex_pair(input[i], input[i + 1]) {
            Some(byte) => result[i / 2] = byte,
            _ => panic!("invalid hex char"),
        }
        i += 2;
    }
    result
}

/// A macro to create an [sia_core::types::Address] from a literal hex string. The string must be 76 characters long.
///
/// The checksum is not verified.
/// ```
/// use sia_core::types::Address;
/// use sia_core::address;
///
/// const addr: Address = address!("8fb49ccf17dfdcc9526dec6ee8a5cca20ff8247302053d3777410b9b0494ba8cdf32abee86f0");
/// ```
#[macro_export]
macro_rules! address {
    ($text:literal) => {{
        if $text.len() != 76 {
            panic!("Address must be 76 characters");
        }
        $crate::types::Address::new($crate::macros::decode_hex_256($text.as_bytes()))
    }};
}

/// A macro to create a [sia_core::types::Hash256] from a literal hex string. The string must be 64 characters long.
///
/// ```
/// use sia_core::types::Hash256;
/// use sia_core::hash_256;
///
/// const h: Hash256 = hash_256!("8fb49ccf17dfdcc9526dec6ee8a5cca20ff8247302053d3777410b9b0494ba8c");
/// ```
#[macro_export]
macro_rules! hash_256 {
    ($text:literal) => {{
        if $text.len() != 64 {
            panic!("Hash256 must be 64 characters");
        }
        $crate::types::Hash256::new($crate::macros::decode_hex_256($text.as_bytes()))
    }};
}

/// A macro to create a [sia_core::types::SiacoinOutputID] from a literal hex string. The string must be 64 characters long.
///
///```
/// use sia_core::types::SiacoinOutputID;
/// use sia_core::siacoin_id;
///
/// const scoid: SiacoinOutputID = siacoin_id!("8fb49ccf17dfdcc9526dec6ee8a5cca20ff8247302053d3777410b9b0494ba8c");
/// ```
#[macro_export]
macro_rules! siacoin_id {
    ($text:literal) => {{
        if $text.len() != 64 {
            panic!("SiacoinOutputID must be 64 characters");
        }
        $crate::types::SiacoinOutputID::new($crate::macros::decode_hex_256($text.as_bytes()))
    }};
}

/// A macro to create a [sia_core::types::SiafundOutputID] from a literal hex string. The string must be 64 characters long.
///
/// ```
/// use sia_core::types::SiafundOutputID;
/// use sia_core::siafund_id;
///
/// const sfoid: SiafundOutputID = siafund_id!("8fb49ccf17dfdcc9526dec6ee8a5cca20ff8247302053d3777410b9b0494ba8c");
/// ```
#[macro_export]
macro_rules! siafund_id {
    ($text:literal) => {{
        if $text.len() != 64 {
            panic!("SiafundOutputID must be 64 characters");
        }
        $crate::types::SiafundOutputID::new($crate::macros::decode_hex_256($text.as_bytes()))
    }};
}

/// A macro to create a [sia_core::types::FileContractID] from a literal hex string. The string must be 64 characters long.
///
/// ```
/// use sia_core::types::FileContractID;
/// use sia_core::contract_id;
///
/// const fcid: FileContractID = contract_id!("8fb49ccf17dfdcc9526dec6ee8a5cca20ff8247302053d3777410b9b0494ba8c");
/// ```
#[macro_export]
macro_rules! contract_id {
    ($text:literal) => {{
        if $text.len() != 64 {
            panic!("FileContractID must be 64 characters");
        }
        $crate::types::FileContractID::new($crate::macros::decode_hex_256($text.as_bytes()))
    }};
}

/// A macro to create a [sia_core::types::TransactionID] from a literal hex string. The string must be 64 characters long.
///
/// ```
/// use sia_core::types::TransactionID;
/// use sia_core::transaction_id;
///
/// const txid: TransactionID = transaction_id!("8fb49ccf17dfdcc9526dec6ee8a5cca20ff8247302053d3777410b9b0494ba8c");
/// ```
#[macro_export]
macro_rules! transaction_id {
    ($text:literal) => {{
        if $text.len() != 64 {
            panic!("TransactionID must be 64 characters");
        }
        $crate::types::TransactionID::new($crate::macros::decode_hex_256($text.as_bytes()))
    }};
}

/// A macro to create a [sia_core::types::BlockID] from a literal hex string. The string must be 64 characters long.
///
/// ```
/// use sia_core::types::BlockID;
/// use sia_core::block_id;
///
/// const bid: BlockID = block_id!("8fb49ccf17dfdcc9526dec6ee8a5cca20ff8247302053d3777410b9b0494ba8c");
/// ```
#[macro_export]
macro_rules! block_id {
    ($text:literal) => {{
        if $text.len() != 64 {
            panic!("BlockID must be 64 characters");
        }
        $crate::types::BlockID::new($crate::macros::decode_hex_256($text.as_bytes()))
    }};
}

/// A macro to create a [sia_core::signing::PublicKey] from a literal hex string. The string must be 72 characters long and start with "ed25519:".
///
/// ```
/// use sia_core::signing::PublicKey;
/// use sia_core::public_key;
///
/// const pk: PublicKey = public_key!("ed25519:8fb49ccf17dfdcc9526dec6ee8a5cca20ff8247302053d3777410b9b0494ba8c");
/// ```
#[macro_export]
macro_rules! public_key {
    ($text:literal) => {{
        if $text.len() != 72 {
            panic!("PublicKey must be 72 characters");
        }
        const ED25519_PREFIX: &[u8; 8] = b"ed25519:";

        let buf = $text.as_bytes();
        let mut s = [0u8; 64];
        let mut i = 0;
        while i < 72 {
            if i < 8 {
                if buf[i] != ED25519_PREFIX[i] {
                    panic!("PublicKey must start with ed25519:")
                }
            } else {
                s[i - 8] = buf[i];
            }
            i += 1;
        }

        $crate::signing::PublicKey::new($crate::macros::decode_hex_256(&s))
    }};
}

/// A macro to create a [sia_core::signing::Signature] from a literal hex string. The string must be 128 characters long.
///
/// ```
/// use sia_core::signing::Signature;
/// use sia_core::signature;
///
/// const sig: Signature = signature!("458283fd707c9d170d5e1814944f35893c53c9445fd46c74a6b285bf3029bf404c9af509ea271d811726bd20d8c7d8fe4b9efdc4bebb445f18059eca886ece03");
/// ```
#[macro_export]
macro_rules! signature {
    ($text:literal) => {{
        if $text.len() != 128 {
            panic!("Signature must be 128 characters");
        }
        $crate::signing::Signature::new($crate::macros::decode_hex_512($text.as_bytes()))
    }};
}

#[cfg(test)]
mod tests {
    use crate::signing::PublicKey;
    use crate::types::{
        Address, BlockID, FileContractID, SiacoinOutputID, SiafundOutputID, TransactionID,
    };

    const EXPECTED_BYTES: [u8; 32] = [
        94, 183, 15, 20, 19, 135, 223, 30, 46, 205, 67, 75, 34, 190, 80, 191, 245, 122, 110, 8, 72,
        79, 56, 144, 254, 68, 21, 166, 211, 35, 181, 233,
    ];

    #[test]
    fn test_address_macro() {
        const ADDRESS: Address = address!(
            "5eb70f141387df1e2ecd434b22be50bff57a6e08484f3890fe4415a6d323b5e9e758b4f79b34"
        );
        assert_eq!(ADDRESS.as_ref(), EXPECTED_BYTES);
    }

    #[test]
    #[should_panic]
    fn test_bad_address() {
        address!("5eb70f141387df1e2ecd434b22be50bff57a6e08484f3890fe4415a6d323b5e9e758b4");
    }

    #[test]
    fn test_public_key_macro() {
        const PUBLIC_KEY: PublicKey =
            public_key!("ed25519:5eb70f141387df1e2ecd434b22be50bff57a6e08484f3890fe4415a6d323b5e9");
        assert_eq!(PUBLIC_KEY.as_ref(), EXPECTED_BYTES);
    }

    #[test]
    fn test_block_id_macro() {
        const BLOCK_ID: BlockID =
            block_id!("5eb70f141387df1e2ecd434b22be50bff57a6e08484f3890fe4415a6d323b5e9");
        assert_eq!(BLOCK_ID.as_ref(), EXPECTED_BYTES);
    }

    #[test]
    fn test_transaction_id_macro() {
        const TRANSACTION_ID: TransactionID =
            transaction_id!("5eb70f141387df1e2ecd434b22be50bff57a6e08484f3890fe4415a6d323b5e9");
        assert_eq!(TRANSACTION_ID.as_ref(), EXPECTED_BYTES);
    }

    #[test]
    fn test_contract_id_macro() {
        const CONTRACT_ID: FileContractID =
            contract_id!("5eb70f141387df1e2ecd434b22be50bff57a6e08484f3890fe4415a6d323b5e9");
        assert_eq!(CONTRACT_ID.as_ref(), EXPECTED_BYTES);
    }

    #[test]
    fn test_siacoin_id_macro() {
        const SIACOIN_ID: SiacoinOutputID =
            siacoin_id!("5eb70f141387df1e2ecd434b22be50bff57a6e08484f3890fe4415a6d323b5e9");
        assert_eq!(SIACOIN_ID.as_ref(), EXPECTED_BYTES);
    }

    #[test]
    fn test_siafund_id_macro() {
        const SIAFUND_ID: SiafundOutputID =
            siafund_id!("5eb70f141387df1e2ecd434b22be50bff57a6e08484f3890fe4415a6d323b5e9");
        assert_eq!(SIAFUND_ID.as_ref(), EXPECTED_BYTES);
    }
}