From ce9b075894f3dee530150dc02cd66cbe89290120 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Thu, 1 Dec 2022 18:17:26 -0800
Subject: [PATCH] Fix channel cipher

---
 go.mod           |  4 ++--
 go.sum           |  8 ++++----
 wasm/channels.go | 45 ++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/go.mod b/go.mod
index 37df3864..1fc810d6 100644
--- a/go.mod
+++ b/go.mod
@@ -7,8 +7,8 @@ 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/v4 v4.3.9-0.20221117174054-7a48e9fb3283
-	gitlab.com/elixxir/crypto v0.0.7-0.20221121233335-83f145891bc7
+	gitlab.com/elixxir/client/v4 v4.3.9-0.20221202021328-118de4434344
+	gitlab.com/elixxir/crypto v0.0.7-0.20221202020255-46eeab272a7f
 	gitlab.com/elixxir/primitives v0.0.3-0.20221114231218-cc461261a6af
 	gitlab.com/xx_network/crypto v0.0.5-0.20221121220724-8eefdbb0eb46
 	gitlab.com/xx_network/primitives v0.0.4-0.20221110180011-fd6ea3058225
diff --git a/go.sum b/go.sum
index c4ba4b59..0d48a47c 100644
--- a/go.sum
+++ b/go.sum
@@ -369,12 +369,12 @@ github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo=
 github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4=
 gitlab.com/elixxir/bloomfilter v0.0.0-20211222005329-7d931ceead6f h1:yXGvNBqzZwAhDYlSnxPRbgor6JWoOt1Z7s3z1O9JR40=
 gitlab.com/elixxir/bloomfilter v0.0.0-20211222005329-7d931ceead6f/go.mod h1:H6jztdm0k+wEV2QGK/KYA+MY9nj9Zzatux/qIvDDv3k=
-gitlab.com/elixxir/client/v4 v4.3.9-0.20221117174054-7a48e9fb3283 h1:TYsCqI18+g4DgENJoFahnQFQKBZwg/t5XgYMCBV5e5M=
-gitlab.com/elixxir/client/v4 v4.3.9-0.20221117174054-7a48e9fb3283/go.mod h1:4jD++3DAylNUM4BL5+wEIoe+oAvkbnc//wjCgnhyKpw=
+gitlab.com/elixxir/client/v4 v4.3.9-0.20221202021328-118de4434344 h1:iUAwK+Ti3VObJy5mXG5/CfMoOs05oMi30XUMJIG1XTI=
+gitlab.com/elixxir/client/v4 v4.3.9-0.20221202021328-118de4434344/go.mod h1:6zBHVEOwB42N3Ba3879GW0huu595RLSar04KlNsa7QE=
 gitlab.com/elixxir/comms v0.0.4-0.20221110181420-84bca6216fe4 h1:bLRjVCyMVde4n2hTVgoyyIAWrKI4CevpChchkPeb6A0=
 gitlab.com/elixxir/comms v0.0.4-0.20221110181420-84bca6216fe4/go.mod h1:XhI2/CMng+xcH3mAs+1aPz29PSNu1079XMJ8V+xxihw=
-gitlab.com/elixxir/crypto v0.0.7-0.20221121233335-83f145891bc7 h1:yhc8jQ27JKypdRE41NpfJPaYRS0sNkOwugaIyoscDiU=
-gitlab.com/elixxir/crypto v0.0.7-0.20221121233335-83f145891bc7/go.mod h1:oRh3AwveOEvpk9E3kRcMGK8fImcEnN0PY4jr9HDgQE8=
+gitlab.com/elixxir/crypto v0.0.7-0.20221202020255-46eeab272a7f h1:e7h3InxgzRnYOqgUgHrTfK6CW8qNM2SiYRj9tOvsFjA=
+gitlab.com/elixxir/crypto v0.0.7-0.20221202020255-46eeab272a7f/go.mod h1:oRh3AwveOEvpk9E3kRcMGK8fImcEnN0PY4jr9HDgQE8=
 gitlab.com/elixxir/ekv v0.2.1 h1:dtwbt6KmAXG2Tik5d60iDz2fLhoFBgWwST03p7T+9Is=
 gitlab.com/elixxir/ekv v0.2.1/go.mod h1:USLD7xeDnuZEavygdrgzNEwZXeLQJK/w1a+htpN+JEU=
 gitlab.com/elixxir/primitives v0.0.3-0.20221114231218-cc461261a6af h1:xcPqknK1ehNb9xwcutTdoR0YgD7DC/ySh9z49tIpSxQ=
