borderless-id-types 0.1.5

Definition of various identifier types for the borderless ecosystem
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
//! # Borderless ID-Types
//!
//! This library contains various types that are used as IDs for different things in the borderless ecosystem.

use core::fmt;
use std::fmt::Display;

use borderless_hash::Hash256;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use serde::{Deserialize, Serialize};

pub use uuid::Uuid;

/// Generic macro to define wrapper types around u128 and uuid.
macro_rules! impl_uuid {
    ($type:ident, $prefix_upper:literal, $prefix_lower:literal) => {
        impl $type {
            /// Generates a new ID
            #[cfg(any(all(feature = "generate_ids", not(target_arch = "wasm32")), test))]
            pub fn generate() -> Self {
                // Start by generating a v4 uuid
                let uuid = uuid::Uuid::new_v4();
                // Then convert it to a valid $type id
                uuid.into()
            }

            /// Converts the ID into a [`uuid::Uuid`]
            pub fn into_uuid(self) -> uuid::Uuid {
                self.0
            }

            // NOTE: I am not sure; we have two options here. Either we just construct a uuid based on the bytes,
            // but then it may not be a valid $type. Or, we enforce our bit-pattern here, so we know, that we have a valid $type,
            // BUT this changes the roundtrip encoding (bytes -> $type -> bytes), because we modified the array here.
            pub fn from_bytes(mut bytes: [u8; 16]) -> Self {
                bytes[0] = bytes[0] & $prefix_upper | $prefix_lower;
                $type(uuid::Uuid::new_v8(bytes))
            }

            pub fn from_slice(slice: &[u8]) -> Result<Self, std::array::TryFromSliceError> {
                slice.try_into()
            }

            /// Returns the underlying bytes
            pub fn into_bytes(self) -> [u8; 16] {
                self.0.into_bytes()
            }

            /// Returns a reference to the underlying bytes
            pub fn as_bytes(&self) -> &[u8; 16] {
                self.0.as_bytes()
            }

            /// Parses an ID from a `&str`
            pub fn parse_str(s: &str) -> Result<Self, uuid::Error> {
                let uuid = Uuid::parse_str(s)?;
                Ok(uuid.into())
            }

            /// Merges two IDs commutatively
            ///
            /// Can be used to construct database keys.
            pub fn merge(&self, other: &Self) -> [u8; 16] {
                let mut out = [0; 16];
                for i in 0..16 {
                    out[i] = self.as_bytes()[i] ^ other.as_bytes()[i];
                }
                out
            }

            /// Merges and Compacts two IDs into a `u64`
            ///
            /// Can be used to construct database keys.
            pub fn merge_compact(&self, other: &Self) -> u64 {
                let merged = self.merge(other);
                let mut out = [0; 8];
                for i in 0..8 {
                    out[i] = merged[i] ^ merged[i + 8];
                }
                u64::from_be_bytes(out)
            }

            /// Compacts the current ID into a `u64`
            ///
            /// Can be used to construct database keys.
            pub fn compact(&self) -> u64 {
                let mut out = [0; 8];
                for i in 0..8 {
                    out[i] = self.as_bytes()[i] ^ self.as_bytes()[i + 8];
                }
                u64::from_be_bytes(out)
            }
        }

        impl<'de> serde::Deserialize<'de> for $type {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                // NOTE: We delegate to the From<uuid> here
                let uuid = uuid::Uuid::deserialize(deserializer)?;
                Ok($type::from(uuid))
            }
        }

        impl From<u128> for $type {
            fn from(value: u128) -> Self {
                let mut bytes = value.to_be_bytes();
                bytes[0] = bytes[0] & $prefix_upper | $prefix_lower;
                $type(uuid::Uuid::new_v8(bytes))
            }
        }

        impl From<uuid::Uuid> for $type {
            fn from(value: uuid::Uuid) -> Self {
                let mut bytes = value.into_bytes();
                bytes[0] = bytes[0] & $prefix_upper | $prefix_lower;
                $type(uuid::Uuid::new_v8(bytes))
            }
        }

        impl From<$type> for uuid::Uuid {
            fn from(value: $type) -> uuid::Uuid {
                value.0
            }
        }

        impl From<$type> for u128 {
            fn from(value: $type) -> u128 {
                value.0.as_u128()
            }
        }

        impl TryFrom<&str> for $type {
            type Error = uuid::Error;

            fn try_from(value: &str) -> Result<Self, Self::Error> {
                uuid::Uuid::parse_str(&value).map(Into::into)
            }
        }

        impl TryFrom<String> for $type {
            type Error = uuid::Error;

            fn try_from(value: String) -> Result<Self, Self::Error> {
                $type::try_from(value.as_str())
            }
        }

        impl TryFrom<&[u8]> for $type {
            type Error = std::array::TryFromSliceError;

            fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
                let bytes = value.try_into()?;
                Ok($type::from_bytes(bytes))
            }
        }

        impl std::str::FromStr for $type {
            type Err = uuid::Error;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                uuid::Uuid::parse_str(s).map(Into::into)
            }
        }

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

        impl From<$type> for String {
            fn from(value: $type) -> String {
                value.0.to_string()
            }
        }

        impl From<&$type> for String {
            fn from(value: &$type) -> String {
                value.0.to_string()
            }
        }

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

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

