-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.js
69 lines (59 loc) · 1.71 KB
/
block.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
63
64
65
66
67
68
69
import { v4 as uuidv4 } from "uuid";
class Storage {
constructor(Ipfs, OrbitDB, database) {
this.Ipfs = Ipfs;
this.OrbitDB = OrbitDB;
this.database = database;
}
async create() {
this.node = await this.Ipfs.create({
preload: { enabled: false },
repo: "./ipfs",
EXPERIMENTAL: { pubsub: true },
config: {
Bootstrap: [],
// Addresses: { Swarm: [
// "/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star",
// "/dns4/star-signal.cloud.ipfs.team/tcp/443/wss/p2p-webrtc-star",
// ] },
},
});
this._init();
}
async _init() {
this.orbitdb = await this.OrbitDB.createInstance(this.node);
this.docStore = await this.orbitdb.keyvalue(this.database);
// this.docStore = await this.orbitdb.open(
// "/orbitdb/zdpuAyvWcC1VUeJEz2AbsCXzEshTy7xPn8H2NuEdftVzSADnN/users"
// );
// /orbitdb/zdpuAyvWcC1VUeJEz2AbsCXzEshTy7xPn8H2NuEdftVzSADnN/users
// console.log("this.docStore", this.docStore);
await this.docStore.load();
this.onready ? this.onready() : null;
}
async addNew({ data }) {
var id = uuidv4();
var data = await this.docStore.put(id, JSON.stringify(data));
return id;
}
async getById(id) {
var data = await this.docStore.get(id);
try {
return JSON.parse(data);
} catch (error) {
return {};
}
}
async deleteByid(id) {
return await this.docStore.del(id);
}
}
import * as Ipfs from "ipfs";
import OrbitDB from "orbit-db";
// /orbitdb/zdpuAyvWcC1VUeJEz2AbsCXzEshTy7xPn8H2NuEdftVzSADnN/users
const Blockchain = new Storage(
Ipfs,
OrbitDB,
"/orbitdb/zdpuAyvWcC1VUeJEz2AbsCXzEshTy7xPn8H2NuEdftVzSADnN/users"
);
export default Blockchain;