User messages management + png contactcard to protobuf
This commit is contained in:
		@@ -5,6 +5,7 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"forge.redroom.link/yves/meowlib"
 | 
			
		||||
	"google.golang.org/protobuf/proto"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Peer struct {
 | 
			
		||||
@@ -53,6 +54,18 @@ func (pl *PeerList) GetFromName(name string) *Peer {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (p *Peer) BuildSimpleUserUMessage(message []byte) ([]byte, error) {
 | 
			
		||||
	var msg meowlib.UserMessage
 | 
			
		||||
	msg.From = p.Me.Public
 | 
			
		||||
	msg.Data = message
 | 
			
		||||
	msg.Type = "1"
 | 
			
		||||
	out, err := proto.Marshal(&msg)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return out, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AsymEncryptMessage prepares a message to send to a specific peer contact
 | 
			
		||||
func (p *Peer) AsymEncryptMessage(Message []byte) (EncryptedMessage []byte, Signature []byte, Servers []*meowlib.Server, err error) {
 | 
			
		||||
	EncryptedMessage, Signature, err = meowlib.AsymEncryptAndSign(p.Contact.EncryptionPublicKey, p.Me.Private, Message)
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"github.com/makiuchi-d/gozxing"
 | 
			
		||||
	"github.com/makiuchi-d/gozxing/qrcode"
 | 
			
		||||
	"google.golang.org/protobuf/proto"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func ServerFromUrl(url string) *Server {
 | 
			
		||||
@@ -26,10 +27,12 @@ func (Contact *ContactCard) AddUrls(urls []string) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (Contact *ContactCard) WritePng(filename string) {
 | 
			
		||||
	jsonContact, _ := json.Marshal(Contact)
 | 
			
		||||
	//imgdata := base64.StdEncoding.EncodeToString(jsonContact)
 | 
			
		||||
	size := int(math.Sqrt(float64(len(jsonContact))/3)) + 1
 | 
			
		||||
func (contact *ContactCard) WritePng(filename string) {
 | 
			
		||||
	out, err := proto.Marshal(contact)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		println(err)
 | 
			
		||||
	}
 | 
			
		||||
	size := int(math.Sqrt(float64(len(out))/3)) + 1
 | 
			
		||||
	println(size)
 | 
			
		||||
 | 
			
		||||
	// Create a colored i	mage of the given width and height.
 | 
			
		||||
@@ -37,9 +40,9 @@ func (Contact *ContactCard) WritePng(filename string) {
 | 
			
		||||
 | 
			
		||||
	for y := 0; y < size; y++ {
 | 
			
		||||
		for x := 0; x < size*3; x = x + 3 {
 | 
			
		||||
			p1 := uint8(jsonContact[x+y])
 | 
			
		||||
			p2 := uint8(jsonContact[x+y+1])
 | 
			
		||||
			p3 := uint8(jsonContact[x+y+2])
 | 
			
		||||
			p1 := uint8(out[x+y])
 | 
			
		||||
			p2 := uint8(out[x+y+1])
 | 
			
		||||
			p3 := uint8(out[x+y+2])
 | 
			
		||||
			img.Set(x/3, y, color.NRGBA{
 | 
			
		||||
				R: p1,
 | 
			
		||||
				G: p2,
 | 
			
		||||
 
 | 
			
		||||
@@ -18,16 +18,16 @@ func TestEndToEnd(t *testing.T) {
 | 
			
		||||
	Me, err := client.LoadIdentity("id.enc")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Failed : creating New identity...")
 | 
			
		||||
		///////////////////////////
 | 
			
		||||
		// Creating New Identity //
 | 
			
		||||
		///////////////////////////
 | 
			
		||||
		Me = client.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
 | 
			
		||||
		//
 | 
			
		||||
		////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
		// 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
 | 
			
		||||
@@ -35,8 +35,9 @@ func TestEndToEnd(t *testing.T) {
 | 
			
		||||
		fmt.Println(string(a))
 | 
			
		||||
		// TODO : Convert invitation to QR Code
 | 
			
		||||
		invitation.WritePng("invitation.png")
 | 
			
		||||
		//
 | 
			
		||||
		// Simulate peer invitation response : generate the friend's keypair
 | 
			
		||||
		///////////////////////////////////////
 | 
			
		||||
		// Simulate peer invitation response //
 | 
			
		||||
		///////////////////////////////////////
 | 
			
		||||
		fmt.Println("Simulating first friend answer...")
 | 
			
		||||
		var ReceivedContact meowlib.ContactCard
 | 
			
		||||
 | 
			
		||||
@@ -51,9 +52,10 @@ func TestEndToEnd(t *testing.T) {
 | 
			
		||||
		FriendServer1KP := meowlib.NewKeyPair()
 | 
			
		||||
		FriendServer1 := meowlib.Server{Name: "FriendServer1", Url: "http://myfriend.org/meow/", PublicKey: FriendServer1KP.Public, Description: "Fancy description", ConfidenceLevel: 1}
 | 
			
		||||
		ReceivedContact.PullServers = append(ReceivedContact.PullServers, &FriendServer1)
 | 
			
		||||
		// end Friend simulated invitation
 | 
			
		||||
 | 
			
		||||
		// Finalize the contact with the invitation response
 | 
			
		||||
		///////////////////////////////////////////////////////
 | 
			
		||||
		// Finalize the contact with the invitation response //
 | 
			
		||||
		///////////////////////////////////////////////////////
 | 
			
		||||
		Me.FinalizeInvitation(MyFirstFriend, &ReceivedContact)
 | 
			
		||||
		err = Me.Save("id.enc")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
@@ -63,35 +65,58 @@ func TestEndToEnd(t *testing.T) {
 | 
			
		||||
		a, _ = json.Marshal(Me)
 | 
			
		||||
		ioutil.WriteFile("id.json", a, 0644)
 | 
			
		||||
		fmt.Println(string(a))
 | 
			
		||||
 | 
			
		||||
		// create message to simulated friend
 | 
			
		||||
		sentmessage := "Hello friend!"
 | 
			
		||||
		EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage([]byte(sentmessage))
 | 
			
		||||
		/////////////////////////////////////
 | 
			
		||||
		// Create a message to that friend //
 | 
			
		||||
		/////////////////////////////////////
 | 
			
		||||
		textmessage := "Hello friend!"
 | 
			
		||||
		// Creating User message
 | 
			
		||||
		usermessage, err := MyFirstFriend.BuildSimpleUserUMessage([]byte(textmessage))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println(err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		// Encrypting it
 | 
			
		||||
		EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage(usermessage)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println(err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Println(len(FriendServers))
 | 
			
		||||
		// Packing it
 | 
			
		||||
		packedMsg := MyFirstFriend.Pack(EncMsg, EncMsgSignature)
 | 
			
		||||
 | 
			
		||||
		srv := FriendServers[0]
 | 
			
		||||
		intS1 := client.InternalServerFromServer(srv)
 | 
			
		||||
 | 
			
		||||
		// Creating Server message
 | 
			
		||||
		// Creating Server message for transporting the user message
 | 
			
		||||
		toServerMessage, err := intS1.BuildMessageSendingMessage(&packedMsg)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println(err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		// Encrypting it
 | 
			
		||||
		encToServerMessage, encToServerMessageSignature, err := intS1.AsymEncryptMessage(toServerMessage)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println(err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		// Packing it
 | 
			
		||||
		protoPackedServerMsg, err := intS1.PackServerMessage(encToServerMessage, encToServerMessageSignature)
 | 
			
		||||
		// Sending to server => fake action (network protocol trnasfer are not part of meowlib)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println(err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		///////////////////////
 | 
			
		||||
		// Sending to server //
 | 
			
		||||
		///////////////////////
 | 
			
		||||
		//=> fake action (network protocol transfer are not part of meowlib)
 | 
			
		||||
		// You have to implement the netwok layer
 | 
			
		||||
		// Just FYI printing final byte array size. Those bytes will be sent over the network.
 | 
			
		||||
		println(len(protoPackedServerMsg))
 | 
			
		||||
 | 
			
		||||
		//
 | 
			
		||||
		// Simulating server side processing
 | 
			
		||||
		//
 | 
			
		||||
		///////////////////////////////////////
 | 
			
		||||
		// Simulating server side processing //
 | 
			
		||||
		///////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
		//////////////////////////////////////////////
 | 
			
		||||
		// Back to client, decoding server response //
 | 
			
		||||
		//////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
		//
 | 
			
		||||
		// Back to client, decoding server response
 | 
			
		||||
		//
 | 
			
		||||
		decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), EncMsgSignature)
 | 
			
		||||
		if err2 != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user