forked from mafintosh/hypercore-multi-key
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
54 lines (47 loc) · 1.37 KB
/
storage.js
File metadata and controls
54 lines (47 loc) · 1.37 KB
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
const ras = require('random-access-storage')
const ram = require('random-access-memory')
const crypto = require('@ddatabase/crypto')
module.exports = storage
function storage (feed, keyPair) {
return function createFile (name) {
if (name === 'data') return proxy(feed._storage.data)
if (name === 'bitfield') return proxy(feed._storage.bitfield)
if (name === 'tree') return proxy(feed._storage.tree)
if (name === 'signatures') return createSignature(feed, keyPair)
if (name === 'key') return ram(keyPair.key || keyPair.publicKey)
return ras({ write: nullWrite })
}
}
function nullWrite (req) {
req.callback(null, null)
}
function proxy (s) {
return ras({
write (req) {
s.write(req.offset, req.data, req.callback.bind(req))
},
read (req) {
s.read(req.offset, req.size, req.callback.bind(req))
},
stat (req) {
s.stat(req.callback.bind(req))
},
del (req) {
s.del(req.offset, req.size, req.callback.bind(req))
}
})
}
function createSignature (feed, keyPair) {
const secretKey = keyPair.secretKey
return ras({
write: nullWrite,
read: function (req) {
const offset = req.offset
const i = (offset - 32) / 64
feed.rootHashes(i, function (err, roots) {
if (err) return req.callback(err)
req.callback(null, crypto.sign(crypto.tree(roots), secretKey))
})
}
})
}