From a5cf1ec7acb2bf955b610944cec209fcef48c20f Mon Sep 17 00:00:00 2001 From: ycc Date: Fri, 2 Dec 2022 23:18:13 +0100 Subject: [PATCH] message helper + io/ioutil deprecation --- client/config.go | 10 +++++----- client/identity.go | 8 +++----- client/message.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 client/message.go diff --git a/client/config.go b/client/config.go index da9d576..c8916b1 100644 --- a/client/config.go +++ b/client/config.go @@ -2,7 +2,7 @@ package client import ( "encoding/json" - "io/ioutil" + "os" "sync" ) @@ -30,8 +30,8 @@ type Config struct { GroupChatNotifiactions bool `json:"group_chat_notifiactions,omitempty"` ChannelNotifications bool `json:"channel_notifications,omitempty"` // Inner - memoryPassword string `json:"memory_password,omitempty"` - identityFile string `json:"config_file,omitempty"` + memoryPassword string + identityFile string } var instance *Config @@ -45,7 +45,7 @@ func GetConfig() *Config { } func (c *Config) Load(filename string) error { - data, err := ioutil.ReadFile(filename) + data, err := os.ReadFile(filename) if err != nil { return err } @@ -61,7 +61,7 @@ func (c *Config) Save(filename string) error { if err != nil { return err } - ioutil.WriteFile(filename, data, 0644) + os.WriteFile(filename, data, 0644) if err != nil { return err } diff --git a/client/identity.go b/client/identity.go index a56ec87..762a9e0 100644 --- a/client/identity.go +++ b/client/identity.go @@ -3,15 +3,13 @@ package client import ( "encoding/json" "errors" - "io/ioutil" + "os" "forge.redroom.link/yves/meowlib" "github.com/ProtonMail/gopenpgp/v2/helper" "github.com/google/uuid" ) -const key = "3pw0c8#6ZG8{75b5;3?fe80$2" - type Identity struct { Nickname string `json:"nickname,omitempty"` RootKp meowlib.KeyPair `json:"id_kp,omitempty"` @@ -113,7 +111,7 @@ func LoadIdentity(filename string, password string) (*Identity, error) { var id Identity GetConfig().memoryPassword = password GetConfig().identityFile = filename - indata, err := ioutil.ReadFile(filename) + indata, err := os.ReadFile(filename) if err != nil { return nil, err } @@ -131,6 +129,6 @@ func (id *Identity) Save() error { if err != nil { return err } - err = ioutil.WriteFile(GetConfig().identityFile, []byte(armor), 0600) + err = os.WriteFile(GetConfig().identityFile, []byte(armor), 0600) return err } diff --git a/client/message.go b/client/message.go new file mode 100644 index 0000000..1821e30 --- /dev/null +++ b/client/message.go @@ -0,0 +1,40 @@ +package client + +import "fmt" + +func MakeText(peer *Peer, text string, srv *InternalServer) ([]byte, error) { + // Creating User message + usermessage, err := peer.BuildSimpleUserMessage([]byte(text)) + if err != nil { + return nil, err + } + serializedMessage, err := peer.SerializeUserMessage(usermessage) + if err != nil { + return nil, err + } + // Encrypting it + EncMsg, EncMsgSignature, FriendServers, err := peer.AsymEncryptMessage(serializedMessage) + if err != nil { + return nil, err + } + fmt.Println(len(FriendServers)) + // Packing it + packedMsg := peer.PackUserMessage(EncMsg, EncMsgSignature) + + // Creating Server message for transporting the user message + toServerMessage, err := srv.BuildMessageSendingMessage(&packedMsg) + if err != nil { + return nil, err + } + // Encrypting it + encToServerMessage, encToServerMessageSignature, err := srv.AsymEncryptMessage(toServerMessage) + if err != nil { + return nil, err + } + // Packing it + protoPackedServerMsg, err := srv.PackServerMessage(encToServerMessage, encToServerMessageSignature) + if err != nil { + return nil, err + } + return protoPackedServerMsg, nil +}