2022-01-15 22:19:29 +01:00
|
|
|
package meowlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEndToEnd(t *testing.T) {
|
|
|
|
//
|
|
|
|
// Create my own identity
|
|
|
|
//
|
|
|
|
fmt.Println("Trying to load identity from file.")
|
2022-09-02 12:07:21 +02:00
|
|
|
me, err := LoadIdentity("id.enc")
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed : creating New identity...")
|
|
|
|
me = CreateIdentity("myname")
|
|
|
|
|
|
|
|
//
|
|
|
|
// define my preferences (servers)
|
|
|
|
//
|
|
|
|
me.MessageServers.Name = "Message Servers"
|
|
|
|
me.MessageServers.AddUrls([]string{"http://127.0.0.1/meow/", "mqtt://127.0.0.1", "meow://127.0.0.1"})
|
|
|
|
//
|
|
|
|
// create an invitation for a friend, I want him/her to know me as Bender
|
|
|
|
//
|
|
|
|
fmt.Println("Creating an invitation for the first friend...")
|
|
|
|
myFirstFriend, invitation := me.InvitePeer("Bender", "myfirstfriend", []int{1, 2})
|
|
|
|
// print my invitation
|
|
|
|
a, _ := json.Marshal(invitation)
|
|
|
|
fmt.Println(string(a))
|
|
|
|
// TODO : Convert invitation to QR Code
|
2022-03-01 10:37:11 +01:00
|
|
|
invitation.WritePng("invitation.png")
|
2022-01-15 22:19:29 +01:00
|
|
|
//
|
|
|
|
// Simulate peer invitation response : generate the friend's keypair
|
|
|
|
fmt.Println("Simulating first friend answer...")
|
2022-08-29 15:40:29 +02:00
|
|
|
var receivedContact ContactCard
|
2022-09-02 12:07:21 +02:00
|
|
|
|
|
|
|
// Friend simulated invitation
|
2022-01-15 22:19:29 +01:00
|
|
|
firstFriendContactKp := NewKeyPair()
|
|
|
|
firstFriendEncryptionKp := NewKeyPair()
|
|
|
|
firstFriendLookupKp := NewKeyPair()
|
|
|
|
receivedContact.Name = "I'm the friend"
|
|
|
|
receivedContact.ContactPublicKey = firstFriendContactKp.Public
|
|
|
|
receivedContact.EncryptionPublicKey = firstFriendEncryptionKp.Public
|
|
|
|
receivedContact.LookupPublicKey = firstFriendLookupKp.Public
|
|
|
|
var friendsMessageServers ServerList
|
|
|
|
friendsMessageServers.AddUrls([]string{"http://myfriend.org/meow/"})
|
2022-09-02 12:07:21 +02:00
|
|
|
// end Friend simulated invitation
|
|
|
|
|
2022-01-15 22:19:29 +01:00
|
|
|
for _, srv := range friendsMessageServers.Servers {
|
|
|
|
receivedContact.PullServers = append(receivedContact.PullServers, srv.ServerData)
|
|
|
|
}
|
2022-09-02 12:07:21 +02:00
|
|
|
|
2022-08-29 15:40:29 +02:00
|
|
|
// End simulating contact invitation response
|
2022-01-15 22:19:29 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Finalize the contact with the invitation response
|
|
|
|
//
|
|
|
|
me.FinalizeInvitation(myFirstFriend, &receivedContact)
|
2022-09-02 12:07:21 +02:00
|
|
|
err = me.Save("id.enc")
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
a, _ = json.Marshal(me)
|
|
|
|
ioutil.WriteFile("id.json", a, 0644)
|
|
|
|
fmt.Println(string(a))
|
2022-03-01 10:37:11 +01:00
|
|
|
|
2022-09-02 12:07:21 +02:00
|
|
|
// create message to simulated friend
|
|
|
|
sentmessage := "Hello friend!"
|
|
|
|
lookupK, EncMsg, MsgSignature, Servers, err := myFirstFriend.CreateMessage([]byte(sentmessage))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
fmt.Println(lookupK)
|
|
|
|
fmt.Println(len(Servers))
|
|
|
|
// simulated friend decoding the message
|
|
|
|
//ReadMessage
|
|
|
|
|
|
|
|
// simulates if peer can decrypt my message
|
|
|
|
//Message := "toto"
|
|
|
|
//Signature := "test"
|
|
|
|
decMess, err2 := DecryptAndCheck(myFirstFriend.EncryptionKp.Private, myFirstFriend.Contact.EncryptionPublicKey, []byte(EncMsg), MsgSignature)
|
|
|
|
if err2 != nil {
|
|
|
|
fmt.Println(err2.Error())
|
|
|
|
}
|
|
|
|
fmt.Println(decMess)
|
|
|
|
//
|
|
|
|
}
|
2022-01-15 22:19:29 +01:00
|
|
|
|
|
|
|
}
|