diff --git a/client/helpers/invitationAnswerHelper.go b/client/helpers/invitationAnswerHelper.go index 932a739..159e93b 100644 --- a/client/helpers/invitationAnswerHelper.go +++ b/client/helpers/invitationAnswerHelper.go @@ -11,8 +11,6 @@ import ( ) import ( "errors" - - "google.golang.org/protobuf/proto" ) // InvitationAnswer @@ -90,7 +88,7 @@ func InvitationAnswerFile(invitationFile string, nickname string, myNickname str } // InvitationAnswerMessage -func InvitationAnswerMessage(invitationId string, invitationServerUid string) ([]byte, string, error) { +func InvitationAnswerMessage(invitationId string, invitationServerUid string, timeout int) ([]byte, string, error) { // find the peer with that invitation id var peer *client.Peer @@ -121,19 +119,35 @@ func InvitationAnswerMessage(invitationId string, invitationServerUid string) ([ return nil, "InvitationAnswerMessage: ProcessOutboundUserMessage", err } // Creating Server message for transporting the user message - toServerMessage := invitationServer.BuildToServerMessageFromUserMessage(packedMsg) - // move the payload to invitation - toServerMessage.Messages = nil - invitationPayload, err := proto.Marshal(packedMsg) + toServerMessage, err := invitationServer.BuildToServerMessageInvitationAnswer(packedMsg, peer.MyIdentity.Public, timeout) if err != nil { - return nil, "InvitationAnswerMessage: proto.Marshal", err + return nil, "InvitationAnswerMessage: BuildToServerMessageInvitationAnswer", err } - toServerMessage.Invitation.Payload = invitationPayload - toServerMessage.Invitation = &meowlib.Invitation{Step: 3} - toServerMessage.Invitation.From = peer.MyIdentity.Public + + // Server outbound processing bytemsg, err := invitationServer.ProcessOutboundMessage(toServerMessage) if err != nil { return nil, "InvitationAnswerMessage: ProcessOutboundMessage", err } return bytemsg, "", nil } + +// InvitationAnswerMessageReadResponse +// Called by the invitation receiver +// invitationData: the data received from the server +// invitationServerUid: the uid of the server holding the invitation +func InvitationAnswerMessageReadResponse(invitationData []byte, invitationServerUid string) (*meowlib.Invitation, string, error) { + + server, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid) + if err != nil { + return nil, "InvitationGetMessageReadResponse: LoadServer", err + } + // Server inbound processing : get the invitation server + serverMsg, err := server.ProcessInboundServerResponse(invitationData) + if err != nil { + return nil, "InvitationGetMessageReadResponse: ProcessInboundServerResponse", err + } + + return serverMsg.Invitation, "", nil + +} diff --git a/client/helpers/invitationFinalizeHelper.go b/client/helpers/invitationFinalizeHelper.go new file mode 100644 index 0000000..5c2b7a7 --- /dev/null +++ b/client/helpers/invitationFinalizeHelper.go @@ -0,0 +1,72 @@ +package helpers + +import ( + "forge.redroom.link/yves/meowlib" + "forge.redroom.link/yves/meowlib/client" + "google.golang.org/protobuf/proto" +) + +// InvitationGetAnswer +// Called by the invitation initiator +// invitationId: the id of the invitation +// serverUid: the uid of the server holding the invitation answer +func InvitationGetAnswer(invitationId string, serverUid string) ([]byte, string, error) { + + // check if already in msg servers + srv, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(serverUid) + if err != nil { + return nil, "InvitationGetAnswer: LoadServer", err + } + + // buildserver message + toSrvMsg, err := srv.BuildToServerMessageInvitationAnswerRequest(invitationId) + if err != nil { + return nil, "InvitationGetAnswer: BuildToServerMessageInvitationRequest", err + } + // processoutbound + bytemsg, err := srv.ProcessOutboundMessage(toSrvMsg) + if err != nil { + return nil, "InvitationGetAnswer: ProcessOutboundMessage", err + } + + return bytemsg, "", nil +} + +// InvitationGetAnswerReadResponse +// Called by the invitation initiator +// invitationAnswerData: the data received from the server +// invitationServerUid: the uid of the server holding the invitation +func InvitationGetAnswerReadResponse(invitationAnswerData []byte, invitationServerUid string) (string, error) { + + server, err := client.GetConfig().GetIdentity().MessageServers.LoadServer(invitationServerUid) + if err != nil { + return "InvitationGetAnswerReadResponse: LoadServer", err + } + // Server inbound processing : get the invitation server + serverMsg, err := server.ProcessInboundServerResponse(invitationAnswerData) + if err != nil { + return "InvitationGetAnswerReadResponse: ProcessInboundServerResponse", err + } + // fmt.Println("Inbound OK, Invitation Step: ", serverMsg.Invitation.Step, len(serverMsg.Invitation.Payload)) + // fmt.Println("Invitation Check") + // fmt.Println(hex.EncodeToString(serverMsg.Invitation.Payload)) + // contactCard decode + + // decode the payload + var invitationAnswer meowlib.PackedUserMessage + err = proto.Unmarshal(serverMsg.Invitation.Payload, &invitationAnswer) + if err != nil { + return "InvitationGetAnswerReadResponse: Unmarshal", err + } + // retreive user public key to check usermessage signature + //contactPublikKey := serverMsg.Invitation.From + + // process the packed user message + + // finalize the invitation + // id := client.GetConfig().GetIdentity() + + // cc, err := id.FinalizeInvitation(serverMsg.Invitation.) + + return "", nil +} diff --git a/client/server.go b/client/server.go index c77ce5b..30e30f9 100644 --- a/client/server.go +++ b/client/server.go @@ -198,6 +198,36 @@ func (ints *Server) BuildToServerMessageInvitationRequest(shortcode string, pass return &msg, nil } +// BuildToServerMessageInvitationAnswer creates an invitation answer to server and returns it as a meowlib.ToServerMessage +// it takes as input a contactcard generated by Identity.InvitePeer +func (ints *Server) BuildToServerMessageInvitationAnswer(invitationAnswer *meowlib.PackedUserMessage, myPublicKeyForThatPeer string, timeout int) (*meowlib.ToServerMessage, error) { + var msg meowlib.ToServerMessage + var inv meowlib.Invitation + invitationPayload, err := proto.Marshal(invitationAnswer) + if err != nil { + return nil, err + } + inv.Step = 3 + msg.Type = "1" + msg.From = ints.UserKp.Public + inv.From = myPublicKeyForThatPeer + inv.Payload = invitationPayload + msg.Invitation = &inv + return &msg, nil +} + +// BuildToServerMessageInvitationAnswerRequest requests invitation answer with provided id from server and returns it as a meowlib.ToServerMessage +func (ints *Server) BuildToServerMessageInvitationAnswerRequest(invitationId string) (*meowlib.ToServerMessage, error) { + var msg meowlib.ToServerMessage + var inv meowlib.Invitation + msg.Type = "1" + msg.From = ints.UserKp.Public + inv.Step = 4 + inv.Uuid = invitationId + msg.Invitation = &inv + return &msg, nil +} + // PackServerMessage func (ints *Server) PackServerMessage(payload []byte, signature []byte) (protoPackedMessage []byte, err error) { var msg meowlib.PackedServerMessage diff --git a/server/router.go b/server/router.go index 7326fbf..32c50f7 100644 --- a/server/router.go +++ b/server/router.go @@ -145,18 +145,27 @@ func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) (*meowlib.FromServerMe // accept invitation => store accepted invitation for initiator case 3: - expiry := r.StoreAnswerToInvitation(msg.Invitation.Uuid, int(msg.Invitation.Timeout), msg.Invitation.Payload, r.InvitationTimeout) + data, err := proto.Marshal(msg.Invitation) + if err != nil { + return nil, err + } + expiry := r.StoreAnswerToInvitation(msg.Invitation.Uuid, int(msg.Invitation.Timeout), data, r.InvitationTimeout) from_server.Invitation = &meowlib.Invitation{} from_server.Invitation.Expiry = expiry.UTC().Unix() // get accepted invitation => send accepted invitation to initiator case 4: from_server.Invitation = &meowlib.Invitation{} - answer, err := r.GetAnswerToInvitation(msg.Invitation.Uuid) + var answer meowlib.Invitation + storedAanswer, err := r.GetAnswerToInvitation(msg.Invitation.Uuid) if err != nil { from_server.Invitation.Payload = []byte("invitation answer not found") } else { - from_server.Invitation.Payload = answer + err := proto.Unmarshal(storedAanswer, &answer) + if err != nil { + from_server.Invitation.Payload = []byte("invitation answer corrupted") + } + from_server.Invitation = &answer } }