diff --git a/main.go b/main.go index 9e5d8932e68f54b251dc1955cc98168e3e18a7bc..23fa71270fbdeb94f47fe0f2c9bbd930bd21b0cf 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 0000000000000000000000000000000000000000..9ff8180ed55d2fa2ce7ed2f3558aade2c4900bb8 --- /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 0000000000000000000000000000000000000000..e0f1beb53b19e600cedf434724b4d8520d5c94b0 --- /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 +}