status-go/protocol/group_chat_invitation.go

65 lines
2.1 KiB
Go
Raw Permalink Normal View History

2020-08-07 13:49:37 +00:00
package protocol
import (
"crypto/ecdsa"
"encoding/json"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/protobuf"
)
// Invitation represents a group chat invitation request from a user in the application layer, used for persistence, querying and
// signaling
type GroupChatInvitation struct {
*protobuf.GroupChatInvitation
2020-08-07 13:49:37 +00:00
// From is a public key of the author of the invitation request.
From string `json:"from,omitempty"`
// SigPubKey is the ecdsa encoded public key of the invitation author
SigPubKey *ecdsa.PublicKey `json:"-"`
}
func NewGroupChatInvitation() *GroupChatInvitation {
return &GroupChatInvitation{GroupChatInvitation: &protobuf.GroupChatInvitation{}}
}
2020-08-07 13:49:37 +00:00
// ID is the Keccak256() contatenation of From-ChatId
func (g *GroupChatInvitation) ID() string {
2020-08-07 13:49:37 +00:00
return types.EncodeHex(crypto.Keccak256([]byte(fmt.Sprintf("%s%s", g.From, g.ChatId))))
}
// GetSigPubKey returns an ecdsa encoded public key
// this function is required to implement the ChatEntity interface
func (g *GroupChatInvitation) GetSigPubKey() *ecdsa.PublicKey {
2020-08-07 13:49:37 +00:00
return g.SigPubKey
}
// GetProtoBuf returns the struct's embedded protobuf struct
// this function is required to implement the ChatEntity interface
func (g *GroupChatInvitation) GetProtobuf() proto.Message {
return g.GroupChatInvitation
2020-08-07 13:49:37 +00:00
}
func (g *GroupChatInvitation) MarshalJSON() ([]byte, error) {
2020-08-07 13:49:37 +00:00
item := struct {
ID string `json:"id"`
ChatID string `json:"chatId,omitempty"`
From string `json:"from"`
IntroductionMessage string `json:"introductionMessage,omitempty"`
State protobuf.GroupChatInvitation_State `json:"state,omitempty"`
}{
ID: g.ID(),
ChatID: g.ChatId,
From: g.From,
IntroductionMessage: g.IntroductionMessage,
State: g.State,
}
return json.Marshal(item)
}