Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit 3cb2db4

Browse files
committed
feat: implement /resolve
Signed-off-by: ljedrz <[email protected]>
1 parent d3c51df commit 3cb2db4

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

conformance/test/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ const factory = createFactory(options)
3434
//
3535
tests.miscellaneous(factory, { skip: [
3636
'dns',
37-
'resolve',
37+
// the cidBase param is not implemented yet
38+
'should resolve an IPFS hash and return a base64url encoded CID in path',
39+
// different Cid, the /path/to/testfile.txt suffix shouldn't be there
40+
'should resolve an IPFS path link',
41+
// different Cid, missing "/path/to" in the middle
42+
'should resolve up to the last node across multiple nodes',
43+
// expected "true", got "false"
44+
'should resolve an IPNS DNS link',
45+
// HTTP: not implemented
46+
'should resolve IPNS link recursively',
3847
// these cause a hang 20% of time:
3948
'should respect timeout option when getting the node id',
4049
'should respect timeout option when getting the node version',

http/src/v0.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod block;
66
pub mod dag;
77
pub mod dht;
88
pub mod id;
9+
pub mod ipns;
910
pub mod pin;
1011
pub mod pubsub;
1112
pub mod refs;
@@ -90,6 +91,7 @@ pub fn routes<T: IpfsTypes>(
9091
and_boxed!(warp::path!("get"), root_files::get(ipfs)),
9192
and_boxed!(warp::path!("refs" / "local"), refs::local(ipfs)),
9293
and_boxed!(warp::path!("refs"), refs::refs(ipfs)),
94+
and_boxed!(warp::path!("resolve"), ipns::resolve(ipfs)),
9395
warp::path!("version")
9496
.and(query::<version::Query>())
9597
.and_then(version::version),

http/src/v0/ipns.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use crate::v0::support::{with_ipfs, StringError, StringSerialized};
2+
use ipfs::{Ipfs, IpfsPath, IpfsTypes};
3+
use serde::{Deserialize, Serialize};
4+
use warp::{query, Filter, Rejection, Reply};
5+
6+
#[derive(Debug, Deserialize)]
7+
pub struct ResolveQuery {
8+
// the name to resolve
9+
arg: StringSerialized<IpfsPath>,
10+
#[serde(rename = "dht-record-count")]
11+
dht_record_count: Option<usize>,
12+
#[serde(rename = "dht-timeout")]
13+
dht_timeout: Option<String>,
14+
}
15+
16+
pub fn resolve<T: IpfsTypes>(
17+
ipfs: &Ipfs<T>,
18+
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
19+
with_ipfs(ipfs)
20+
.and(query::<ResolveQuery>())
21+
.and_then(resolve_query)
22+
}
23+
24+
async fn resolve_query<T: IpfsTypes>(
25+
ipfs: Ipfs<T>,
26+
query: ResolveQuery,
27+
) -> Result<impl Reply, Rejection> {
28+
let ResolveQuery { arg, .. } = query;
29+
let name = arg.into_inner();
30+
let path = ipfs
31+
.resolve(&name)
32+
.await
33+
.map_err(StringError::from)?
34+
.to_string();
35+
36+
let response = ResolveResponse { path };
37+
38+
Ok(warp::reply::json(&response))
39+
}
40+
41+
#[derive(Debug, Serialize)]
42+
#[serde(rename_all = "PascalCase")]
43+
struct ResolveResponse {
44+
path: String,
45+
}

src/ipns/dns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use thiserror::Error;
1818
#[error("no dnslink entry")]
1919
pub struct DnsLinkError;
2020

21-
type FutureAnswer = Pin<Box<dyn Future<Output = Result<Answer, io::Error>>>>;
21+
type FutureAnswer = Pin<Box<dyn Future<Output = Result<Answer, io::Error>> + Send>>;
2222

2323
pub struct DnsLinkFuture {
2424
query: SelectOk<FutureAnswer>,

src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@ impl<Types: IpfsTypes> Ipfs<Types> {
852852
refs::iplds_refs(self, iplds, max_depth, unique)
853853
}
854854

855+
pub async fn resolve(&self, path: &IpfsPath) -> Result<IpfsPath, Error> {
856+
self.ipns().resolve(path).await
857+
}
858+
855859
/// Exit daemon.
856860
pub async fn exit_daemon(self) {
857861
// FIXME: this is a stopgap measure needed while repo is part of the struct Ipfs instead of

0 commit comments

Comments
 (0)