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
pub use mainnet::*;
pub use subnet::*;
pub use utils::*;
mod mainnet;
mod subnet;
mod utils {
use std::{env, error::Error, fs, path::Path, str::FromStr};
use libp2p::{multiaddr::Protocol, Multiaddr};
pub fn get_ipfs_path() -> Box<Path> {
env::var("IPFS_PATH")
.map(|ipfs_path| Path::new(&ipfs_path).into())
.unwrap_or_else(|_| {
env::var("HOME")
.map(|home| Path::new(&home).join(".ipfs"))
.expect("could not determine home directory")
.into()
})
}
pub fn get_psk(path: Box<Path>) -> std::io::Result<Option<String>> {
let swarm_key_file = path.join("swarm.key");
match fs::read_to_string(swarm_key_file) {
Ok(text) => Ok(Some(text)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}
pub fn strip_peer_id(addr: &mut Multiaddr) {
let last = addr.pop();
match last {
Some(Protocol::P2p(peer_id)) => {
let mut addr = Multiaddr::empty();
addr.push(Protocol::P2p(peer_id));
println!(
"removing peer id {} so this address can be dialed by rust-libp2p",
addr
);
}
Some(other) => addr.push(other),
_ => {}
}
}
pub fn parse_legacy_multiaddr(text: &str) -> Result<Multiaddr, Box<dyn Error>> {
let sanitized = text
.split('/')
.map(|part| if part == "ipfs" { "p2p" } else { part })
.collect::<Vec<_>>()
.join("/");
let mut res = Multiaddr::from_str(&sanitized)?;
strip_peer_id(&mut res);
Ok(res)
}
#[cfg(test)]
mod tests {
#[test]
fn test() {
let a = 0;
let b = 0;
assert_eq!(a, b)
}
}
}