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
pub mod error;
#[cfg(feature = "keriox")]
pub mod keri;
#[cfg(feature = "didkey")]
pub mod key;
#[cfg(feature = "jolo")]
pub mod jolo;
#[cfg(feature = "didkey")]
use key::DidKeyResolver;
#[cfg(feature = "keriox")]
use crate::keri::DidKeriResolver;
pub use did_key::{Document, KeyFormat, VerificationMethod};
use base58::FromBase58;
use error::Error;
use serde::{Serialize, Deserialize};
pub trait DdoResolver {
fn resolve(&self, did_url: &str) -> Result<Document, Error>;
}
pub trait DdoParser {
fn find_key_agreement(&self, pattern: &str) -> Option<KeyAgreement>;
fn find_public_key_for_curve(&self, curve: &str) -> Option<Vec<u8>>;
fn find_public_key_id_for_curve(&self, curve: &str) -> Option<String>;
fn find_public_key_controller_for_curve(&self, curve: &str) -> Option<String>;
}
impl DdoParser for Document {
fn find_key_agreement(&self, pattern: &str) -> Option<KeyAgreement> {
let agreements = self.key_agreement.clone();
if agreements.is_none() { return None; }
match agreements.unwrap()
.iter().find(|a| a.contains(pattern)) {
Some(a) => serde_json::from_str(a).unwrap_or(None),
None => None
}
}
fn find_public_key_for_curve(&self, curve: &str) -> Option<Vec<u8>> {
if let Some(k) = self.verification_method.iter().find(|m| {
m.key_type.contains(curve)
}) {
if let Some(key) = k.public_key.clone() {
match key {
KeyFormat::Base58(value) => Some(value.from_base58().unwrap()),
KeyFormat::Multibase(value) => Some(value),
KeyFormat::JWK(_value) => todo!()
}
} else { None }
} else {
None
}
}
fn find_public_key_id_for_curve(&self, curve: &str) -> Option<String> {
match get_public_key(self, curve) {
Some(kf) => match kf {
KeyFormat::JWK(key) => key.key_id,
_ => None
},
None => None
}
}
fn find_public_key_controller_for_curve(&self, curve: &str) -> Option<String> {
match self.verification_method.iter().find(|vm| vm.key_type.contains(curve)) {
Some(vm) => Some(vm.controller.to_owned()),
None => None
}
}
}
pub fn try_resolve_any(did_url: &str) -> Result<Document, Error> {
let re = regex::Regex::new(r"^((?P<prefix>did){1}:(?P<method>[a-z]*){1}:(?P<id>.+?))((?P<kerlid>\?kerl=)(?P<kerl>[a-zA-Z0-9]+?))?$").unwrap();
match re.captures(did_url) {
Some(caps) => {
match &caps["method"] {
#[cfg(feature = "didkey")]
"key" => DidKeyResolver{}.resolve(did_url)
.map_err(|e| error::Error::DidKeyError(e.to_string())),
#[cfg(feature = "keriox")]
"keri" => {
match &caps["kerlid"] {
"" => Err(error::Error::DidKeriError("kerl id not found".into())),
_ => match &caps["kerl"] {
"" => Err(error::Error::DidKeriError("kerl not found".into())),
_ => DidKeriResolver::new(&String::from_utf8_lossy(&base64_url::decode(&caps["kerl"])?))
.resolve(&format!("did:keri:{}", &caps["id"]))
}
}
},
_ => Err(error::Error::DidKeyError("not supported key url".into()))
}
},
None => Err(error::Error::DidKeyError("not a did url".into()))
}
}
pub fn resolve_any(did_url: &str) -> Option<Document> {
let re = regex::Regex::new(r"^((?P<prefix>did){1}:(?P<method>[a-z]*){1}:(?P<id>.+?))((?P<kerlid>\?kerl=)(?P<kerl>[a-zA-Z0-9]+?))?$").unwrap();
match re.captures(did_url) {
Some(caps) => {
let resolver: Box<dyn DdoResolver> = match &caps["method"] {
#[cfg(feature = "didkey")]
"key" => Box::new(DidKeyResolver{}),
#[cfg(feature = "keriox")]
"keri" => {
println!("kerl should be here: {}", &caps["kerl"]);
Box::new(
DidKeriResolver::new(
&String::from_utf8_lossy(&base64_url::decode(&caps["kerl"])
.unwrap_or(vec!()))))
},
#[cfg(feature = "didjolo")]
"jolo" => {},
#[cfg(feature = "didweb")]
"web" => {},
_ => return None
};
let parsed_url = format!("{}:{}:{}", &caps["prefix"], &caps["method"], &caps["id"]);
match resolver.resolve(&parsed_url) {
Ok(doc) => Some(doc),
Err(_) => None
}
},
None => None,
}
}
pub fn get_sign_and_crypto_keys<'a>(ddo: &'a Document) -> (Option<&'a [u8]>, Option<&'a [u8]>) {
let _sign_key= ddo.verification_method.iter().fold(
None,
|_, vm| vm.public_key.iter().find(
|k| match k {
KeyFormat::JWK(key) => key.curve == "Ed25519",
_ => false
}));
let _crypto_key = ddo.verification_method.iter().find(|vm| vm.key_type == "X25519");
(None, None)
}
pub(crate) fn get_public_key(doc: &Document, curve: &str) -> Option<KeyFormat> {
match doc.verification_method.iter().find(|m| {
match &m.public_key {
Some(KeyFormat::JWK(jwk)) => jwk.curve.contains(curve),
_ => false
}
}) {
Some(vm) => vm.public_key.clone(),
None => None
}
}
pub(crate) fn key_id_from_didurl(url: &str) -> String {
let re = regex::Regex::new(r"(?x)(?P<prefix>[did]{3}):(?P<method>[a-z]*):(?P<key_id>[a-zA-Z0-9]*)([:?/]?)(S)*??").unwrap();
match re.captures(url) {
Some(s) =>
match s.name("key_id") {
Some(name) =>
format!("#{}", name.as_str()),
None => String::default(),
},
None =>
String::default()
}
}
#[cfg(feature = "didkey")]
#[derive(Serialize, Deserialize, Debug)]
pub struct KeyAgreement {
pub id: String,
pub r#type: String,
pub controller: String,
#[serde(rename = "publicKeyBase58")]
pub public_key_base58: String,
}