-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfsNodeHandler.js
43 lines (40 loc) · 1.14 KB
/
ipfsNodeHandler.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
let ipfsHttpClient = require('ipfs-http-client');
let ipfs = ipfsHttpClient('http://localhost:5001');
let genCert = async (candidatePubKey, cert) => {
try {
let results = [];
if(ipfs) {
const fileObj = [{
path: "certificates/" + candidatePubKey + ".cert",
content: JSON.stringify(cert)
}];
for await (const result of ipfs.add(fileObj)){
console.log("result");
results.push(result);
}
} else {
results = new Error("set HttpProvider for ipfs first");
}
return results;
} catch(err) {
return new Error(err);
}
};
let getCert = async (cid) => {
try {
for await (const file of ipfs.get(cid)) {
const BufferList = require('bl/BufferList');
const content = new BufferList();
for await (const chunk of file.content) {
content.append(chunk)
}
return JSON.parse(content.toString());
}
} catch(err) {
return new Error(err);
}
};
module.exports = {
genCert,
getCert
};