@@ -15,9 +15,9 @@ const defaultRecordTtl = 60 * 60 * 1000
1515
1616// IpnsPublisher is capable of publishing and resolving names to the IPFS routing system.
1717class IpnsPublisher {
18- constructor ( routing , repo ) {
18+ constructor ( routing , datastore ) {
1919 this . _routing = routing
20- this . _repo = repo
20+ this . _datastore = datastore
2121 }
2222
2323 // publish record with a eol
@@ -56,7 +56,6 @@ class IpnsPublisher {
5656 log . error ( errMsg )
5757 return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_PEER_ID' ) )
5858 }
59-
6059 const publicKey = peerId . _pubKey
6160
6261 ipns . embedPublicKey ( publicKey , record , ( err , embedPublicKeyRecord ) => {
@@ -74,9 +73,10 @@ class IpnsPublisher {
7473
7574 series ( [
7675 ( 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 )
8080 ] , ( err ) => {
8181 if ( err ) {
8282 log . error ( err )
@@ -159,50 +159,57 @@ class IpnsPublisher {
159159 }
160160
161161 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
166163
164+ this . _datastore . get ( ipns . getLocalKey ( peerId . id ) , ( err , dsVal ) => {
167165 if ( err ) {
168166 if ( err . code !== 'ERR_NOT_FOUND' ) {
169167 const errMsg = `unexpected error getting the ipns record ${ peerId . id } from datastore`
170168
171169 log . error ( errMsg )
172170 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- }
180171 }
181- }
182172
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+ }
187176
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+ }
191185
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+ }
197190
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 )
200197 }
201-
202- callback ( null , result )
203198 } )
204199 }
205200
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+
206213 _updateOrCreateRecord ( privKey , value , validity , peerId , callback ) {
207214 if ( ! ( PeerId . isPeerId ( peerId ) ) ) {
208215 const errMsg = `peerId received is not valid`
@@ -212,12 +219,17 @@ class IpnsPublisher {
212219 }
213220
214221 const getPublishedOptions = {
215- checkRouting : false // TODO ROUTING - change to true
222+ checkRouting : true
216223 }
217224
218225 this . _getPublished ( peerId , getPublishedOptions , ( err , record ) => {
219226 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+ }
221233 }
222234
223235 // Determinate the record sequence number
@@ -241,7 +253,7 @@ class IpnsPublisher {
241253 const data = ipns . marshal ( entryData )
242254
243255 // 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 ) => {
245257 if ( err ) {
246258 const errMsg = `ipns record for ${ value } could not be stored in the datastore`
247259
0 commit comments