truthlinked-sdk 0.1.1

TruthLinked smart-contract SDK
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Serialization and deserialization traits and utilities.
//!
//! This module provides two encoding strategies for contract data:
//!
//! - **`Codec32`**: Fixed 32-byte encoding for storage slots
//! - **`BytesCodec`**: Variable-length encoding for complex types
//!
//! Both traits support `#[derive]` macros for automatic implementation.
//!
//! # Codec32 - Fixed-Size Storage
//!
//! Used for values stored in 32-byte storage slots. All primitive integers,
//! booleans, and 32-byte arrays implement this trait.
//!
//! ```ignore
//! use truthlinked_sdk::codec::Codec32;
//!
//! let value = 42u64;
//! let encoded = value.encode_32(); // [42, 0, 0, ..., 0] (32 bytes)
//! let decoded = u64::decode_32(&encoded)?; // 42
//! ```
//!
//! # BytesCodec - Variable-Length Encoding
//!
//! Used for complex types, strings, and variable-length data. Supports
//! automatic derivation for structs and enums.
//!
//! ```ignore
//! #[derive(BytesCodec)]
//! struct Transfer {
//!     to: [u8; 32],
//!     amount: u64,
//! }
//!
//! let transfer = Transfer { to: [0; 32], amount: 100 };
//! let bytes = transfer.encode_bytes();
//! let decoded = Transfer::decode_bytes(&bytes)?;
//! ```
//!
//! # Encoder/Decoder - Builder Pattern
//!
//! For manual encoding of complex types:
//!
//! ```ignore
//! use truthlinked_sdk::codec::{Encoder, Decoder};
//!
//! // Encoding
//! let mut enc = Encoder::new();
//! enc.push_u64(42);
//! enc.push_string("hello");
//! enc.push_bool(true);
//! let bytes = enc.into_vec();
//!
//! // Decoding
//! let mut dec = Decoder::new(&bytes);
//! let num = dec.read_u64()?;
//! let text = dec.read_string()?;
//! let flag = dec.read_bool()?;
//! dec.finish()?; // Ensure all bytes consumed
//! ```

extern crate alloc;

use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

use crate::error::{Error, Result};

/// Error code for general codec failures.
pub const ERR_CODEC: i32 = 20;

/// Error code for unexpected end of input during decoding.
pub const ERR_CODEC_EOF: i32 = 21;

/// Error code for invalid UTF-8 sequences in string decoding.
pub const ERR_CODEC_UTF8: i32 = 22;

/// Trait for types that can be encoded/decoded to/from 32-byte slots.
///
/// This trait is used for storage operations where all values must fit
/// in fixed 32-byte slots. Primitive integers are stored in little-endian
/// format with zero-padding.
///
/// # Derivable
///
/// Use `#[derive(Codec32)]` for custom types (requires `BytesCodec`):
///
/// ```ignore
/// #[derive(BytesCodec, Codec32)]
/// struct Balance {
///     amount: u64,
///     locked: u64,
/// }
/// ```
///
/// # Example
///
/// ```ignore
/// let value = 1000u64;
/// let slot = value.encode_32();
/// let decoded = u64::decode_32(&slot)?;
/// assert_eq!(decoded, 1000);
/// ```
pub trait Codec32: Sized {
    /// Encodes the value into a 32-byte array.
    fn encode_32(&self) -> [u8; 32];

    /// Decodes a value from a 32-byte array.
    fn decode_32(bytes: &[u8; 32]) -> Result<Self>;
}

/// Trait for types that can be encoded/decoded to/from variable-length bytes.
///
/// This trait is used for complex types, strings, and data that doesn't fit
/// in 32 bytes. All encoding uses little-endian format for integers.
///
/// # Derivable
///
/// Use `#[derive(BytesCodec)]` for automatic implementation:
///
/// ```ignore
/// #[derive(BytesCodec)]
/// struct User {
///     name: String,
///     age: u32,
///     active: bool,
/// }
/// ```
///
/// # Example
///
/// ```ignore
/// let data = vec![1, 2, 3, 4];
/// let bytes = data.encode_bytes();
/// let decoded = Vec::<u8>::decode_bytes(&bytes)?;
/// assert_eq!(decoded, data);
/// ```
pub trait BytesCodec: Sized {
    /// Encodes the value into a byte vector.
    fn encode_bytes(&self) -> Vec<u8>;

    /// Decodes a value from a byte slice.
    fn decode_bytes(bytes: &[u8]) -> Result<Self>;
}

/// Builder for encoding multiple values into a byte stream.
///
/// `Encoder` provides a convenient API for serializing complex types
/// by chaining method calls. All integers are encoded in little-endian format.
///
/// # Example
///
/// ```ignore
/// let mut enc = Encoder::with_capacity(64);
/// enc.push_u64(42);
/// enc.push_string("hello");
/// enc.push_bool(true);
/// enc.push_bytes(&[1, 2, 3]);
///
/// let bytes = enc.into_vec();
/// ```
#[derive(Clone, Debug, Default)]
pub struct Encoder {
    bytes: Vec<u8>,
}

