mirror of
https://github.com/status-im/status-go.git
synced 2025-02-19 18:28:18 +00:00
feat(new-urls-format): Usage the chat key instead of signature (#3779)
This commit is contained in:
parent
d956a3e854
commit
fa46f23f7e
@ -1,7 +1,6 @@
|
|||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ecdsa"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@ -24,6 +23,7 @@ type CommunityURLData struct {
|
|||||||
MembersCount uint32 `json:"membersCount"`
|
MembersCount uint32 `json:"membersCount"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
TagIndices []uint32 `json:"tagIndices"`
|
TagIndices []uint32 `json:"tagIndices"`
|
||||||
|
CommunityID string `json:"communityId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommunityChannelURLData struct {
|
type CommunityChannelURLData struct {
|
||||||
@ -31,11 +31,13 @@ type CommunityChannelURLData struct {
|
|||||||
DisplayName string `json:"displayName"`
|
DisplayName string `json:"displayName"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
|
ChannelUUID string `json:"channelUuid"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContactURLData struct {
|
type ContactURLData struct {
|
||||||
DisplayName string `json:"displayName"`
|
DisplayName string `json:"displayName"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
PublicKey string `json:"publicKey"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type URLDataResponse struct {
|
type URLDataResponse struct {
|
||||||
@ -96,6 +98,7 @@ func (m *Messenger) prepareCommunityData(community *communities.Community) Commu
|
|||||||
MembersCount: uint32(community.MembersCount()),
|
MembersCount: uint32(community.MembersCount()),
|
||||||
Color: community.Identity().GetColor(),
|
Color: community.Identity().GetColor(),
|
||||||
TagIndices: community.TagsIndices(),
|
TagIndices: community.TagsIndices(),
|
||||||
|
CommunityID: community.IDString(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,25 +122,6 @@ func (m *Messenger) parseCommunityURLWithChatKey(urlData string) (*URLDataRespon
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Messenger) prepareEncodedRawData(rawData []byte, privateKey *ecdsa.PrivateKey) (string, string, error) {
|
|
||||||
encodedData, err := urls.EncodeDataURL(rawData)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
signature, err := crypto.SignBytes([]byte(encodedData), privateKey)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
encodedSignature, err := urls.EncodeDataURL(signature)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return encodedData, encodedSignature, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Messenger) prepareEncodedCommunityData(community *communities.Community) (string, string, error) {
|
func (m *Messenger) prepareEncodedCommunityData(community *communities.Community) (string, string, error) {
|
||||||
communityProto := &protobuf.Community{
|
communityProto := &protobuf.Community{
|
||||||
DisplayName: community.Identity().DisplayName,
|
DisplayName: community.Identity().DisplayName,
|
||||||
@ -152,7 +136,16 @@ func (m *Messenger) prepareEncodedCommunityData(community *communities.Community
|
|||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.prepareEncodedRawData(communityData, community.PrivateKey())
|
shortKey, err := m.SerializePublicKey(community.ID())
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
encodedData, err := urls.EncodeDataURL(communityData)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return encodedData, shortKey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Messenger) ShareCommunityURLWithData(communityID types.HexBytes) (string, error) {
|
func (m *Messenger) ShareCommunityURLWithData(communityID types.HexBytes) (string, error) {
|
||||||
@ -165,25 +158,16 @@ func (m *Messenger) ShareCommunityURLWithData(communityID types.HexBytes) (strin
|
|||||||
return "", fmt.Errorf("community with communityID %s not found", communityID)
|
return "", fmt.Errorf("community with communityID %s not found", communityID)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, signature, err := m.prepareEncodedCommunityData(community)
|
data, shortKey, err := m.prepareEncodedCommunityData(community)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%s/c/%s#%s", baseShareURL, data, signature), nil
|
return fmt.Sprintf("%s/c/%s#%s", baseShareURL, data, shortKey), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Messenger) verifySignature(data string, rawSignature string) (*ecdsa.PublicKey, error) {
|
func (m *Messenger) parseCommunityURLWithData(data string, chatKey string) (*URLDataResponse, error) {
|
||||||
signature, err := urls.DecodeDataURL(rawSignature)
|
communityID, err := m.DeserializePublicKey(chatKey)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return crypto.SigToPub(crypto.Keccak256([]byte(data)), signature)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Messenger) parseCommunityURLWithData(data string, signature string) (*URLDataResponse, error) {
|
|
||||||
_, err := m.verifySignature(data, signature)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -206,6 +190,7 @@ func (m *Messenger) parseCommunityURLWithData(data string, signature string) (*U
|
|||||||
MembersCount: communityProto.MembersCount,
|
MembersCount: communityProto.MembersCount,
|
||||||
Color: communityProto.Color,
|
Color: communityProto.Color,
|
||||||
TagIndices: communityProto.TagIndices,
|
TagIndices: communityProto.TagIndices,
|
||||||
|
CommunityID: types.EncodeHex(communityID),
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -299,7 +284,16 @@ func (m *Messenger) prepareEncodedCommunityChannelData(community *communities.Co
|
|||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.prepareEncodedRawData(channelData, community.PrivateKey())
|
shortKey, err := m.SerializePublicKey(community.ID())
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
encodedData, err := urls.EncodeDataURL(channelData)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return encodedData, shortKey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Messenger) ShareCommunityChannelURLWithData(request *requests.CommunityChannelShareURL) (string, error) {
|
func (m *Messenger) ShareCommunityChannelURLWithData(request *requests.CommunityChannelShareURL) (string, error) {
|
||||||
@ -326,16 +320,16 @@ func (m *Messenger) ShareCommunityChannelURLWithData(request *requests.Community
|
|||||||
return "", fmt.Errorf("channel with channelID %s not found", request.ChannelID)
|
return "", fmt.Errorf("channel with channelID %s not found", request.ChannelID)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, signature, err := m.prepareEncodedCommunityChannelData(community, channel, request.ChannelID)
|
data, shortKey, err := m.prepareEncodedCommunityChannelData(community, channel, request.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%s/cc/%s#%s", baseShareURL, data, signature), nil
|
return fmt.Sprintf("%s/cc/%s#%s", baseShareURL, data, shortKey), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Messenger) parseCommunityChannelURLWithData(data string, signature string) (*URLDataResponse, error) {
|
func (m *Messenger) parseCommunityChannelURLWithData(data string, chatKey string) (*URLDataResponse, error) {
|
||||||
_, err := m.verifySignature(data, signature)
|
communityID, err := m.DeserializePublicKey(chatKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -358,12 +352,14 @@ func (m *Messenger) parseCommunityChannelURLWithData(data string, signature stri
|
|||||||
MembersCount: channelProto.Community.MembersCount,
|
MembersCount: channelProto.Community.MembersCount,
|
||||||
Color: channelProto.Community.Color,
|
Color: channelProto.Community.Color,
|
||||||
TagIndices: channelProto.Community.TagIndices,
|
TagIndices: channelProto.Community.TagIndices,
|
||||||
|
CommunityID: types.EncodeHex(communityID),
|
||||||
},
|
},
|
||||||
Channel: CommunityChannelURLData{
|
Channel: CommunityChannelURLData{
|
||||||
Emoji: channelProto.Emoji,
|
Emoji: channelProto.Emoji,
|
||||||
DisplayName: channelProto.DisplayName,
|
DisplayName: channelProto.DisplayName,
|
||||||
Description: channelProto.Description,
|
Description: channelProto.Description,
|
||||||
Color: channelProto.Color,
|
Color: channelProto.Color,
|
||||||
|
ChannelUUID: channelProto.Uuid,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -434,7 +430,22 @@ func (m *Messenger) prepareEncodedUserData(contact *Contact) (string, string, er
|
|||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.prepareEncodedRawData(userData, m.identity)
|
pk, err := contact.PublicKey()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
shortKey, err := m.SerializePublicKey(crypto.CompressPubkey(pk))
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
encodedData, err := urls.EncodeDataURL(userData)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return encodedData, shortKey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Messenger) ShareUserURLWithData(contactID string) (string, error) {
|
func (m *Messenger) ShareUserURLWithData(contactID string) (string, error) {
|
||||||
@ -443,20 +454,15 @@ func (m *Messenger) ShareUserURLWithData(contactID string) (string, error) {
|
|||||||
return "", ErrContactNotFound
|
return "", ErrContactNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
data, signature, err := m.prepareEncodedUserData(contact)
|
data, shortKey, err := m.prepareEncodedUserData(contact)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%s/u/%s#%s", baseShareURL, data, signature), nil
|
return fmt.Sprintf("%s/u/%s#%s", baseShareURL, data, shortKey), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Messenger) parseUserURLWithData(data string, signature string) (*URLDataResponse, error) {
|
func (m *Messenger) parseUserURLWithData(data string, chatKey string) (*URLDataResponse, error) {
|
||||||
_, err := m.verifySignature(data, signature)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
userData, err := urls.DecodeDataURL(data)
|
userData, err := urls.DecodeDataURL(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -472,6 +478,7 @@ func (m *Messenger) parseUserURLWithData(data string, signature string) (*URLDat
|
|||||||
Contact: ContactURLData{
|
Contact: ContactURLData{
|
||||||
DisplayName: userProto.DisplayName,
|
DisplayName: userProto.DisplayName,
|
||||||
Description: userProto.Description,
|
Description: userProto.Description,
|
||||||
|
PublicKey: chatKey,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -190,11 +190,12 @@ func (s *MessengerShareUrlsSuite) TestShareCommunityURLWithData() {
|
|||||||
url, err := s.m.ShareCommunityURLWithData(community.ID())
|
url, err := s.m.ShareCommunityURLWithData(community.ID())
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
communityData, signature, err := s.m.prepareEncodedCommunityData(community)
|
communityData, chatKey, err := s.m.prepareEncodedCommunityData(community)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
expectedURL := fmt.Sprintf("%s/c/%s#%s", baseShareURL, communityData, signature)
|
expectedURL := fmt.Sprintf("%s/c/%s#%s", baseShareURL, communityData, chatKey)
|
||||||
s.Require().Equal(expectedURL, url)
|
s.Require().Equal(expectedURL, url)
|
||||||
|
s.Require().Equal(communityData, "GzAAAETnNgpsRpPBwMzNmQiokxNsTUoRiCDpdc4UDkxk1F7pOu65DV_H8-xat9EQ9g==")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MessengerShareUrlsSuite) TestParseCommunityURLWithData() {
|
func (s *MessengerShareUrlsSuite) TestParseCommunityURLWithData() {
|
||||||
@ -270,20 +271,20 @@ func (s *MessengerShareUrlsSuite) TestShareCommunityChannelURLWithData() {
|
|||||||
url, err := s.m.ShareCommunityChannelURLWithData(request)
|
url, err := s.m.ShareCommunityChannelURLWithData(request)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
communityData, signature, err := s.m.prepareEncodedCommunityChannelData(community, channel, channelID)
|
communityChannelData, chatKey, err := s.m.prepareEncodedCommunityChannelData(community, channel, channelID)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
expectedURL := fmt.Sprintf("%s/cc/%s#%s", baseShareURL, communityData, signature)
|
expectedURL := fmt.Sprintf("%s/cc/%s#%s", baseShareURL, communityChannelData, chatKey)
|
||||||
s.Require().Equal(expectedURL, url)
|
s.Require().Equal(expectedURL, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MessengerShareUrlsSuite) TestParseCommunityChannelURLWithData() {
|
func (s *MessengerShareUrlsSuite) TestParseCommunityChannelURLWithData() {
|
||||||
community, channel, channelID := s.createCommunityWithChannel()
|
community, channel, channelID := s.createCommunityWithChannel()
|
||||||
|
|
||||||
communityChannelData, signature, err := s.m.prepareEncodedCommunityChannelData(community, channel, channelID)
|
communityChannelData, chatKey, err := s.m.prepareEncodedCommunityChannelData(community, channel, channelID)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/cc/%s#%s", baseShareURL, communityChannelData, signature)
|
url := fmt.Sprintf("%s/cc/%s#%s", baseShareURL, communityChannelData, chatKey)
|
||||||
|
|
||||||
urlData, err := s.m.ParseSharedURL(url)
|
urlData, err := s.m.ParseSharedURL(url)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
@ -369,20 +370,21 @@ func (s *MessengerShareUrlsSuite) TestShareUserURLWithData() {
|
|||||||
url, err := s.m.ShareUserURLWithData(contact.ID)
|
url, err := s.m.ShareUserURLWithData(contact.ID)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
userData, signature, err := s.m.prepareEncodedUserData(contact)
|
userData, chatKey, err := s.m.prepareEncodedUserData(contact)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
expectedURL := fmt.Sprintf("%s/u/%s#%s", baseShareURL, userData, signature)
|
expectedURL := fmt.Sprintf("%s/u/%s#%s", baseShareURL, userData, chatKey)
|
||||||
s.Require().Equal(expectedURL, url)
|
s.Require().Equal(expectedURL, url)
|
||||||
|
s.Require().Equal(userData, "Ow==")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MessengerShareUrlsSuite) TestParseUserURLWithData() {
|
func (s *MessengerShareUrlsSuite) TestParseUserURLWithData() {
|
||||||
_, contact := s.createContact()
|
_, contact := s.createContact()
|
||||||
|
|
||||||
userData, signature, err := s.m.prepareEncodedUserData(contact)
|
userData, chatKey, err := s.m.prepareEncodedUserData(contact)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/u/%s#%s", baseShareURL, userData, signature)
|
url := fmt.Sprintf("%s/u/%s#%s", baseShareURL, userData, chatKey)
|
||||||
|
|
||||||
urlData, err := s.m.ParseSharedURL(url)
|
urlData, err := s.m.ParseSharedURL(url)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user