Error: dialed to the wrong peer, Ids do not match
I faced similar issue when connecting Rust & JS via libp2p. The problem is that peer-id JS computes peer id from public key like this:
const computeDigest = (pubKey, cb) => {
if (pubKey.bytes.length <= 42) {
const digest = mh.encode(pubKey.bytes, 'identity') // This get's executed
cb(null, digest)
} else {
pubKey.hash((err, digest) => {
cb(err, digest)
})
}
}
Result is that in JS mh.encode(pubKey.bytes, 'identity')
results in addresses of format 12D3KooWSwNXzEeGjgwEocRJBzbdoDqxbz3LdrwgSuKmKeGvbM4G
, while in Rust PeerId for the same public key looks like QmTESkr2vWDCKqiHVsyvf4iRQCBgvNDqBJ6P3yTTDb6haw
.
Error then happens in libp2p-secio/src/handshake/crypto.js
in identify
function in JS:
if (state.id.remote.toB58String() !== remoteId.toB58String()) { // <-- remoteId here is computed from public key in 12D3 format, while state.id.remote is in Qm format
return callback(new Error('dialed to the wrong peer, Ids do not match'))
}
Currently, I don’t have a solution for that except to patch either JS or Rust.