1use crate::*;
2
3use async_trait::async_trait;
4use crate::signature::{ObjSignature, SignatureSource};
5
6pub struct RsaCPUObjectSigner {
9 public_key: PublicKey,
10 secret: PrivateKey,
11}
12
13impl RsaCPUObjectSigner {
14 pub fn new(public_key: PublicKey, secret: PrivateKey) -> Self {
15 RsaCPUObjectSigner { public_key, secret }
16 }
17}
18
19#[async_trait]
20impl ObjSigner for RsaCPUObjectSigner {
21 fn public_key(&self) -> &PublicKey {
22 return &self.public_key;
23 }
24
25 async fn sign(&self, data: &[u8], sign_source: &SignatureSource) -> BuckyResult<ObjSignature> {
26 let sign = self.secret.sign(data)?;
27 Ok(ObjSignature::from((sign_source.clone(), 0, sign)))
28 }
29}
30
31pub async fn sign_and_set_named_object<D, S, N>(
35 signer: &S,
36 obj: &mut N,
37 sign_source: &SignatureSource,
38) -> BuckyResult<()>
39where
40 D: ObjectType,
41 D::DescType: RawEncode,
42 D::ContentType: RawEncode + BodyContent,
43 S: ObjSigner,
44 N: NamedObject<D>,
45{
46 let desc_sign = sign_named_object_desc(signer, obj, sign_source).await?;
47 obj.signs_mut().set_desc_sign(desc_sign);
48 if obj.body().is_some() {
49 let body_sign = sign_named_object_body(signer, obj, sign_source).await?;
50 obj.signs_mut().set_body_sign(body_sign);
51 }
52 Ok(())
53}
54
55pub async fn sign_and_set_named_object_desc<D, S, N>(
58 signer: &S,
59 obj: &mut N,
60 sign_source: &SignatureSource,
61) -> BuckyResult<()>
62where
63 D: ObjectType,
64 D::DescType: RawEncode,
65 D::ContentType: RawEncode + BodyContent,
66 S: ObjSigner,
67 N: NamedObject<D>,
68{
69 let sign = sign_named_object_desc(signer, obj, sign_source).await?;
70 obj.signs_mut().set_desc_sign(sign);
71 Ok(())
72}
73
74pub async fn sign_and_set_named_object_body<D, S, N>(
77 signer: &S,
78 obj: &mut N,
79 sign_source: &SignatureSource,
80) -> BuckyResult<()>
81where
82 D: ObjectType,
83 D::DescType: RawEncode,
84 D::ContentType: RawEncode + BodyContent,
85 S: ObjSigner,
86 N: NamedObject<D>,
87{
88 let body_sign = sign_named_object_body(signer, obj, sign_source).await?;
89 obj.signs_mut().set_body_sign(body_sign);
90 Ok(())
91}
92
93pub async fn sign_and_push_named_object<D, S, N>(
96 signer: &S,
97 obj: &mut N,
98 sign_source: &SignatureSource,
99) -> BuckyResult<()>
100where
101 D: ObjectType,
102 D::DescType: RawEncode,
103 D::ContentType: RawEncode + BodyContent,
104 S: ObjSigner,
105 N: NamedObject<D>,
106{
107 let desc_sign = sign_named_object_desc(signer, obj, sign_source).await?;
108 obj.signs_mut().push_desc_sign(desc_sign);
109 if obj.body().is_some() {
110 let body_sign = sign_named_object_body(signer, obj, sign_source).await?;
111 obj.signs_mut().push_body_sign(body_sign);
112 }
113 Ok(())
114}
115
116pub async fn sign_and_push_named_object_desc<D, S, N>(
118 signer: &S,
119 obj: &mut N,
120 sign_source: &SignatureSource,
121) -> BuckyResult<()>
122where
123 D: ObjectType,
124 D::DescType: RawEncode,
125 D::ContentType: RawEncode + BodyContent,
126 S: ObjSigner,
127 N: NamedObject<D>,
128{
129 let body_sign = sign_named_object_desc(signer, obj, sign_source).await?;
130 obj.signs_mut().push_desc_sign(body_sign);
131 Ok(())
132}
133
134pub async fn sign_and_push_named_object_body<D, S, N>(
136 signer: &S,
137 obj: &mut N,
138 sign_source: &SignatureSource,
139) -> BuckyResult<()>
140where
141 D: ObjectType,
142 D::DescType: RawEncode,
143 D::ContentType: RawEncode + BodyContent,
144 S: ObjSigner,
145 N: NamedObject<D>,
146{
147 let body_sign = sign_named_object_body(signer, obj, sign_source).await?;
148 obj.signs_mut().push_body_sign(body_sign);
149 Ok(())
150}
151
152pub async fn sign_named_object_desc<D, S, N>(
153 signer: &S,
154 obj: &N,
155 sign_source: &SignatureSource,
156) -> BuckyResult<ObjSignature>
157where
158 D: ObjectType,
159 D::DescType: RawEncode,
160 D::ContentType: RawEncode + BodyContent,
161 S: ObjSigner,
162 N: NamedObject<D>,
163{
164 let hash_value = obj.desc().raw_hash_value()?;
165 signer.sign(hash_value.as_slice(), sign_source).await
166}
167
168pub async fn sign_named_object_body<D, S, N>(
169 signer: &S,
170 obj: &N,
171 sign_source: &SignatureSource,
172) -> BuckyResult<ObjSignature>
173where
174 D: ObjectType,
175 D::DescType: RawEncode,
176 D::ContentType: RawEncode + BodyContent,
177 S: ObjSigner,
178 N: NamedObject<D>,
179{
180 let hash_value = obj.body().as_ref().unwrap().raw_hash_value()?;
181 signer.sign(hash_value.as_slice(), sign_source).await
182}
183
184pub struct AnyNamedObjectSignHelper;
185
186impl AnyNamedObjectSignHelper {
187 pub async fn sign_and_set<S>(
188 signer: &S,
189 obj: &mut AnyNamedObject,
190 sign_source: &SignatureSource,
191 ) -> BuckyResult<()>
192 where
193 S: ObjSigner,
194 {
195 Self::sign_and_set_desc(signer, obj, sign_source).await?;
196
197 if obj.has_body()? {
198 Self::sign_and_set_body(signer, obj, sign_source).await?;
199 }
200
201 Ok(())
202 }
203 pub async fn sign_and_set_desc<S>(
206 signer: &S,
207 obj: &mut AnyNamedObject,
208 sign_source: &SignatureSource,
209 ) -> BuckyResult<()>
210 where
211 S: ObjSigner,
212 {
213 let sign = Self::sign_desc(signer, obj, sign_source).await?;
214 obj.signs_mut().unwrap().set_desc_sign(sign);
215
216 Ok(())
217 }
218
219 pub async fn sign_and_set_body<S>(
222 signer: &S,
223 obj: &mut AnyNamedObject,
224 sign_source: &SignatureSource,
225 ) -> BuckyResult<()>
226 where
227 S: ObjSigner,
228 {
229 let sign = Self::sign_body(signer, obj, sign_source).await?;
230 obj.signs_mut().unwrap().set_body_sign(sign);
231
232 Ok(())
233 }
234 pub async fn sign_and_push<S>(
237 signer: &S,
238 obj: &mut AnyNamedObject,
239 sign_source: &SignatureSource,
240 ) -> BuckyResult<()>
241 where
242 S: ObjSigner,
243 {
244 Self::sign_and_push_desc(signer, obj, sign_source).await?;
245
246 if obj.has_body()? {
247 Self::sign_and_push_body(signer, obj, sign_source).await?;
248 }
249
250 Ok(())
251 }
252 pub async fn sign_and_push_desc<S>(
254 signer: &S,
255 obj: &mut AnyNamedObject,
256 sign_source: &SignatureSource,
257 ) -> BuckyResult<()>
258 where
259 S: ObjSigner,
260 {
261 let body_sign = Self::sign_desc(signer, obj, sign_source).await?;
262 obj.signs_mut().unwrap().push_desc_sign(body_sign);
263
264 Ok(())
265 }
266 pub async fn sign_and_push_body<S>(
268 signer: &S,
269 obj: &mut AnyNamedObject,
270 sign_source: &SignatureSource,
271 ) -> BuckyResult<()>
272 where
273 S: ObjSigner,
274 {
275 let body_sign = Self::sign_body(signer, obj, sign_source).await?;
276 obj.signs_mut().unwrap().push_body_sign(body_sign);
277
278 Ok(())
279 }
280
281 pub async fn sign_desc<S>(
282 signer: &S,
283 obj: &AnyNamedObject,
284 sign_source: &SignatureSource,
285 ) -> BuckyResult<ObjSignature>
286 where
287 S: ObjSigner,
288 {
289 let hash_value = obj.desc_hash()?;
290 signer.sign(hash_value.as_slice(), sign_source).await
291 }
292
293 pub async fn sign_body<S>(
294 signer: &S,
295 obj: &AnyNamedObject,
296 sign_source: &SignatureSource,
297 ) -> BuckyResult<ObjSignature>
298 where
299 S: ObjSigner,
300 {
301 match obj.body_hash()? {
302 Some(hash_value) => signer.sign(hash_value.as_slice(), sign_source).await,
303 None => {
304 let msg = format!("object has no body: {}", obj.calculate_id());
305 error!("{}", msg);
306 Err(BuckyError::new(BuckyErrorCode::NotFound, msg))
307 }
308 }
309 }
310}