From 4db6edd7809a16f41d494bb5bdedc3f5bcc4b118 Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Tue, 30 May 2023 18:26:25 +0000
Subject: [PATCH] Add NewSynchronizedCmix

---
 go.mod       |  2 +-
 go.sum       |  2 ++
 main.go      |  2 ++
 wasm/cmix.go | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/go.mod b/go.mod
index c2c091b9..b9d4ab69 100644
--- a/go.mod
+++ b/go.mod
@@ -9,7 +9,7 @@ require (
 	github.com/spf13/cobra v1.7.0
 	github.com/spf13/jwalterweatherman v1.1.0
 	github.com/stretchr/testify v1.8.2
-	gitlab.com/elixxir/client/v4 v4.6.4-0.20230530170051-243baab3da48
+	gitlab.com/elixxir/client/v4 v4.6.4-0.20230530182422-d12ab437257f
 	gitlab.com/elixxir/crypto v0.0.7-0.20230522162218-45433d877235
 	gitlab.com/elixxir/primitives v0.0.3-0.20230214180039-9a25e2d3969c
 	gitlab.com/elixxir/wasm-utils v0.0.0-20230522231408-a43b2c1481b2
diff --git a/go.sum b/go.sum
index 5689b511..d4c71901 100644
--- a/go.sum
+++ b/go.sum
@@ -521,6 +521,8 @@ gitlab.com/elixxir/client/v4 v4.6.4-0.20230530165750-f6ea41bc69e4 h1:dgq4fwLdVzs
 gitlab.com/elixxir/client/v4 v4.6.4-0.20230530165750-f6ea41bc69e4/go.mod h1:fegbuF1/6a+H3QgsoMG8teLnyuKtDxkELMw8pn5WlZ8=
 gitlab.com/elixxir/client/v4 v4.6.4-0.20230530170051-243baab3da48 h1:2HP8w4HOlqjg0FcvSnhMmpwUa1pcU0WbvWMOUNiy/hE=
 gitlab.com/elixxir/client/v4 v4.6.4-0.20230530170051-243baab3da48/go.mod h1:fegbuF1/6a+H3QgsoMG8teLnyuKtDxkELMw8pn5WlZ8=
+gitlab.com/elixxir/client/v4 v4.6.4-0.20230530182422-d12ab437257f h1:8TJwqKjeD0eZSvWIlPwjgIwfgdzcHhTzGwaiUJCwwuI=
+gitlab.com/elixxir/client/v4 v4.6.4-0.20230530182422-d12ab437257f/go.mod h1:fegbuF1/6a+H3QgsoMG8teLnyuKtDxkELMw8pn5WlZ8=
 gitlab.com/elixxir/comms v0.0.4-0.20230519211512-4a998f4b0938 h1:f27+QUFiGWrprKm+fstOg3ABkYLpWcZi3+8Lf5eDnqY=
 gitlab.com/elixxir/comms v0.0.4-0.20230519211512-4a998f4b0938/go.mod h1:z+qW0D9VpY5QKTd7wRlb5SK4kBNqLYsa4DXBcUXue9Q=
 gitlab.com/elixxir/crypto v0.0.7-0.20230522162218-45433d877235 h1:0BySdXTzRWxzH8k5RiNNMmmn2lpuQWLVcDDA/7ehyqc=
diff --git a/main.go b/main.go
index 267bb55b..9bf2a059 100644
--- a/main.go
+++ b/main.go
@@ -156,6 +156,8 @@ func setGlobals() {
 
 	// wasm/cmix.go
 	js.Global().Set("NewCmix", js.FuncOf(wasm.NewCmix))
+	js.Global().Set("NewSynchronizedCmix",
+		js.FuncOf(wasm.NewSynchronizedCmix))
 	js.Global().Set("LoadCmix", js.FuncOf(wasm.LoadCmix))
 	js.Global().Set("LoadSynchronizedCmix",
 		js.FuncOf(wasm.LoadSynchronizedCmix))
diff --git a/wasm/cmix.go b/wasm/cmix.go
index be7ad6d3..c33cbb98 100644
--- a/wasm/cmix.go
+++ b/wasm/cmix.go
@@ -110,6 +110,38 @@ func NewCmix(_ js.Value, args []js.Value) any {
 	return utils.CreatePromise(promiseFn)
 }
 
+// NewSynchronizedCmix clones a cMix from remote storage.
+//
+// Users of this function should delete the storage directory on error.
+//
+// Parameters:
+//   - args[0] - NDF JSON ([ndf.NetworkDefinition]) (string).
+//   - args[1] - Storage directory path (string).
+//   - args[2] - Password used for storage (Uint8Array).
+//   - args[3] - Javascript [RemoteStore] implementation.
+//
+// Returns a promise:
+//   - Resolves on success.
+//   - Rejected with an error if creating a new cMix client fails.
+func NewSynchronizedCmix(_ js.Value, args []js.Value) any {
+	ndfJSON := args[0].String()
+	storageDir := args[1].String()
+	password := utils.CopyBytesToGo(args[2])
+	rs := newRemoteStore(args[3])
+
+	promiseFn := func(resolve, reject func(args ...any) js.Value) {
+		err := bindings.NewSynchronizedCmix(ndfJSON, storageDir,
+			password, rs)
+		if err != nil {
+			reject(exception.NewTrace(err))
+		} else {
+			resolve()
+		}
+	}
+
+	return utils.CreatePromise(promiseFn)
+}
+
 // LoadCmix will load an existing user storage from the storageDir using the
 // password. This will fail if the user storage does not exist or the password
 // is incorrect.
-- 
GitLab