-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
85 lines (69 loc) · 2.61 KB
/
mod.rs
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
#![allow(dead_code)]
use std::str::FromStr;
use catalyst_signed_doc::*;
pub fn test_metadata() -> (UuidV7, UuidV4, serde_json::Value) {
let uuid_v7 = UuidV7::new();
let uuid_v4 = UuidV4::new();
let metadata_fields = serde_json::json!({
"alg": Algorithm::EdDSA.to_string(),
"content-type": ContentType::Json.to_string(),
"content-encoding": ContentEncoding::Brotli.to_string(),
"type": uuid_v4.to_string(),
"id": uuid_v7.to_string(),
"ver": uuid_v7.to_string(),
"ref": {"id": uuid_v7.to_string()},
"reply": {"id": uuid_v7.to_string(), "ver": uuid_v7.to_string()},
"template": {"id": uuid_v7.to_string()},
"section": "$".to_string(),
"collabs": vec!["Alex1".to_string(), "Alex2".to_string()],
"campaign_id": {"id": uuid_v7.to_string()},
"election_id": uuid_v4.to_string(),
"brand_id": {"id": uuid_v7.to_string()},
"category_id": {"id": uuid_v7.to_string()},
});
(uuid_v7, uuid_v4, metadata_fields)
}
pub fn create_dummy_key_pair() -> anyhow::Result<(
ed25519_dalek::SigningKey,
ed25519_dalek::VerifyingKey,
IdUri,
)> {
let sk = create_signing_key();
let pk = sk.verifying_key();
let kid = IdUri::from_str(&format!(
"id.catalyst://cardano/{}/0/0",
base64_url::encode(pk.as_bytes())
))?;
Ok((sk, pk, kid))
}
pub fn create_dummy_doc(doc_type_id: Uuid) -> anyhow::Result<(CatalystSignedDocument, UuidV7)> {
let empty_json = serde_json::to_vec(&serde_json::json!({}))?;
let doc_id = UuidV7::new();
let doc = Builder::new()
.with_json_metadata(serde_json::json!({
"id": doc_id,
"type": doc_type_id,
"content-type": ContentType::Json.to_string(),
"template": { "id": doc_id.to_string() }
}))?
.with_decoded_content(empty_json.clone())
.build();
Ok((doc, doc_id))
}
pub fn create_signing_key() -> ed25519_dalek::SigningKey {
let mut csprng = rand::rngs::OsRng;
ed25519_dalek::SigningKey::generate(&mut csprng)
}
pub fn create_dummy_signed_doc(
with_metadata: Option<serde_json::Value>,
) -> anyhow::Result<(CatalystSignedDocument, ed25519_dalek::VerifyingKey, IdUri)> {
let (sk, pk, kid) = create_dummy_key_pair()?;
let content = serde_json::to_vec(&serde_json::Value::Null)?;
let (_, _, metadata) = test_metadata();
let signed_doc = Builder::new()
.with_decoded_content(content)
.with_json_metadata(with_metadata.unwrap_or(metadata))?
.add_signature(sk.to_bytes(), kid.clone())?
.build();
Ok((signed_doc, pk, kid))
}