impl Encoder {
    /// Creates a new empty encoder.
    pub fn new() -> Self {
        Self { bytes: Vec::new() }
    }

    /// Creates a new encoder with pre-allocated capacity.
    ///
    /// Use this when you know the approximate size to avoid reallocations.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            bytes: Vec::with_capacity(capacity),
        }
    }

    /// Appends a `u8` value.
    pub fn push_u8(&mut self, value: u8) {
        self.bytes.push(value);
    }

    /// Appends a `u16` value (little-endian).
    pub fn push_u16(&mut self, value: u16) {
        self.bytes.extend_from_slice(&value.to_le_bytes());
    }

    /// Appends a `u32` value (little-endian).
    pub fn push_u32(&mut self, value: u32) {
        self.bytes.extend_from_slice(&value.to_le_bytes());
    }

    /// Appends a `u64` value (little-endian).
    pub fn push_u64(&mut self, value: u64) {
        self.bytes.extend_from_slice(&value.to_le_bytes());
    }

    /// Appends a `u128` value (little-endian).
    pub fn push_u128(&mut self, value: u128) {
        self.bytes.extend_from_slice(&value.to_le_bytes());
    }

    /// Appends a boolean value (0 or 1).
    pub fn push_bool(&mut self, value: bool) {
        self.push_u8(if value { 1 } else { 0 });
    }

    /// Appends raw bytes without length prefix.
    pub fn push_raw(&mut self, bytes: &[u8]) {
        self.bytes.extend_from_slice(bytes);
    }

    /// Appends a byte slice with a u32 length prefix.
    ///
    /// Format: `[length: u32][data: bytes]`
    pub fn push_bytes(&mut self, bytes: &[u8]) {
        self.push_u32(bytes.len() as u32);
        self.push_raw(bytes);
    }

    /// Appends a string with a u32 length prefix.
    ///
    /// The string is encoded as UTF-8 bytes.
    pub fn push_string(&mut self, value: &str) {
        self.push_bytes(value.as_bytes());
    }

    /// Appends a value that implements `BytesCodec` with a length prefix.
    pub fn push_codec<T: BytesCodec>(&mut self, value: &T) {
        self.push_bytes(&value.encode_bytes());
    }

    /// Returns the encoded bytes as a slice.
    pub fn as_slice(&self) -> &[u8] {
        &self.bytes
    }

    /// Consumes the encoder and returns the encoded bytes.
    pub fn into_vec(self) -> Vec<u8> {
        self.bytes
    }
}

/// Parser for decoding values from a byte stream.
///
/// `Decoder` provides a cursor-based API for deserializing values encoded
/// by `Encoder`. All integers are decoded from little-endian format.
///
/// # Example
///
/// ```ignore
/// let mut dec = Decoder::new(&bytes);
/// let num = dec.read_u64()?;
/// let text = dec.read_string()?;
/// let flag = dec.read_bool()?;
/// dec.finish()?; // Verify all bytes consumed
/// ```
pub struct Decoder<'a> {
    bytes: &'a [u8],
    cursor: usize,
}

