telegabber/e2ee/peer.go
2026-07-28 20:32:56 -04:00

25 lines
1.1 KiB
Go

package e2ee
import "strings"
// OwnedPeer constructs the PeerID for an identity this gateway owns and
// generates keys for - a bridged chat pseudo-JID.
//
// Telegram chat ids (and therefore their pseudo-JIDs) are only unique
// within one Telegram login, not globally - telegabber bridges multiple
// Telegram accounts under the same XMPP domain, so two different logins
// could each have a chat whose pseudo-JID is the same string. An owned
// PeerID must therefore be scoped by login too. A real remote peer's
// PeerID (the other end of a conversation) needs no such scoping - it's
// just their bare JID - since their sessions/trust are already nested
// under the owning identity's own scope (see badgerstore's key layout).
func OwnedPeer(login, bareJID string) PeerID {
return PeerID(login + "\x00" + bareJID)
}
// SplitOwnedPeer reverses OwnedPeer. ok is false if peer wasn't built by
// OwnedPeer (e.g. a plain remote-peer PeerID was passed by mistake).
func SplitOwnedPeer(peer PeerID) (login, bareJID string, ok bool) {
login, bareJID, ok = strings.Cut(string(peer), "\x00")
return
}