libp2p / js-peer-id

peer-id implementation in JavaScript. Deprecated; use https://github.com/libp2p/js-libp2p-peer-id instead.
https://github.com/libp2p/js-libp2p-peer-id
MIT License
81 stars 44 forks source link

Calling toString on a peer id breaks deep comparison #141

Closed achingbrain closed 3 years ago

achingbrain commented 3 years ago
const PeerId = require('peer-id')

const peerId1 = PeerId.createFromB58String('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
const peerId2 = PeerId.createFromB58String('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')

assert.deepEqual(peerId1, peerId2)
// 'bafzbeifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'

peerId1.toString()

assert.deepEqual(peerId1, peerId2)
// Uncaught AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal:

This is because .toString() has the side effect of setting a ._idCIDString property on this which the other peer ID doesn't have.

vasco-santos commented 3 years ago

Users should not rely on deepEqual in my opinion, as if you create the same PeerId via privateKey, or B58String they are the same PeerId (per PeerId.equals), but the deep will fail.

Even though, we should address this problem. If this is not a major problem, we should probably wait until we move into using default format from RFC 0001 by default in libp2p

achingbrain commented 3 years ago

Users should not rely on deepEqual in my opinion

It's a pretty common test assertion which we can't use. It could be fixed my marking using Object.defineProperty to mark the property as non-enumerable?