This repository was 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
/
Copy pathpublisher.js
297 lines (237 loc) · 8.38 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
287
288
289
290
291
292
293
294
295
296
297
'use strict'
const PeerId = require('peer-id')
const { Key, Errors } = require('interface-datastore')
const errcode = require('err-code')
const debug = require('debug')
const log = Object.assign(debug('ipfs:ipns:publisher'), {
error: debug('ipfs:ipns:publisher:error')
})
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayEquals = require('uint8arrays/equals')
const ipns = require('ipns')
/**
* @typedef {import('libp2p-crypto').PrivateKey} PrivateKey
* @typedef {import('libp2p-crypto').PublicKey} PublicKey
* @typedef {import('ipns').IPNSEntry} IPNSEntry
*/
const ERR_NOT_FOUND = Errors.notFoundError().code
const defaultRecordLifetime = 60 * 60 * 1000
// IpnsPublisher is capable of publishing and resolving names to the IPFS routing system.
class IpnsPublisher {
/**
* @param {import('ipfs-core-types/src/utils').BufferStore} routing
* @param {import('interface-datastore').Datastore} datastore
*/
constructor (routing, datastore) {
this._routing = routing
this._datastore = datastore
}
/**
* Publish record with a eol
*
* @param {PrivateKey} privKey
* @param {Uint8Array} value
* @param {number} lifetime
*/
async publishWithEOL (privKey, value, lifetime) {
if (!privKey || !privKey.bytes) {
throw errcode(new Error('invalid private key'), 'ERR_INVALID_PRIVATE_KEY')
}
const peerId = await PeerId.createFromPrivKey(privKey.bytes)
const record = await this._updateOrCreateRecord(privKey, value, lifetime, peerId)
return this._putRecordToRouting(record, peerId)
}
/**
* Accepts a keypair, as well as a value (ipfsPath), and publishes it out to the routing system
*
* @param {PrivateKey} privKey
* @param {Uint8Array} value
*/
publish (privKey, value) {
return this.publishWithEOL(privKey, value, defaultRecordLifetime)
}
/**
* @param {IPNSEntry} record
* @param {PeerId} peerId
*/
async _putRecordToRouting (record, peerId) {
if (!(PeerId.isPeerId(peerId))) {
const errMsg = 'peerId received is not valid'
log.error(errMsg)
throw errcode(new Error(errMsg), 'ERR_INVALID_PEER_ID')
}
// @ts-ignore - accessing private property isn't allowed
const publicKey = peerId._pubKey
const embedPublicKeyRecord = await ipns.embedPublicKey(publicKey, record)
const keys = ipns.getIdKeys(peerId.toBytes())
await this._publishEntry(keys.routingKey, embedPublicKeyRecord || record)
// Publish the public key to support old go-ipfs nodes that are looking for it in the routing
// We will be able to deprecate this part in the future, since the public keys will be only
// in IPNS record and the peerId.
await this._publishPublicKey(keys.routingPubKey, publicKey)
return embedPublicKeyRecord || record
}
/**
* @param {Key} key
* @param {IPNSEntry} entry
*/
async _publishEntry (key, entry) {
if (!(Key.isKey(key))) {
const errMsg = 'datastore key does not have a valid format'
log.error(errMsg)
throw errcode(new Error(errMsg), 'ERR_INVALID_DATASTORE_KEY')
}
let entryData
try {
// Marshal record
entryData = ipns.marshal(entry)
} catch (err) {
log.error(err)
throw err
}
// Add record to routing (buffer key)
try {
const res = await this._routing.put(key.uint8Array(), entryData)
log(`ipns record for ${uint8ArrayToString(key.uint8Array(), 'base64')} was stored in the routing`)
return res
} catch (err) {
const errMsg = `ipns record for ${uint8ArrayToString(key.uint8Array(), 'base64')} could not be stored in the routing`
log.error(errMsg)
log.error(err)
throw errcode(new Error(errMsg), 'ERR_PUTTING_TO_ROUTING')
}
}
/**
* @param {Key} key
* @param {PublicKey} publicKey
*/
async _publishPublicKey (key, publicKey) {
if ((!Key.isKey(key))) {
const errMsg = 'datastore key does not have a valid format'
log.error(errMsg)
throw 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)
throw errcode(new Error(errMsg), 'ERR_UNDEFINED_PARAMETER')
}
// Add public key to routing (buffer key)
try {
const res = await this._routing.put(key.uint8Array(), publicKey.bytes)
log(`public key for ${uint8ArrayToString(key.uint8Array(), 'base64')} was stored in the routing`)
return res
} catch (err) {
const errMsg = `public key for ${uint8ArrayToString(key.uint8Array(), 'base64')} could not be stored in the routing`
log.error(errMsg)
log.error(err)
throw errcode(new Error(errMsg), 'ERR_PUTTING_TO_ROUTING')
}
}
/**
* 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.
*
* @param {PeerId} peerId
* @param {object} options
* @param {boolean} [options.checkRouting]
*/
async _getPublished (peerId, options = {}) {
if (!(PeerId.isPeerId(peerId))) {
const errMsg = 'peerId received is not valid'
log.error(errMsg)
throw errcode(new Error(errMsg), 'ERR_INVALID_PEER_ID')
}
const checkRouting = options.checkRouting !== false
try {
const dsVal = await this._datastore.get(ipns.getLocalKey(peerId.id))
// unmarshal data
return this._unmarshalData(dsVal)
} catch (err) {
if (err.code !== ERR_NOT_FOUND) {
const errMsg = `unexpected error getting the ipns record ${peerId.id} from datastore`
log.error(errMsg)
throw errcode(new Error(errMsg), 'ERR_UNEXPECTED_DATASTORE_RESPONSE')
}
if (!checkRouting) {
throw errcode(err, 'ERR_NOT_FOUND_AND_CHECK_ROUTING_NOT_ENABLED')
}
// Try to get from routing
try {
const keys = ipns.getIdKeys(peerId.toBytes())
const res = await this._routing.get(keys.routingKey.uint8Array())
// unmarshal data
return this._unmarshalData(res)
} catch (err) {
log.error(err)
throw err
}
}
}
/**
* @param {Uint8Array} data
*/
_unmarshalData (data) {
try {
return ipns.unmarshal(data)
} catch (err) {
throw errcode(err, 'ERR_INVALID_RECORD_DATA')
}
}
/**
* @param {PrivateKey} privKey
* @param {Uint8Array} value
* @param {number} lifetime
* @param {PeerId} peerId
*/
async _updateOrCreateRecord (privKey, value, lifetime, peerId) {
if (!(PeerId.isPeerId(peerId))) {
const errMsg = 'peerId received is not valid'
log.error(errMsg)
throw errcode(new Error(errMsg), 'ERR_INVALID_PEER_ID')
}
const getPublishedOptions = {
checkRouting: true
}
let record
try {
record = await this._getPublished(peerId, getPublishedOptions)
} catch (err) {
if (err.code !== ERR_NOT_FOUND) {
const errMsg = `unexpected error when determining the last published IPNS record for ${peerId.id} ${err.stack}`
log.error(errMsg)
throw errcode(new Error(errMsg), 'ERR_DETERMINING_PUBLISHED_RECORD')
}
}
// Determinate the record sequence number
let seqNumber = 0n
if (record && record.sequence !== undefined) {
seqNumber = !uint8ArrayEquals(record.value, value) ? BigInt(record.sequence) + 1n : BigInt(record.sequence)
}
let entryData
try {
// Create record
entryData = await ipns.create(privKey, value, seqNumber, lifetime)
} catch (err) {
const errMsg = `ipns record for ${value} could not be created`
log.error(err)
throw errcode(new Error(errMsg), 'ERR_CREATING_IPNS_RECORD')
}
// TODO IMPROVEMENT - set ttl (still experimental feature for go)
try {
// Marshal record
const data = ipns.marshal(entryData)
// Store the new record
await this._datastore.put(ipns.getLocalKey(peerId.id), data)
log(`ipns record for ${uint8ArrayToString(value, 'base32')} was stored in the datastore`)
return entryData
} catch (err) {
const errMsg = `ipns record for ${value} could not be stored in the datastore`
log.error(errMsg)
throw errcode(new Error(errMsg), 'ERR_STORING_IN_DATASTORE')
}
}
}
IpnsPublisher.defaultRecordLifetime = defaultRecordLifetime
exports = module.exports = IpnsPublisher