2022-09-06 09:30:45 +02:00
|
|
|
package client
|
2022-01-15 22:19:29 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2022-09-06 09:30:45 +02:00
|
|
|
"forge.redroom.link/yves/meowlib"
|
2022-01-15 22:19:29 +01:00
|
|
|
"github.com/ProtonMail/gopenpgp/v2/helper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const key = "3pw0c8#6ZG8{75b5;3?fe80$2"
|
2021-10-18 21:05:44 +02:00
|
|
|
|
|
|
|
type Identity struct {
|
2022-09-06 09:30:45 +02:00
|
|
|
Nickname string `json:"nickname,omitempty"`
|
2022-09-06 17:07:35 +02:00
|
|
|
RootKp meowlib.KeyPair `json:"id_kp,omitempty"`
|
2022-09-06 09:30:45 +02:00
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
Peers PeerList `json:"peers,omitempty"`
|
|
|
|
KnownServers InternalServerList `json:"known_servers,omitempty"`
|
|
|
|
MessageServers InternalServerList `json:"message_servers,omitempty"`
|
2021-10-18 21:05:44 +02:00
|
|
|
}
|
|
|
|
|
2022-01-15 22:19:29 +01:00
|
|
|
func CreateIdentity(nickname string) *Identity {
|
2021-10-18 21:05:44 +02:00
|
|
|
var id Identity
|
2022-01-15 22:19:29 +01:00
|
|
|
id.Nickname = nickname
|
2022-09-06 17:07:35 +02:00
|
|
|
id.RootKp = meowlib.NewKeyPair()
|
2022-01-15 22:19:29 +01:00
|
|
|
return &id
|
2021-10-18 21:05:44 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerIdxs []int) (*Peer, *meowlib.ContactCard) {
|
2021-10-18 21:05:44 +02:00
|
|
|
var peer Peer
|
2022-09-06 09:30:45 +02:00
|
|
|
var myContactCard meowlib.ContactCard
|
|
|
|
peer.Me = meowlib.NewKeyPair()
|
|
|
|
peer.EncryptionKp = meowlib.NewKeyPair()
|
|
|
|
peer.LookupKp = meowlib.NewKeyPair()
|
2022-09-06 17:07:35 +02:00
|
|
|
peer.Name = ContactName
|
|
|
|
for _, i := range MessageServerIdxs {
|
2022-09-06 09:30:45 +02:00
|
|
|
srv := id.MessageServers.Servers[i].ServerData
|
|
|
|
myContactCard.PullServers = append(myContactCard.PullServers, &srv)
|
2022-01-15 22:19:29 +01:00
|
|
|
}
|
2022-09-06 17:07:35 +02:00
|
|
|
myContactCard.Name = MyName
|
2022-01-15 22:19:29 +01:00
|
|
|
myContactCard.ContactPublicKey = peer.Me.Public
|
|
|
|
myContactCard.EncryptionPublicKey = peer.EncryptionKp.Public
|
|
|
|
myContactCard.LookupPublicKey = peer.LookupKp.Public
|
|
|
|
|
|
|
|
id.Peers = append(id.Peers, peer)
|
|
|
|
|
|
|
|
return &id.Peers[len(id.Peers)-1], &myContactCard
|
2021-10-18 21:05:44 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
func (*Identity) FinalizeInvitation(peer *Peer, ReceivedContact *meowlib.ContactCard) {
|
|
|
|
peer.Contact = *ReceivedContact
|
2022-01-15 22:19:29 +01:00
|
|
|
|
2021-10-18 21:05:44 +02:00
|
|
|
}
|
|
|
|
|
2022-01-15 22:19:29 +01:00
|
|
|
func LoadIdentity(file string) (*Identity, error) {
|
|
|
|
var id Identity
|
|
|
|
indata, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pass, err := helper.DecryptMessageWithPassword([]byte(key), string(indata))
|
2022-09-02 12:07:21 +02:00
|
|
|
if err != nil {
|
2022-01-15 22:19:29 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-02 12:07:21 +02:00
|
|
|
err = json.Unmarshal([]byte(pass), &id)
|
2022-01-15 22:19:29 +01:00
|
|
|
return &id, err
|
|
|
|
}
|
2021-10-18 21:05:44 +02:00
|
|
|
|
2022-01-15 22:19:29 +01:00
|
|
|
func (id *Identity) Save(file string) error {
|
|
|
|
b, _ := json.Marshal(id)
|
|
|
|
armor, err := helper.EncryptMessageWithPassword([]byte(key), string(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(file, []byte(armor), 0644)
|
|
|
|
return err
|
2021-10-18 21:05:44 +02:00
|
|
|
}
|