package tdesktop
import (
"github.com/go-faster/errors"
"github.com/gotd/td/bin"
"github.com/gotd/td/internal/crypto"
)
type MTPAuthorization struct {
UserID uint64
MainDC int
Keys map [int ]crypto .Key
}
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" )
}
decrypted = decrypted [4 :]
r := qtReader {buf : bin .Buffer {Buf : decrypted }}
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 )
}
m .Keys [int (dcID )] = key
}
return nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .