11'use strict'
22
33const PeerId = require ( 'peer-id' )
4- const Record = require ( 'libp2p-record' ) . Record
54const { Key } = require ( 'interface-datastore' )
65const series = require ( 'async/series' )
76const errcode = require ( 'err-code' )
@@ -57,7 +56,6 @@ class IpnsPublisher {
5756 log . error ( errMsg )
5857 return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_PEER_ID' ) )
5958 }
60-
6159 const publicKey = peerId . _pubKey
6260
6361 ipns . embedPublicKey ( publicKey , record , ( err , embedPublicKeyRecord ) => {
@@ -97,19 +95,17 @@ class IpnsPublisher {
9795 return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_DATASTORE_KEY' ) )
9896 }
9997
100- let rec
98+ let entryData
10199 try {
102100 // Marshal record
103- const entryData = ipns . marshal ( entry )
104- // Marshal to libp2p record
105- rec = new Record ( key . toBuffer ( ) , entryData )
101+ entryData = ipns . marshal ( entry )
106102 } catch ( err ) {
107103 log . error ( err )
108104 return callback ( err )
109105 }
110106
111107 // Add record to routing (buffer key)
112- this . _routing . put ( key . toBuffer ( ) , rec . serialize ( ) , ( err , res ) => {
108+ this . _routing . put ( key . toBuffer ( ) , entryData , ( err , res ) => {
113109 if ( err ) {
114110 const errMsg = `ipns record for ${ key . toString ( ) } could not be stored in the routing`
115111
@@ -137,17 +133,8 @@ class IpnsPublisher {
137133 return callback ( errcode ( new Error ( errMsg ) , 'ERR_UNDEFINED_PARAMETER' ) )
138134 }
139135
140- let rec
141- try {
142- // Marshal to libp2p record
143- rec = new Record ( key . toBuffer ( ) , publicKey . bytes )
144- } catch ( err ) {
145- log . error ( err )
146- return callback ( err )
147- }
148-
149136 // Add public key to routing (buffer key)
150- this . _routing . put ( key . toBuffer ( ) , rec . serialize ( ) , ( err , res ) => {
137+ this . _routing . put ( key . toBuffer ( ) , publicKey . bytes , ( err , res ) => {
151138 if ( err ) {
152139 const errMsg = `public key for ${ key . toString ( ) } could not be stored in the routing`
153140
@@ -174,45 +161,55 @@ class IpnsPublisher {
174161 const checkRouting = ! ( options . checkRouting === false )
175162
176163 this . _repo . datastore . get ( ipns . getLocalKey ( peerId . id ) , ( err , dsVal ) => {
177- let result
178-
179164 if ( err ) {
180165 if ( err . code !== 'ERR_NOT_FOUND' ) {
181166 const errMsg = `unexpected error getting the ipns record ${ peerId . id } from datastore`
182167
183168 log . error ( errMsg )
184169 return callback ( errcode ( new Error ( errMsg ) , 'ERR_UNEXPECTED_DATASTORE_RESPONSE' ) )
185- } else {
186- 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 } ` )
187188 return callback ( null , null )
188- } else {
189- // TODO ROUTING - get from DHT
190- return callback ( new Error ( 'not implemented yet' ) )
191189 }
192- }
193- }
194190
195- if ( Buffer . isBuffer ( dsVal ) ) {
196- result = dsVal
191+ // unmarshal data
192+ this . _unmarshalData ( res , callback )
193+ } )
197194 } else {
198- const errMsg = `found ipns record that we couldn't convert to a value`
199-
200- log . error ( errMsg )
201- return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_IPNS_RECORD' ) )
195+ // unmarshal data
196+ this . _unmarshalData ( dsVal , callback )
202197 }
198+ } )
199+ }
203200
204- // unmarshal data
205- try {
206- result = ipns . unmarshal ( dsVal )
207- } catch ( err ) {
208- 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`
209207
210- log . error ( errMsg )
211- return callback ( null , null )
212- }
208+ log . error ( errMsg )
209+ return callback ( null , null )
210+ }
213211
214- callback ( null , result )
215- } )
212+ callback ( null , result )
216213 }
217214
218215 _updateOrCreateRecord ( privKey , value , validity , peerId , callback ) {
@@ -224,7 +221,7 @@ class IpnsPublisher {
224221 }
225222
226223 const getPublishedOptions = {
227- checkRouting : false // TODO ROUTING - change to true
224+ checkRouting : true
228225 }
229226
230227 this . _getPublished ( peerId , getPublishedOptions , ( err , record ) => {
0 commit comments