Skip to content
Snippets Groups Projects
Commit bbd19c36 authored by Jono Wenger's avatar Jono Wenger
Browse files

Implement authenticatedConnection.go

parent 32b48d82
No related branches found
No related tags found
1 merge request!60Revert "Fail a test to be sure it works"
////////////////////////////////////////////////////////////////////////////////
// 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)
}
////////////////////////////////////////////////////////////////////////////////
// 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)
}
}
}
////////////////////////////////////////////////////////////////////////////////
// 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 (
......
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment