bucky_objects/
signature.rs

1use bucky_crypto::{PublicKeyValue, Signature, SignData};
2use bucky_raw_codec::{RawDecode, RawEncode, RawEncodePurpose};
3use crate::ObjectLink;
4
5pub const SIGNATURE_REF_INDEX: u8 = 0b_00000000;
6pub const SIGNATURE_OBJECT: u8 = 0b_00000001;
7pub const SIGNATURE_KEY: u8 = 0b_00000010;
8
9// 1.obj_desc.ref_objs,取值范围为[0, 127]
10pub const SIGNATURE_SOURCE_REFINDEX_REF_OBJ_BEGIN: u8 = 0;
11pub const SIGNATURE_SOURCE_REFINDEX_REF_OBJ_END: u8 = 127;
12
13/*
142.逻辑ref (从128-255(可以根据需要扩展)
15ref[255] = 自己 (适用于有权对象)
16ref[254] = owner (使用于有主对象)
17ref[253] = author (适用于填写了作者的对象)
18ref[252-236] = ood_list[x] (适用于所在Zone的ood对象)
19*/
20pub const SIGNATURE_SOURCE_REFINDEX_SELF: u8 = 255;
21pub const SIGNATURE_SOURCE_REFINDEX_OWNER: u8 = 254;
22pub const SIGNATURE_SOURCE_REFINDEX_AUTHOR: u8 = 253;
23
24pub const SIGNATURE_SOURCE_REFINDEX_ZONE_OOD_BEGIN: u8 = 252;
25pub const SIGNATURE_SOURCE_REFINDEX_ZONE_OOD_END: u8 = 236;
26
27#[derive(Clone, Eq, PartialEq, Debug, RawEncode, RawDecode)]
28pub enum SignatureSource {
29    RefIndex(u8),
30    Object(ObjectLink),
31    Key(PublicKeyValue),
32}
33
34impl Default for ObjSignature {
35    fn default() -> Self {
36        Self {
37            sign_source: SignatureSource::RefIndex(0),
38            sign_key_index: 0,
39            sign: Signature::default(),
40        }
41    }
42}
43
44#[derive(Clone, Eq, PartialEq, Debug, RawEncode, RawDecode)]
45pub struct ObjSignature {
46    sign_source: SignatureSource,
47    sign_key_index: u8,
48    sign: Signature
49}
50
51impl From<(SignatureSource, u8, Signature)> for ObjSignature {
52    fn from(value: (SignatureSource, u8, Signature)) -> Self {
53        Self {
54            sign_source: value.0,
55            sign_key_index: value.1,
56            sign: value.2
57        }
58    }
59}
60
61impl ObjSignature {
62    pub fn new(
63        sign_source: SignatureSource,
64        sign_key_index: u8,
65        sign_time: u64,
66        sign: SignData,
67    ) -> Self {
68        Self {
69            sign_source: sign_source,
70            sign_key_index,
71            sign: Signature::new(sign_time, sign)
72        }
73    }
74
75    pub fn sign(&self) -> &SignData {
76        self.sign.sign()
77    }
78
79    pub fn as_slice<'a>(&self) -> &'a [u8] {
80        self.sign.as_slice()
81    }
82
83    fn sign_source_with_ref_index(&self) -> u8 {
84        match self.sign_source {
85            SignatureSource::RefIndex(_index) => {
86                // sign_key_index[. . . . . . x x] type[. .]
87                SIGNATURE_REF_INDEX | (self.sign_key_index << 2)
88            }
89            SignatureSource::Object(_) => SIGNATURE_OBJECT | (self.sign_key_index << 2),
90            SignatureSource::Key(_) => SIGNATURE_KEY,
91        }
92    }
93
94    pub fn is_ref_index(&self) -> bool {
95        match self.sign_source {
96            SignatureSource::RefIndex(_) => true,
97            _ => false,
98        }
99    }
100
101    pub fn is_object(&self) -> bool {
102        match self.sign_source {
103            SignatureSource::Object(_) => true,
104            _ => false,
105        }
106    }
107
108    pub fn is_key(&self) -> bool {
109        match self.sign_source {
110            SignatureSource::Key(_) => true,
111            _ => false,
112        }
113    }
114
115    pub fn sign_source(&self) -> &SignatureSource {
116        &self.sign_source
117    }
118
119    pub fn sign_time(&self) -> u64 {
120        self.sign.sign_time()
121    }
122
123    pub fn sign_key_index(&self) -> u8 {
124        self.sign_key_index
125    }
126
127    pub fn compare_source(&self, other: &Self) -> bool {
128        self.sign_source == other.sign_source && self.sign_key_index == other.sign_key_index
129    }
130
131    pub fn signature(&self) -> &Signature {
132        &self.sign
133    }
134}