From 6663cc6ad2c8f91565f2655481a6085ce50e302c Mon Sep 17 00:00:00 2001 From: Jono Wenger <jono@elixxir.io> Date: Fri, 9 Sep 2022 09:36:23 -0700 Subject: [PATCH] Fix request function --- main.go | 11 +--------- utils/array.go | 17 +++++++++++++++ utils/utils.go | 16 +++++++------- wasm/e2e.go | 2 +- wasm/e2eAuth.go | 56 ++++++++++++++++++++++++++++++------------------- 5 files changed, 61 insertions(+), 41 deletions(-) diff --git a/main.go b/main.go index e44e8571..a3c8678b 100644 --- a/main.go +++ b/main.go @@ -11,8 +11,6 @@ package main import ( "fmt" - "github.com/pkg/errors" - jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/bindings" "gitlab.com/elixxir/xxdk-wasm/utils" "gitlab.com/elixxir/xxdk-wasm/wasm" @@ -27,6 +25,7 @@ func main() { // utils/array.go js.Global().Set("Uint8ArrayToBase64", js.FuncOf(utils.Uint8ArrayToBase64)) js.Global().Set("Base64ToUint8Array", js.FuncOf(utils.Base64ToUint8Array)) + js.Global().Set("Uint8ArrayEquals", js.FuncOf(utils.Uint8ArrayEquals)) // wasm/backup.go js.Global().Set("NewCmixFromBackup", js.FuncOf(wasm.NewCmixFromBackup)) @@ -126,14 +125,6 @@ func main() { js.Global().Set("GetGitVersion", js.FuncOf(wasm.GetGitVersion)) js.Global().Set("GetDependencies", js.FuncOf(wasm.GetDependencies)) - defer func() { - jww.CRITICAL.Printf("Before recover\n") - if rec := recover(); rec != nil { - utils.Throw(utils.TypeError, errors.Errorf(fmt.Sprintf("%+v", rec))) - } - jww.CRITICAL.Printf("After recover\n") - }() - <-make(chan bool) os.Exit(0) } diff --git a/utils/array.go b/utils/array.go index 1157dce2..9ec8df39 100644 --- a/utils/array.go +++ b/utils/array.go @@ -8,6 +8,7 @@ package utils import ( + "bytes" "encoding/base64" "syscall/js" ) @@ -39,3 +40,19 @@ func Base64ToUint8Array(_ js.Value, args []js.Value) interface{} { return CopyBytesToJS(b) } + +// Uint8ArrayEquals returns true if the two Uint8Array are equal and false +// otherwise. +// +// Parameters: +// - args[0] - array A (Uint8Array) +// - args[1] - array B (Uint8Array) +// +// Returns: +// - If the two arrays are equal (boolean). +func Uint8ArrayEquals(_ js.Value, args []js.Value) interface{} { + a := CopyBytesToGo(args[0]) + b := CopyBytesToGo(args[1]) + + return bytes.Equal(a, b) +} diff --git a/utils/utils.go b/utils/utils.go index 47066401..4c0323a1 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -24,6 +24,11 @@ import ( "syscall/js" ) +var ( + promiseConstructor = js.Global().Get("Promise") + Uint8Array = js.Global().Get("Uint8Array") +) + // CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is // a wrapper for js.CopyBytesToGo to make it more convenient. func CopyBytesToGo(src js.Value) []byte { @@ -35,7 +40,7 @@ func CopyBytesToGo(src js.Value) []byte { // CopyBytesToJS copies the []byte to a Uint8Array stored in a js.Value. This is // a wrapper for js.CopyBytesToJS to make it more convenient. func CopyBytesToJS(src []byte) js.Value { - dst := js.Global().Get("Uint8Array").New(len(src)) + dst := Uint8Array.New(len(src)) js.CopyBytesToJS(dst, src) return dst } @@ -95,20 +100,15 @@ type PromiseFn func(resolve, reject func(args ...interface{}) js.Value) func CreatePromise(f PromiseFn) interface{} { // Create handler for promise (this will be a Javascript function) handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} { - // It receives two arguments, which are JS functions: resolve and reject - resolve := args[0] - reject := args[1] - // Spawn a new go routine to perform the blocking function - go func() { + go func(resolve, reject js.Value) { f(resolve.Invoke, reject.Invoke) - }() + }(args[0], args[1]) return nil }) // Create and return the Promise object - promiseConstructor := js.Global().Get("Promise") return promiseConstructor.New(handler) } diff --git a/wasm/e2e.go b/wasm/e2e.go index 06d02154..70a66436 100644 --- a/wasm/e2e.go +++ b/wasm/e2e.go @@ -222,8 +222,8 @@ func (a *authCallbacks) Confirm( a.confirm(utils.CopyBytesToJS(contact), utils.CopyBytesToJS(receptionId), ephemeralId, roundId) } - } + func (a *authCallbacks) Reset( contact, receptionId []byte, ephemeralId, roundId int64) { if a.reset != nil { diff --git a/wasm/e2eAuth.go b/wasm/e2eAuth.go index abcff102..3d6efa43 100644 --- a/wasm/e2eAuth.go +++ b/wasm/e2eAuth.go @@ -38,7 +38,7 @@ import ( // // Returns: // - A promise that returns the ID of the round (int). -// - Throws error if the request fails. +// - Throws an error if the request fails. func (e *E2e) Request(_ js.Value, args []js.Value) interface{} { partnerContact := utils.CopyBytesToGo(args[0]) factsListJson := utils.CopyBytesToGo(args[1]) @@ -75,17 +75,21 @@ func (e *E2e) Request(_ js.Value, args []js.Value) interface{} { // - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array). // // Returns: -// - ID of the round (int). -// - Throws TypeError if the confirmation fails. +// - A promise that returns the ID of the round (int). +// - Throws an error if the confirmation fails. func (e *E2e) Confirm(_ js.Value, args []js.Value) interface{} { partnerContact := utils.CopyBytesToGo(args[0]) - rid, err := e.api.Confirm(partnerContact) - if err != nil { - utils.Throw(utils.TypeError, err) - return nil + + promiseFn := func(resolve, reject func(args ...interface{}) js.Value) { + rid, err := e.api.Confirm(partnerContact) + if err != nil { + reject(utils.JsTrace(err)) + } else { + resolve(rid) + } } - return rid + return utils.CreatePromise(promiseFn) } // Reset sends a contact reset request from the user identity in the imported @@ -106,17 +110,21 @@ func (e *E2e) Confirm(_ js.Value, args []js.Value) interface{} { // - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array). // // Returns: -// - ID of the round (int). -// - Throws TypeError if the reset fails. +// - A promise that returns the ID of the round (int). +// - Throws an error if the reset fails. func (e *E2e) Reset(_ js.Value, args []js.Value) interface{} { partnerContact := utils.CopyBytesToGo(args[0]) - rid, err := e.api.Reset(partnerContact) - if err != nil { - utils.Throw(utils.TypeError, err) - return nil + + promiseFn := func(resolve, reject func(args ...interface{}) js.Value) { + rid, err := e.api.Reset(partnerContact) + if err != nil { + reject(utils.JsTrace(err)) + } else { + resolve(rid) + } } - return rid + return utils.CreatePromise(promiseFn) } // ReplayConfirm resends a confirmation to the partner. It will fail to send if @@ -131,17 +139,21 @@ func (e *E2e) Reset(_ js.Value, args []js.Value) interface{} { // - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array). // // Returns: -// - ID of the round (int). -// - Throws TypeError if the confirmation fails. +// - A promise that returns the ID of the round (int). +// - Throws an error if the confirmation fails. func (e *E2e) ReplayConfirm(_ js.Value, args []js.Value) interface{} { partnerContact := utils.CopyBytesToGo(args[0]) - rid, err := e.api.ReplayConfirm(partnerContact) - if err != nil { - utils.Throw(utils.TypeError, err) - return nil + + promiseFn := func(resolve, reject func(args ...interface{}) js.Value) { + rid, err := e.api.ReplayConfirm(partnerContact) + if err != nil { + reject(utils.JsTrace(err)) + } else { + resolve(rid) + } } - return rid + return utils.CreatePromise(promiseFn) } // CallAllReceivedRequests will iterate through all pending contact requests and -- GitLab