impl<'a> Decoder<'a> {
    /// Creates a new decoder from a byte slice.
    pub fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, cursor: 0 }
    }

    /// Returns the number of unread bytes remaining.
    pub fn remaining(&self) -> usize {
        self.bytes.len().saturating_sub(self.cursor)
    }

    /// Reads exactly `len` bytes, advancing the cursor.
    ///
    /// Returns an error if not enough bytes are available.
    fn read_exact(&mut self, len: usize) -> Result<&'a [u8]> {
        let end = self
            .cursor
            .checked_add(len)
            .ok_or_else(|| Error::new(ERR_CODEC_EOF))?;
        if end > self.bytes.len() {
            return Err(Error::new(ERR_CODEC_EOF));
        }
        let out = &self.bytes[self.cursor..end];
        self.cursor = end;
        Ok(out)
    }

    /// Reads a fixed-size byte array.
    pub fn read_array<const N: usize>(&mut self) -> Result<[u8; N]> {
        let bytes = self.read_exact(N)?;
        let mut out = [0u8; N];
        out.copy_from_slice(bytes);
        Ok(out)
    }

    /// Reads a `u8` value.
    pub fn read_u8(&mut self) -> Result<u8> {
        Ok(self.read_exact(1)?[0])
    }

    /// Reads a `u16` value (little-endian).
    pub fn read_u16(&mut self) -> Result<u16> {
        Ok(u16::from_le_bytes(self.read_array()?))
    }

    /// Reads a `u32` value (little-endian).
    pub fn read_u32(&mut self) -> Result<u32> {
        Ok(u32::from_le_bytes(self.read_array()?))
    }

    /// Reads a `u64` value (little-endian).
    pub fn read_u64(&mut self) -> Result<u64> {
        Ok(u64::from_le_bytes(self.read_array()?))
    }

    /// Reads a `u128` value (little-endian).
    pub fn read_u128(&mut self) -> Result<u128> {
        Ok(u128::from_le_bytes(self.read_array()?))
    }

    /// Reads a boolean value (0 = false, 1 = true).
    ///
    /// Returns an error if the byte is neither 0 nor 1.
    pub fn read_bool(&mut self) -> Result<bool> {
        match self.read_u8()? {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(Error::new(ERR_CODEC)),
        }
    }

    /// Reads exactly `len` raw bytes without a length prefix.
    pub fn read_raw(&mut self, len: usize) -> Result<&'a [u8]> {
        self.read_exact(len)
    }

    /// Reads a byte slice with a u32 length prefix.
    ///
    /// Format: `[length: u32][data: bytes]`
    pub fn read_bytes(&mut self) -> Result<&'a [u8]> {
        let len = self.read_u32()? as usize;
        self.read_exact(len)
    }

    /// Reads a UTF-8 string with a u32 length prefix.
    ///
    /// Returns an error if the bytes are not valid UTF-8.
    pub fn read_string(&mut self) -> Result<String> {
        let bytes = self.read_bytes()?;
        let value = core::str::from_utf8(bytes).map_err(|_| Error::new(ERR_CODEC_UTF8))?;
        Ok(String::from(value))
    }

    /// Reads a value that implements `BytesCodec` with a length prefix.
    pub fn read_codec<T: BytesCodec>(&mut self) -> Result<T> {
        let bytes = self.read_bytes()?;
        T::decode_bytes(bytes)
    }

    /// Verifies that all bytes have been consumed.
    ///
    /// Returns an error if there are unread bytes remaining.
    /// Use this at the end of decoding to ensure data integrity.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let mut dec = Decoder::new(&bytes);
    /// let value = dec.read_u64()?;
    /// dec.finish()?; // Ensure no trailing bytes
    /// ```
    pub fn finish(self) -> Result<()> {
        if self.cursor == self.bytes.len() {
            Ok(())
        } else {
            Err(Error::new(ERR_CODEC))
        }
    }
}

// ============================================================================
// Codec32 Implementations
// ============================================================================

impl Codec32 for [u8; 32] {
    fn encode_32(&self) -> [u8; 32] {
        *self
    }

    fn decode_32(bytes: &[u8; 32]) -> Result<Self> {
        Ok(*bytes)
    }
}

impl Codec32 for bool {
    fn encode_32(&self) -> [u8; 32] {
        let mut out = [0u8; 32];
        out[0] = if *self { 1 } else { 0 };
        out
    }

    fn decode_32(bytes: &[u8; 32]) -> Result<Self> {
        match bytes[0] {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(Error::new(ERR_CODEC)),
        }
    }
}

/// Implements `Codec32` for integer types using little-endian encoding.
macro_rules! impl_codec32_int {
    ($ty:ty, $width:expr) => {
        impl Codec32 for $ty {
            fn encode_32(&self) -> [u8; 32] {
                let mut out = [0u8; 32];
                out[..$width].copy_from_slice(&self.to_le_bytes());
                out
            }

            fn decode_32(bytes: &[u8; 32]) -> Result<Self> {
                let mut raw = [0u8; $width];
                raw.copy_from_slice(&bytes[..$width]);
                Ok(<$ty>::from_le_bytes(raw))
            }
        }
    };
}

impl_codec32_int!(u8, 1);
impl_codec32_int!(u16, 2);
impl_codec32_int!(u32, 4);
impl_codec32_int!(u64, 8);
impl_codec32_int!(u128, 16);
impl_codec32_int!(i8, 1);
impl_codec32_int!(i16, 2);
impl_codec32_int!(i32, 4);
impl_codec32_int!(i64, 8);
impl_codec32_int!(i128, 16);

// ============================================================================
// BytesCodec Implementations
// ============================================================================

impl BytesCodec for bool {
    fn encode_bytes(&self) -> Vec<u8> {
        vec![if *self { 1 } else { 0 }]
    }

    fn decode_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() != 1 {
            return Err(Error::new(ERR_CODEC));
        }
        match bytes[0] {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(Error::new(ERR_CODEC)),
        }
    }
}

