package meowlib import ( "encoding/json" "io/ioutil" "github.com/ProtonMail/gopenpgp/v2/helper" ) const key = "3pw0c8#6ZG8{75b5;3?fe80$2" type Identity struct { Nickname string `json:"nickname,omitempty"` PublicKey string `json:"public_key,omitempty"` PrivateKey string `json:"private_key,omitempty"` Status string `json:"status,omitempty"` Peers PeerList `json:"peers,omitempty"` KnownServers ServerList `json:"known_servers,omitempty"` MessageServers ServerList `json:"message_servers,omitempty"` } func CreateIdentity(nickname string) *Identity { var id Identity id.Nickname = nickname kp := NewKeyPair() id.PublicKey = kp.Public id.PrivateKey = kp.Private return &id } func (id *Identity) InvitePeer(myName string, contactName string, messageServerIdxs []int) (*Peer, *Contact) { var peer Peer var myContactCard Contact peer.Me = NewKeyPair() peer.EncryptionKp = NewKeyPair() peer.LookupKp = NewKeyPair() peer.Name = contactName for _, i := range messageServerIdxs { myContactCard.PullServers = append(myContactCard.PullServers, id.MessageServers.Servers[i].ServerData) } myContactCard.Name = myName 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 } func (*Identity) FinalizeInvitation(peer *Peer, receivedContact *Contact) { peer.Contact = *receivedContact } func (*Identity) AddPeer(name string, peerPublicKey string) string { var peer Peer peer.Me = NewKeyPair() peer.Contact.Name = name peer.Contact.ContactPublicKey = peerPublicKey return peer.Me.Public } 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)) if err == nil { err = json.Unmarshal([]byte(pass), &id) return nil, err } return &id, err } 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 }