-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipns.js
36 lines (35 loc) · 880 Bytes
/
ipns.js
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
// fetchPeerInfo will try to resolve a PeerID over IPNS to get their profile information, returning it as an object
async function fetchPeerInfo(id, timeout) {
if (timeout == undefined) {
timeout = 5000;
}
let peer = undefined;
for await (const name of ipfs.name.resolve(id, {timeout: timeout})) {
peer = name;
}
if (peer == undefined) {
return;
}
const content = [];
try {
for await (const chunk of ipfs.cat(peer, {timeout: timeout, length: 1024})) {
content.push(chunk);
}
} catch {
return;
}
if (content.length == 0) {
return;
}
try {
peer = JSON.parse(new TextDecoder().decode(content[0]));
} catch {
return;
}
return peer;
}
// publishProfile publishes our profile information to IPNS
async function publishProfile() {
let cid = await ipfs.add(JSON.stringify({nick: currentNick, img: currentImg}));
await ipfs.name.publish(cid.cid);
}