1
1
'use strict'
2
2
3
3
const PeerId = require ( 'peer-id' )
4
- const Record = require ( 'libp2p-record' ) . Record
5
4
const { Key } = require ( 'interface-datastore' )
6
5
const series = require ( 'async/series' )
7
6
const errcode = require ( 'err-code' )
@@ -57,7 +56,6 @@ class IpnsPublisher {
57
56
log . error ( errMsg )
58
57
return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_PEER_ID' ) )
59
58
}
60
-
61
59
const publicKey = peerId . _pubKey
62
60
63
61
ipns . embedPublicKey ( publicKey , record , ( err , embedPublicKeyRecord ) => {
@@ -97,19 +95,17 @@ class IpnsPublisher {
97
95
return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_DATASTORE_KEY' ) )
98
96
}
99
97
100
- let rec
98
+ let entryData
101
99
try {
102
100
// 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 )
106
102
} catch ( err ) {
107
103
log . error ( err )
108
104
return callback ( err )
109
105
}
110
106
111
107
// 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 ) => {
113
109
if ( err ) {
114
110
const errMsg = `ipns record for ${ key . toString ( ) } could not be stored in the routing`
115
111
@@ -137,17 +133,8 @@ class IpnsPublisher {
137
133
return callback ( errcode ( new Error ( errMsg ) , 'ERR_UNDEFINED_PARAMETER' ) )
138
134
}
139
135
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
-
149
136
// 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 ) => {
151
138
if ( err ) {
152
139
const errMsg = `public key for ${ key . toString ( ) } could not be stored in the routing`
153
140
@@ -174,45 +161,55 @@ class IpnsPublisher {
174
161
const checkRouting = ! ( options . checkRouting === false )
175
162
176
163
this . _repo . datastore . get ( ipns . getLocalKey ( peerId . id ) , ( err , dsVal ) => {
177
- let result
178
-
179
164
if ( err ) {
180
165
if ( err . code !== 'ERR_NOT_FOUND' ) {
181
166
const errMsg = `unexpected error getting the ipns record ${ peerId . id } from datastore`
182
167
183
168
log . error ( errMsg )
184
169
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 } ` )
187
188
return callback ( null , null )
188
- } else {
189
- // TODO ROUTING - get from DHT
190
- return callback ( new Error ( 'not implemented yet' ) )
191
189
}
192
- }
193
- }
194
190
195
- if ( Buffer . isBuffer ( dsVal ) ) {
196
- result = dsVal
191
+ // unmarshal data
192
+ this . _unmarshalData ( res , callback )
193
+ } )
197
194
} 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 )
202
197
}
198
+ } )
199
+ }
203
200
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`
209
207
210
- log . error ( errMsg )
211
- return callback ( null , null )
212
- }
208
+ log . error ( errMsg )
209
+ return callback ( null , null )
210
+ }
213
211
214
- callback ( null , result )
215
- } )
212
+ callback ( null , result )
216
213
}
217
214
218
215
_updateOrCreateRecord ( privKey , value , validity , peerId , callback ) {
@@ -224,7 +221,7 @@ class IpnsPublisher {
224
221
}
225
222
226
223
const getPublishedOptions = {
227
- checkRouting : false // TODO ROUTING - change to true
224
+ checkRouting : true
228
225
}
229
226
230
227
this . _getPublished ( peerId , getPublishedOptions , ( err , record ) => {
0 commit comments