/// An agent-id used to identify software-agents in the borderless-ecosystem.
///
/// These ids are version 8 [uuids](https://en.wikipedia.org/wiki/Universally_unique_identifier), where
/// the first four bits of the uuid are set to `0xa`, to indicate that it is an agent-id and not another uuid based id.
///
/// Example:
/// ```sh
/// afc23cb3-f447-8107-8f93-9bfb8e1d157d
/// ```
///
/// All uuid-based ids used in the borderless-ecosystem have a different prefix, based on what the id is used for.
/// This mechanism ensures that you cannot mistake an agent-id for e.g. a contract-id and vice versa. Even if you convert the agent-id
/// back into a uuid and the result into a contract-id, the results are different.
///
/// The implementation of the IDs is compliant with [RFC9562](https://www.ietf.org/rfc/rfc9562.html#name-uuid-version-8),
/// as we utilize standard version 8 uuids.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Hash, PartialOrd, Ord)]
pub struct AgentId(uuid::Uuid);
impl_uuid!(AgentId, 0xaf, 0xa0);

/// The borderless-id used to identify participants in the borderless-network.
///
/// These ids are version 8 [uuids](https://en.wikipedia.org/wiki/Universally_unique_identifier), where
/// the first four bits of the uuid are set to `0xb`, to indicate that it is a borderless-id and not another uuid based id.
///
/// Example:
/// ```sh
/// 0bc23cb3-f447-8107-8f93-9bfb8e1d157d
/// ```
///
/// All uuid-based ids used in the borderless-ecosystem have a different prefix, based on what the id is used for.
/// This mechanism ensures that you cannot mistake a participant-id for e.g. a contract-id and vice versa. Even if you convert the participant-id
/// back into a uuid and the result into a contract-id, the results are different.
///
/// The implementation of the IDs is compliant with [RFC9562](https://www.ietf.org/rfc/rfc9562.html#name-uuid-version-8),
/// as we utilize standard version 8 uuids.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Hash, PartialOrd, Ord)]
pub struct BorderlessId(uuid::Uuid);
impl_uuid!(BorderlessId, 0xbf, 0xb0);

/// A contract-id used to itentify different SmartContracts in the borderless-ecosystem.
///
/// These ids are version 8 [uuids](https://en.wikipedia.org/wiki/Universally_unique_identifier), where
/// the first four bits of the uuid are set to `0xc`, to indicate that it is a contract-id and not another uuid based id.
///
/// Example:
/// ```sh
/// cfc23cb3-f447-8107-8f93-9bfb8e1d157d
/// ```
///
/// All uuid-based ids used in the borderless-ecosystem have a different prefix, based on what the id is used for.
/// This mechanism ensures that you cannot mistake a contract-id for e.g. a process-id and vice versa. Even if you convert the contract-id
/// back into a uuid and the result into a process-id, the results are different.
///
/// The implementation of the IDs is compliant with [RFC9562](https://www.ietf.org/rfc/rfc9562.html#name-uuid-version-8),
/// as we utilize standard version 8 uuids.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Hash, PartialOrd, Ord)]
pub struct ContractId(uuid::Uuid);
impl_uuid!(ContractId, 0xcf, 0xc0);

/// A decentralized-id used to itentify different assets and documents in the borderless-ecosystem.
///
/// These ids are version 8 [uuids](https://en.wikipedia.org/wiki/Universally_unique_identifier), where
/// the first four bits of the uuid are set to `0xd`, to indicate that it is a decentralized-id and not another uuid based id.
///
/// Example:
/// ```sh
/// dfc23cb3-f447-8107-8f93-9bfb8e1d157d
/// ```
///
/// All uuid-based ids used in the borderless-ecosystem have a different prefix, based on what the id is used for.
/// This mechanism ensures that you cannot mistake a contract-id for e.g. a process-id and vice versa. Even if you convert the contract-id
/// back into a uuid and the result into a process-id, the results are different.
///
/// The implementation of the IDs is compliant with [RFC9562](https://www.ietf.org/rfc/rfc9562.html#name-uuid-version-8),
/// as we utilize standard version 8 uuids.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Hash, PartialOrd, Ord)]
pub struct Did(uuid::Uuid);
impl_uuid!(Did, 0xdf, 0xd0);

