forked from ethereumjs/merkle-patricia-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secure-interface.js
39 lines (33 loc) · 863 Bytes
/
secure-interface.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
const ethUtil = require('ethereumjs-util')
module.exports = secureInterface
function secureInterface (trie) {
// overwrites
trie.copy = copy.bind(trie, trie.copy.bind(trie))
trie.get = get.bind(trie, trie.get.bind(trie))
trie.put = put.bind(trie, trie.put.bind(trie))
trie.del = del.bind(trie, trie.del.bind(trie))
}
// adds the interface when copying the trie
function copy (_super) {
var trie = _super()
secureInterface(trie)
return trie
}
function get (_super, key, cb) {
var hash = ethUtil.sha3(key)
_super(hash, cb)
}
// for a falsey value, use the original key
// to avoid double hashing the key
function put (_super, key, val, cb) {
if (!val) {
this.del(key, cb)
} else {
var hash = ethUtil.sha3(key)
_super(hash, val, cb)
}
}
function del (_super, key, cb) {
var hash = ethUtil.sha3(key)
_super(hash, cb)
}