From b514c31888e4e1c740b97a052b0980724734fbaf Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Fri, 14 Oct 2022 11:25:21 -0700
Subject: [PATCH] Add ImportPrivateIdentity

---
 go.mod           |  2 +-
 go.sum           |  2 ++
 main.go          |  2 ++
 wasm/channels.go | 30 ++++++++++++++++++++++++++++--
 4 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/go.mod b/go.mod
index 081543d7..88d37252 100644
--- a/go.mod
+++ b/go.mod
@@ -7,7 +7,7 @@ require (
 	github.com/hack-pad/go-indexeddb v0.2.0
 	github.com/pkg/errors v0.9.1
 	github.com/spf13/jwalterweatherman v1.1.0
-	gitlab.com/elixxir/client v1.5.1-0.20221014170508-d65477656611
+	gitlab.com/elixxir/client v1.5.1-0.20221014182132-0a0ce8f77375
 	gitlab.com/elixxir/crypto v0.0.7-0.20221014161526-ce04e14b9f80
 	gitlab.com/elixxir/primitives v0.0.3-0.20220901220638-1acc75fabdc6
 	gitlab.com/xx_network/crypto v0.0.5-0.20220913213008-98764f5b3287
diff --git a/go.sum b/go.sum
index 370f2f02..d655884b 100644
--- a/go.sum
+++ b/go.sum
@@ -632,6 +632,8 @@ gitlab.com/elixxir/client v1.5.1-0.20221014161854-3e3de2653aa4 h1:zXEWsMHQ3aQr/8
 gitlab.com/elixxir/client v1.5.1-0.20221014161854-3e3de2653aa4/go.mod h1:oSbLC/5yibV2zZk9iI6Cm8F44+I80fQfP6qw9QaGaeI=
 gitlab.com/elixxir/client v1.5.1-0.20221014170508-d65477656611 h1:aZc2X7dDfjfxVuhZYXrICpyl09jkvnrVvekZaO8y4es=
 gitlab.com/elixxir/client v1.5.1-0.20221014170508-d65477656611/go.mod h1:oSbLC/5yibV2zZk9iI6Cm8F44+I80fQfP6qw9QaGaeI=
+gitlab.com/elixxir/client v1.5.1-0.20221014182132-0a0ce8f77375 h1:QQz79TDy3gZP7WeiQG+VZf54mKoqwmarhqICfSqJDx0=
+gitlab.com/elixxir/client v1.5.1-0.20221014182132-0a0ce8f77375/go.mod h1:oSbLC/5yibV2zZk9iI6Cm8F44+I80fQfP6qw9QaGaeI=
 gitlab.com/elixxir/comms v0.0.4-0.20221011183106-8c4450ba3cfb h1:aw7Ao1oqho+97gO35HkzBmv2e25qluRXEiNXw/oD8MM=
 gitlab.com/elixxir/comms v0.0.4-0.20221011183106-8c4450ba3cfb/go.mod h1:oRteMH+R5t1j/FZ+KJJnZUcqJO2sLXnWksN5HPkZUIo=
 gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c=
diff --git a/main.go b/main.go
index 79d522f7..3781c8d8 100644
--- a/main.go
+++ b/main.go
@@ -50,6 +50,8 @@ func main() {
 	// wasm/channels.go
 	js.Global().Set("GenerateChannelIdentity",
 		js.FuncOf(wasm.GenerateChannelIdentity))
+	js.Global().Set("ImportPrivateIdentity",
+		js.FuncOf(wasm.ImportPrivateIdentity))
 	js.Global().Set("GetPublicChannelIdentity",
 		js.FuncOf(wasm.GetPublicChannelIdentity))
 	js.Global().Set("GetPublicChannelIdentityFromPrivate",
diff --git a/wasm/channels.go b/wasm/channels.go
index 401bec7a..951e36dc 100644
--- a/wasm/channels.go
+++ b/wasm/channels.go
@@ -83,6 +83,30 @@ func GenerateChannelIdentity(_ js.Value, args []js.Value) interface{} {
 	pi, err := bindings.GenerateChannelIdentity(args[0].Int())
 	if err != nil {
 		utils.Throw(utils.TypeError, err)
+		return nil
+	}
+
+	return utils.CopyBytesToJS(pi)
+}
+
+// ImportPrivateIdentity generates a new [channel.PrivateIdentity] from exported
+// data.
+//
+// Parameters:
+//  - args[0] - The password used to encrypt the identity (string).
+//  - args[2] - The encrypted data (Uint8Array).
+//
+// Returns:
+//  - JSON of [channel.PrivateIdentity] (Uint8Array).
+//  - Throws a TypeError if importing the identity fails.
+func ImportPrivateIdentity(_ js.Value, args []js.Value) interface{} {
+	password := args[0].String()
+	data := utils.CopyBytesToGo(args[1])
+
+	pi, err := bindings.ImportPrivateIdentity(password, data)
+	if err != nil {
+		utils.Throw(utils.TypeError, err)
+		return nil
 	}
 
 	return utils.CopyBytesToJS(pi)
@@ -103,6 +127,7 @@ func GetPublicChannelIdentity(_ js.Value, args []js.Value) interface{} {
 	pi, err := bindings.GetPublicChannelIdentity(marshaledPublic)
 	if err != nil {
 		utils.Throw(utils.TypeError, err)
+		return nil
 	}
 
 	return utils.CopyBytesToJS(pi)
@@ -122,10 +147,11 @@ func GetPublicChannelIdentity(_ js.Value, args []js.Value) interface{} {
 //    fails.
 func GetPublicChannelIdentityFromPrivate(_ js.Value, args []js.Value) interface{} {
 	marshaledPrivate := utils.CopyBytesToGo(args[0])
-	identity, err := bindings.GetPublicChannelIdentityFromPrivate(
-		marshaledPrivate)
+	identity, err :=
+		bindings.GetPublicChannelIdentityFromPrivate(marshaledPrivate)
 	if err != nil {
 		utils.Throw(utils.TypeError, err)
+		return nil
 	}
 
 	return utils.CopyBytesToJS(identity)
-- 
GitLab