Before the user successfully logged in to an account, unfortunately the jid might not be known. He might eg. use a burner jid for example. Unfortunately the smack-omemo keystore uses the jid as an identifier to store keys, trust decisions etc.
Because connection.getUser() returnes null before the user is logged in, smack-omemo has a workaround in place to retrieve the username from the AbstractConnection, which never returns null, but instead the returned value might still differ from the jid the user ends up with.
All this eventually leads to multiple OmemoManager instances being created along with multiple key stores.
In order to fix this, I propose to change the way OmemoManager instances are identified:
Instead of the tuple (BareJid, deviceID), we use a single String (identifier), which is independent from BareJid and deviceId. The deviceId is - instead of being the primary key to the accounts store, also a value inside the store.
That way, we can instanciate OmemoManager instances by using OmemoManager.getInstanceFor(connection, identifier) at all times, even pre-authentication. We might recommend to construct the identifier as the concatenation of the bareJid and the deviceId (eg “firstname.lastname@example.org”), but that’s not necessary.
I already started to create a PR that addresses the issue in the way I proposed and it seams as if not too many changes are necessary. Unfortunately I don’t see a very clear way for existing instances to migrate though, but maybe we’ll figure something out