This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
publisher.js
286 lines (228 loc) · 8.39 KB
/
publisher.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict'
const PeerId = require('peer-id')
const Record = require('libp2p-record').Record
const { Key } = require('interface-datastore')
const series = require('async/series')
const errcode = require('err-code')
const debug = require('debug')
const log = debug('jsipfs:ipns:publisher')
log.error = debug('jsipfs:ipns:publisher:error')
const ipns = require('ipns')
const defaultRecordTtl = 60 * 60 * 1000
// IpnsPublisher is capable of publishing and resolving names to the IPFS routing system.
class IpnsPublisher {
constructor (routing, repo) {
this._routing = routing
this._repo = repo
}
// publish record with a eol
publishWithEOL (privKey, value, lifetime, callback) {
if (!privKey || !privKey.bytes) {
const errMsg = `one or more of the provided parameters are not defined`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_UNDEFINED_PARAMETER'))
}
PeerId.createFromPrivKey(privKey.bytes, (err, peerId) => {
if (err) {
callback(err)
}
this._updateOrCreateRecord(privKey, value, lifetime, peerId, (err, record) => {
if (err) {
return callback(err)
}
this._putRecordToRouting(record, peerId, privKey, callback)
})
})
}
// Accepts a keypair, as well as a value (ipfsPath), and publishes it out to the routing system
publish (privKey, value, callback) {
this.publishWithEOL(privKey, value, defaultRecordTtl, callback)
}
_putRecordToRouting (record, peerId, privKey, callback) {
if (!(peerId instanceof PeerId)) {
const errMsg = `peerId received is not valid`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_PEER_ID'))
}
const publicKey = peerId._pubKey
ipns.embedPublicKey(publicKey, record, (err, embedPublicKeyRecord) => {
if (err) {
return callback(err)
}
let keys
try {
keys = ipns.getIdKeys(peerId.id)
} catch (err) {
log.error(err)
return callback(err)
}
series([
(cb) => this._publishEntry(keys.ipnsKey, embedPublicKeyRecord || record, peerId, privKey, cb),
// Publish the public key if a public key cannot be extracted from the ID
// We will be able to deprecate this part in the future, since the public keys will be only in the peerId
(cb) => embedPublicKeyRecord ? this._publishPublicKey(keys.pkKey, publicKey, peerId, privKey, cb) : cb()
], (err) => {
if (err) {
log.error(err)
return callback(err)
}
callback(null, embedPublicKeyRecord || record)
})
})
}
_publishEntry (key, entry, peerId, privKey, callback) {
if (!(key instanceof Key)) {
const errMsg = `datastore key does not have a valid format`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_DATASTORE_KEY'))
}
let rec
try {
// Marshal record
const entryData = ipns.marshal(entry)
// Marshal to libp2p record
rec = new Record(key.toBuffer(), entryData, peerId)
} catch (err) {
log.error(err)
return callback(err)
}
rec.serializeSigned(privKey, (err, serializedRecord) => {
if (err) {
log.error(err)
return callback(err)
}
// TODO Routing - this should be replaced by a put to the DHT
this._repo.datastore.put(key, serializedRecord, (err, res) => {
if (err) {
const errMsg = `ipns record for ${key.toString()} could not be stored in the routing`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_STORING_IN_DATASTORE'))
}
log(`ipns record for ${key.toString()} was stored in the routing`)
callback(null, res)
})
})
}
_publishPublicKey (key, publicKey, peerId, privKey, callback) {
if (!(key instanceof Key)) {
const errMsg = `datastore key does not have a valid format`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_DATASTORE_KEY'))
}
if (!publicKey || !publicKey.bytes) {
const errMsg = `one or more of the provided parameters are not defined`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_UNDEFINED_PARAMETER'))
}
let rec
try {
// Marshal to libp2p record
rec = new Record(key.toBuffer(), publicKey.bytes, peerId)
} catch (err) {
log.error(err)
return callback(err)
}
rec.serializeSigned(privKey, (err, serializedRecord) => {
if (err) {
log.error(err)
return callback(err)
}
// TODO Routing - this should be replaced by a put to the DHT
this._repo.datastore.put(key, serializedRecord, (err, res) => {
if (err) {
const errMsg = `public key for ${key.toString()} could not be stored in the routing`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_STORING_IN_DATASTORE'))
}
log(`public key for ${key.toString()} was stored in the routing`)
callback(null, res)
})
})
}
// Returns the record this node has published corresponding to the given peer ID.
// If `checkRouting` is true and we have no existing record, this method will check the routing system for any existing records.
_getPublished (peerId, options, callback) {
if (!(peerId instanceof PeerId)) {
const errMsg = `peerId received is not valid`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_PEER_ID'))
}
options = options || {}
const checkRouting = !(options.checkRouting === false)
this._repo.datastore.get(ipns.getLocalKey(peerId.id), (err, dsVal) => {
let result
if (err) {
if (!err.notFound) {
const errMsg = `unexpected error getting the ipns record ${peerId.id} from datastore`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_UNEXPECTED_DATASTORE_RESPONSE'))
} else {
if (!checkRouting) {
return callback(null, null)
} else {
// TODO ROUTING - get from DHT
return callback(new Error('not implemented yet'))
}
}
}
if (Buffer.isBuffer(dsVal)) {
result = dsVal
} else {
const errMsg = `found ipns record that we couldn't convert to a value`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_IPNS_RECORD'))
}
// unmarshal data
try {
result = ipns.unmarshal(dsVal)
} catch (err) {
const errMsg = `found ipns record that we couldn't convert to a value`
log.error(errMsg)
return callback(null, null)
}
callback(null, result)
})
}
_updateOrCreateRecord (privKey, value, validity, peerId, callback) {
if (!(peerId instanceof PeerId)) {
const errMsg = `peerId received is not valid`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_PEER_ID'))
}
const getPublishedOptions = {
checkRouting: false // TODO ROUTING - change to true
}
this._getPublished(peerId, getPublishedOptions, (err, record) => {
if (err) {
return callback(err)
}
// Determinate the record sequence number
let seqNumber = 0
if (record && record.sequence !== undefined) {
seqNumber = record.value.toString() !== value ? record.sequence + 1 : record.sequence
}
// Create record
ipns.create(privKey, value, seqNumber, validity, (err, entryData) => {
if (err) {
const errMsg = `ipns record for ${value} could not be created`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_CREATING_IPNS_RECORD'))
}
// TODO IMPROVEMENT - set ttl (still experimental feature for go)
// Marshal record
const data = ipns.marshal(entryData)
// Store the new record
this._repo.datastore.put(ipns.getLocalKey(peerId.id), data, (err, res) => {
if (err) {
const errMsg = `ipns record for ${value} could not be stored in the datastore`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_STORING_IN_DATASTORE'))
}
log(`ipns record for ${value} was stored in the datastore`)
callback(null, entryData)
})
})
})
}
}
exports = module.exports = IpnsPublisher