-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
62 lines (51 loc) · 1.54 KB
/
test.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
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
import IPFS from "ipfs-core";
import { existsSync } from "fs";
import { join } from "path";
import { homedir } from "os";
import PeerId from "peer-id";
import { execSync } from "child_process";
async function main() {
let peerID = await PeerId.create({ bits: 256, keyType: "secp256k1" });
let jsdir = join(homedir(), ".jsipfs");
if (existsSync(jsdir)) {
console.log("removing old repo");
execSync(`rm -rf ${jsdir}`);
}
const TCP_HOST = process.env.TCP_HOST || "0.0.0.0";
const IPFS_SWARM_TCP_PORT = 6601;
const IPFS_SWARM_WS_PORT = 6602;
let options = {
privateKey: peerID,
EXPERIMENTAL: { ipnsPubsub: true },
repo: jsdir,
config: {
Addresses: {
Swarm: [
`/ip4/${TCP_HOST}/tcp/${IPFS_SWARM_TCP_PORT}`,
`/ip4/${TCP_HOST}/tcp/${IPFS_SWARM_WS_PORT}/ws`,
],
},
},
};
const node = await IPFS.create(options);
let peers = await node.swarm.peers();
console.log(peers);
const version = await node.version();
const fileAdded = await node.add({
path: "hello.txt",
content: `TEST FILE ${new Date().toISOString()}`,
});
console.log("Added file:", fileAdded, fileAdded.path, fileAdded.cid);
const addr = `/ipfs/${fileAdded.cid}`;
let res = await node.name.publish(addr, {
allowOffline: true,
resolve: false,
lifetime: "2h",
});
console.log(`https://gateway.ipfs.io${addr}`);
console.log(`https://gateway.ipfs.io/ipns/${res.name}`);
for await (const name of node.name.resolve(`/ipns/${res.name}`)) {
console.log(name);
}
}
main();