From 46812ef58b4f15620f78197e2db6335f76979837 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Mon, 24 Oct 2022 14:13:17 -0700
Subject: [PATCH] Add GetSavedChannelPrivateKeyUNSAFE

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

diff --git a/go.mod b/go.mod
index 57d5f803..972eabec 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.20221024202334-80c87bc2908c
+	gitlab.com/elixxir/client v1.5.1-0.20221024210920-177a44943bef
 	gitlab.com/elixxir/crypto v0.0.7-0.20221024012326-cf941c375c1f
 	gitlab.com/elixxir/primitives v0.0.3-0.20221017172918-6176818d1aba
 	gitlab.com/xx_network/crypto v0.0.5-0.20221017172404-b384a8d8b171
diff --git a/go.sum b/go.sum
index efbf6c47..a828bf7d 100644
--- a/go.sum
+++ b/go.sum
@@ -634,6 +634,8 @@ gitlab.com/elixxir/client v1.5.1-0.20221024201221-688c766ef5c7 h1:dhWRgVnk7+9tqW
 gitlab.com/elixxir/client v1.5.1-0.20221024201221-688c766ef5c7/go.mod h1:KVllKaLZKT9fLruj+0woZK5t8JKZNJTDJLsCNF+yIqQ=
 gitlab.com/elixxir/client v1.5.1-0.20221024202334-80c87bc2908c h1:deQEaPPJrjkmy6ij8RzxLFm3hfBLu3iZT/y0vmzoYno=
 gitlab.com/elixxir/client v1.5.1-0.20221024202334-80c87bc2908c/go.mod h1:KVllKaLZKT9fLruj+0woZK5t8JKZNJTDJLsCNF+yIqQ=
+gitlab.com/elixxir/client v1.5.1-0.20221024210920-177a44943bef h1:ZrL1sZfNMblXZRcUYv1QkSskT50fiYhbo3nEXwzuI+w=
+gitlab.com/elixxir/client v1.5.1-0.20221024210920-177a44943bef/go.mod h1:KVllKaLZKT9fLruj+0woZK5t8JKZNJTDJLsCNF+yIqQ=
 gitlab.com/elixxir/comms v0.0.4-0.20221024050701-bced94c1b026 h1:CdqvzyM91wN6u4MmGj0n+gKO/0tJabWPN3EQ4SFsZsg=
 gitlab.com/elixxir/comms v0.0.4-0.20221024050701-bced94c1b026/go.mod h1:NevrBdsi5wJvitUeMsid3xI1FrzzuzfxKy4Bapnhzao=
 gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c=
diff --git a/main.go b/main.go
index fbec3d11..4a2f6f66 100644
--- a/main.go
+++ b/main.go
@@ -75,6 +75,8 @@ func main() {
 	js.Global().Set("NewChannelsManagerWithIndexedDbUnsafe",
 		js.FuncOf(wasm.NewChannelsManagerWithIndexedDbUnsafe))
 	js.Global().Set("GenerateChannel", js.FuncOf(wasm.GenerateChannel))
+	js.Global().Set("GetSavedChannelPrivateKeyUNSAFE",
+		js.FuncOf(wasm.GetSavedChannelPrivateKeyUNSAFE))
 	js.Global().Set("DecodePublicURL", js.FuncOf(wasm.DecodePublicURL))
 	js.Global().Set("DecodePrivateURL", js.FuncOf(wasm.DecodePrivateURL))
 	js.Global().Set("GetChannelJSON", js.FuncOf(wasm.GetChannelJSON))
diff --git a/wasm/channels.go b/wasm/channels.go
index e884ed23..dc68f919 100644
--- a/wasm/channels.go
+++ b/wasm/channels.go
@@ -94,7 +94,7 @@ func GenerateChannelIdentity(_ js.Value, args []js.Value) interface{} {
 	return utils.CopyBytesToJS(pi)
 }
 
-// identityMap stores identifies previously generated by ConstructIdentity.
+// identityMap stores identities previously generated by ConstructIdentity.
 var identityMap sync.Map
 
 // ConstructIdentity constructs a [channel.Identity] from a user's public key
@@ -547,6 +547,29 @@ func GenerateChannel(_ js.Value, args []js.Value) interface{} {
 	return utils.CopyBytesToJS(gen)
 }
 
+// GetSavedChannelPrivateKeyUNSAFE loads the private key from storage for the
+// given channel ID.
+//
+// NOTE: This function is unsafe and only for debugging purposes only.
+//
+// Parameters:
+//  - args[0] - ID of [Cmix] object in tracker (int).
+//  - args[1] - The [id.ID] of the channel in base 64 encoding (string).
+//
+// Returns:
+//  - The PEM file of the private key (string).
+//  - Throws a TypeError if retrieving the [Cmix] object or the private key
+//    fails.
+func GetSavedChannelPrivateKeyUNSAFE(_ js.Value, args []js.Value) interface{} {
+	privKey, err := bindings.GetSavedChannelPrivateKeyUNSAFE(args[0].Int(), args[1].String())
+	if err != nil {
+		utils.Throw(utils.TypeError, err)
+		return nil
+	}
+
+	return privKey
+}
+
 // DecodePublicURL decodes the channel URL into a channel pretty print. This
 // function can only be used for public channel URLs. To get the privacy level
 // of a channel URL, use [GetShareUrlType].
@@ -646,7 +669,7 @@ func GetChannelInfo(_ js.Value, args []js.Value) interface{} {
 //
 // Parameters:
 //  - args[0] - A portable channel string. Should be received from another user
-//    or generated via GenerateChannel (string).
+//    or generated via [GenerateChannel] (string).
 //
 // The pretty print will be of the format:
 //  <Speakeasy-v2:Test_Channel|description:Channel description.|level:Public|secrets:+oHcqDbJPZaT3xD5NcdLY8OjOMtSQNKdKgLPmr7ugdU=|rCI0wr01dHFStjSFMvsBzFZClvDIrHLL5xbCOPaUOJ0=|493|1|7cBhJxVfQxWo+DypOISRpeWdQBhuQpAZtUbQHjBm8NQ=>
-- 
GitLab