move gotd fork into repo. (#111)

- update to latest telegram layer
- remove some references to fields in tg.Entities that don't exist in
the schema
- originally added here:
https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e
  - referenced here
-
https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3
-
https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
This commit is contained in:
Adam Van Ymeren
2025-06-27 20:03:37 -07:00
committed by GitHub
parent 0952df0244
commit 7a04f298d2
19264 changed files with 1539697 additions and 84 deletions
@@ -0,0 +1,115 @@
package tdesktop
import (
"github.com/go-faster/errors"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
"go.mau.fi/mautrix-telegram/pkg/gotd/crypto"
)
// MTPAuthorization is a Telegram Desktop storage structure which stores MTProto session info.
//
// See https://github.com/telegramdesktop/tdesktop/blob/dev/Telegram/SourceFiles/main/main_account.cpp#L359.
type MTPAuthorization struct {
// UserID is a Telegram user ID.
UserID uint64
// MainDC is a main DC ID of this user.
MainDC int
// Key is a map of keys per DC ID.
Keys map[int]crypto.Key // DC ID -> Key
}
// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/storage/storage_account.cpp#L898.
func readMTPData(tgf *tdesktopFile, localKey crypto.Key) (MTPAuthorization, error) {
encrypted, err := tgf.readArray()
if err != nil {
return MTPAuthorization{}, errors.Wrap(err, "read encrypted data")
}
decrypted, err := decryptLocal(encrypted, localKey)
if err != nil {
return MTPAuthorization{}, errors.Wrap(err, "decrypt data")
}
// Skip decrypted data length (uint32).
decrypted = decrypted[4:]
r := qtReader{buf: bin.Buffer{Buf: decrypted}}
// TODO(tdakkota): support other IDs.
var m MTPAuthorization
if err := m.deserialize(&r); err != nil {
return MTPAuthorization{}, errors.Wrap(err, "deserialize MTPAuthorization")
}
return m, err
}
func readKey(r *qtReader, k *crypto.Key) (uint32, error) {
dcID, err := r.readUint32()
if err != nil {
return 0, errors.Wrap(err, "read DC ID")
}
if err := r.consumeN(k[:], 256); err != nil {
return 0, errors.Wrap(err, "read auth key")
}
return dcID, nil
}
func (m *MTPAuthorization) deserialize(r *qtReader) error {
id, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read dbi ID")
}
if id != dbiMtpAuthorization {
return errors.Errorf("unexpected id %d", id)
}
if err := r.skip(4); err != nil {
return errors.Wrap(err, "read mainLength")
}
legacyUserID, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read legacyUserID")
}
legacyMainDCID, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read legacyMainDCID")
}
if (uint64(legacyUserID)<<32)|uint64(legacyMainDCID) == kWideIdsTag {
userID, err := r.readUint64()
if err != nil {
return errors.Wrap(err, "read userID")
}
mainDC, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read mainDcID")
}
m.UserID = userID
m.MainDC = int(mainDC)
} else {
m.UserID = uint64(legacyUserID)
m.MainDC = int(legacyMainDCID)
}
keys, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read keys length")
}
if m.Keys == nil {
m.Keys = make(map[int]crypto.Key, keys)
}
for i := 0; i < int(keys); i++ {
var key crypto.Key
dcID, err := readKey(r, &key)
if err != nil {
return errors.Wrapf(err, "read key %d", i)
}
// FIXME(tdakkota): what if there is more than one session per DC?
m.Keys[int(dcID)] = key
}
return nil
}