/// Implements `BytesCodec` for integer types using little-endian encoding.
macro_rules! impl_bytescodec_int {
    ($ty:ty, $width:expr) => {
        impl BytesCodec for $ty {
            fn encode_bytes(&self) -> Vec<u8> {
                self.to_le_bytes().to_vec()
            }

            fn decode_bytes(bytes: &[u8]) -> Result<Self> {
                if bytes.len() != $width {
                    return Err(Error::new(ERR_CODEC));
                }
                let mut raw = [0u8; $width];
                raw.copy_from_slice(bytes);
                Ok(<$ty>::from_le_bytes(raw))
            }
        }
    };
}

impl_bytescodec_int!(u8, 1);
impl_bytescodec_int!(u16, 2);
impl_bytescodec_int!(u32, 4);
impl_bytescodec_int!(u64, 8);
impl_bytescodec_int!(u128, 16);
impl_bytescodec_int!(i8, 1);
impl_bytescodec_int!(i16, 2);
impl_bytescodec_int!(i32, 4);
impl_bytescodec_int!(i64, 8);
impl_bytescodec_int!(i128, 16);

impl BytesCodec for Vec<u8> {
    fn encode_bytes(&self) -> Vec<u8> {
        self.clone()
    }

    fn decode_bytes(bytes: &[u8]) -> Result<Self> {
        Ok(bytes.to_vec())
    }
}

impl<const N: usize> BytesCodec for [u8; N] {
    fn encode_bytes(&self) -> Vec<u8> {
        self.to_vec()
    }

    fn decode_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() != N {
            return Err(Error::new(ERR_CODEC));
        }
        let mut out = [0u8; N];
        out.copy_from_slice(bytes);
        Ok(out)
    }
}

impl BytesCodec for String {
    fn encode_bytes(&self) -> Vec<u8> {
        self.as_bytes().to_vec()
    }

    fn decode_bytes(bytes: &[u8]) -> Result<Self> {
        let s = core::str::from_utf8(bytes).map_err(|_| Error::new(ERR_CODEC_UTF8))?;
        Ok(String::from(s))
    }
}

impl<T: BytesCodec> BytesCodec for Option<T> {
    fn encode_bytes(&self) -> Vec<u8> {
        match self {
            Some(value) => {
                let mut out = Vec::with_capacity(1);
                out.push(1);
                out.extend_from_slice(&value.encode_bytes());
                out
            }
            None => vec![0],
        }
    }

    fn decode_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.is_empty() {
            return Err(Error::new(ERR_CODEC));
        }
        match bytes[0] {
            0 => {
                if bytes.len() != 1 {
                    return Err(Error::new(ERR_CODEC));
                }
                Ok(None)
            }
            1 => Ok(Some(T::decode_bytes(&bytes[1..])?)),
            _ => Err(Error::new(ERR_CODEC)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::String;

    #[derive(crate::BytesCodec, crate::Codec32, Debug, PartialEq, Eq)]
    struct SmallRecord {
        id: u16,
        enabled: bool,
    }

    #[derive(crate::BytesCodec, Debug, PartialEq, Eq)]
    enum Action {
        Inc(u64),
        Set { value: u64 },
        Reset,
    }

    #[test]
    fn codec32_roundtrip_u64() {
        let raw = 55u64.encode_32();
        let decoded = u64::decode_32(&raw).unwrap();
        assert_eq!(decoded, 55);
    }

    #[test]
    fn bytescodec_roundtrip_string() {
        let s = String::from("hello");
        let bytes = s.encode_bytes();
        let decoded = String::decode_bytes(&bytes).unwrap();
        assert_eq!(decoded, "hello");
    }

    #[test]
    fn encoder_decoder_roundtrip() {
        let mut enc = Encoder::new();
        enc.push_u64(99);
        enc.push_bool(true);
        enc.push_string("abc");

        let bytes = enc.into_vec();
        let mut dec = Decoder::new(&bytes);
        assert_eq!(dec.read_u64().unwrap(), 99);
        assert!(dec.read_bool().unwrap());
        assert_eq!(dec.read_string().unwrap(), "abc");
        dec.finish().unwrap();
    }

    #[test]
    fn derive_bytescodec_roundtrip_struct_and_enum() {
        let rec = SmallRecord {
            id: 7,
            enabled: true,
        };
        let rec_bytes = rec.encode_bytes();
        let rec_decoded = SmallRecord::decode_bytes(&rec_bytes).unwrap();
        assert_eq!(rec, rec_decoded);

        let action = Action::Set { value: 44 };
        let action_bytes = action.encode_bytes();
        let action_decoded = Action::decode_bytes(&action_bytes).unwrap();
        assert_eq!(action, action_decoded);
    }

    #[test]
    fn derive_codec32_roundtrip_small_record() {
        let rec = SmallRecord {
            id: 42,
            enabled: false,
        };
        let raw = rec.encode_32();
        let decoded = SmallRecord::decode_32(&raw).unwrap();
        assert_eq!(decoded, rec);
    }
}