-
Notifications
You must be signed in to change notification settings - Fork 5
/
identityDB.js
259 lines (215 loc) · 6.56 KB
/
identityDB.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var debug = require('debug')('identityDB')
var typeforce = require('typeforce')
var extend = require('xtend')
var map = require('map-stream')
var levelup = require('levelup')
var Identity = require('@tradle/identity').Identity
var constants = require('@tradle/constants')
var TYPE = constants.TYPE
var ROOT_HASH = constants.ROOT_HASH
var CUR_HASH = constants.CUR_HASH
var Simple = require('logbase').Simple
var rebuf = require('./rebufEncoding')
var EventType = require('./eventType')
var DEBUG = require('./debug')
var ENTRY_TIMEOUT = DEBUG ? false : 5000
var prefixes = {
fingerprint: 'f'
}
prefixes[CUR_HASH] = CUR_HASH
prefixes[ROOT_HASH] = ROOT_HASH
module.exports = function mkIdentityDB (path, options) {
typeforce('String', path)
typeforce({
leveldown: 'Function',
log: 'Log',
keeper: 'Object'
}, options)
var log = options.log
var db = levelup(path, {
db: options.leveldown,
valueEncoding: rebuf
})
db = Simple({
db: db,
log: log,
process: processEntry,
timeout: typeof options.timeout === 'undefined' ? ENTRY_TIMEOUT : options.timeout,
autostart: options.autostart
})
var hashes = []
var keeper = options.keeper
var createReadStream = db.createReadStream
db.createReadStream = function (options) {
options = options || {}
return createReadStream.call(db, extend(options, {
start: prefixes[ROOT_HASH],
end: prefixes[ROOT_HASH] + '\xff'
}))
.pipe(map(function (data, cb) {
if (options.keys !== false) {
var key = options.values === false ? data : data.key
key = unprefixKey(ROOT_HASH, key)
if (options.values === false) data = key
else data.key = key
}
if (options.values === false) return cb(null, data)
var id = options.keys === false ? data : data.value
getContents(id, function (err, contents) {
if (err) return cb(err)
var ret = options.keys === false ?
contents :
{
key: data.key,
value: contents
}
cb(null, ret)
})
}))
}
// db.createKeyStream = function (options) {
// return db.createReadStream(extend({ values: false }, options))
// }
// db.createValueStream = function (options) {
// return db.createReadStream(extend({ keys: false }, options))
// }
db.query = function (query, cb) {
// by one and only one prop
if (Object.keys(query).length !== 1 ||
!(query.fingerprint || query[ROOT_HASH] || query[CUR_HASH])) {
return process.nextTick(function () {
cb(new Error('specify "fingerprint", ' + ROOT_HASH + ' OR ' + CUR_HASH))
})
}
if (query.fingerprint) {
db.byFingerprint(query.fingerprint, cb)
} else if (query[ROOT_HASH]) {
db.byRootHash(query[ROOT_HASH], cb)
} else if (query[CUR_HASH]) {
db.byCurHash(query[CUR_HASH], cb)
}
}
db.byFingerprint = function (fingerprint, cb) {
db.rootHashByFingerprint(fingerprint, function (err, rootHash) {
if (err) return cb(err)
return db.byRootHash(rootHash, cb)
})
}
db.rootHashByFingerprint = db.liveOnly(function (fingerprint, cb) {
db.get(prefixKey('fingerprint', fingerprint), cb)
})
db.byRootHash = db.liveOnly(function (rootHash, cb) {
db.get(prefixKey(ROOT_HASH, rootHash), function (err, id) {
if (err) return cb(err)
getContents(id, cb)
})
})
db.byCurHash = db.liveOnly(function (curHash, cb) {
db.get(prefixKey(CUR_HASH, curHash), function (err, id) {
if (err) return cb(err)
getContents(id, cb)
})
})
return db
function getContents (id, cb) {
log.get(id, function (err, entry) {
if (err) return cb(err)
var curHash = entry.get(CUR_HASH)
keeper.getOne(curHash)
.then(function (buf) {
var ret = {
identity: toJSON(buf)
}
ret[ROOT_HASH] = entry.get(ROOT_HASH)
ret[CUR_HASH] = curHash
cb(null, ret)
})
.catch(cb)
})
}
function processEntry (entry, cb) {
if (entry.get(TYPE) !== Identity.TYPE ||
entry.get('type') !== EventType.chain.readSuccess) {
return cb()
}
var rootHash = entry.get(ROOT_HASH)
if (!rootHash) throw new Error('missing root hash ("' + ROOT_HASH + '")')
var curHash = entry.get(CUR_HASH)
if (!curHash) throw new Error('missing current hash ("' + CUR_HASH + '")')
db.get(prefixKey(CUR_HASH, curHash), function (err, val) {
if (err){
if (!err.notFound) throw new Error('database choked')
save()
} else {
cb()
}
})
function save () {
keeper.getOne(curHash)
.then(function (buf) {
var identityJSON = toJSON(buf)
var batch = identityJSON.pubkeys.map(function (k) {
return {
type: 'put',
key: prefixKey('fingerprint', k.fingerprint),
value: rootHash
}
})
hashes.push(rootHash)
batch.push({
type: 'put',
key: prefixKey(ROOT_HASH, rootHash),
value: entry.id()
}, {
type: 'put',
key: prefixKey(CUR_HASH, curHash),
value: entry.id()
})
debug(db.name, 'stored identity', curHash)
cb(batch)
// return Q.ninvoke(db, 'batch', batch)
})
.catch(function (err) {
if (err instanceof TypeError ||
err instanceof ReferenceError) {
debug('rethrowing ReferenceError', err, err.stack)
throw err
}
debug('unable to get identity from keeper', err)
cb()
})
}
}
}
function toJSON (buf) {
if (Buffer.isBuffer(buf)) buf = buf.toString('binary')
if (typeof buf === 'string') buf = JSON.parse(buf)
return buf
}
function prefixKey (type, key) {
if (!(type in prefixes)) throw new Error('no such type: ' + type)
return prefixes[type] + '!' + key
}
function unprefixKey (type, key) {
if (!(type in prefixes)) throw new Error('no such type: ' + type)
if (key.indexOf(type) !== 0) throw new Error('key has wrong prefix')
return key.slice(type.length + 1)
}
// function logify (cb, errsOnly) {
// return function (err) {
// if (errsOnly) {
// if (err) debug(err)
// } else {
// debug(arguments)
// }
// cb(arguments[0], arguments[1], arguments[2], arguments[3])
// }
// }
// function liveOnly (db, fn, ctx) {
// return function () {
// var args = arguments
// db.onLive(function () {
// return fn.apply(ctx, args)
// })
// }
// }