diff --git a/wasm/channels.go b/wasm/channels.go
index 70e5ec6c..3f050456 100644
--- a/wasm/channels.go
+++ b/wasm/channels.go
@@ -1438,9 +1438,11 @@ type ChannelDbCipher struct {
 func newChannelDbCipherJS(api *bindings.ChannelDbCipher) map[string]any {
 	c := ChannelDbCipher{api}
 	channelDbCipherMap := map[string]any{
-		"GetID":   js.FuncOf(c.GetID),
-		"Encrypt": js.FuncOf(c.Encrypt),
-		"Decrypt": js.FuncOf(c.Decrypt),
+		"GetID":         js.FuncOf(c.GetID),
+		"Encrypt":       js.FuncOf(c.Encrypt),
+		"Decrypt":       js.FuncOf(c.Decrypt),
+		"MarshalJSON":   js.FuncOf(c.MarshalJSON),
+		"UnmarshalJSON": js.FuncOf(c.UnmarshalJSON),
 	}
 
 	return channelDbCipherMap
@@ -1495,7 +1497,6 @@ func (c *ChannelDbCipher) GetID(js.Value, []js.Value) any {
 //   - The ciphertext of the plaintext passed in (Uint8Array).
 //   - Throws a TypeError if it fails to encrypt the plaintext.
 func (c *ChannelDbCipher) Encrypt(_ js.Value, args []js.Value) any {
-
 	ciphertext, err := c.api.Encrypt(utils.CopyBytesToGo(args[0]))
 	if err != nil {
 		utils.Throw(utils.TypeError, err)
@@ -1503,7 +1504,6 @@ func (c *ChannelDbCipher) Encrypt(_ js.Value, args []js.Value) any {
 	}
 
 	return utils.CopyBytesToJS(ciphertext)
-
 }
 
 // Decrypt will decrypt the passed in encrypted value. The plaintext will be
@@ -1525,5 +1525,40 @@ func (c *ChannelDbCipher) Decrypt(_ js.Value, args []js.Value) any {
 	}
 
 	return utils.CopyBytesToJS(plaintext)
+}
+
+// MarshalJSON marshals the cipher into valid JSON.
+//
+// Returns:
+//   - JSON of the cipher (Uint8Array).
+//   - Throws a TypeError if marshalling fails.
+func (c *ChannelDbCipher) MarshalJSON(js.Value, []js.Value) any {
+	data, err := c.api.MarshalJSON()
+	if err != nil {
+		utils.Throw(utils.TypeError, err)
+		return nil
+	}
+
+	return utils.CopyBytesToJS(data)
+}
 
+// UnmarshalJSON unmarshalls JSON into the cipher. This function adheres to the
+// json.Unmarshaler interface.
+//
+// Note that this function does not transfer the internal RNG. Use
+// [channel.NewCipherFromJSON] to properly reconstruct a cipher from JSON.
+//
+// Parameters:
+//   - args[0] - JSON data to unmarshal (Uint8Array).
+//
+// Returns:
+//   - JSON of the cipher (Uint8Array).
+//   - Throws a TypeError if marshalling fails.
+func (c *ChannelDbCipher) UnmarshalJSON(_ js.Value, args []js.Value) any {
+	err := c.api.UnmarshalJSON(utils.CopyBytesToGo(args[0]))
+	if err != nil {
+		utils.Throw(utils.TypeError, err)
+		return nil
+	}
+	return nil
 }
-- 
GitLab