@@ -15,9 +15,9 @@ const defaultRecordTtl = 60 * 60 * 1000
15
15
16
16
// IpnsPublisher is capable of publishing and resolving names to the IPFS routing system.
17
17
class IpnsPublisher {
18
- constructor ( routing , repo ) {
18
+ constructor ( routing , datastore ) {
19
19
this . _routing = routing
20
- this . _repo = repo
20
+ this . _datastore = datastore
21
21
}
22
22
23
23
// publish record with a eol
@@ -56,7 +56,6 @@ class IpnsPublisher {
56
56
log . error ( errMsg )
57
57
return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_PEER_ID' ) )
58
58
}
59
-
60
59
const publicKey = peerId . _pubKey
61
60
62
61
ipns . embedPublicKey ( publicKey , record , ( err , embedPublicKeyRecord ) => {
@@ -74,9 +73,10 @@ class IpnsPublisher {
74
73
75
74
series ( [
76
75
( cb ) => this . _publishEntry ( keys . routingKey , embedPublicKeyRecord || record , peerId , cb ) ,
77
- // Publish the public key if a public key cannot be extracted from the ID
78
- // We will be able to deprecate this part in the future, since the public keys will be only in the peerId
79
- ( cb ) => embedPublicKeyRecord ? this . _publishPublicKey ( keys . routingPubKey , publicKey , peerId , cb ) : cb ( )
76
+ // Publish the public key to support old go-ipfs nodes that are looking for it in the routing
77
+ // We will be able to deprecate this part in the future, since the public keys will be only
78
+ // in IPNS record and the peerId.
79
+ ( cb ) => this . _publishPublicKey ( keys . routingPubKey , publicKey , peerId , cb )
80
80
] , ( err ) => {
81
81
if ( err ) {
82
82
log . error ( err )
@@ -159,50 +159,57 @@ class IpnsPublisher {
159
159
}
160
160
161
161
options = options || { }
162
- const checkRouting = ! ( options . checkRouting === false )
163
-
164
- this . _repo . datastore . get ( ipns . getLocalKey ( peerId . id ) , ( err , dsVal ) => {
165
- let result
162
+ const checkRouting = options . checkRouting !== false
166
163
164
+ this . _datastore . get ( ipns . getLocalKey ( peerId . id ) , ( err , dsVal ) => {
167
165
if ( err ) {
168
166
if ( err . code !== 'ERR_NOT_FOUND' ) {
169
167
const errMsg = `unexpected error getting the ipns record ${ peerId . id } from datastore`
170
168
171
169
log . error ( errMsg )
172
170
return callback ( errcode ( new Error ( errMsg ) , 'ERR_UNEXPECTED_DATASTORE_RESPONSE' ) )
173
- } else {
174
- if ( ! checkRouting ) {
175
- return callback ( null , null )
176
- } else {
177
- // TODO ROUTING - get from DHT
178
- return callback ( new Error ( 'not implemented yet' ) )
179
- }
180
171
}
181
- }
182
172
183
- if ( Buffer . isBuffer ( dsVal ) ) {
184
- result = dsVal
185
- } else {
186
- const errMsg = `found ipns record that we couldn't convert to a value`
173
+ if ( ! checkRouting ) {
174
+ return callback ( ( errcode ( err ) ) )
175
+ }
187
176
188
- log . error ( errMsg )
189
- return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_IPNS_RECORD' ) )
190
- }
177
+ // Try to get from routing
178
+ let keys
179
+ try {
180
+ keys = ipns . getIdKeys ( peerId . toBytes ( ) )
181
+ } catch ( err ) {
182
+ log . error ( err )
183
+ return callback ( err )
184
+ }
191
185
192
- // unmarshal data
193
- try {
194
- result = ipns . unmarshal ( dsVal )
195
- } catch ( err ) {
196
- const errMsg = `found ipns record that we couldn't convert to a value`
186
+ this . _routing . get ( keys . routingKey . toBuffer ( ) , ( err , res ) => {
187
+ if ( err ) {
188
+ return callback ( err )
189
+ }
197
190
198
- log . error ( errMsg )
199
- return callback ( null , null )
191
+ // unmarshal data
192
+ this . _unmarshalData ( res , callback )
193
+ } )
194
+ } else {
195
+ // unmarshal data
196
+ this . _unmarshalData ( dsVal , callback )
200
197
}
201
-
202
- callback ( null , result )
203
198
} )
204
199
}
205
200
201
+ _unmarshalData ( data , callback ) {
202
+ let result
203
+ try {
204
+ result = ipns . unmarshal ( data )
205
+ } catch ( err ) {
206
+ log . error ( err )
207
+ return callback ( errcode ( err , 'ERR_INVALID_RECORD_DATA' ) )
208
+ }
209
+
210
+ callback ( null , result )
211
+ }
212
+
206
213
_updateOrCreateRecord ( privKey , value , validity , peerId , callback ) {
207
214
if ( ! ( PeerId . isPeerId ( peerId ) ) ) {
208
215
const errMsg = `peerId received is not valid`
@@ -212,12 +219,17 @@ class IpnsPublisher {
212
219
}
213
220
214
221
const getPublishedOptions = {
215
- checkRouting : false // TODO ROUTING - change to true
222
+ checkRouting : true
216
223
}
217
224
218
225
this . _getPublished ( peerId , getPublishedOptions , ( err , record ) => {
219
226
if ( err ) {
220
- return callback ( err )
227
+ if ( err . code !== 'ERR_NOT_FOUND' ) {
228
+ const errMsg = `unexpected error when determining the last published IPNS record for ${ peerId . id } `
229
+
230
+ log . error ( errMsg )
231
+ return callback ( errcode ( new Error ( errMsg ) , 'ERR_DETERMINING_PUBLISHED_RECORD' ) )
232
+ }
221
233
}
222
234
223
235
// Determinate the record sequence number
@@ -241,7 +253,7 @@ class IpnsPublisher {
241
253
const data = ipns . marshal ( entryData )
242
254
243
255
// Store the new record
244
- this . _repo . datastore . put ( ipns . getLocalKey ( peerId . id ) , data , ( err , res ) => {
256
+ this . _datastore . put ( ipns . getLocalKey ( peerId . id ) , data , ( err , res ) => {
245
257
if ( err ) {
246
258
const errMsg = `ipns record for ${ value } could not be stored in the datastore`
247
259
0 commit comments