@@ -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 ) => {
@@ -162,45 +161,55 @@ class IpnsPublisher {
162161 const checkRouting = ! ( options . checkRouting === false )
163162
164163 this . _repo . datastore . get ( ipns . getLocalKey ( peerId . id ) , ( err , dsVal ) => {
165- let result
166-
167164 if ( err ) {
168165 if ( err . code !== 'ERR_NOT_FOUND' ) {
169166 const errMsg = `unexpected error getting the ipns record ${ peerId . id } from datastore`
170167
171168 log . error ( errMsg )
172169 return callback ( errcode ( new Error ( errMsg ) , 'ERR_UNEXPECTED_DATASTORE_RESPONSE' ) )
173- } else {
174- if ( ! checkRouting ) {
170+ }
171+
172+ if ( ! checkRouting ) {
173+ return callback ( null , null )
174+ }
175+
176+ // Try to get from routing
177+ let keys
178+ try {
179+ keys = ipns . getIdKeys ( peerId . toBytes ( ) )
180+ } catch ( err ) {
181+ log . error ( err )
182+ return callback ( err )
183+ }
184+
185+ this . _routing . get ( keys . routingKey , ( err , res ) => {
186+ if ( err ) {
187+ log ( `error when determining the last published IPNS record for ${ peerId . id } ` )
175188 return callback ( null , null )
176- } else {
177- // TODO ROUTING - get from DHT
178- return callback ( new Error ( 'not implemented yet' ) )
179189 }
180- }
181- }
182190
183- if ( Buffer . isBuffer ( dsVal ) ) {
184- result = dsVal
191+ // unmarshal data
192+ this . _unmarshalData ( res , callback )
193+ } )
185194 } else {
186- const errMsg = `found ipns record that we couldn't convert to a value`
187-
188- log . error ( errMsg )
189- return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_IPNS_RECORD' ) )
195+ // unmarshal data
196+ this . _unmarshalData ( dsVal , callback )
190197 }
198+ } )
199+ }
191200
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`
201+ _unmarshalData ( data , callback ) {
202+ let result
203+ try {
204+ result = ipns . unmarshal ( data )
205+ } catch ( err ) {
206+ const errMsg = `found ipns record that we couldn't convert to a value`
197207
198- log . error ( errMsg )
199- return callback ( null , null )
200- }
208+ log . error ( errMsg )
209+ return callback ( null , null )
210+ }
201211
202- callback ( null , result )
203- } )
212+ callback ( null , result )
204213 }
205214
206215 _updateOrCreateRecord ( privKey , value , validity , peerId , callback ) {
@@ -212,7 +221,7 @@ class IpnsPublisher {
212221 }
213222
214223 const getPublishedOptions = {
215- checkRouting : false // TODO ROUTING - change to true
224+ checkRouting : true
216225 }
217226
218227 this . _getPublished ( peerId , getPublishedOptions , ( err , record ) => {
0 commit comments