-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrevocation-list.js
114 lines (86 loc) · 2.42 KB
/
revocation-list.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const hyperswarm = require('hyperswarm')
const hypercore = require('hypercore')
const pump = require('pump')
const path = require('path')
const keys = require('./lib/keygen')
const EventEmitter = require('events')
module.exports = class RevocationList extends EventEmitter {
constructor (storage, certId, opts) {
super()
this.certId = typeof certId === 'string' ? Buffer.from(certId, 'hex') : certId
this.storage = storage
this.key = opts ? opts.key : null
this.feed = null
this.swarm = null
this.revokedKeys = []
// this.ready = thunky(this._ready.bind(this))
}
_ready (cb) {
}
create (cb) {
const self = this
const feed = hypercore(path.join(this.storage, this.certId.toString('hex')), null, {
valueEncoding: 'binary'
})
this.key = feed.key
const swarm = hyperswarm()
swarm.join(self.certId, {
lookup: true,
announce: true
})
feed.on('ready', () => {
swarm.on('connection', function (socket, info) {
pump(socket, feed.replicate(true, { live: true }), socket)
})
self.feed = feed
self.swarm = swarm
cb()
})
}
init (cb) {
const self = this
const feed = hypercore(path.join(this.storage, this.certId.toString('hex')), this.key, {
valueEncoding: 'binary'
})
if (this.key === null) this.key = feed.key
const swarm = hyperswarm()
swarm.join(self.certId, {
lookup: true,
announce: true
})
feed.on('ready', () => {
swarm.on('connection', function (socket, info) {
pump(socket, feed.replicate(false, { live: true }), socket)
cb()
})
self.feed = feed
self.swarm = swarm
})
feed.on('download', function (i, id) {
// TODO: remove hardcoded depth
// self.emit('revocation', id)
})
}
serialize (buf, offset) {
if (!buf) buf = Buffer.alloc(4 + this.revokedKeys.length * 32)
if (!offset) offset = 0
buf.writeUInt32LE(this.revokedKeys.length, offset)
offset += 4
for (const key of revokedKeys) {
buf.set(key, offset)
offset += 32
}
this.serialize.bytes = offset - startIndex
return buf
}
add (key, cb) {
this.feed.append(key, cb)
}
// TODO: specify time since last sync / live sync then check
has (key, opts, cb) {
for (const revoked of this.revokedKeys) {
if (Buffer.compare(key, revoked) === 0) return true
}
return false
}
}