diff --git a/libraries/key-identifier/__tests__/key-identifier.test.js b/libraries/key-identifier/__tests__/key-identifier.test.js index a3bd8671..34bb6908 100644 --- a/libraries/key-identifier/__tests__/key-identifier.test.js +++ b/libraries/key-identifier/__tests__/key-identifier.test.js @@ -200,6 +200,26 @@ describe('KeyIdentifier', () => { expect(KeyIdentifier.compare(keyIdA, keyIdB)).toBe(false) expect(KeyIdentifier.compare('not-an-object', keyIdB)).toBe(false) }) + + it('should return false when key identifier A is null', () => { + const keyIdA = new KeyIdentifier({ + derivationAlgorithm: 'BIP32', + assetName: 'ethereum', + derivationPath: "m/44'/60'/0'/0/0", + }) + + expect(KeyIdentifier.compare(keyIdA, null)).toBe(false) + }) + + it('should return false when key identifier B is null', () => { + const keyIdB = new KeyIdentifier({ + derivationAlgorithm: 'BIP32', + assetName: 'ethereum', + derivationPath: "m/44'/60'/0'/0/0", + }) + + expect(KeyIdentifier.compare(null, keyIdB)).toBe(false) + }) }) describe('.toString()', () => { diff --git a/libraries/key-identifier/src/key-identifier.js b/libraries/key-identifier/src/key-identifier.js index 0c5f331b..a0c1769a 100644 --- a/libraries/key-identifier/src/key-identifier.js +++ b/libraries/key-identifier/src/key-identifier.js @@ -90,12 +90,19 @@ export default class KeyIdentifier { } static compare = (keyIdA, keyIdB) => { - if (typeof keyIdA !== 'object' || typeof keyIdB !== 'object') { + if ( + typeof keyIdA !== 'object' || + typeof keyIdB !== 'object' || + [keyIdA, keyIdB].includes(null) + ) { return false } return !['derivationAlgorithm', 'derivationPath', 'assetName', 'keyType'].some( - (fieldName) => keyIdA[fieldName] !== keyIdB[fieldName] + (fieldName) => + Object.prototype.hasOwnProperty.call(keyIdA, fieldName) && + Object.prototype.hasOwnProperty.call(keyIdB, fieldName) && + keyIdA[fieldName] !== keyIdB[fieldName] ) } }