tangled
alpha
login
or
join now
vielle.dev
/
tangled-on-commit
5
fork
atom
Listen to git commits for a specific repo and run a shell command
5
fork
atom
overview
issues
pulls
pipelines
Add helpful error messages
vielle.dev
6 months ago
bcbb66b8
46e72bb1
verified
This commit was signed with the committer's
known signature
.
vielle.dev
SSH Key Fingerprint:
SHA256:/4bvxqoEh9iMdjAPgcgAgXKZZQTROL3ULiPt6nH9RSs=
+55
-18
3 changed files
expand all
collapse all
unified
split
src
args.rs
did.rs
main.rs
+1
src/args.rs
···
92
92
if val.contains("/") {
93
93
let entries: Vec<_> = val.split("/").collect();
94
94
if entries.len() != 2 {
95
95
+
println!("Invalid argument: `{}` is malformed", val);
95
96
return Err(());
96
97
}
97
98
handle = Some(entries[0][1..].to_string());
+52
-10
src/did.rs
···
12
12
// create a txt resolver
13
13
let resolver = match Resolver::new(ResolverConfig::default(), ResolverOpts::default()) {
14
14
Ok(val) => val,
15
15
-
Err(_) => return Err(()),
15
15
+
Err(_) => {
16
16
+
println!(" Couldn't create a DNS resolver");
17
17
+
return Err(());
18
18
+
}
16
19
};
17
20
18
21
// resolve _atproto.handle to a TXT record
19
22
let txt_res = match resolver.txt_lookup("_atproto.".to_owned() + &handle) {
20
23
Ok(val) => val,
21
21
-
Err(_) => return Err(()), // collect all entries and convert to strings
24
24
+
Err(_) => {
25
25
+
println!(" Couldn't resolve to a TXT record");
26
26
+
return Err(());
27
27
+
} // collect all entries and convert to strings
22
28
}
23
29
.into_iter()
24
30
.map(|x| x.to_string())
···
32
38
// only 1 did= can exist
33
39
// https://atproto.com/specs/handle#:~:text=If%20multiple%20valid%20records%20with%20different%20DIDs%20are%20present,%20resolution%20should%20fail.
34
40
if did_res.len() != 1 {
41
41
+
println!(" Found too many DIDs for this handle");
35
42
return Err(());
36
43
}
37
44
···
39
46
}
40
47
41
48
fn get_http_did(handle: &String) -> Result<String, ()> {
42
42
-
println!(" Trying https for https://{}/.well-known/atproto-did", handle);
49
49
+
println!(
50
50
+
" Trying https for https://{}/.well-known/atproto-did",
51
51
+
handle
52
52
+
);
43
53
let res =
44
54
match reqwest::blocking::get("https://".to_owned() + handle + "/.well-known/atproto-did") {
45
55
Ok(val) => val,
46
46
-
Err(_) => return Err(()),
56
56
+
Err(_) => {
57
57
+
println!(" GET request failed");
58
58
+
return Err(());
59
59
+
}
47
60
};
48
61
49
62
// as per spec, non 2xx code means failure
50
63
if !res.status().is_success() {
64
64
+
println!(
65
65
+
" Got non 2xx status code: {}",
66
66
+
res.status().as_str()
67
67
+
);
51
68
return Err(());
52
69
}
53
70
54
71
let did_unparsed = match res.text() {
55
72
Ok(val) => val,
56
56
-
Err(_) => return Err(()),
73
73
+
Err(_) => {
74
74
+
println!(" Missing or malformed body response");
75
75
+
return Err(());
76
76
+
}
57
77
};
58
78
59
79
let did = did_unparsed.trim();
60
80
61
81
if !did.starts_with("did:") {
82
82
+
println!(" Did not find a DID");
62
83
return Err(());
63
84
};
64
85
return Ok(String::from(did));
···
67
88
fn parse_doc(did: String, text: String) -> Result<DidDoc, ()> {
68
89
let text_json = match json::parse(&text) {
69
90
Ok(val) => val,
70
70
-
Err(_) => return Err(()),
91
91
+
Err(_) => {
92
92
+
println!(" Malformed DID document");
93
93
+
return Err(());
94
94
+
}
71
95
};
72
96
73
97
for service in text_json["service"].members() {
···
86
110
}
87
111
}
88
112
113
113
+
println!(" Missing fields from DID document");
89
114
return Err(());
90
115
}
91
116
92
117
fn get_plc_doc(plc: &str) -> Result<DidDoc, ()> {
93
118
let res = match reqwest::blocking::get("https://plc.directory/did:plc:".to_owned() + plc) {
94
119
Ok(val) => val,
95
95
-
Err(_) => return Err(()),
120
120
+
Err(_) => {
121
121
+
println!(" GET request failed");
122
122
+
return Err(());
123
123
+
}
96
124
};
97
125
98
126
if !res.status().is_success() {
127
127
+
println!(" Got non 2xx status code: {}", res.status().as_str());
128
128
+
99
129
return Err(());
100
130
}
101
131
···
103
133
"did:plc:".to_owned() + plc,
104
134
match res.text() {
105
135
Ok(val) => val,
106
106
-
Err(_) => return Err(()),
136
136
+
Err(_) => {
137
137
+
println!(" Missing or malformed body response");
138
138
+
return Err(());
139
139
+
}
107
140
},
108
141
);
109
142
}
···
111
144
fn get_web_doc(web: &str) -> Result<DidDoc, ()> {
112
145
let res = match reqwest::blocking::get("https://".to_owned() + web + "/.well-known/did.json") {
113
146
Ok(val) => val,
114
114
-
Err(_) => return Err(()),
147
147
+
Err(_) => {
148
148
+
println!(" GET request failed");
149
149
+
return Err(());
150
150
+
}
115
151
};
116
152
117
153
if !res.status().is_success() {
154
154
+
println!(" Got non 2xx status code: {}", res.status().as_str());
118
155
return Err(());
119
156
}
120
157
···
122
159
"did:web:".to_owned() + web,
123
160
match res.text() {
124
161
Ok(val) => val,
125
125
-
Err(_) => return Err(()),
162
162
+
Err(_) => {
163
163
+
println!(" Missing or malformed body response");
164
164
+
return Err(());
165
165
+
}
126
166
},
127
167
);
128
168
}
···
135
175
if let Ok(did) = get_http_did(&handle) {
136
176
did
137
177
} else {
178
178
+
println!(" Could not get a DID");
138
179
return Err(());
139
180
}
140
181
};
···
145
186
} else if did.starts_with("did:web:") {
146
187
get_web_doc(&did[8..])
147
188
} else {
189
189
+
println!(" Could not get a DID document");
148
190
Err(())
149
191
};
150
192
+2
-8
src/main.rs
···
5
5
// load configuration
6
6
let config = match args::load_config() {
7
7
Ok(res) => res,
8
8
-
Err(_) => {
9
9
-
// q
10
10
-
return Err(());
11
11
-
}
8
8
+
Err(_) => return Err(()),
12
9
};
13
10
14
11
// resolve handle to did
15
12
println!("Resolving {}", config.handle);
16
13
let did_doc = match did::get_did(config.handle) {
17
14
Ok(res) => res,
18
18
-
Err(_) => {
19
19
-
// q
20
20
-
return Err(());
21
21
-
}
15
15
+
Err(_) => return Err(()),
22
16
};
23
17
// resolve did+repoName to knotserver
24
18