From bbd19c362903fbe3e732ed4cbb446834f5dc0e34 Mon Sep 17 00:00:00 2001 From: Jono Wenger <jono@elixxir.io> Date: Tue, 16 Aug 2022 14:02:11 -0700 Subject: [PATCH] Implement authenticatedConnection.go --- wasm/authenticatedConnection.go | 64 ++++++++++++++++++++++++++++ wasm/authenticatedConnection_test.go | 35 +++++++++++++++ wasm/broadcast_test.go | 9 ++++ wasm/cmix.go | 3 ++ wasm/connect.go | 14 +++--- 5 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 wasm/authenticatedConnection.go create mode 100644 wasm/authenticatedConnection_test.go diff --git a/wasm/authenticatedConnection.go b/wasm/authenticatedConnection.go new file mode 100644 index 00000000..9742895e --- /dev/null +++ b/wasm/authenticatedConnection.go @@ -0,0 +1,64 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +//go:build js && wasm + +package wasm + +import ( + "gitlab.com/elixxir/client/bindings" + "syscall/js" +) + +// AuthenticatedConnection wraps the [bindings.AuthenticatedConnection] object +// so its methods can be wrapped to be Javascript compatible. +type AuthenticatedConnection struct { + api *bindings.AuthenticatedConnection +} + +// newAuthenticatedConnectionJS creates a new Javascript compatible object +// (map[string]interface{}) that matches the AuthenticatedConnection structure. +func newAuthenticatedConnectionJS( + api *bindings.AuthenticatedConnection) map[string]interface{} { + ac := AuthenticatedConnection{api} + acMap := map[string]interface{}{ + "IsAuthenticated": js.FuncOf(ac.IsAuthenticated), + } + + return acMap +} + +func (ac *AuthenticatedConnection) IsAuthenticated(js.Value, []js.Value) interface{} { + return ac.api.IsAuthenticated() +} + +// ConnectWithAuthentication is called by the client (i.e., the one establishing +// connection with the server). Once a connect.Connection has been established +// with the server, it then authenticates their identity to the server. +// accepts a marshalled ReceptionIdentity and contact.Contact object +// +// Parameters: +// - args[0] - ID of E2e object in tracker (int) +// - args[1] - marshalled recipient [contact.Contact] (Uint8Array). +// - args[3] - JSON of [xxdk.E2EParams] (Uint8Array). +// +// Returns: +// - Javascript representation of the Connection object +// - throws a TypeError if creating loading the parameters or connecting fails +func (c *Cmix) ConnectWithAuthentication(_ js.Value, args []js.Value) interface{} { + recipientContact := CopyBytesToGo(args[1]) + e2eParamsJSON := CopyBytesToGo(args[2]) + + ac, err := c.api.ConnectWithAuthentication( + args[0].Int(), recipientContact, e2eParamsJSON) + if err != nil { + Throw(TypeError, err.Error()) + return nil + } + + return newAuthenticatedConnectionJS(ac) +} diff --git a/wasm/authenticatedConnection_test.go b/wasm/authenticatedConnection_test.go new file mode 100644 index 00000000..e700d3f9 --- /dev/null +++ b/wasm/authenticatedConnection_test.go @@ -0,0 +1,35 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +package wasm + +import ( + "gitlab.com/elixxir/client/bindings" + "reflect" + "testing" +) + +// Tests that the map representing AuthenticatedConnection returned by +// newAuthenticatedConnectionJS contains all of the methods on +// AuthenticatedConnection. +func Test_newAuthenticatedConnectionJS(t *testing.T) { + acType := reflect.TypeOf(&AuthenticatedConnection{}) + + ch := newAuthenticatedConnectionJS(&bindings.AuthenticatedConnection{}) + if len(ch) != acType.NumMethod() { + t.Errorf("AuthenticatedConnection JS object does not have all methods."+ + "\nexpected: %d\nreceived: %d", acType.NumMethod(), len(ch)) + } + + for i := 0; i < acType.NumMethod(); i++ { + method := acType.Method(i) + + if _, exists := ch[method.Name]; !exists { + t.Errorf("Method %s does not exist.", method.Name) + } + } +} diff --git a/wasm/broadcast_test.go b/wasm/broadcast_test.go index 39600d7f..e240f260 100644 --- a/wasm/broadcast_test.go +++ b/wasm/broadcast_test.go @@ -1,3 +1,12 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +//go:build js && wasm + package wasm import ( diff --git a/wasm/cmix.go b/wasm/cmix.go index c27142f6..3c26acd0 100644 --- a/wasm/cmix.go +++ b/wasm/cmix.go @@ -50,6 +50,9 @@ func newCmixJS(api *bindings.Cmix) map[string]interface{} { // delivery.go "WaitForRoundResult": js.FuncOf(c.WaitForRoundResult), + + // authenticatedConnection.go + "ConnectWithAuthentication": js.FuncOf(c.ConnectWithAuthentication), } return cmix diff --git a/wasm/connect.go b/wasm/connect.go index 3d053cf9..1149fbe4 100644 --- a/wasm/connect.go +++ b/wasm/connect.go @@ -22,8 +22,8 @@ type Connection struct { // newConnectJS creates a new Javascript compatible object // (map[string]interface{}) that matches the Connection structure. -func newConnectJS(conn *bindings.Connection) map[string]interface{} { - c := Connection{conn} +func newConnectJS(api *bindings.Connection) map[string]interface{} { + c := Connection{api} connection := map[string]interface{}{ // connect.go "GetID": js.FuncOf(c.GetID), @@ -51,9 +51,9 @@ func (c *Connection) GetID(js.Value, []js.Value) interface{} { // [partner.Manager] is confirmed. // // Parameters: -// - args[0] - ID of the E2E object in the E2E tracker (int) -// - args[1] - marshalled recipient [contact.Contact] object (Uint8Array) -// - args[3] - JSON of [xxdk.E2EParams] (Uint8Array) +// - args[0] - ID of the E2E object in the E2E tracker (int). +// - args[1] - marshalled recipient [contact.Contact] (Uint8Array). +// - args[3] - JSON of [xxdk.E2EParams] (Uint8Array). // // Returns: // - Javascript representation of the Connection object @@ -61,13 +61,13 @@ func (c *Connection) GetID(js.Value, []js.Value) interface{} { func (c *Cmix) Connect(_ js.Value, args []js.Value) interface{} { recipientContact := CopyBytesToGo(args[1]) e2eParamsJSON := CopyBytesToGo(args[2]) - conn, err := c.api.Connect(args[0].Int(), recipientContact, e2eParamsJSON) + api, err := c.api.Connect(args[0].Int(), recipientContact, e2eParamsJSON) if err != nil { Throw(TypeError, err.Error()) return nil } - return newConnectJS(conn) + return newConnectJS(api) } // SendE2E is a wrapper for sending specifically to the Connection's -- GitLab