1use core::fmt;
15use std::cmp::Ordering;
16use std::collections::hash_map::DefaultHasher;
17use std::hash::{Hash, Hasher};
18
19use once_cell::sync::OnceCell;
20use sbbf_rs_safe::Filter;
21use serde::{Deserialize, Serialize};
22use ng_threshold_crypto::serde_impl::SerdeSecret;
23use ng_threshold_crypto::SignatureShare;
24use zeroize::{Zeroize, ZeroizeOnDrop};
25
26use crate::errors::NgError;
27use crate::utils::{
28 decode_key, decode_priv_key, dh_pubkey_array_from_ed_pubkey_slice,
29 dh_pubkey_from_ed_pubkey_slice, ed_privkey_to_ed_pubkey, from_ed_privkey_to_dh_privkey,
30 random_key, verify,
31};
32
33pub type Blake3Digest32 = [u8; 32];
39
40#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
42pub enum Digest {
43 Blake3Digest32(Blake3Digest32),
44}
45
46impl Ord for Digest {
47 fn cmp(&self, other: &Self) -> Ordering {
48 match self {
49 Self::Blake3Digest32(left) => match other {
50 Self::Blake3Digest32(right) => left.cmp(right),
51 },
52 }
53 }
54}
55
56impl PartialOrd for Digest {
57 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
58 Some(self.cmp(other))
59 }
60}
61
62impl Digest {
63 pub fn from_slice(slice: [u8; 32]) -> Digest {
64 Digest::Blake3Digest32(slice)
65 }
66 pub fn slice(&self) -> &[u8; 32] {
67 match self {
68 Self::Blake3Digest32(o) => o,
69 }
70 }
71 pub fn to_slice(self) -> [u8; 32] {
72 match self {
73 Self::Blake3Digest32(o) => o,
74 }
75 }
76 pub fn get_hash(&self) -> u64 {
79 let mut hasher = DefaultHasher::new();
80 let ser = serde_bare::to_vec(&self).unwrap();
81 for e in ser {
82 e.hash(&mut hasher);
83 }
84 hasher.finish()
85 }
86
87 pub fn print_all(all: &[Digest]) -> String {
88 all.iter()
89 .map(|d| d.to_string())
90 .collect::<Vec<String>>()
91 .join(" ")
92 }
93
94 pub fn print_iter(all: impl Iterator<Item = Digest>) -> String {
95 all.map(|d| d.to_string())
96 .collect::<Vec<String>>()
97 .join(" ")
98 }
99
100 pub fn print_iter_ref<'a>(all: impl Iterator<Item = &'a Digest>) -> String {
101 all.map(|d| d.to_string())
102 .collect::<Vec<String>>()
103 .join(" ")
104 }
105
106 pub fn print_all_ref(all: &[&Digest]) -> String {
107 all.into_iter()
108 .map(|d| d.to_string())
109 .collect::<Vec<String>>()
110 .join(" ")
111 }
112}
113
114impl fmt::Display for Digest {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 write!(f, "{}", std::string::String::from(self))
117 }
118}
119
120impl From<&Vec<u8>> for Digest {
121 fn from(ser: &Vec<u8>) -> Self {
122 let hash = blake3::hash(ser.as_slice());
123 Digest::Blake3Digest32(hash.as_bytes().clone())
124 }
125}
126
127impl From<&[u8; 32]> for Digest {
128 fn from(ser: &[u8; 32]) -> Self {
129 let hash = blake3::hash(ser);
130 Digest::Blake3Digest32(hash.as_bytes().clone())
131 }
132}
133
134impl From<&PubKey> for Digest {
135 fn from(key: &PubKey) -> Self {
136 key.slice().into()
137 }
138}
139
140pub type ChaCha20Key = [u8; 32];
142
143#[derive(Clone, Zeroize, ZeroizeOnDrop, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
145pub enum SymKey {
146 ChaCha20Key(ChaCha20Key),
147}
148
149impl SymKey {
150 pub fn slice(&self) -> &[u8; 32] {
151 match self {
152 SymKey::ChaCha20Key(o) => o,
153 }
154 }
155 pub fn random() -> Self {
156 SymKey::ChaCha20Key(random_key())
157 }
158 pub fn from_array(array: [u8; 32]) -> Self {
159 SymKey::ChaCha20Key(array)
160 }
161 pub fn nil() -> Self {
162 SymKey::ChaCha20Key([0; 32])
163 }
164 #[cfg(any(test, feature = "testing"))]
165 pub fn dummy() -> Self {
166 SymKey::ChaCha20Key([0; 32])
167 }
168}
169
170impl fmt::Display for SymKey {
171 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 let mut ser = serde_bare::to_vec(&self).unwrap();
173 ser.reverse();
174 write!(f, "{}", base64_url::encode(&ser))
175 }
176}
177
178impl TryFrom<&[u8]> for SymKey {
179 type Error = NgError;
180 fn try_from(buf: &[u8]) -> Result<Self, NgError> {
181 let sym_key_array = *slice_as_array!(buf, [u8; 32]).ok_or(NgError::InvalidKey)?;
182 let sym_key = SymKey::ChaCha20Key(sym_key_array);
183 Ok(sym_key)
184 }
185}
186
187pub type Ed25519PubKey = [u8; 32];
189
190pub type X25519PubKey = [u8; 32];
192
193pub type Ed25519PrivKey = [u8; 32];
195
196pub type X25519PrivKey = [u8; 32];
198
199#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
201pub enum PubKey {
202 Ed25519PubKey(Ed25519PubKey),
203 X25519PubKey(X25519PubKey),
204}
205
206impl Default for PubKey {
207 fn default() -> Self {
208 Self::nil()
209 }
210}
211
212impl PubKey {
213 pub fn to_dh(self) -> X25519PubKey {
214 match self {
215 Self::X25519PubKey(x) => x,
216 _ => panic!("cannot call to_dh on an Edward key"),
217 }
218 }
219 pub fn slice(&self) -> &[u8; 32] {
220 match self {
221 PubKey::Ed25519PubKey(o) | PubKey::X25519PubKey(o) => o,
222 }
223 }
224 pub fn to_dh_from_ed(&self) -> PubKey {
225 match self {
226 PubKey::Ed25519PubKey(ed) => dh_pubkey_from_ed_pubkey_slice(ed),
227 _ => panic!(
228 "there is no need to convert a Montgomery key to Montgomery. it is already one. check your code"
229 ),
230 }
231 }
232 pub fn to_dh_slice(&self) -> [u8; 32] {
236 match self {
237 PubKey::Ed25519PubKey(o) => dh_pubkey_array_from_ed_pubkey_slice(o),
238 _ => panic!("can only convert an edward key to montgomery"),
239 }
240 }
241
242 pub fn nil() -> Self {
243 PubKey::Ed25519PubKey([0u8; 32])
244 }
245
246 pub fn to_hash_string(&self) -> String {
247 let ser = serde_bare::to_vec(&self).unwrap();
248 let hash = blake3::hash(&ser);
249 base64_url::encode(&hash.as_bytes())
250 }
251}
252
253impl fmt::Display for PubKey {
254 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
255 let mut ser = serde_bare::to_vec(&self).unwrap();
256 ser.reverse();
257 write!(f, "{}", base64_url::encode(&ser))
258 }
259}
260
261impl TryFrom<&str> for PubKey {
262 type Error = NgError;
263 fn try_from(str: &str) -> Result<Self, NgError> {
264 decode_key(str)
265 }
266}
267
268#[derive(Clone, Zeroize, ZeroizeOnDrop, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
270pub enum PrivKey {
271 Ed25519PrivKey(Ed25519PrivKey),
272 X25519PrivKey(X25519PrivKey),
273}
274
275#[allow(deprecated)]
276impl Default for PrivKey {
277 fn default() -> Self {
278 Self::nil()
279 }
280}
281
282impl PrivKey {
283 pub fn slice(&self) -> &[u8; 32] {
284 match self {
285 PrivKey::Ed25519PrivKey(o) | PrivKey::X25519PrivKey(o) => o,
286 }
287 }
288 pub fn to_pub(&self) -> PubKey {
289 match self {
290 PrivKey::Ed25519PrivKey(_) => ed_privkey_to_ed_pubkey(self),
291 _ => panic!("X25519PrivKey to pub not implemented"),
292 }
293 }
294
295 pub fn nil() -> PrivKey {
296 PrivKey::Ed25519PrivKey([0u8; 32])
297 }
298
299 #[cfg(any(test, feature = "testing"))]
300 pub fn dummy() -> PrivKey {
301 PrivKey::Ed25519PrivKey([0u8; 32])
302 }
303
304 pub fn to_dh(&self) -> PrivKey {
305 from_ed_privkey_to_dh_privkey(self)
306 }
307
308 pub fn random_ed() -> Self {
309 PrivKey::Ed25519PrivKey(random_key())
310 }
311}
312
313impl From<[u8; 32]> for PrivKey {
314 fn from(buf: [u8; 32]) -> Self {
315 let priv_key = PrivKey::Ed25519PrivKey(buf);
316 priv_key
317 }
318}
319
320impl TryFrom<&[u8]> for PrivKey {
321 type Error = NgError;
322 fn try_from(buf: &[u8]) -> Result<Self, NgError> {
323 let priv_key_array = *slice_as_array!(buf, [u8; 32]).ok_or(NgError::InvalidKey)?;
324 let priv_key = PrivKey::Ed25519PrivKey(priv_key_array);
325 Ok(priv_key)
326 }
327}
328
329impl TryFrom<&str> for PrivKey {
330 type Error = NgError;
331 fn try_from(str: &str) -> Result<Self, NgError> {
332 decode_priv_key(str)
333 }
334}
335
336impl fmt::Display for PrivKey {
337 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
338 let mut ser = serde_bare::to_vec(&self).unwrap();
339 ser.reverse();
340 write!(f, "{}", base64_url::encode(&ser))
341 }
342}
343
344pub type Ed25519Sig = [[u8; 32]; 2];
346
347#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
349pub enum Sig {
350 Ed25519Sig(Ed25519Sig),
351}
352
353impl fmt::Display for Sig {
354 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
355 match self {
356 Self::Ed25519Sig(ed) => {
357 write!(
358 f,
359 "{} {}",
360 base64_url::encode(&ed[0]),
361 base64_url::encode(&ed[1])
362 )
363 }
364 }
365 }
366}
367
368impl Sig {
369 pub fn nil() -> Self {
370 Sig::Ed25519Sig([[0; 32]; 2])
371 }
372}
373
374pub type Timestamp = u32;
376
377pub const EPOCH_AS_UNIX_TIMESTAMP: u64 = 1645568520;
378
379#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
381pub enum RelTime {
382 Seconds(u8),
383 Minutes(u8),
384 Hours(u8),
385 Days(u8),
386 None,
387}
388
389impl fmt::Display for RelTime {
390 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
391 match self {
392 Self::Seconds(s) => writeln!(f, "{} sec.", s),
393 Self::Minutes(s) => writeln!(f, "{} min.", s),
394 Self::Hours(s) => writeln!(f, "{} h.", s),
395 Self::Days(s) => writeln!(f, "{} d.", s),
396 Self::None => writeln!(f, "None"),
397 }
398 }
399}
400
401#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
403pub struct BloomFilterV0 {
404 #[serde(with = "serde_bytes")]
406 pub f: Vec<u8>,
407}
408
409#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
410pub enum BloomFilter {
411 V0(BloomFilterV0),
412}
413
414impl BloomFilter {
415 pub fn filter(&self) -> Filter {
416 match self {
417 Self::V0(v0) => Filter::from_bytes(&v0.f).unwrap(),
418 }
419 }
420 pub fn from_filter(filter: &Filter) -> Self {
421 BloomFilter::V0(BloomFilterV0 {
422 f: filter.as_bytes().to_vec(),
423 })
424 }
425}
426
427pub type RepoId = PubKey;
433
434pub type RepoHash = Digest;
436
437impl From<RepoId> for RepoHash {
438 fn from(id: RepoId) -> Self {
439 Digest::Blake3Digest32(*blake3::hash(id.slice()).as_bytes())
440 }
441}
442
443pub type TopicId = PubKey;
451
452pub type UserId = PubKey;
454
455pub type BranchId = PubKey;
457
458pub type BlockId = Digest;
460
461pub type BlockKey = SymKey;
462
463#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
465pub struct BlockRef {
466 pub id: BlockId,
468
469 pub key: BlockKey,
471}
472
473impl Default for BlockId {
474 fn default() -> Self {
475 Self::nil()
476 }
477}
478
479impl BlockId {
480 #[cfg(any(test, feature = "testing"))]
481 pub fn dummy() -> Self {
482 Digest::Blake3Digest32([0u8; 32])
483 }
484
485 pub fn nil() -> Self {
486 Digest::Blake3Digest32([0u8; 32])
487 }
488}
489
490impl BlockRef {
491 #[cfg(any(test, feature = "testing"))]
492 pub fn dummy() -> Self {
493 BlockRef {
494 id: Digest::Blake3Digest32([0u8; 32]),
495 key: SymKey::ChaCha20Key([0u8; 32]),
496 }
497 }
498
499 pub fn nil() -> Self {
500 BlockRef {
501 id: Digest::Blake3Digest32([0u8; 32]),
502 key: SymKey::ChaCha20Key([0u8; 32]),
503 }
504 }
505
506 pub fn from_id_key(id: BlockId, key: BlockKey) -> Self {
507 BlockRef { id, key }
508 }
509
510 pub fn object_nuri(&self) -> String {
511 format!("j:{}:k:{}", self.id, self.key)
512 }
513
514 pub fn commit_nuri(&self) -> String {
515 format!("c:{}:k:{}", self.id, self.key)
516 }
517
518 pub fn readcap_nuri(&self) -> String {
519 let ser = serde_bare::to_vec(self).unwrap();
520 format!("r:{}", base64_url::encode(&ser))
521 }
522
523 pub fn tokenize(&self) -> Digest {
524 let ser = serde_bare::to_vec(self).unwrap();
525 Digest::Blake3Digest32(*blake3::hash(&ser).as_bytes())
526 }
527}
528
529impl From<BlockRef> for (BlockId, BlockKey) {
530 fn from(blockref: BlockRef) -> (BlockId, BlockKey) {
531 (blockref.id.clone(), blockref.key.clone())
532 }
533}
534
535impl From<(&BlockId, &BlockKey)> for BlockRef {
536 fn from(id_key: (&BlockId, &BlockKey)) -> Self {
537 BlockRef {
538 id: id_key.0.clone(),
539 key: id_key.1.clone(),
540 }
541 }
542}
543
544impl fmt::Display for BlockRef {
545 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
546 write!(f, "{} {}", self.id, self.key)
547 }
548}
549
550pub type ObjectId = BlockId;
552
553pub type ObjectKey = BlockKey;
555
556pub type ObjectRef = BlockRef;
558
559pub type ReadCap = ObjectRef;
566
567pub type ReadCapSecret = ObjectKey;
571
572pub type RepoWriteCapSecret = SymKey;
574
575pub type BranchWriteCapSecret = PrivKey;
577
578pub type OuterOverlayId = Digest;
598
599pub type InnerOverlayId = Digest;
600
601#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
610pub enum OverlayId {
611 Outer(Blake3Digest32),
612 Inner(Blake3Digest32),
613 Global,
614}
615
616impl fmt::Display for OverlayId {
617 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
618 let mut ser = serde_bare::to_vec(&self).unwrap();
619 ser.reverse();
620 write!(f, "{}", base64_url::encode(&ser))
621 }
622}
623
624impl OverlayId {
625 pub fn inner(
629 store_id: &PubKey,
630 store_overlay_branch_readcap_secret: &ReadCapSecret,
631 ) -> OverlayId {
632 let store_id = serde_bare::to_vec(store_id).unwrap();
633 let mut store_overlay_branch_readcap_secret_ser =
634 serde_bare::to_vec(store_overlay_branch_readcap_secret).unwrap();
635 let mut key: [u8; 32] = blake3::derive_key(
636 "NextGraph Overlay ReadCapSecret BLAKE3 key",
637 store_overlay_branch_readcap_secret_ser.as_slice(),
638 );
639 let key_hash = blake3::keyed_hash(&key, &store_id);
640 store_overlay_branch_readcap_secret_ser.zeroize();
641 key.zeroize();
642 OverlayId::Inner(*key_hash.as_bytes())
643 }
644
645 pub fn outer(store_id: &PubKey) -> OverlayId {
646 let store_id = serde_bare::to_vec(store_id).unwrap();
647 let d: Digest = (&store_id).into();
648 OverlayId::Outer(d.to_slice())
649 }
650 #[cfg(any(test, feature = "testing"))]
651 pub fn dummy() -> OverlayId {
652 OverlayId::Outer(Digest::dummy().to_slice())
653 }
654 pub fn nil() -> OverlayId {
655 OverlayId::Outer(Digest::nil().to_slice())
656 }
657
658 pub fn is_inner(&self) -> bool {
659 match self {
660 Self::Inner(_) => true,
661 _ => false,
662 }
663 }
664
665 pub fn is_outer(&self) -> bool {
666 match self {
667 Self::Outer(_) => true,
668 _ => false,
669 }
670 }
671}
672
673#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
675pub enum StoreOverlayV0 {
676 PublicStore(PubKey),
677 ProtectedStore(PubKey),
678 PrivateStore(PubKey),
679 Group(PubKey),
680 Dialog(Digest),
681}
682
683impl fmt::Display for StoreOverlayV0 {
684 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
685 write!(f, "StoreOverlay V0 ")?;
686 match self {
687 StoreOverlayV0::PublicStore(k) => writeln!(f, "PublicStore: {}", k),
688 StoreOverlayV0::ProtectedStore(k) => writeln!(f, "ProtectedStore: {}", k),
689 StoreOverlayV0::PrivateStore(k) => writeln!(f, "PrivateStore: {}", k),
690 StoreOverlayV0::Group(k) => writeln!(f, "Group: {}", k),
691 StoreOverlayV0::Dialog(k) => writeln!(f, "Dialog: {}", k),
692 }
693 }
694}
695
696#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
697pub enum StoreOverlay {
698 V0(StoreOverlayV0),
699 OwnV0(StoreOverlayV0), }
701
702impl fmt::Display for StoreOverlay {
703 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
704 match self {
705 Self::V0(v0) => writeln!(f, "{}", v0),
706 Self::OwnV0(v0) => writeln!(f, "Own: {}", v0),
707 }
708 }
709}
710
711impl StoreOverlay {
712 pub fn from_store_repo(store_repo: &StoreRepo, overlay_branch: BranchId) -> StoreOverlay {
713 match store_repo {
714 StoreRepo::V0(v0) => match v0 {
715 StoreRepoV0::PublicStore(_id) => {
716 StoreOverlay::V0(StoreOverlayV0::PublicStore(overlay_branch))
717 }
718 StoreRepoV0::ProtectedStore(_id) => {
719 StoreOverlay::V0(StoreOverlayV0::ProtectedStore(overlay_branch))
720 }
721 StoreRepoV0::PrivateStore(_id) => {
722 StoreOverlay::V0(StoreOverlayV0::PrivateStore(overlay_branch))
723 }
724 StoreRepoV0::Group(_id) => StoreOverlay::V0(StoreOverlayV0::Group(overlay_branch)),
725 StoreRepoV0::Dialog((_, d)) => StoreOverlay::V0(StoreOverlayV0::Dialog(d.clone())),
726 },
727 }
728 }
729
730 pub fn overlay_id_for_read_purpose(&self) -> OverlayId {
731 match self {
732 StoreOverlay::V0(StoreOverlayV0::PublicStore(id))
733 | StoreOverlay::V0(StoreOverlayV0::ProtectedStore(id))
734 | StoreOverlay::V0(StoreOverlayV0::PrivateStore(id))
735 | StoreOverlay::V0(StoreOverlayV0::Group(id)) => OverlayId::outer(id),
736 StoreOverlay::V0(StoreOverlayV0::Dialog(d)) => OverlayId::Inner(d.clone().to_slice()),
737 StoreOverlay::OwnV0(_) => unimplemented!(),
738 }
739 }
740
741 pub fn overlay_id_for_write_purpose(
742 &self,
743 store_overlay_branch_readcap_secret: ReadCapSecret,
744 ) -> OverlayId {
745 match self {
746 StoreOverlay::V0(StoreOverlayV0::PublicStore(id))
747 | StoreOverlay::V0(StoreOverlayV0::ProtectedStore(id))
748 | StoreOverlay::V0(StoreOverlayV0::PrivateStore(id))
749 | StoreOverlay::V0(StoreOverlayV0::Group(id)) => {
750 OverlayId::inner(id, &store_overlay_branch_readcap_secret)
751 }
752 StoreOverlay::V0(StoreOverlayV0::Dialog(d)) => OverlayId::Inner(d.clone().to_slice()),
753 StoreOverlay::OwnV0(_) => unimplemented!(),
754 }
755 }
756}
757
758impl From<&StoreRepo> for StoreOverlay {
759 fn from(store_repo: &StoreRepo) -> Self {
760 match store_repo {
761 StoreRepo::V0(v0) => match v0 {
762 StoreRepoV0::PublicStore(id) => {
763 StoreOverlay::V0(StoreOverlayV0::PublicStore(id.clone()))
764 }
765 StoreRepoV0::ProtectedStore(id) => {
766 StoreOverlay::V0(StoreOverlayV0::ProtectedStore(id.clone()))
767 }
768 StoreRepoV0::PrivateStore(id) => {
769 StoreOverlay::V0(StoreOverlayV0::PrivateStore(id.clone()))
770 }
771 StoreRepoV0::Group(id) => StoreOverlay::V0(StoreOverlayV0::Group(id.clone())),
772 StoreRepoV0::Dialog((_, d)) => StoreOverlay::V0(StoreOverlayV0::Dialog(d.clone())),
773 },
774 }
775 }
776}
777
778#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
780pub enum StoreRepoV0 {
781 PublicStore(RepoId),
782 ProtectedStore(RepoId),
783 PrivateStore(RepoId),
784 Group(RepoId),
785 Dialog((RepoId, Digest)),
786}
787
788#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
789pub enum StoreRepo {
790 V0(StoreRepoV0),
791}
792
793impl fmt::Display for StoreRepo {
794 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
795 write!(
796 f,
797 "StoreRepo V0 {} {}",
798 match self {
799 StoreRepo::V0(v0) => match v0 {
800 StoreRepoV0::PublicStore(_) => "PublicStore",
801 StoreRepoV0::ProtectedStore(_) => "ProtectedStore",
802 StoreRepoV0::PrivateStore(_) => "PrivateStore",
803 StoreRepoV0::Group(_) => "Group",
804 StoreRepoV0::Dialog(_) => "Dialog",
805 },
806 },
807 self.repo_id()
808 )
809 }
810}
811
812impl StoreRepo {
813 pub fn from_type_and_repo(store_type: &String, repo_id_str: &String) -> Result<Self, NgError> {
814 let repo_id: RepoId = repo_id_str.as_str().try_into()?;
815 Ok(StoreRepo::V0(match store_type.as_str() {
816 "public" => StoreRepoV0::PublicStore(repo_id),
817 "protected" => StoreRepoV0::ProtectedStore(repo_id),
818 "private" => StoreRepoV0::PrivateStore(repo_id),
819 "group" => StoreRepoV0::Group(repo_id),
820 "dialog" | _ => unimplemented!(),
821 }))
822 }
823
824 pub fn store_type_for_app(&self) -> String {
825 match self {
826 Self::V0(v0) => match v0 {
827 StoreRepoV0::PublicStore(_) => "public",
828 StoreRepoV0::ProtectedStore(_) => "protected",
829 StoreRepoV0::PrivateStore(_) => "private",
830 StoreRepoV0::Group(_) => "group",
831 StoreRepoV0::Dialog(_) => "dialog",
832 },
833 }
834 .to_string()
835 }
836
837 pub fn repo_id(&self) -> &RepoId {
838 match self {
839 Self::V0(v0) => match v0 {
840 StoreRepoV0::PublicStore(id)
841 | StoreRepoV0::ProtectedStore(id)
842 | StoreRepoV0::PrivateStore(id)
843 | StoreRepoV0::Group(id)
844 | StoreRepoV0::Dialog((id, _)) => id,
845 },
846 }
847 }
848 #[cfg(any(test, feature = "testing"))]
849 #[allow(deprecated)]
850 pub fn dummy_public_v0() -> Self {
851 let store_pubkey = PubKey::nil();
852 StoreRepo::V0(StoreRepoV0::PublicStore(store_pubkey))
853 }
854 #[cfg(any(test, feature = "testing"))]
855 pub fn dummy_with_key(repo_pubkey: PubKey) -> Self {
856 StoreRepo::V0(StoreRepoV0::PublicStore(repo_pubkey))
857 }
858
859 pub fn nil() -> Self {
860 let store_pubkey = PubKey::nil();
861 StoreRepo::V0(StoreRepoV0::PublicStore(store_pubkey))
862 }
863
864 pub fn new_private(repo_pubkey: PubKey) -> Self {
865 StoreRepo::V0(StoreRepoV0::PrivateStore(repo_pubkey))
866 }
867
868 pub fn outer_overlay(&self) -> OverlayId {
869 self.overlay_id_for_read_purpose()
870 }
871
872 pub fn overlay_id_for_read_purpose(&self) -> OverlayId {
873 let store_overlay: StoreOverlay = self.into();
874 store_overlay.overlay_id_for_read_purpose()
875 }
877
878 pub fn is_private(&self) -> bool {
879 match self {
880 Self::V0(StoreRepoV0::PrivateStore(_)) => true,
881 _ => false,
882 }
883 }
884
885 pub fn overlay_id_for_storage_purpose(&self) -> OverlayId {
903 match self {
904 Self::V0(StoreRepoV0::PublicStore(_id))
905 | Self::V0(StoreRepoV0::ProtectedStore(_id))
906 | Self::V0(StoreRepoV0::Group(_id))
907 | Self::V0(StoreRepoV0::PrivateStore(_id)) => self.overlay_id_for_read_purpose(),
908 Self::V0(StoreRepoV0::Dialog(d)) => OverlayId::Inner(d.1.clone().to_slice()),
909 }
910 }
911
912 pub fn overlay_id_for_write_purpose(
913 &self,
914 store_overlay_branch_readcap_secret: &ReadCapSecret,
915 ) -> OverlayId {
916 match self {
917 Self::V0(StoreRepoV0::PublicStore(id))
918 | Self::V0(StoreRepoV0::ProtectedStore(id))
919 | Self::V0(StoreRepoV0::Group(id))
920 | Self::V0(StoreRepoV0::PrivateStore(id)) => {
921 OverlayId::inner(id, store_overlay_branch_readcap_secret)
922 }
923 Self::V0(StoreRepoV0::Dialog(d)) => OverlayId::Inner(d.1.clone().to_slice()),
924 }
925 }
926}
927
928#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
930pub enum SiteType {
931 Org,
932 Individual((PrivKey, ReadCap)), }
934
935#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
937pub struct SiteStore {
938 pub id: PubKey,
939
940 pub store_type: SiteStoreType,
941}
942
943#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
945pub enum SiteStoreType {
946 Public,
947 Protected,
948 Private,
949}
950
951#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
953pub enum SiteName {
954 Personal,
955 Name(String),
956}
957
958#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
960pub struct ReducedSiteV0 {
961 pub user_key: PrivKey,
962
963 pub private_store_read_cap: ReadCap,
964
965 pub core: PubKey,
966 pub bootstraps: Vec<PubKey>,
967}
968
969pub type InternalNode = Vec<BlockKey>;
975
976#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
978pub enum ChunkContentV0 {
979 InternalNode(InternalNode),
981
982 #[serde(with = "serde_bytes")]
983 DataChunk(Vec<u8>),
984}
985
986#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
988pub struct CommitHeaderV0 {
989 #[serde(skip)]
991 pub id: Option<ObjectId>,
992
993 pub deps: Vec<ObjectId>,
995
996 pub ndeps: Vec<ObjectId>,
998
999 pub compact: bool,
1003
1004 pub acks: Vec<ObjectId>,
1006
1007 pub nacks: Vec<ObjectId>,
1009
1010 pub files: Vec<ObjectId>,
1012
1013 pub nfiles: Vec<ObjectId>,
1016}
1017
1018#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1019pub enum CommitHeader {
1020 V0(CommitHeaderV0),
1021}
1022
1023#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1025pub struct CommitHeaderKeysV0 {
1026 pub deps: Vec<ObjectKey>,
1028
1029 pub acks: Vec<ObjectKey>,
1033
1034 pub nacks: Vec<ObjectKey>,
1036
1037 pub files: Vec<ObjectRef>,
1040 }
1043
1044#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1045pub enum CommitHeaderKeys {
1046 V0(CommitHeaderKeysV0),
1047}
1048
1049#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1050pub enum CommitHeaderObject {
1051 Id(ObjectId),
1052 EncryptedContent(Vec<u8>),
1053 None,
1054 RandomAccess,
1055}
1056
1057#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1058pub struct CommitHeaderRef {
1059 pub obj: CommitHeaderObject,
1060 pub key: ObjectKey,
1061}
1062
1063impl CommitHeaderRef {
1064 pub fn from_id_key(id: BlockId, key: ObjectKey) -> Self {
1065 CommitHeaderRef {
1066 obj: CommitHeaderObject::Id(id),
1067 key,
1068 }
1069 }
1070 pub fn from_content_key(content: Vec<u8>, key: ObjectKey) -> Self {
1071 CommitHeaderRef {
1072 obj: CommitHeaderObject::EncryptedContent(content),
1073 key,
1074 }
1075 }
1076 pub fn encrypted_content_len(&self) -> usize {
1077 match &self.obj {
1078 CommitHeaderObject::EncryptedContent(ec) => ec.len(),
1079 _ => 0,
1080 }
1081 }
1082}
1083
1084#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1086pub struct BlockContentV0 {
1087 pub commit_header: CommitHeaderObject,
1092
1093 pub children: Vec<BlockId>,
1096
1097 #[serde(with = "serde_bytes")]
1107 pub encrypted_content: Vec<u8>,
1108}
1109
1110#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1112pub enum BlockContent {
1113 V0(BlockContentV0),
1114}
1115
1116impl BlockContent {
1117 pub fn commit_header_obj(&self) -> &CommitHeaderObject {
1118 match self {
1119 Self::V0(v0) => &v0.commit_header,
1120 }
1121 }
1122}
1123
1124#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1129pub struct BlockV0 {
1130 #[serde(skip)]
1132 pub id: Option<BlockId>,
1133
1134 #[serde(skip)]
1136 pub key: Option<SymKey>,
1137
1138 pub commit_header_key: Option<ObjectKey>,
1146
1147 pub content: BlockContent,
1148}
1149
1150#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1152pub enum Block {
1153 V0(BlockV0),
1154}
1155
1156#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1167pub struct RepositoryV0 {
1168 pub id: RepoId,
1170
1171 #[serde(with = "serde_bytes")]
1173 pub verification_program: Vec<u8>,
1174
1175 #[serde(with = "serde_bytes")]
1178 pub fork_of: Vec<u8>,
1179
1180 pub creator: Option<UserId>,
1182
1183 #[serde(with = "serde_bytes")]
1190 pub metadata: Vec<u8>,
1191}
1192
1193#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1195pub enum Repository {
1196 V0(RepositoryV0),
1197}
1198
1199impl fmt::Display for Repository {
1200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1201 match self {
1202 Self::V0(v0) => {
1203 writeln!(f, "V0")?;
1204 writeln!(f, "repo_id: {}", v0.id)?;
1205 writeln!(
1206 f,
1207 "creator: {}",
1208 v0.creator.map_or("None".to_string(), |c| format!("{}", c))
1209 )?;
1210 Ok(())
1211 }
1212 }
1213 }
1214}
1215
1216#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1223pub struct RootBranchV0 {
1224 pub id: PubKey,
1226
1227 pub repo: ObjectRef,
1229
1230 pub store: StoreOverlay,
1233
1234 pub store_sig: Option<Signature>,
1238
1239 pub topic: TopicId,
1241
1242 #[serde(with = "serde_bytes")]
1249 pub topic_privkey: Vec<u8>,
1250
1251 pub inherit_perms_users_and_quorum_from_store: Option<ReadCap>,
1261
1262 pub quorum: Option<ObjectRef>,
1265
1266 pub reconciliation_interval: RelTime,
1268
1269 pub owners: Vec<UserId>,
1271
1272 pub owners_write_cap: Vec<serde_bytes::ByteBuf>,
1276
1277 #[serde(with = "serde_bytes")]
1279 pub metadata: Vec<u8>,
1280}
1281
1282#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1284pub enum RootBranch {
1285 V0(RootBranchV0),
1286}
1287
1288impl RootBranch {
1289 pub fn topic(&self) -> &TopicId {
1290 match self {
1291 Self::V0(v0) => &v0.topic,
1292 }
1293 }
1294 pub fn repo_id(&self) -> &RepoId {
1295 match self {
1296 Self::V0(v0) => &v0.id,
1297 }
1298 }
1299 pub fn owners(&self) -> &Vec<UserId> {
1300 match self {
1301 Self::V0(v0) => &v0.owners,
1302 }
1303 }
1304 pub fn encrypt_write_cap(
1305 for_user: &UserId,
1306 write_cap: &RepoWriteCapSecret,
1307 ) -> Result<Vec<u8>, NgError> {
1308 let ser = serde_bare::to_vec(write_cap).unwrap();
1309 let mut rng = crypto_box::aead::OsRng {};
1310 let cipher = crypto_box::seal(&mut rng, &for_user.to_dh_slice().into(), &ser)
1311 .map_err(|_| NgError::EncryptionError)?;
1312 Ok(cipher)
1313 }
1314 pub fn decrypt_write_cap(
1315 by_user: &PrivKey,
1316 cipher: &Vec<u8>,
1317 ) -> Result<RepoWriteCapSecret, NgError> {
1318 let ser = crypto_box::seal_open(&(*by_user.to_dh().slice()).into(), cipher)
1319 .map_err(|_| NgError::DecryptionError)?;
1320 let write_cap: RepoWriteCapSecret =
1321 serde_bare::from_slice(&ser).map_err(|_| NgError::SerializationError)?;
1322 Ok(write_cap)
1323 }
1324}
1325
1326impl fmt::Display for RootBranch {
1327 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1328 match self {
1329 Self::V0(v0) => {
1330 writeln!(f, "V0")?;
1331 writeln!(f, "repo_id: {}", v0.id)?;
1332 writeln!(f, "repo_ref: {}", v0.repo)?;
1333 write!(f, "store: {}", v0.store)?;
1334 writeln!(
1335 f,
1336 "store_sig: {}",
1337 v0.store_sig
1338 .as_ref()
1339 .map_or("None".to_string(), |c| format!("{}", c))
1340 )?;
1341 writeln!(f, "topic: {}", v0.topic)?;
1342 writeln!(
1343 f,
1344 "inherit_perms: {}",
1345 v0.inherit_perms_users_and_quorum_from_store
1346 .as_ref()
1347 .map_or("None".to_string(), |c| format!("{}", c))
1348 )?;
1349 writeln!(
1350 f,
1351 "quorum: {}",
1352 v0.quorum
1353 .as_ref()
1354 .map_or("None".to_string(), |c| format!("{}", c))
1355 )?;
1356 writeln!(f, "reconciliation_interval: {}", v0.reconciliation_interval)?;
1357 Ok(())
1358 }
1359 }
1360 }
1361}
1362
1363#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1367pub struct QuorumV0 {
1368 pub partial_order_quorum: u32,
1370
1371 pub partial_order_users: Vec<UserId>,
1373
1374 pub total_order_quorum: u32,
1376
1377 pub total_order_users: Vec<UserId>,
1379
1380 #[serde(with = "serde_bytes")]
1384 pub metadata: Vec<u8>,
1385}
1386
1387#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1390pub enum Quorum {
1391 V0(QuorumV0),
1392}
1393
1394#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1395pub enum BranchCrdt {
1396 Graph(String),
1397 YMap(String),
1398 YArray(String),
1399 YXml(String),
1400 YText(String),
1401 Automerge(String),
1402 Elmer(String),
1403 None, }
1409
1410impl BranchCrdt {
1411 pub fn is_graph(&self) -> bool {
1412 match self {
1413 BranchCrdt::Graph(_) => true,
1414 _ => false,
1415 }
1416 }
1417 pub fn name(&self) -> String {
1418 match self {
1419 BranchCrdt::Graph(_) => "Graph",
1420 BranchCrdt::YMap(_) => "YMap",
1421 BranchCrdt::YArray(_) => "YArray",
1422 BranchCrdt::YXml(_) => "YXml",
1423 BranchCrdt::YText(_) => "YText",
1424 BranchCrdt::Automerge(_) => "Automerge",
1425 BranchCrdt::Elmer(_) => "Elmer",
1426 BranchCrdt::None => panic!("BranchCrdt::None does not have a name"),
1427 }
1428 .to_string()
1429 }
1430 pub fn class(&self) -> &String {
1431 match self {
1432 BranchCrdt::Graph(c)
1433 | BranchCrdt::YMap(c)
1434 | BranchCrdt::YArray(c)
1435 | BranchCrdt::YXml(c)
1436 | BranchCrdt::YText(c)
1437 | BranchCrdt::Automerge(c)
1438 | BranchCrdt::Elmer(c) => c,
1439 BranchCrdt::None => panic!("BranchCrdt::None does not have a class"),
1440 }
1441 }
1442 pub fn from(name: String, class: String) -> Result<Self, NgError> {
1443 Ok(match name.as_str() {
1444 "Graph" => BranchCrdt::Graph(class),
1445 "YMap" => BranchCrdt::YMap(class),
1446 "YArray" => BranchCrdt::YArray(class),
1447 "YXml" => BranchCrdt::YXml(class),
1448 "YText" => BranchCrdt::YText(class),
1449 "Automerge" => BranchCrdt::Automerge(class),
1450 "Elmer" => BranchCrdt::Elmer(class),
1451 _ => return Err(NgError::InvalidClass),
1452 })
1453 }
1454}
1455
1456#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1465pub struct BranchV0 {
1466 pub id: PubKey,
1468
1469 pub crdt: BranchCrdt,
1470
1471 pub repo: ObjectRef,
1473
1474 pub root_branch_readcap_id: ObjectId,
1478
1479 pub topic: PubKey,
1481
1482 #[serde(with = "serde_bytes")]
1488 pub topic_privkey: Vec<u8>,
1489
1490 #[serde(with = "serde_bytes")]
1493 pub pulled_from: Vec<u8>,
1494
1495 #[serde(with = "serde_bytes")]
1497 pub metadata: Vec<u8>,
1498}
1499
1500impl fmt::Display for Branch {
1501 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1502 match self {
1503 Self::V0(v0) => {
1504 writeln!(f, "V0")?;
1505 writeln!(f, "id: {}", v0.id)?;
1506 writeln!(f, "repo: {}", v0.repo)?;
1507 writeln!(f, "root_branch_readcap_id: {}", v0.root_branch_readcap_id)?;
1508 writeln!(f, "topic: {}", v0.topic)?;
1509 writeln!(f, "topic_privkey: {:?}", v0.topic_privkey)?;
1510 Ok(())
1511 }
1512 }
1513 }
1514}
1515
1516#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1518pub enum Branch {
1519 V0(BranchV0),
1520}
1521
1522#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1523pub enum BranchType {
1524 Main, Store,
1526 Overlay,
1527 User,
1528 Chat,
1530 Stream,
1531 Comments,
1532 BackLinks,
1533 Context,
1534 Transactional, Root, Header,
1538}
1539
1540impl BranchType {
1541 pub fn is_main(&self) -> bool {
1542 match self {
1543 Self::Main => true,
1544 _ => false,
1545 }
1546 }
1547 pub fn is_header(&self) -> bool {
1548 match self {
1549 Self::Header => true,
1550 _ => false,
1551 }
1552 }
1553}
1554
1555impl fmt::Display for BranchType {
1556 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1557 write!(
1558 f,
1559 "{}",
1560 match self {
1561 Self::Main => "Main",
1562 Self::Header => "Header",
1563 Self::Store => "Store",
1564 Self::Overlay => "Overlay",
1565 Self::User => "User",
1566 Self::Transactional => "Transactional",
1567 Self::Root => "Root",
1568 Self::Chat => "Chat",
1569 Self::Stream => "Stream",
1570 Self::Comments => "Comments",
1571 Self::BackLinks => "BackLinks",
1572 Self::Context => "Context",
1573 }
1576 )
1577 }
1578}
1579
1580#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1584pub struct AddBranchV0 {
1585 pub topic_id: Option<TopicId>,
1589 pub branch_read_cap: Option<ReadCap>,
1593
1594 pub crdt: BranchCrdt,
1595
1596 pub branch_id: BranchId,
1597
1598 pub branch_type: BranchType,
1599
1600 pub fork_of: Option<BranchId>,
1601
1602 pub merged_in: Option<BranchId>,
1603}
1604
1605impl fmt::Display for AddBranch {
1606 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1607 match self {
1608 Self::V0(v0) => {
1609 writeln!(f, "V0 {}", v0.branch_type)?;
1610 writeln!(f, "branch_id: {}", v0.branch_id)?;
1611 if v0.topic_id.is_some() {
1612 writeln!(f, "topic_id: {}", v0.topic_id.as_ref().unwrap())?;
1613 }
1614 if v0.branch_read_cap.is_some() {
1615 writeln!(
1616 f,
1617 "branch_read_cap: {}",
1618 v0.branch_read_cap.as_ref().unwrap()
1619 )?;
1620 }
1621 if v0.fork_of.is_some() {
1622 writeln!(f, "fork_of: {}", v0.fork_of.as_ref().unwrap())?;
1623 }
1624 if v0.merged_in.is_some() {
1625 writeln!(f, "merged_in: {}", v0.merged_in.as_ref().unwrap())?;
1626 }
1627 Ok(())
1628 }
1629 }
1630 }
1631}
1632
1633#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1635pub enum AddBranch {
1636 V0(AddBranchV0),
1637}
1638
1639pub type RemoveBranchV0 = ();
1640
1641#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1645pub enum RemoveBranch {
1646 V0(RemoveBranchV0),
1647}
1648
1649#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1651pub struct AddMemberV0 {
1652 pub member: UserId,
1654
1655 #[serde(with = "serde_bytes")]
1658 pub metadata: Vec<u8>,
1659}
1660
1661#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1662pub enum AddMember {
1663 V0(AddMemberV0),
1664}
1665
1666#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1671pub struct RemoveMemberV0 {
1672 pub member: UserId,
1674
1675 pub banned: bool,
1677
1678 #[serde(with = "serde_bytes")]
1681 pub metadata: Vec<u8>,
1682}
1683
1684#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1685pub enum RemoveMember {
1686 V0(RemoveMemberV0),
1687}
1688
1689#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1691pub struct SignerCap {
1692 pub repo: RepoId,
1693
1694 pub epoch: ObjectId,
1696
1697 pub owner: Option<SerdeSecret<ng_threshold_crypto::SecretKeyShare>>,
1698
1699 pub total_order: Option<SerdeSecret<ng_threshold_crypto::SecretKeyShare>>,
1700
1701 pub partial_order: Option<SerdeSecret<ng_threshold_crypto::SecretKeyShare>>,
1702}
1703
1704impl SignerCap {
1705 pub fn sign_with_owner(&self, content: &[u8]) -> Result<SignatureShare, NgError> {
1706 if let Some(key_share) = &self.owner {
1707 Ok(key_share.sign(content))
1708 } else {
1709 Err(NgError::KeyShareNotFound)
1710 }
1711 }
1712}
1713
1714#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
1716pub enum PermissionV0 {
1717 Create, Owner, AddReadMember, RemoveMember, AddWritePermission, WriteAsync, WriteSync, Compact, RemoveWritePermission, AddBranch, RemoveBranch, ChangeName, RefreshReadCap, RefreshWriteCap, ChangeQuorum, Admin, ChangeMainBranch,
1744
1745 Chat, Inbox, PermaShare, UpdateStore, RefreshOverlay, }
1752
1753#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1755pub struct AddPermissionV0 {
1756 pub member: UserId,
1758
1759 pub permission: PermissionV0,
1761
1762 #[serde(with = "serde_bytes")]
1770 pub metadata: Vec<u8>,
1771}
1772
1773#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1774pub enum AddPermission {
1775 V0(AddPermissionV0),
1776}
1777
1778impl AddPermission {
1779 pub fn permission_v0(&self) -> &PermissionV0 {
1780 match self {
1781 Self::V0(v0) => &v0.permission,
1782 }
1783 }
1784}
1785
1786#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1788pub struct RemovePermissionV0 {
1789 pub member: UserId,
1791
1792 pub permission: PermissionV0,
1794
1795 #[serde(with = "serde_bytes")]
1801 pub metadata: Vec<u8>,
1802}
1803
1804#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1805pub enum RemovePermission {
1806 V0(RemovePermissionV0),
1807}
1808
1809impl RemovePermission {
1810 pub fn permission_v0(&self) -> &PermissionV0 {
1811 match self {
1812 Self::V0(v0) => &v0.permission,
1813 }
1814 }
1815}
1816
1817#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1818pub enum RepoNamedItemV0 {
1819 Branch(BranchId),
1820 Commit(ObjectRef),
1821}
1822
1823#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1824pub enum RepoNamedItem {
1825 V0(RepoNamedItemV0),
1826}
1827
1828#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1833pub struct AddNameV0 {
1834 pub name: String,
1836
1837 pub item: RepoNamedItem,
1839
1840 #[serde(with = "serde_bytes")]
1842 pub metadata: Vec<u8>,
1843}
1844
1845#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1846pub enum AddName {
1847 V0(AddNameV0),
1848}
1849
1850#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1854pub struct RemoveNameV0 {
1855 pub name: String,
1857
1858 #[serde(with = "serde_bytes")]
1860 pub metadata: Vec<u8>,
1861}
1862
1863#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1864pub enum RemoveName {
1865 V0(RemoveNameV0),
1866}
1867
1868#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1877pub struct AddRepoV0 {
1878 pub read_cap: ReadCap,
1879
1880 #[serde(with = "serde_bytes")]
1882 pub metadata: Vec<u8>,
1883}
1884
1885#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1886pub enum AddRepo {
1887 V0(AddRepoV0),
1888}
1889
1890impl AddRepo {
1891 pub fn read_cap(&self) -> &ReadCap {
1892 match self {
1893 Self::V0(v0) => &v0.read_cap,
1894 }
1895 }
1896}
1897
1898#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1902pub struct RemoveRepoV0 {
1903 pub id: RepoId,
1904
1905 #[serde(with = "serde_bytes")]
1907 pub metadata: Vec<u8>,
1908}
1909
1910#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1911pub enum RemoveRepo {
1912 V0(RemoveRepoV0),
1913}
1914
1915#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1926pub struct AddLinkV0 {
1927 pub read_cap: ReadCap,
1928
1929 #[serde(with = "serde_bytes")]
1931 pub metadata: Vec<u8>,
1932}
1933
1934#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1935pub enum AddLink {
1936 V0(AddLinkV0),
1937}
1938
1939#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1943pub struct RemoveLinkV0 {
1944 pub id: RepoId,
1945
1946 #[serde(with = "serde_bytes")]
1948 pub metadata: Vec<u8>,
1949}
1950
1951#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1952pub enum RemoveLink {
1953 V0(RemoveLinkV0),
1954}
1955
1956#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1963pub struct AddSignerCapV0 {
1964 pub cap: SignerCap,
1965
1966 #[serde(with = "serde_bytes")]
1968 pub metadata: Vec<u8>,
1969}
1970
1971#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1972pub enum AddSignerCap {
1973 V0(AddSignerCapV0),
1974}
1975
1976impl fmt::Display for AddSignerCap {
1977 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1978 match self {
1979 Self::V0(v0) => {
1980 writeln!(f, "V0")?;
1981 writeln!(f, "cap: {:?}", v0.cap)?;
1982
1983 Ok(())
1984 }
1985 }
1986 }
1987}
1988
1989#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
1993pub struct RemoveSignerCapV0 {
1994 pub id: RepoId,
1995
1996 #[serde(with = "serde_bytes")]
1998 pub metadata: Vec<u8>,
1999}
2000
2001#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2002pub enum RemoveSignerCap {
2003 V0(RemoveSignerCapV0),
2004}
2005
2006#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2010pub struct WalletUpdateV0 {
2011 #[serde(with = "serde_bytes")]
2012 pub op: Vec<u8>,
2013
2014 #[serde(with = "serde_bytes")]
2016 pub metadata: Vec<u8>,
2017}
2018
2019#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2020pub enum WalletUpdate {
2021 V0(WalletUpdateV0),
2022}
2023
2024#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2030pub struct StoreUpdateV0 {
2031 pub store: StoreRepo,
2033
2034 pub store_read_cap: ReadCap,
2035
2036 pub overlay_branch_read_cap: ReadCap,
2037
2038 #[serde(with = "serde_bytes")]
2040 pub metadata: Vec<u8>,
2041}
2042
2043#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2044pub enum StoreUpdate {
2045 V0(StoreUpdateV0),
2046}
2047
2048impl fmt::Display for StoreUpdate {
2049 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2050 match self {
2051 Self::V0(v0) => {
2052 writeln!(f, "V0")?;
2053 writeln!(f, "store: {}", v0.store)?;
2054 writeln!(f, "store_read_cap: {}", v0.store_read_cap)?;
2055 write!(
2056 f,
2057 "overlay_branch_read_cap: {}",
2058 v0.overlay_branch_read_cap
2059 )?;
2060 Ok(())
2061 }
2062 }
2063 }
2064}
2065
2066pub type TransactionV0 = Vec<u8>;
2074
2075#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2076pub enum Transaction {
2077 #[serde(with = "serde_bytes")]
2078 V0(TransactionV0),
2079}
2080
2081impl Transaction {
2082 pub fn body_type(&self) -> u8 {
2083 match self {
2084 Self::V0(v0) => v0[0],
2085 }
2086 }
2087}
2088
2089#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2093pub struct AddFileV0 {
2094 pub name: Option<String>,
2096
2097 #[serde(with = "serde_bytes")]
2099 pub metadata: Vec<u8>,
2100}
2101
2102#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2103pub enum AddFile {
2104 V0(AddFileV0),
2105}
2106
2107impl fmt::Display for AddFile {
2108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2109 match self {
2110 Self::V0(v0) => {
2111 writeln!(f, "V0")?;
2112 writeln!(f, "name: {:?}", v0.name)
2113 }
2114 }
2115 }
2116}
2117
2118impl AddFile {
2119 pub fn name(&self) -> &Option<String> {
2120 match self {
2121 Self::V0(v0) => &v0.name,
2122 }
2123 }
2124}
2125
2126pub type RemoveFileV0 = ();
2132
2133#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2134pub enum RemoveFile {
2135 V0(RemoveFileV0),
2136}
2137
2138#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2143pub struct SnapshotV0 {
2144 pub heads: Vec<ObjectId>,
2146
2147 pub content: ObjectRef,
2149}
2150
2151#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2153pub enum Snapshot {
2154 V0(SnapshotV0),
2155}
2156
2157impl Snapshot {
2158 pub fn snapshot_ref(&self) -> &ObjectRef {
2159 match self {
2160 Self::V0(v0) => &v0.content,
2161 }
2162 }
2163}
2164
2165impl fmt::Display for Snapshot {
2166 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2167 match self {
2168 Self::V0(v0) => {
2169 writeln!(f, "V0\r\nheads:")?;
2170 for h in v0.heads.iter() {
2171 writeln!(f, "{h}")?;
2172 }
2173 writeln!(f, "content: {}", v0.content)?;
2174 Ok(())
2175 }
2176 }
2177 }
2178}
2179
2180#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2191pub struct CompactV0 {
2192 pub heads: Vec<ObjectId>,
2194
2195 #[serde(with = "serde_bytes")]
2197 pub origin: Vec<u8>,
2198
2199 pub content: ObjectRef,
2201}
2202
2203#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2205pub enum Compact {
2206 V0(CompactV0),
2207}
2208
2209#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2221pub enum AsyncSignature {
2222 V0(ObjectRef),
2223}
2224
2225impl AsyncSignature {
2226 pub fn verify_(&self) -> bool {
2227 unimplemented!();
2229 }
2230 pub fn reference(&self) -> &ObjectRef {
2231 match self {
2232 Self::V0(v0) => v0,
2233 }
2234 }
2235}
2236
2237impl fmt::Display for AsyncSignature {
2238 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2239 match self {
2240 Self::V0(v0) => {
2241 writeln!(f, "V0\r\nsignature object ref: {}", v0)?;
2242 Ok(())
2243 }
2244 }
2245 }
2246}
2247
2248#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2261pub enum SyncSignature {
2262 V0(ObjectRef),
2263}
2264
2265impl SyncSignature {
2266 pub fn verify_quorum(&self) -> bool {
2267 unimplemented!();
2269 }
2270 pub fn reference(&self) -> &ObjectRef {
2271 match self {
2272 Self::V0(v0) => v0,
2273 }
2274 }
2275}
2276
2277impl fmt::Display for SyncSignature {
2278 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2279 match self {
2280 Self::V0(v0) => {
2281 writeln!(f, "V0")?;
2282 writeln!(f, "{}", v0)?;
2283 Ok(())
2284 }
2285 }
2286 }
2287}
2288
2289#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2291pub struct RefreshSecretV0(SymKey, Option<SymKey>);
2292
2293#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2294pub struct RefreshCapV0 {
2295 pub refresh_secret: Vec<(Digest, serde_bytes::ByteBuf)>,
2300}
2301
2302#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2304pub enum RefreshCap {
2305 V0(RefreshCapV0),
2306}
2307
2308#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2321pub struct RootCapRefreshV0 {
2322 pub refresh_ref: ObjectRef,
2324
2325 pub write_cap: Option<RepoWriteCapSecret>,
2328}
2329
2330#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2332pub enum RootCapRefresh {
2333 V0(RootCapRefreshV0),
2334}
2335
2336#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2347pub struct BranchCapRefreshV0 {
2348 pub refresh_ref: ObjectRef,
2350}
2351
2352#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2354pub enum BranchCapRefresh {
2355 V0(BranchCapRefreshV0),
2356}
2357
2358#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2360pub struct BranchCapRefreshedV0 {
2361 pub continuation_of: ReadCap,
2363
2364 pub refresh: ObjectRef,
2366
2367 pub new_read_cap: ReadCap,
2369}
2370
2371#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2373pub enum BranchCapRefreshed {
2374 V0(BranchCapRefreshedV0),
2375}
2376
2377#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2379pub struct SignatureContentV0 {
2380 pub commits: Vec<ObjectId>,
2382}
2383
2384#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2386pub enum SignatureContent {
2387 V0(SignatureContentV0),
2388}
2389
2390impl SignatureContent {
2391 pub fn commits(&self) -> &[ObjectId] {
2392 match self {
2393 Self::V0(v0) => &v0.commits,
2394 }
2395 }
2396}
2397
2398impl fmt::Display for SignatureContent {
2399 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2400 match self {
2401 Self::V0(v0) => {
2402 writeln!(f, "V0 == Commits: {}", v0.commits.len())?;
2403 let mut i = 0;
2404 for block_id in &v0.commits {
2405 writeln!(f, "========== {:03}: {}", i, block_id)?;
2406 i += 1;
2407 }
2408 Ok(())
2409 }
2410 }
2411 }
2412}
2413
2414#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2416pub enum ThresholdSignatureV0 {
2417 PartialOrder(ng_threshold_crypto::Signature),
2418 TotalOrder(ng_threshold_crypto::Signature),
2419 Owners(ng_threshold_crypto::Signature),
2420}
2421
2422impl fmt::Display for ThresholdSignatureV0 {
2423 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2424 match self {
2425 Self::PartialOrder(_) => {
2426 writeln!(f, "PartialOrder")
2427 }
2428 Self::TotalOrder(_) => {
2429 writeln!(f, "TotalOrder")
2430 }
2431 Self::Owners(_) => {
2432 writeln!(f, "Owners")
2433 }
2434 }
2435 }
2436}
2437
2438#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2440pub struct SignatureV0 {
2441 pub content: SignatureContent,
2443
2444 pub threshold_sig: ThresholdSignatureV0,
2446
2447 pub certificate_ref: ObjectRef,
2449}
2450
2451impl SignatureV0 {
2452 pub fn verify(&self, cert: &CertificateV0) -> Result<(), NgError> {
2453 let ser = serde_bare::to_vec(&self.content).unwrap();
2454 match &self.threshold_sig {
2455 ThresholdSignatureV0::Owners(sig) => {
2456 if !cert.get_owners_pub_key().verify(sig, &ser) {
2457 return Err(NgError::InvalidSignature);
2458 }
2459 return Ok(());
2460 }
2461 _ => unimplemented!(),
2462 }
2463 }
2464}
2465
2466impl fmt::Display for Signature {
2467 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2468 match self {
2469 Self::V0(v0) => {
2470 writeln!(f, "V0")?;
2471 writeln!(f, "content: {}", v0.content)?;
2472 writeln!(f, "threshold_sig: {}", v0.threshold_sig)?;
2473 writeln!(f, "certificate_ref:{}", v0.certificate_ref)?;
2474 Ok(())
2475 }
2476 }
2477 }
2478}
2479
2480impl Signature {
2481 pub fn certificate_ref(&self) -> &ObjectRef {
2482 match self {
2483 Self::V0(v0) => &v0.certificate_ref,
2484 }
2485 }
2486 pub fn signed_commits(&self) -> &[ObjectId] {
2487 match self {
2488 Self::V0(v0) => match &v0.content {
2489 SignatureContent::V0(v0) => &v0.commits,
2490 },
2491 }
2492 }
2493}
2494
2495#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2497pub enum Signature {
2498 V0(SignatureV0),
2499}
2500
2501#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2508pub enum OrdersPublicKeySetsV0 {
2509 Store(ObjectRef),
2510 Repo(
2511 (
2512 ng_threshold_crypto::PublicKey,
2513 Option<ng_threshold_crypto::PublicKey>,
2514 ),
2515 ),
2516 None, }
2518
2519#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2521pub struct CertificateContentV0 {
2522 pub previous: ObjectRef,
2524
2525 pub readcap_id: ObjectId,
2528
2529 pub owners_pk_set: ng_threshold_crypto::PublicKey,
2531
2532 pub orders_pk_sets: OrdersPublicKeySetsV0,
2534}
2535
2536#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2538pub enum CertificateSignatureV0 {
2539 Repo(Sig),
2541 TotalOrder(ng_threshold_crypto::Signature),
2543 Owners(ng_threshold_crypto::Signature),
2550 Store,
2553}
2554
2555#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2557pub struct CertificateV0 {
2558 pub content: CertificateContentV0,
2560
2561 pub sig: CertificateSignatureV0,
2563}
2564
2565impl CertificateV0 {
2566 pub fn verify_with_repo_id(&self, repo_id: &RepoId) -> Result<(), NgError> {
2567 let ser = serde_bare::to_vec(&self.content).unwrap();
2568 match self.sig {
2569 CertificateSignatureV0::Repo(sig) => verify(&ser, sig, repo_id.clone()),
2570 _ => Err(NgError::InvalidArgument),
2571 }
2572 }
2573 pub fn get_owners_pub_key(&self) -> &ng_threshold_crypto::PublicKey {
2574 &self.content.owners_pk_set
2575 }
2576}
2577
2578#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2580pub enum Certificate {
2581 V0(CertificateV0),
2582}
2583
2584#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2586pub enum CommitBodyV0 {
2587 Repository(Repository), RootBranch(RootBranch), UpdateRootBranch(RootBranch), RootCapRefresh(RootCapRefresh), AddMember(AddMember), RemoveMember(RemoveMember), AddPermission(AddPermission),
2597 RemovePermission(RemovePermission),
2598 AddBranch(AddBranch),
2599 RemoveBranch(RemoveBranch),
2600 AddName(AddName),
2601 RemoveName(RemoveName),
2602 Delete(()), Branch(Branch), BranchCapRefresh(BranchCapRefresh), UpdateBranch(Branch), Snapshot(Snapshot), AsyncTransaction(Transaction), SyncTransaction(Transaction), AddFile(AddFile),
2616 RemoveFile(RemoveFile),
2617 Compact(Compact), AsyncSignature(AsyncSignature),
2621
2622 CapRefreshed(BranchCapRefreshed), SyncSignature(SyncSignature),
2627
2628 AddRepo(AddRepo),
2632 RemoveRepo(RemoveRepo),
2633
2634 AddLink(AddLink),
2638 RemoveLink(RemoveLink),
2639 AddSignerCap(AddSignerCap),
2640 RemoveSignerCap(RemoveSignerCap),
2641 WalletUpdate(WalletUpdate),
2642 StoreUpdate(StoreUpdate),
2643}
2644
2645#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2647pub enum CommitBody {
2648 V0(CommitBodyV0),
2649}
2650
2651#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2652pub enum QuorumType {
2653 NoSigning,
2654 PartialOrder,
2655 TotalOrder,
2656 Owners,
2657 IamTheSignature,
2658}
2659
2660impl QuorumType {
2661 pub fn final_consistency(&self) -> bool {
2662 match self {
2663 Self::TotalOrder => true,
2664 _ => false,
2665 }
2666 }
2667}
2668
2669impl CommitBody {
2670 pub fn get_type(&self) -> CommitType {
2671 match self {
2672 Self::V0(v0) => v0.get_type(),
2673 }
2674 }
2675 pub fn get_signature_reference(&self) -> Option<ObjectRef> {
2676 match self {
2677 Self::V0(v0) => v0.get_signature_reference(),
2678 }
2679 }
2680}
2681
2682impl CommitBodyV0 {
2683 pub fn get_type(&self) -> CommitType {
2684 match self {
2685 Self::Branch(_) => CommitType::Branch,
2686 Self::BranchCapRefresh(_) => CommitType::BranchCapRefresh,
2687 Self::UpdateBranch(_) => CommitType::UpdateBranch,
2688 Self::Snapshot(_) => CommitType::Snapshot,
2689 Self::AsyncTransaction(t) | Self::SyncTransaction(t) => match t.body_type() {
2690 0 => CommitType::TransactionGraph,
2691 1 => CommitType::TransactionDiscrete,
2692 2 => CommitType::TransactionBoth,
2693 _ => panic!("invalid TransactionBody"),
2694 },
2695 Self::AddFile(_) => CommitType::FileAdd,
2696 Self::RemoveFile(_) => CommitType::FileRemove,
2697 Self::Compact(_) => CommitType::Compact,
2698 Self::AsyncSignature(_) => CommitType::AsyncSignature,
2699 Self::CapRefreshed(_) => CommitType::CapRefreshed,
2700 Self::SyncSignature(_) => CommitType::SyncSignature,
2701 _ => CommitType::Other,
2702 }
2703 }
2704
2705 pub fn get_signature_reference(&self) -> Option<ObjectRef> {
2706 match self {
2707 Self::AsyncSignature(s) => Some(s.reference().clone()),
2708 Self::SyncSignature(s) => Some(s.reference().clone()),
2709 _ => None,
2710 }
2711 }
2712}
2713
2714#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2715pub enum CommitType {
2716 TransactionGraph,
2717 TransactionDiscrete,
2718 TransactionBoth,
2719 FileAdd,
2720 FileRemove,
2721 Snapshot,
2722 Compact,
2723 AsyncSignature,
2724 SyncSignature,
2725 Branch,
2726 UpdateBranch,
2727 BranchCapRefresh,
2728 CapRefreshed,
2729 Other,
2730}
2731
2732#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2734pub struct CommitContentV0 {
2735 pub author: Digest,
2742
2743 pub branch: BranchId,
2747
2748 pub perms: Vec<ObjectId>,
2750
2751 pub header_keys: Option<CommitHeaderKeys>,
2753
2754 pub quorum: QuorumType,
2756
2757 pub timestamp: Timestamp,
2758
2759 #[serde(with = "serde_bytes")]
2761 pub metadata: Vec<u8>,
2762
2763 pub body: ObjectRef,
2766}
2767
2768#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2770pub enum CommitContent {
2771 V0(CommitContentV0),
2772}
2773
2774impl CommitContent {
2775 pub fn header_keys(&self) -> &Option<CommitHeaderKeys> {
2776 match self {
2777 CommitContent::V0(v0) => &v0.header_keys,
2778 }
2779 }
2780 pub fn author(&self) -> &Digest {
2781 match self {
2782 CommitContent::V0(v0) => &v0.author,
2783 }
2784 }
2785 pub fn timestamp(&self) -> Timestamp {
2786 match self {
2787 CommitContent::V0(v0) => v0.timestamp,
2788 }
2789 }
2790 pub fn branch(&self) -> &BranchId {
2791 match self {
2792 CommitContent::V0(v0) => &v0.branch,
2793 }
2794 }
2795
2796 pub fn final_consistency(&self) -> bool {
2797 match self {
2798 CommitContent::V0(v0) => v0.quorum.final_consistency(),
2799 }
2800 }
2801
2802 pub fn author_digest(author: &UserId, overlay: OverlayId) -> Digest {
2803 let author_id = serde_bare::to_vec(author).unwrap();
2804 let overlay_id = serde_bare::to_vec(&overlay).unwrap();
2805 let mut key: [u8; 32] = blake3::derive_key(
2806 "NextGraph UserId Hash Overlay Id for Commit BLAKE3 key",
2807 overlay_id.as_slice(),
2808 );
2809 let key_hash = blake3::keyed_hash(&key, &author_id);
2810 key.zeroize();
2811 Digest::from_slice(*key_hash.as_bytes())
2812 }
2813}
2814
2815#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2819pub struct CommitV0 {
2820 #[serde(skip)]
2822 pub id: Option<ObjectId>,
2823
2824 #[serde(skip)]
2826 pub key: Option<SymKey>,
2827
2828 #[serde(skip)]
2830 pub header: Option<CommitHeader>,
2831
2832 #[serde(skip)]
2834 pub body: OnceCell<CommitBody>,
2835
2836 #[serde(skip)]
2838 pub blocks: Vec<BlockId>,
2839
2840 pub content: CommitContent,
2842
2843 pub sig: Sig,
2845}
2846
2847#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2849pub enum Commit {
2850 V0(CommitV0),
2851}
2852
2853#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2855pub struct SmallFileV0 {
2856 pub content_type: String,
2857
2858 #[serde(with = "serde_bytes")]
2859 pub metadata: Vec<u8>,
2860
2861 #[serde(with = "serde_bytes")]
2862 pub content: Vec<u8>,
2863}
2864
2865#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2867pub enum SmallFile {
2868 V0(SmallFileV0),
2869}
2870
2871#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2873pub struct RandomAccessFileMetaV0 {
2874 pub content_type: String,
2875
2876 #[serde(with = "serde_bytes")]
2877 pub metadata: Vec<u8>,
2878
2879 pub total_size: u64,
2880
2881 pub chunk_size: u32,
2882
2883 pub arity: u16,
2884
2885 pub depth: u8,
2886}
2887
2888#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2890pub enum RandomAccessFileMeta {
2891 V0(RandomAccessFileMetaV0),
2892}
2893
2894impl RandomAccessFileMeta {
2895 pub fn arity(&self) -> u16 {
2896 match self {
2897 Self::V0(v0) => v0.arity,
2898 }
2899 }
2900
2901 pub fn depth(&self) -> u8 {
2902 match self {
2903 Self::V0(v0) => v0.depth,
2904 }
2905 }
2906
2907 pub fn set_depth(&mut self, depth: u8) {
2908 match self {
2909 Self::V0(v0) => {
2910 v0.depth = depth;
2911 }
2912 }
2913 }
2914
2915 pub fn chunk_size(&self) -> u32 {
2916 match self {
2917 Self::V0(v0) => v0.chunk_size,
2918 }
2919 }
2920
2921 pub fn total_size(&self) -> u64 {
2922 match self {
2923 Self::V0(v0) => v0.total_size,
2924 }
2925 }
2926
2927 pub fn set_total_size(&mut self, size: u64) {
2928 match self {
2929 Self::V0(v0) => {
2930 v0.total_size = size;
2931 }
2932 }
2933 }
2934
2935 pub fn metadata(&self) -> &Vec<u8> {
2936 match self {
2937 Self::V0(v0) => &v0.metadata,
2938 }
2939 }
2940
2941 pub fn content_type(&self) -> &String {
2942 match self {
2943 Self::V0(v0) => &v0.content_type,
2944 }
2945 }
2946}
2947
2948#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2950pub enum ObjectContentV0 {
2951 Commit(Commit),
2952 CommitBody(CommitBody),
2953 CommitHeader(CommitHeader),
2954 Quorum(Quorum),
2955 Signature(Signature),
2956 Certificate(Certificate),
2957 SmallFile(SmallFile),
2958 RandomAccessFileMeta(RandomAccessFileMeta),
2959 RefreshCap(RefreshCap),
2960 #[serde(with = "serde_bytes")]
2961 Snapshot(Vec<u8>), }
2963
2964#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
2966pub enum ObjectContent {
2967 V0(ObjectContentV0),
2968}
2969
2970pub trait IObject {
2975 fn block_ids(&self) -> Vec<BlockId>;
2976
2977 fn id(&self) -> Option<ObjectId>;
2978
2979 fn key(&self) -> Option<SymKey>;
2980}
2981
2982pub type DirectPeerId = PubKey;
2983
2984pub type ForwardedPeerId = PubKey;
2985
2986#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
2988pub enum PeerId {
2989 Direct(DirectPeerId),
2990 Forwarded(ForwardedPeerId),
2991 ForwardedObfuscated(Digest),
2994}
2995
2996impl fmt::Display for PeerId {
2997 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2998 match self {
2999 Self::Direct(p) => {
3000 write!(f, "Direct : {}", p)
3001 }
3002 Self::Forwarded(p) => {
3003 write!(f, "Forwarded : {}", p)
3004 }
3005 Self::ForwardedObfuscated(p) => {
3006 write!(f, "ForwardedObfuscated : {}", p)
3007 }
3008 }
3009 }
3010}
3011
3012impl PeerId {
3013 pub fn get_pub_key(&self) -> PubKey {
3014 match self {
3015 Self::Direct(pk) | Self::Forwarded(pk) => pk.clone(),
3016 _ => panic!("cannot get a pubkey for ForwardedObfuscated"),
3017 }
3018 }
3019}
3020
3021#[derive(Clone, Debug, Serialize, Deserialize)]
3026pub struct EventContentV0 {
3027 pub topic: TopicId,
3029
3030 pub publisher: PeerId,
3033
3034 pub seq: u64,
3036
3037 pub blocks: Vec<Block>,
3045
3046 pub file_ids: Vec<BlockId>,
3049
3050 #[serde(with = "serde_bytes")]
3065 pub key: Vec<u8>,
3066}
3067
3068#[derive(Clone, Debug, Serialize, Deserialize)]
3072pub struct EventV0 {
3073 pub content: EventContentV0,
3074
3075 pub topic_sig: Sig,
3077
3078 pub peer_sig: Sig,
3080}
3081
3082#[derive(Clone, Debug, Serialize, Deserialize)]
3084pub enum Event {
3085 V0(EventV0),
3086}