Skip to content

Commit 5d74500

Browse files
committed
net: add get_name
1 parent f9fc246 commit 5d74500

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/net.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ where
157157
})
158158
}
159159

160+
/// get a kimap name from namehash
161+
pub fn get_name(namehash: &str, timeout: Option<u64>) -> anyhow::Result<String> {
162+
let res = Request::to(("our", "net", "distro", "sys"))
163+
.body(rmp_serde::to_vec(&NetAction::GetName(namehash.to_string())).unwrap())
164+
.send_and_await_response(timeout.unwrap_or(5))??;
165+
166+
let response = rmp_serde::from_slice::<NetResponse>(res.body())?;
167+
if let NetResponse::Name(name) = response {
168+
// is returning an option optimal?
169+
// getting an error for send/malformatted hash/not found seems better
170+
if let Some(name) = name {
171+
return Ok(name);
172+
} else {
173+
return Err(anyhow::anyhow!("name not found"));
174+
}
175+
} else {
176+
Err(anyhow::anyhow!("unexpected response: {:?}", response))
177+
}
178+
}
179+
160180
/// take a DNSwire-formatted node ID from chain and convert it to a String
161181
pub fn dnswire_decode(wire_format_bytes: &[u8]) -> Result<String, DnsDecodeError> {
162182
let mut i = 0;

0 commit comments

Comments
 (0)