This repository was archived by the owner on Oct 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathid.rs
More file actions
88 lines (78 loc) · 2.76 KB
/
Copy pathid.rs
File metadata and controls
88 lines (78 loc) · 2.76 KB
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
use super::{with_ipfs, InvalidPeerId, NotImplemented, StringError};
use ipfs::{Ipfs, IpfsTypes, PeerId};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use warp::{query, Filter};
pub fn identity<T: IpfsTypes>(
ipfs: &Ipfs<T>,
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
with_ipfs(ipfs)
.and(optional_peer_id())
.and_then(identity_query)
}
fn optional_peer_id() -> impl Filter<Extract = (Option<PeerId>,), Error = warp::Rejection> + Copy {
query::<Query>().and_then(|mut q: Query| async move {
q.arg
.take()
.map(|arg| PeerId::from_str(&arg))
.map_or(Ok(None), |parsed| parsed.map(Some))
.map_err(|_| warp::reject::custom(InvalidPeerId))
})
}
// FIXME: /api/v0/id has argument `arg: PeerId` which is not implemented.
//
// https://docs.ipfs.io/reference/api/http/#api-v0-id
async fn identity_query<T: IpfsTypes>(
ipfs: Ipfs<T>,
peer: Option<PeerId>,
) -> Result<impl warp::Reply, warp::reject::Rejection> {
use multibase::Base::Base64Pad;
if peer.is_some() {
// TODO: this reply has Id, no public key, addresses and no versions. "no" as in empty
// string
return Err(warp::reject::custom(NotImplemented));
}
match ipfs.identity().await {
Ok((public_key, addresses, protocols)) => {
let peer_id = public_key.clone().into_peer_id();
let id = peer_id.to_string();
let public_key = Base64Pad.encode(public_key.into_protobuf_encoding());
let addresses = addresses.into_iter().map(|addr| addr.to_string()).collect();
let response = Response {
id,
public_key,
addresses,
agent_version: "rust-ipfs/0.1.0",
protocol_version: "ipfs/0.1.0",
protocols,
};
Ok(warp::reply::json(&response))
}
Err(e) => Err(warp::reject::custom(StringError::from(e))),
}
}
/// Query string of /api/v0/id?arg=peerid&format=notsure
#[derive(Debug, Deserialize)]
pub struct Query {
// the peer id to query
arg: Option<String>,
// this does not seem to be reacted to by go-ipfs
format: Option<String>,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct Response {
// PeerId
#[serde(rename = "ID")]
id: String,
// looks like Base64
public_key: String,
// Multiaddrs
addresses: Vec<String>,
// Multiaddr alike <agent_name>/<version>, like rust-ipfs/0.0.1
agent_version: &'static str,
// Multiaddr alike ipfs/0.1.0 ... not sure if there are plans to bump this anytime soon
protocol_version: &'static str,
// the list of supported libp2p protocols
protocols: Vec<String>,
}