From 3fbb5c16812a9654ea135fe50c4f7c272c86974d Mon Sep 17 00:00:00 2001
From: Jake Taylor <jake@elixxir.io>
Date: Fri, 21 Oct 2022 17:14:45 -0500
Subject: [PATCH] added optional db encryption

---
 indexedDb/implementation.go | 11 +++++++++++
 indexedDb/init.go           | 20 ++++++++++++--------
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/indexedDb/implementation.go b/indexedDb/implementation.go
index 410358d7..d9d2f702 100644
--- a/indexedDb/implementation.go
+++ b/indexedDb/implementation.go
@@ -39,6 +39,7 @@ const dbTimeout = time.Second
 // channel.
 type wasmModel struct {
 	db                *idb.Database
+	cipher            cryptoChannel.Cipher
 	receivedMessageCB MessageReceivedCallback
 	updateMux         sync.Mutex
 }
@@ -206,6 +207,16 @@ func (w *wasmModel) ReceiveMessage(channelID *id.ID,
 	timestamp time.Time, lease time.Duration, round rounds.Round,
 	mType channels.MessageType, status channels.SentStatus) uint64 {
 
+	// Handle encryption, if it is present
+	if w.cipher != nil {
+		cipherText, err := w.cipher.Encrypt([]byte(text))
+		if err != nil {
+			jww.ERROR.Printf("Failed to encrypt Message: %+v", err)
+			return 0
+		}
+		text = string(cipherText)
+	}
+
 	msgToInsert := buildMessage(
 		channelID.Marshal(), messageID.Bytes(), nil, nickname, text, pubKey,
 		codeset, timestamp, lease, round.ID, mType, status)
diff --git a/indexedDb/init.go b/indexedDb/init.go
index 461c031e..e34cb56f 100644
--- a/indexedDb/init.go
+++ b/indexedDb/init.go
@@ -10,6 +10,7 @@
 package indexedDb
 
 import (
+	cryptoChannel "gitlab.com/elixxir/crypto/channel"
 	"syscall/js"
 
 	"github.com/hack-pad/go-indexeddb/idb"
@@ -37,25 +38,25 @@ type MessageReceivedCallback func(uuid uint64, channelID *id.ID, update bool)
 // NewWASMEventModelBuilder returns an EventModelBuilder which allows
 // the channel manager to define the path but the callback is the same
 // across the board.
-func NewWASMEventModelBuilder(
+func NewWASMEventModelBuilder(encryption cryptoChannel.Cipher,
 	cb MessageReceivedCallback) channels.EventModelBuilder {
 	fn := func(path string) (channels.EventModel, error) {
-		return NewWASMEventModel(path, cb)
+		return NewWASMEventModel(path, encryption, cb)
 	}
 	return fn
 }
 
 // NewWASMEventModel returns a [channels.EventModel] backed by a wasmModel.
 // The name should be a base64 encoding of the users public key.
-func NewWASMEventModel(path string, cb MessageReceivedCallback) (
-	channels.EventModel, error) {
+func NewWASMEventModel(path string, encryption cryptoChannel.Cipher,
+	cb MessageReceivedCallback) (channels.EventModel, error) {
 	databaseName := path + databaseSuffix
-	return newWASMModel(databaseName, cb)
+	return newWASMModel(databaseName, encryption, cb)
 }
 
 // newWASMModel creates the given [idb.Database] and returns a wasmModel.
-func newWASMModel(databaseName string, cb MessageReceivedCallback) (
-	*wasmModel, error) {
+func newWASMModel(databaseName string, encryption cryptoChannel.Cipher,
+	cb MessageReceivedCallback) (*wasmModel, error) {
 	// Attempt to open database object
 	ctx, cancel := newContext()
 	defer cancel()
@@ -88,7 +89,10 @@ func newWASMModel(databaseName string, cb MessageReceivedCallback) (
 	// Wait for database open to finish
 	db, err := openRequest.Await(ctx)
 
-	return &wasmModel{db: db, receivedMessageCB: cb}, err
+	if encryption == nil {
+		jww.WARN.Printf("IndexedDb encryption disabled!")
+	}
+	return &wasmModel{db: db, receivedMessageCB: cb, cipher: encryption}, err
 }
 
 // v1Upgrade performs the v0 -> v1 database upgrade.
-- 
GitLab