gmessages/libgm/payload/registerPhoneRelay.go
2023-07-16 01:56:11 +03:00

60 lines
1.6 KiB
Go

package payload
import (
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/crypto"
"go.mau.fi/mautrix-gmessages/libgm/util"
)
func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContainer, error) {
id := util.RandomUUIDv4()
encryptedKeys, encryptErr := uncompressKey(jwk)
if encryptErr != nil {
return nil, nil, encryptErr
}
payloadData := &binary.AuthenticationContainer{
AuthMessage: &binary.AuthMessage{
RequestID: id,
Network: &Network,
ConfigVersion: ConfigMessage,
},
BrowserDetails: BrowserDetailsMessage,
Data: &binary.AuthenticationContainer_KeyData{
KeyData: &binary.KeyData{
EcdsaKeys: &binary.ECDSAKeys{
Field1: 2,
EncryptedKeys: encryptedKeys,
},
},
},
}
encoded, err4 := proto.Marshal(payloadData)
if err4 != nil {
return nil, payloadData, err4
}
return encoded, payloadData, nil
}
func uncompressKey(jwk *crypto.JWK) ([]byte, error) {
uncompressedPublicKey, err3 := jwk.MarshalPubKey()
if err3 != nil {
return nil, err3
}
var emptyByteArray []byte
crypto.EncodeValues(&emptyByteArray, crypto.SequenceOne)
crypto.EncodeValues(&emptyByteArray, crypto.SequenceTwo)
var copiedByteArray []byte
copiedByteArray = crypto.AppendByteSequence(copiedByteArray, emptyByteArray, uncompressedPublicKey)
for _, value := range uncompressedPublicKey {
copiedByteArray = crypto.HelperAppendBytes(copiedByteArray, value)
}
var encryptedKeys []byte
encryptedKeys = crypto.AppendBytes(encryptedKeys, copiedByteArray[0:])
return encryptedKeys, nil
}