@@ -4,7 +4,7 @@ const ipns = require('ipns')
44const crypto = require ( 'libp2p-crypto' )
55const PeerId = require ( 'peer-id' )
66const errcode = require ( 'err-code' )
7- const parallel = require ( 'async/parallel ' )
7+ const auto = require ( 'async/auto ' )
88
99const debug = require ( 'debug' )
1010const log = debug ( 'jsipfs:ipns:resolver' )
@@ -100,13 +100,13 @@ class IpnsResolver {
100100
101101 const { routingKey, routingPubKey } = ipns . getIdKeys ( peerId . toBytes ( ) )
102102
103- parallel ( [
103+ auto ( {
104104 // Name should be the hash of a public key retrievable from ipfs.
105105 // We retrieve public key to add it to the PeerId, as the IPNS record may not have it.
106- ( cb ) => this . _routing . get ( routingPubKey . toBuffer ( ) , cb ) ,
107- ( cb ) => this . _routing . get ( routingKey . toBuffer ( ) , cb )
108- ] , ( err , res ) => {
109- if ( err ) {
106+ pubKey : ( cb ) => this . _routing . get ( routingPubKey . toBuffer ( ) , cb ) ,
107+ record : ( cb ) => this . _routing . get ( routingKey . toBuffer ( ) , cb )
108+ } , ( err , res ) => {
109+ if ( err && ! res . record ) {
110110 if ( err . code !== 'ERR_NOT_FOUND' ) {
111111 const errMsg = `unexpected error getting the ipns record ${ peerId . id } `
112112
@@ -119,21 +119,24 @@ class IpnsResolver {
119119 return callback ( errcode ( new Error ( errMsg ) , 'ERR_NO_RECORD_FOUND' ) )
120120 }
121121
122- // Public key
123- try {
124- // Insert it into the peer id public key, to be validated by IPNS validator
125- peerId . pubKey = crypto . keys . unmarshalPublicKey ( res [ 0 ] )
126- } catch ( err ) {
127- const errMsg = `found public key record that we couldn't convert to a value`
122+ // If public key was found in the routing, add it to the peer id
123+ // otherwise, wait to check if it is embedded in the record.
124+ if ( res . pubKey ) {
125+ try {
126+ // Insert it into the peer id public key, to be validated by IPNS validator
127+ peerId . pubKey = crypto . keys . unmarshalPublicKey ( res . pubKey )
128+ } catch ( err ) {
129+ const errMsg = `found public key record that we couldn't convert to a value`
128130
129- log . error ( errMsg )
130- return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_PUB_KEY_RECEIVED' ) )
131+ log . error ( errMsg )
132+ return callback ( errcode ( new Error ( errMsg ) , 'ERR_INVALID_PUB_KEY_RECEIVED' ) )
133+ }
131134 }
132135
133136 // IPNS entry
134137 let ipnsEntry
135138 try {
136- ipnsEntry = ipns . unmarshal ( res [ 1 ] )
139+ ipnsEntry = ipns . unmarshal ( res . record )
137140 } catch ( err ) {
138141 const errMsg = `found ipns record that we couldn't convert to a value`
139142
0 commit comments