-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
86 lines (71 loc) · 2.57 KB
/
index.ts
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
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Fluence, testNet, Relay } from "@fluencelabs/js-client";
import { put, get_from, set_timeout } from "./generated/export.js";
import { multiaddr } from "@multiformats/multiaddr";
import { create } from "kubo-rpc-client";
import all from "it-all";
import uint8ArrayConcat from "uint8arrays/concat.js";
// Multi address of the IPFS node
// we will work with through the Fluence Network
const IPFS_MULTIADDR = multiaddr("/dns4/ipfs.fluence.dev/tcp/5001");
/**
* @param environment - array of fluence network nodes (two are needed)
* @note Pass addresses of local nodes to experiment locally
*/
async function main(environment: Relay[]) {
const relay = environment[0];
const node = environment[1];
const ipfs = await create({ url: IPFS_MULTIADDR });
console.log("📗 Created IPFS HTTP Client");
const content = "Hola, Fluence!";
const encoder = new TextEncoder();
const added = await ipfs.add(encoder.encode(content));
console.log("📗 Uploaded content, got CID:", added.cid.toString());
let stream = await ipfs.cat(added.path);
let data = uint8ArrayConcat(await all(stream));
const decoder = new TextDecoder();
console.log("📗 Retrieved content: ", decoder.decode(data));
await Fluence.connect(relay);
const client = Fluence.getClient();
console.log(
"📗 Created a Fluence Peer %s with Relay %s",
client.getPeerId(),
client.getRelayPeerId()
);
// default IPFS timeout is 1 sec,
// set to 10 secs to retrieve file from remote node
await set_timeout(node.peerId, 10);
console.log("📘 Ipfs.set_timeout");
let getResult = await get_from(
node.peerId,
added.cid.toString(),
IPFS_MULTIADDR.toString(),
{ ttl: 20000 }
);
console.log("📘 Ipfs.get_from", getResult);
let putResult = await put(node.peerId, getResult.path, {
ttl: 20000,
});
console.log("📘 Ipfs.put", putResult);
await ipfs.stop();
}
main(testNet)
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});