From 85bc267dcdc3b7822600b316f4a5acd5c3eb57d7 Mon Sep 17 00:00:00 2001 From: Jono Wenger <jono@elixxir.io> Date: Tue, 16 Aug 2022 16:20:30 -0700 Subject: [PATCH] Implement restlike.go and restlikeSingle.go --- main.go | 10 +++++ wasm/restlike.go | 69 ++++++++++++++++++++++++++++++++++ wasm/restlikeSingle.go | 84 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 wasm/restlike.go create mode 100644 wasm/restlikeSingle.go diff --git a/main.go b/main.go index 9e5d8932..23fa7127 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,16 @@ func main() { // bindings/group.go js.Global().Set("NewGroupChat", js.FuncOf(wasm.NewGroupChat)) + // bindings/restlike.go + js.Global().Set("RestlikeRequest", js.FuncOf(wasm.RestlikeRequest)) + js.Global().Set("RestlikeRequestAuth", js.FuncOf(wasm.RestlikeRequestAuth)) + + // bindings/restlikeSingle.go + js.Global().Set("RequestRestLike", + js.FuncOf(wasm.RequestRestLike)) + js.Global().Set("AsyncRequestRestLike", + js.FuncOf(wasm.AsyncRequestRestLike)) + <-make(chan bool) os.Exit(0) } diff --git a/wasm/restlike.go b/wasm/restlike.go new file mode 100644 index 00000000..9ff8180e --- /dev/null +++ b/wasm/restlike.go @@ -0,0 +1,69 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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" +) + +// RestlikeRequest performs a normal restlike request. +// +// Parameters: +// - args[0] - ID of Cmix object in tracker (int). +// - args[1] - ID of Connection object in tracker (int). +// - args[2] - JSON of [bindings.RestlikeMessage] (Uint8Array). +// - args[3] - JSON of [xxdk.E2EParams] (Uint8Array). +// +// Returns: +// - JSON of [bindings.RestlikeMessage] (Uint8Array). +// - Throws a TypeError if parsing the parameters or making the request fails. +func RestlikeRequest(_ js.Value, args []js.Value) interface{} { + cmixId := args[0].Int() + connectionID := args[1].Int() + request := CopyBytesToGo(args[2]) + e2eParamsJSON := CopyBytesToGo(args[3]) + + msg, err := bindings.RestlikeRequest( + cmixId, connectionID, request, e2eParamsJSON) + if err != nil { + Throw(TypeError, err.Error()) + return nil + } + + return CopyBytesToJS(msg) +} + +// RestlikeRequestAuth performs an authenticated restlike request. +// +// Parameters: +// - args[0] - ID of Cmix object in tracker (int). +// - args[1] - ID of AuthenticatedConnection object in tracker (int). +// - args[2] - JSON of [bindings.RestlikeMessage] (Uint8Array). +// - args[3] - JSON of [xxdk.E2EParams] (Uint8Array). +// +// Returns: +// - JSON of [bindings.RestlikeMessage] (Uint8Array). +// - Throws a TypeError if parsing the parameters or making the request fails. +func RestlikeRequestAuth(_ js.Value, args []js.Value) interface{} { + cmixId := args[0].Int() + authConnectionID := args[1].Int() + request := CopyBytesToGo(args[2]) + e2eParamsJSON := CopyBytesToGo(args[3]) + + msg, err := bindings.RestlikeRequestAuth( + cmixId, authConnectionID, request, e2eParamsJSON) + if err != nil { + Throw(TypeError, err.Error()) + return nil + } + + return CopyBytesToJS(msg) +} diff --git a/wasm/restlikeSingle.go b/wasm/restlikeSingle.go new file mode 100644 index 00000000..e0f1beb5 --- /dev/null +++ b/wasm/restlikeSingle.go @@ -0,0 +1,84 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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" +) + +// RestlikeCallback wraps Javascript callbacks to adhere to the +// [bindings.RestlikeCallback] interface. +type restlikeCallback struct { + callback func(args ...interface{}) js.Value +} + +func (rlc *restlikeCallback) Callback(payload []byte, err error) { + rlc.callback(CopyBytesToJS(payload), err.Error()) +} + +// RequestRestLike sends a restlike request to a given contact. +// +// Parameters: +// - args[0] - ID of E2e object in tracker (int). +// - args[1] - marshalled recipient [contact.Contact] (Uint8Array). +// - args[2] - JSON of [bindings.RestlikeMessage] (Uint8Array). +// - args[3] - JSON of [single.RequestParams] (Uint8Array). +// +// Returns: +// - JSON of [restlike.Message] (Uint8Array). +// - Throws a TypeError if parsing the parameters or making the request fails. +func RequestRestLike(_ js.Value, args []js.Value) interface{} { + e2eID := args[0].Int() + recipient := CopyBytesToGo(args[1]) + request := CopyBytesToGo(args[2]) + paramsJSON := CopyBytesToGo(args[3]) + + msg, err := bindings.RequestRestLike(e2eID, recipient, request, paramsJSON) + if err != nil { + Throw(TypeError, err.Error()) + return nil + } + + return CopyBytesToJS(msg) +} + +// AsyncRequestRestLike sends an asynchronous restlike request to a given +// contact. +// +// The RestlikeCallback will be called with the results of JSON marshalling the +// response when received. +// +// Parameters: +// - args[0] - ID of E2e object in tracker (int). +// - args[1] - marshalled recipient [contact.Contact] (Uint8Array). +// - args[2] - JSON of [bindings.RestlikeMessage] (Uint8Array). +// - args[3] - JSON of [single.RequestParams] (Uint8Array). +// - args[4] - Javascript object that has functions that implement the +// [bindings.RestlikeCallback] interface. +// +// Returns: +// - Throws a TypeError if parsing the parameters or making the request fails. +func AsyncRequestRestLike(_ js.Value, args []js.Value) interface{} { + e2eID := args[0].Int() + recipient := CopyBytesToGo(args[1]) + request := CopyBytesToGo(args[2]) + paramsJSON := CopyBytesToGo(args[3]) + cb := &restlikeCallback{args[4].Get("Callback").Invoke} + + err := bindings.AsyncRequestRestLike( + e2eID, recipient, request, paramsJSON, cb) + if err != nil { + Throw(TypeError, err.Error()) + return nil + } + + return nil +} -- GitLab