/// An external-id used to identify external entities, that are not in the borderless-network.
///
/// These ids are version 8 [uuids](https://en.wikipedia.org/wiki/Universally_unique_identifier), where
/// the first four bits of the uuid are set to `0xe`, to indicate that it is an external-id and not another uuid based id.
///
/// Example:
/// ```sh
/// ebc23cb3-f447-8107-8f93-9bfb8e1d157d
/// ```
///
/// All uuid-based ids used in the borderless-ecosystem have a different prefix, based on what the id is used for.
/// This mechanism ensures that you cannot mistake an participant-id for e.g. a contract-id and vice versa. Even if you convert the participant-id
/// back into a uuid and the result into a contract-id, the results are different.
///
/// The implementation of the IDs is compliant with [RFC9562](https://www.ietf.org/rfc/rfc9562.html#name-uuid-version-8),
/// as we utilize standard version 8 uuids.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Hash, PartialOrd, Ord)]
pub struct ExternalId(uuid::Uuid);
impl_uuid!(ExternalId, 0xef, 0xe0);
// TODO: Add tests for external ID and prefix checks

/// A flow-id used to identify `Flows` in a contract.
///
/// These ids are version 8 [uuids](https://en.wikipedia.org/wiki/Universally_unique_identifier), where
/// the first four bits of the uuid are set to `0xf`, to indicate that it is a flow-id and not another uuid based id.
///
/// Example:
/// ```sh
/// fbc23cb3-f447-8107-8f93-9bfb8e1d157d
/// ```
///
/// All uuid-based ids used in the borderless-ecosystem have a different prefix, based on what the id is used for.
/// This mechanism ensures that you cannot mistake an participant-id for e.g. a contract-id and vice versa. Even if you convert the participant-id
/// back into a uuid and the result into a contract-id, the results are different.
///
/// The implementation of the IDs is compliant with [RFC9562](https://www.ietf.org/rfc/rfc9562.html#name-uuid-version-8),
/// as we utilize standard version 8 uuids.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Hash, PartialOrd, Ord)]
pub struct FlowId(uuid::Uuid);
impl_uuid!(FlowId, 0xff, 0xf0);

/// Check weather or not an array of bytes contains the prefix of an [`AgentId`].
///
/// Useful for filtering in key-value storages, when multiple ID types are used as keys or key-prefixes.
pub fn aid_prefix(bytes: impl AsRef<[u8]>) -> bool {
    let bytes = bytes.as_ref();
    if bytes.is_empty() {
        return false;
    }
    bytes[0] | 0x0f == 0xaf
}

/// Check weather or not an array of bytes contains the prefix of a [`BorderlessId`].
///
/// Useful for filtering in key-value storages, when multiple ID types are used as keys or key-prefixes.
pub fn bid_prefix(bytes: impl AsRef<[u8]>) -> bool {
    let bytes = bytes.as_ref();
    if bytes.is_empty() {
        return false;
    }
    bytes[0] | 0x0f == 0xbf
}

/// Check weather or not an array of bytes contains the prefix of a [`ContractId`].
///
/// Useful for filtering in key-value storages, when multiple ID types are used as keys or key-prefixes.
pub fn cid_prefix(bytes: impl AsRef<[u8]>) -> bool {
    let bytes = bytes.as_ref();
    if bytes.is_empty() {
        return false;
    }
    bytes[0] | 0x0f == 0xcf
}

/// Check weather or not an array of bytes contains the prefix of a [`Did`].
///
/// Useful for filtering in key-value storages, when multiple ID types are used as keys or key-prefixes.
pub fn did_prefix(bytes: impl AsRef<[u8]>) -> bool {
    let bytes = bytes.as_ref();
    if bytes.is_empty() {
        return false;
    }
    bytes[0] | 0x0f == 0xdf
}

/// Check weather or not an array of bytes contains the prefix of an [`ExternalId`].
///
/// Useful for filtering in key-value storages, when multiple ID types are used as keys or key-prefixes.
pub fn eid_prefix(bytes: impl AsRef<[u8]>) -> bool {
    let bytes = bytes.as_ref();
    if bytes.is_empty() {
        return false;
    }
    bytes[0] | 0x0f == 0xef
}

/// Check weather or not an array of bytes contains the prefix of a [`FlowId`].
///
/// Useful for filtering in key-value storages, when multiple ID types are used as keys or key-prefixes.
pub fn fid_prefix(bytes: impl AsRef<[u8]>) -> bool {
    let bytes = bytes.as_ref();
    if bytes.is_empty() {
        return false;
    }
    bytes[0] | 0x0f == 0xff
}

/// Type used to identify blocks.
///
/// In principle a block is uniquely defined by its hash, however it may be useful to know where to find the block.
/// A `BlockIdentifier` adds the chain-id and block-number to the hash, so we can easily lookup a block based on its identifier.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct BlockIdentifier {
    /// Chain-ID of the block.
    pub chain_id: u32,
    /// Number of the block.
    pub number: u64,
    /// Hash of the block.
    ///
    /// Note: The hash of a block is not the hash over the serialized data.
    /// It is the sum of three hashes: `Hash(Header) + MerkleRoot(Txs) + MerkleRoot(SignatureWithKeys)`
    pub hash: Hash256,
}

impl BlockIdentifier {
    /// Constructs a new `BlockIdentifier` from its raw parts
    pub fn new(chain_id: u32, number: u64, hash: Hash256) -> Self {
        Self {
            chain_id,
            number,
            hash,
        }
    }

    pub fn genesis(chain_id: u32) -> Self {
        Self::new(chain_id, 0, Hash256::empty())
    }

    /// Decodes a `BlockIdentifier` from bytes
    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, InvalidBlockIdentifier> {
        if bytes.len() != ENCODED_BLOCK_ID_LEN {
            Err(InvalidBlockIdentifier(bytes.len()))
        } else {
            let mut bytes = Bytes::from(bytes);
            let chain_id = bytes.get_u32();
            let number = bytes.get_u64();
            let mut hash_slice = [0u8; 32];
            bytes.copy_to_slice(&mut hash_slice);
            Ok(BlockIdentifier {
                chain_id,
                number,
                hash: hash_slice.into(),
            })
        }
    }

    /// Encodes a `BlockIdentifier` to bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = BytesMut::with_capacity(std::mem::size_of::<BlockIdentifier>());
        buf.put_u32(self.chain_id);
        buf.put_u64(self.number);
        buf.put_slice(self.hash.as_ref());
        buf.into()
    }
}

/// Lenght of an encoded TxIdentifier
///
/// 4 bytes for the chain-id (u32)
/// 8 bytes for the number (u64)
/// 32 bytes for the hash (which is basically [u8; 32])
const ENCODED_BLOCK_ID_LEN: usize = 4 + 8 + 32;

#[derive(Debug, PartialEq)]
pub struct InvalidBlockIdentifier(pub usize);
impl Display for InvalidBlockIdentifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "failed to decode block identifier - expected {ENCODED_BLOCK_ID_LEN} bytes, got {}",
            self.0
        )
    }
}
impl std::error::Error for InvalidBlockIdentifier {}

// This is necessary for the update functionality.
// We need a lexicographical sort of the incoming block-ids,
// sorted by their hash.
impl PartialOrd for BlockIdentifier {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for BlockIdentifier {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.hash.cmp(&other.hash)
    }
}

impl Display for BlockIdentifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.chain_id, self.number, self.hash)
    }
}

/// Type used to identify transactions.
///
/// In principal a transaction is uniquely defined by its hash, however it may be useful to know where to find the transaction.
/// An identifier adds the chain-id and block-number to the hash, so we can easily lookup a transaction based on its identifier.
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TxIdentifier {
    /// Chain-ID of the transaction.
    pub chain_id: u32,
    /// Block-Number of the block that holds the transaction.
    pub number: u64,
    /// Hash of the transaction.
    pub hash: Hash256,
}

impl TxIdentifier {
    /// Constructs a new [`TxIdentifier`]
    pub fn new(chain_id: u32, number: u64, hash: Hash256) -> Self {
        TxIdentifier {
            chain_id,
            number,
            hash,
        }
    }

    /// Decodes a `TxIdentifier` from bytes
    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, InvalidTxIdentifier> {
        if bytes.len() != ENCODED_TX_ID_LEN {
            Err(InvalidTxIdentifier(bytes.len()))
        } else {
            let mut bytes = Bytes::from(bytes);
            let chain_id = bytes.get_u32();
            let number = bytes.get_u64();
            let mut hash_slice = [0u8; 32];
            bytes.copy_to_slice(&mut hash_slice);
            Ok(TxIdentifier {
                chain_id,
                number,
                hash: hash_slice.into(),
            })
        }
    }

    /// Encodes a `TxIdentifier` as bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = BytesMut::with_capacity(std::mem::size_of::<TxIdentifier>());
        buf.put_u32(self.chain_id);
        buf.put_u64(self.number);
        buf.put_slice(self.hash.as_ref());
        buf.into()
    }
}

/// Lenght of an encoded TxIdentifier
///
/// 4 bytes for the chain-id (u32)
/// 8 bytes for the number (u64)
/// 32 bytes for the hash (which is basically [u8; 32])
const ENCODED_TX_ID_LEN: usize = 4 + 8 + 32;

#[derive(Debug, PartialEq)]
pub struct InvalidTxIdentifier(pub usize);
impl Display for InvalidTxIdentifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "failed to decode transaction identifier - expected {ENCODED_TX_ID_LEN} bytes, got {}",
            self.0
        )
    }
}
impl std::error::Error for InvalidTxIdentifier {}

impl Display for TxIdentifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.chain_id, self.number, self.hash)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::de::DeserializeOwned;
    use uuid::Uuid;

    #[test]
    fn tx_id_encode_decode() {
        let tx_id = TxIdentifier::new(1, 2, Hash256::empty());
        let bytes = tx_id.to_bytes();
        let result = TxIdentifier::from_bytes(bytes);
        assert!(result.is_ok(), "decoding failed: {}", result.unwrap_err());
        assert_eq!(result.unwrap(), tx_id);
    }

    #[test]
    fn tx_id_display() {
        let tx_id = TxIdentifier::new(1, 2, Hash256::empty());
        assert_eq!(format!("{tx_id}"), "1.2.a7ffc6f8");
    }

    #[test]
    fn block_id_encode_decode() {
        let block_id = BlockIdentifier::new(1, 2, Hash256::empty());
        let bytes = block_id.to_bytes();
        let result = BlockIdentifier::from_bytes(bytes);
        assert!(result.is_ok(), "decoding failed: {}", result.unwrap_err());
        assert_eq!(result.unwrap(), block_id);
    }

    #[test]
    fn block_id_display() {
        let block_id = BlockIdentifier::new(1, 2, Hash256::empty());
        assert_eq!(format!("{block_id}"), "1.2.a7ffc6f8");
    }

    #[test]
    fn block_id_ordering() {
        // Simple test
        let first = BlockIdentifier::new(10, 20, Hash256::zero());
        let last = BlockIdentifier::new(1, 2, Hash256::empty());
        assert!(first < last);

        // Do some fuzzing
        for i in 0..1_000u64 {
            let h1 = Hash256::digest(&i.to_be_bytes());
            let h2 = Hash256::digest(&i.to_le_bytes());
            let b1 = BlockIdentifier::new(i as u32, i, h1);
            let b2 = BlockIdentifier::new(i as u32 + 1, i + 1, h2);
            assert_eq!(
                h1 < h2,
                b1 < b2,
                "block identifiers must be sorted based on their hash"
            );
        }
    }

    #[test]
    fn agent_id_prefix() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let process_id = AgentId::from(base_id);
            let pid_string = process_id.to_string();
            assert_eq!(
                pid_string.chars().next(),
                Some('a'),
                "Process-IDs must be prefixed with 'a' in string representation"
            );
            let back_to_uuid: Uuid = process_id.into();
            assert_ne!(base_id, back_to_uuid);
            // Check, if first four bits match 'a'
            // NOTE: Bit-level-hacking here: bits abcdefgh & 11110000 = abcd0000
            // -> so i can match on byte level agains abcd0000 (in this case 0xb0)
            assert_eq!(back_to_uuid.as_bytes()[0] & 0xF0, 0xa0);
        }
    }

    #[test]
    fn borderless_id_prefix() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let borderless_id = BorderlessId::from(base_id);
            let pid_string = borderless_id.to_string();
            assert_eq!(
                pid_string.chars().next(),
                Some('b'),
                "Borderless-IDs must be prefixed with 'b' in string representation"
            );
            let back_to_uuid: Uuid = borderless_id.into();
            assert_ne!(base_id, back_to_uuid);
            // Check, if first four bits match 'b'
            // NOTE: Bit-level-hacking here: bits abcdefgh & 11110000 = abcd0000
            // -> so i can match on byte level agains abcd0000 (in this case 0x00)
            assert_eq!(back_to_uuid.as_bytes()[0] & 0xF0, 0xb0);
        }
    }

    #[test]
    fn contract_id_prefix() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let contract_id = ContractId::from(base_id);
            let cid_string = contract_id.to_string();
            assert_eq!(
                cid_string.chars().next(),
                Some('c'),
                "Contract-IDs must be prefixed with 'c' in string representation"
            );
            let back_to_uuid: Uuid = contract_id.into();
            assert_ne!(base_id, back_to_uuid);
            // Check, if first four bits match 'c'
            // NOTE: Bit-level-hacking here: bits abcdefgh & 11110000 = abcd0000
            // -> so i can match on byte level agains abcd0000 (in this case 0xc0)
            assert_eq!(back_to_uuid.as_bytes()[0] & 0xF0, 0xc0);
        }
    }

    #[test]
    fn decentralized_id_prefix() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let asset_id = Did::from(base_id);
            let aid_string = asset_id.to_string();
            assert_eq!(
                aid_string.chars().next(),
                Some('d'),
                "Decentralized-IDs must be prefixed with 'd' in string representation"
            );
            let back_to_uuid: Uuid = asset_id.into();
            assert_ne!(base_id, back_to_uuid);
            // Check, if first four bits match 'd'
            // NOTE: Bit-level-hacking here: bits abcdefgh & 11110000 = abcd0000
            // -> so i can match on byte level agains abcd0000 (in this case 0xd0)
            assert_eq!(back_to_uuid.as_bytes()[0] & 0xF0, 0xd0);
        }
    }

    #[test]
    fn external_id_prefix() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let asset_id = ExternalId::from(base_id);
            let aid_string = asset_id.to_string();
            assert_eq!(
                aid_string.chars().next(),
                Some('e'),
                "External-IDs must be prefixed with 'e' in string representation"
            );
            let back_to_uuid: Uuid = asset_id.into();
            assert_ne!(base_id, back_to_uuid);
            // Check, if first four bits match 'e'
            // NOTE: Bit-level-hacking here: bits abcdefgh & 11110000 = abcd0000
            // -> so i can match on byte level agains abcd0000 (in this case 0xe0)
            assert_eq!(back_to_uuid.as_bytes()[0] & 0xF0, 0xe0);
        }
    }

    #[test]
    fn differentiate_id_types() {
        // This test ensures, that all ID types generate different bit-level representations.
        // In other words: They do not match, even if I use the same uuid to generate them.
        // This allows us to easily spot, which ID type we have and prevents cross-matches.
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let agent_id: Uuid = AgentId::from(base_id).into();
            let borderless_id: Uuid = BorderlessId::from(base_id).into();
            let contract_id: Uuid = ContractId::from(base_id).into();
            let did: Uuid = Did::from(base_id).into();
            let external_id: Uuid = ExternalId::from(base_id).into();
            let flow_id: Uuid = FlowId::from(base_id).into();
            let ids = [
                base_id,
                agent_id,
                borderless_id,
                contract_id,
                did,
                external_id,
                flow_id,
            ];
            // NOTE: Check all permutations, just to be sure:
            for i in 0..ids.len() {
                for j in i..ids.len() {
                    assert_eq!(ids[i] == ids[j], i == j);
                }
            }
        }
    }

    #[test]
    fn agent_id_construction() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let base_u128 = base_id.as_u128();
            let from_uuid = AgentId::from(base_id);
            let from_u128 = AgentId::from(base_u128);
            assert_eq!(from_uuid, from_u128);
            let back_to_uuid: Uuid = from_uuid.into();
            let back_to_u128: u128 = from_u128.into();
            assert_eq!(back_to_uuid, Uuid::from_u128(back_to_u128));
            assert_eq!(back_to_uuid.as_u128(), back_to_u128); // this is redundant - but let's stay paranoid
            assert_ne!(base_id, back_to_uuid);
            assert_ne!(base_id.as_u128(), back_to_u128);
        }
    }

    #[test]
    fn borderless_id_construction() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let base_u128 = base_id.as_u128();
            let from_uuid = BorderlessId::from(base_id);
            let from_u128 = BorderlessId::from(base_u128);
            assert_eq!(from_uuid, from_u128);
            let back_to_uuid: Uuid = from_uuid.into();
            let back_to_u128: u128 = from_u128.into();
            assert_eq!(back_to_uuid, Uuid::from_u128(back_to_u128));
            assert_eq!(back_to_uuid.as_u128(), back_to_u128); // this is redundant - but let's stay paranoid
            assert_ne!(base_id, back_to_uuid);
            assert_ne!(base_id.as_u128(), back_to_u128);
        }
    }

    #[test]
    fn contract_id_construction() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let base_u128 = base_id.as_u128();
            let from_uuid = ContractId::from(base_id);
            let from_u128 = ContractId::from(base_u128);
            assert_eq!(from_uuid, from_u128);
            let back_to_uuid: Uuid = from_uuid.into();
            let back_to_u128: u128 = from_u128.into();
            assert_eq!(back_to_uuid, Uuid::from_u128(back_to_u128));
            assert_eq!(back_to_uuid.as_u128(), back_to_u128); // this is redundant - but let's stay paranoid
            assert_ne!(base_id, back_to_uuid);
            assert_ne!(base_id.as_u128(), back_to_u128);
        }
    }

    #[test]
    fn did_construction() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let base_u128 = base_id.as_u128();
            let from_uuid = Did::from(base_id);
            let from_u128 = Did::from(base_u128);
            assert_eq!(from_uuid, from_u128);
            let back_to_uuid: Uuid = from_uuid.into();
            let back_to_u128: u128 = from_u128.into();
            assert_eq!(back_to_uuid, Uuid::from_u128(back_to_u128));
            assert_eq!(back_to_uuid.as_u128(), back_to_u128); // this is redundant - but let's stay paranoid
            assert_ne!(base_id, back_to_uuid);
            assert_ne!(base_id.as_u128(), back_to_u128);
        }
    }

    #[test]
    fn external_id_construction() {
        for _ in 0..1_000_000 {
            let base_id = Uuid::new_v4();
            let base_u128 = base_id.as_u128();
            let from_uuid = ExternalId::from(base_id);
            let from_u128 = ExternalId::from(base_u128);
            assert_eq!(from_uuid, from_u128);
            let back_to_uuid: Uuid = from_uuid.into();
            let back_to_u128: u128 = from_u128.into();
            assert_eq!(back_to_uuid, Uuid::from_u128(back_to_u128));
            assert_eq!(back_to_uuid.as_u128(), back_to_u128); // this is redundant - but let's stay paranoid
            assert_ne!(base_id, back_to_uuid);
            assert_ne!(base_id.as_u128(), back_to_u128);
        }
    }

    #[test]
    fn check_aid_prefix() {
        for _ in 0..1_000_000 {
            assert!(aid_prefix(&AgentId::generate()));
            assert!(!aid_prefix(&BorderlessId::generate()));
            assert!(!aid_prefix(&ContractId::generate()));
            assert!(!aid_prefix(&Did::generate()));
            assert!(!aid_prefix(&ExternalId::generate()));
            assert!(!aid_prefix(&FlowId::generate()));
            assert!(!aid_prefix(&[]));
        }
    }

    #[test]
    fn check_bid_prefix() {
        for _ in 0..1_000_000 {
            assert!(!bid_prefix(&AgentId::generate()));
            assert!(bid_prefix(&BorderlessId::generate()));
            assert!(!bid_prefix(&ContractId::generate()));
            assert!(!bid_prefix(&Did::generate()));
            assert!(!bid_prefix(&ExternalId::generate()));
            assert!(!bid_prefix(&FlowId::generate()));
            assert!(!bid_prefix(&[]));
        }
    }

    #[test]
    fn check_cid_prefix() {
        for _ in 0..1_000_000 {
            assert!(!cid_prefix(&AgentId::generate()));
            assert!(!cid_prefix(&BorderlessId::generate()));
            assert!(cid_prefix(&ContractId::generate()));
            assert!(!cid_prefix(&Did::generate()));
            assert!(!cid_prefix(&ExternalId::generate()));
            assert!(!cid_prefix(&FlowId::generate()));
            assert!(!cid_prefix(&[]));
        }
    }

    #[test]
    fn check_did_prefix() {
        for _ in 0..1_000_000 {
            assert!(!did_prefix(&AgentId::generate()));
            assert!(!did_prefix(&BorderlessId::generate()));
            assert!(!did_prefix(&ContractId::generate()));
            assert!(did_prefix(&Did::generate()));
            assert!(!did_prefix(&ExternalId::generate()));
            assert!(!did_prefix(&FlowId::generate()));
            assert!(!did_prefix(&[]));
        }
    }

    #[test]
    fn check_eid_prefix() {
        for _ in 0..1_000_000 {
            assert!(!eid_prefix(&AgentId::generate()));
            assert!(!eid_prefix(&BorderlessId::generate()));
            assert!(!eid_prefix(&ContractId::generate()));
            assert!(!eid_prefix(&Did::generate()));
            assert!(eid_prefix(&ExternalId::generate()));
            assert!(!eid_prefix(&FlowId::generate()));
            assert!(!eid_prefix(&[]));
        }
    }

    #[test]
    fn check_fid_prefix() {
        for _ in 0..1_000_000 {
            assert!(!fid_prefix(&AgentId::generate()));
            assert!(!fid_prefix(&BorderlessId::generate()));
            assert!(!fid_prefix(&ContractId::generate()));
            assert!(!fid_prefix(&Did::generate()));
            assert!(!fid_prefix(&ExternalId::generate()));
            assert!(fid_prefix(&FlowId::generate()));
            assert!(!fid_prefix(&[]));
        }
    }

    #[test]
    fn string_representation() {
        fn parse<T>(id: T)
        where
            T: Into<String> + TryFrom<String> + fmt::Debug + Eq + Copy,
        {
            let s1: String = id.into();
            let a1: Result<T, _> = s1.try_into();
            assert!(a1.is_ok());
            let id2 = unsafe { a1.unwrap_unchecked() };
            assert_eq!(id, id2);
        }
        parse(AgentId::generate());
        parse(BorderlessId::generate());
        parse(ContractId::generate());
        parse(Did::generate());
        parse(ExternalId::generate());
        parse(FlowId::generate());
    }

    #[test]
    fn uuid_conversion() {
        let uid = Uuid::new_v4();
        let aid: AgentId = uid.into();
        let bid: BorderlessId = uid.into();
        let cid: ContractId = uid.into();
        let did: Did = uid.into();
        let eid: ExternalId = uid.into();
        let fid: FlowId = uid.into();
        // They must never match the uuid, because of the prefix
        assert_ne!(aid.into_bytes(), uid.into_bytes());
        assert_ne!(bid.into_bytes(), uid.into_bytes());
        assert_ne!(cid.into_bytes(), uid.into_bytes());
        assert_ne!(did.into_bytes(), uid.into_bytes());
        assert_ne!(eid.into_bytes(), uid.into_bytes());
        assert_ne!(fid.into_bytes(), uid.into_bytes());
        assert_ne!(aid.into_uuid(), uid);
        assert_ne!(bid.into_uuid(), uid);
        assert_ne!(cid.into_uuid(), uid);
        assert_ne!(did.into_uuid(), uid);
        assert_ne!(eid.into_uuid(), uid);
        assert_ne!(fid.into_uuid(), uid);

        // But roundtrip will work
        assert_eq!(AgentId::from(aid.into_uuid()), aid);
        assert_eq!(BorderlessId::from(aid.into_uuid()), bid);
        assert_eq!(ContractId::from(aid.into_uuid()), cid);
        assert_eq!(Did::from(aid.into_uuid()), did);
        assert_eq!(ExternalId::from(aid.into_uuid()), eid);
        assert_eq!(FlowId::from(aid.into_uuid()), fid);
    }

    #[test]
    fn merge_is_commutative() {
        for _ in 0..1_000_000 {
            let bid_1 = BorderlessId::generate();
            let bid_2 = BorderlessId::generate();
            assert_eq!(bid_1.merge(&bid_2), bid_2.merge(&bid_1));
            // Merge with self is basically 0
            assert_eq!(bid_1.merge(&bid_1), [0; 16]);
        }
    }

    #[test]
    fn merge_compact_is_commutative() {
        for _ in 0..1_000_000 {
            let bid_1 = BorderlessId::generate();
            let bid_2 = BorderlessId::generate();
            assert_eq!(bid_1.merge_compact(&bid_2), bid_2.merge_compact(&bid_1));
            // Merge with self is 0
            assert_eq!(bid_1.merge_compact(&bid_1), 0);
        }
    }

    #[test]
    fn compact() {
        for _ in 0..1_000_000 {
            let bid_1 = BorderlessId::generate();
            let bid_2 = BorderlessId::generate();
            assert_ne!(bid_1.compact(), bid_2.compact());
            // Compacting is not creating a 0
            assert_ne!(bid_1.compact(), 0);
        }
    }

    #[test]
    fn compact_merge() {
        for _ in 0..1_000_000 {
            let bid_1 = BorderlessId::generate();
            let bid_2 = BorderlessId::generate();
            let merge_compact = bid_1.merge_compact(&bid_2);
            let merged_bytes = bid_1.merge(&bid_2);
            let merged_id = BorderlessId::from_bytes(merged_bytes);
            let compacted = merged_id.compact();
            // Merging and then compacting creates the same result, as calling merge_compact
            // ONLY IF the byte-array would be left untouched (which is not really the case)
            assert_eq!(
                compacted == merge_compact,
                merged_bytes == *merged_id.as_bytes()
            );
            // If we would do it manually however, it will work fine:
            let mut manual_compact = [0; 8];
            for i in 0..8 {
                manual_compact[i] = merged_bytes[i] ^ merged_bytes[i + 8];
            }
            assert_eq!(merge_compact, u64::from_be_bytes(manual_compact));
        }
    }

    #[test]
    fn serde_conversion_is_ensured() {
        fn check_id<T: DeserializeOwned + From<Uuid> + Into<Uuid> + std::fmt::Debug + Eq>(
            base_id: Uuid,
        ) {
            let json_id = serde_json::to_string(&base_id).unwrap();
            let result: Result<T, _> = serde_json::from_str(&json_id);
            assert!(result.is_ok(), "{}", result.unwrap_err());
            let bid = result.unwrap();
            assert_eq!(bid, T::from(base_id));
            assert_ne!(bid.into(), base_id);
        }
        for _ in 0..100_000 {
            let base_id = Uuid::new_v4();
            check_id::<AgentId>(base_id);
            check_id::<BorderlessId>(base_id);
            check_id::<ContractId>(base_id);
            check_id::<Did>(base_id);
            check_id::<ExternalId>(base_id);
            check_id::<FlowId>(base_id);
        }
    }
}