meowlib/client/peer.go
2022-09-06 09:30:45 +02:00

84 lines
2.6 KiB
Go

package client
import (
"fmt"
"time"
"forge.redroom.link/yves/meowlib"
)
/*
type ContactCard struct {
Name string `json:"name,omitempty"`
ContactPublicKey string `json:"contact_public_key,omitempty"`
EncryptionPublicKey string `json:"encryption_public_key,omitempty"`
LookupPublicKey string `json:"lookup_public_key,omitempty"`
PullServers []meowlib.Server `json:"pull_servers,omitempty"`
}
*/
type Peer struct {
Name string `json:"name,omitempty"`
// Conversation []InternalMessage `json:"conversation,omitempty"`
// My own keys for that peer
Me meowlib.KeyPair `json:"me,omitempty"`
EncryptionKp meowlib.KeyPair `json:"conversation_kp,omitempty"`
LookupKp meowlib.KeyPair `json:"lookup_kp,omitempty"`
// Peer keys and infos
Contact meowlib.ContactCard `json:"contact,omitempty"`
// Internal management attributes
Visible bool `json:"visible,omitempty"`
VisiblePassword string `json:"visible_password,omitempty"`
PasswordType string `json:"password_type,omitempty"`
Blocked bool `json:"blocked,omitempty"`
MessageNotification string `json:"message_notification,omitempty"`
OnionMode bool `json:"onion_mode,omitempty"`
LastMessage time.Time `json:"last_message,omitempty"`
}
type PeerList []Peer
type Group struct {
Name string `json:"name,omitempty"`
Members []Peer `json:"members,omitempty"`
}
func (pl *PeerList) GetFromPublicKey(publickey string) *Peer {
for _, peer := range *pl {
if peer.Contact.ContactPublicKey == publickey {
return &peer
}
}
return nil
}
func (pl *PeerList) GetFromName(name string) *Peer {
for _, peer := range *pl {
if peer.Contact.Name == name {
return &peer
}
}
return nil
}
func (p *Peer) AsymEncryptMessage(Message []byte) (lookupK string, EncryptedMsg []byte, Signature []byte, Servers []*meowlib.Server, err error) {
// prepares a message to send to a specific peer contact
EncryptedMsg, Signature, err = meowlib.EncryptAndSign(p.Contact.EncryptionPublicKey, p.Me.Private, Message)
if err != nil {
fmt.Println(err.Error())
return "", nil, nil, nil, err
}
return p.LookupKp.Public, EncryptedMsg, Signature, p.Contact.PullServers, err
}
func (p *Peer) AsymDecryptMessage(Message []byte, Signature []byte) (DecryptedMsg []byte, err error) {
// reads a message from a specific peer contact
DecryptedMsg, err = meowlib.DecryptAndCheck(p.Me.Private, p.Contact.ContactPublicKey, Message, Signature)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
return DecryptedMsg, err
}