-
Notifications
You must be signed in to change notification settings - Fork 0
/
blue.js
88 lines (82 loc) · 2.46 KB
/
blue.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const { bluzelle } = require('bluzelle');
const uuidv4 = require('uuid/v4')();
/*
#proxy
ssh -L [localport]:[remotehost]:[remoteport] [username]@[server]
ssh -L 5001:45
*/
/**
* Description:
* This is the main interface into the bluzelle
* decentralized kV
*
* func initialize: only used by the parent node (me)
* func read: given a key return the value from bluzelle
* func write: give a key, value pair write to bluzelle
* func stats: get stats about your node
* func getKeys: get all the keys stored in the bluzelle db
* func setWriters: set a writer to the db (not implemented, but should be)
* func get Writers: gets all the writer authorized to access the current db
*/
const api = bluzelle({
entry: 'ws://testnet.bluzelle.com:51010',
uuid: 'c74b4854-eb86-4629-8891-ab7e5e4c79e6',
private_pem: 'MHQCAQEEIFRwVj7ZrqnSNNJeMsz4qAKDIZyBgKH3fUhkdjQzpb+1oAcGBSuBBAAKoUQDQgAEDgftWwbXDSj3IgPYh4p1S/NQSUhVmBjnkejAOxKgxB30UyDIC5uzCOfrqCdilzBWtO0sS7unKggtitftXhEijA=='
});
var blue = {
initialize: async() => {
if (await api.hasDB() == false) {
await api.createDB();
console.log('blue initialized!')
return uuidv4
}
console.log('blue already initialized!')
},
read: async (key) => {
try {
return await api.quickread(key);
} catch (e) {
//value = await db.get(key);
value = 'haha';
await api.update(key, value);
return value;
}
},
write: async (key, value) => {
try {
await api.create(key, value);
await api.update(key, value);
} catch (e) {
console.log(e.message)
if(e.message === 'RECORD_EXISTS'){
return await blue.read(key)
}
}
},
stats: async() => {
const stats = {
uuid: uuidv4,
writers: await api.getWriters(),
pk: await api.publicKey(),
has: await api.hasDB(),
size: await api.size(),
keys: await api.keys(),
};
console.log(JSON.stringify(stats, null, 4));
return stats.pk;
},
getKeys: async() => {
//console.log(await api.keys())
return await api.keys()
},
setWriters: async() => {
await api.addWriters('MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE4V08tYSrs5O10YhGmNVpxCofn8q5liNDfj1I3IOkByrb0SHMNMr5FgBhsWYLI+ncRP6AAs1aH7E9PFPygjeOvw==');
},
getWriters: async() => {
console.log(await api.getWriters())
},
deleteField: async(key) => {
await api.delete(key);
}
}
module.exports = blue;