-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_role.js
75 lines (58 loc) · 2.57 KB
/
2_role.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
// const KVStore = require('@geekberry/solidity-kvstore');
const KVStore = require('../');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// ============================================================================
async function main() {
const kvStoreAdmin = new KVStore({
// url: 'http://testnet-jsonrpc.conflux-chain.org:12537', // conflux test-net
url: 'http://localhost:12537',
// KVStore contract address
address: '0x8bfc6fd9437cf1879fb84aade867b6e81efb5631',
// admin privateKey
account: '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
// for debug
logger: {
info: v => console.log(JSON.stringify(v, null, 2)),
error: v => console.error(JSON.stringify(v, null, 2)),
},
});
console.log(await kvStoreAdmin.getRole(kvStoreAdmin.account));
// NamedTuple(isReader,isWriter,isAdmin) [ true, true, true ]
console.log(await kvStoreAdmin.set('key', 'value')); // return transaction hash
await sleep(5000); // you might need wait seconds here
console.log(await kvStoreAdmin.get('key')); // as you see, set success!
// <Buffer 76 61 6c 75 65>
console.log(await kvStoreAdmin.removeWriter(kvStoreAdmin.account)); // return transaction hash
await sleep(5000); // you might need wait seconds here
console.log(await kvStoreAdmin.getRole(kvStoreAdmin.account));
// NamedTuple(isReader,isWriter,isAdmin) [ true, false, true ] // not writer any more
try {
console.log(await kvStoreAdmin.set('key', 'value')); // can not set
} catch (e) {
console.error(e); // set failed
}
/* set other user to read (or write) */
const kvStoreReader = new KVStore({
url: 'http://localhost:12537',
address: '0x8bfc6fd9437cf1879fb84aade867b6e81efb5631',
account: '0x0123456789012345678901234567890123456789', // account address is enough for read
});
console.log(await kvStoreAdmin.getRole(kvStoreReader.account));
// NamedTuple(isReader,isWriter,isAdmin) [ false, false, false ]
try {
await kvStoreReader.get('key');
} catch (e) {
console.error(e.message); // Permissions error!
}
await sleep(5000); // you might need wait seconds here
console.log(await kvStoreAdmin.addReader(kvStoreReader.account));
await sleep(5000); // you might need wait seconds here
console.log(await kvStoreAdmin.getRole(kvStoreReader.account));
// NamedTuple(isReader,isWriter,isAdmin) [ true, false, false ]
console.log(await kvStoreReader.get('key')); // can read
// <Buffer 76 61 6c 75 65>
// use `await kvStoreAdmin.addWrite(address)` to allow account to write
}
main();