From 9afeb516fa35ec227bc63e464d8bcd420bac034d Mon Sep 17 00:00:00 2001
From: Benjamin Wenger <ben@elixxir.ioo>
Date: Tue, 8 Mar 2022 16:06:23 -0800
Subject: [PATCH] implemented json addition to backup needs tests+bindings

---
 backup/backup.go      | 20 ++++++++++++++++++++
 backup/jsonStorage.go | 30 ++++++++++++++++++++++++++++++
 go.mod                |  2 +-
 go.sum                |  2 ++
 4 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 backup/jsonStorage.go

diff --git a/backup/backup.go b/backup/backup.go
index 866aa394e..68515d18e 100644
--- a/backup/backup.go
+++ b/backup/backup.go
@@ -53,6 +53,8 @@ type Backup struct {
 	store           *storage.Session
 	backupContainer *interfaces.BackupContainer
 	rng             *fastRNG.StreamGenerator
+
+	jsonParams string
 }
 
 // UpdateBackupFn is the callback that encrypted backup data is returned on
@@ -141,6 +143,7 @@ func resumeBackup(updateBackupCb UpdateBackupFn, c *api.Client,
 		store:           store,
 		backupContainer: backupContainer,
 		rng:             rng,
+		jsonParams:      loadJson(store.GetKV()),
 	}
 
 	// Setting backup trigger in client
@@ -208,6 +211,20 @@ func (b *Backup) TriggerBackup(reason string) {
 	}
 }
 
+func (b *Backup) AddJson(newJson string) {
+	b.mux.Lock()
+	defer b.mux.Unlock()
+
+	currentJson := loadJson(b.store.GetKV())
+	if newJson != currentJson {
+		b.jsonParams = newJson
+		if err := storeJson(newJson, b.store.GetKV()); err != nil {
+			jww.FATAL.Panicf("Failed to store json: %+v", err)
+		}
+		go b.TriggerBackup("New Json")
+	}
+}
+
 // StopBackup stops the backup processes and deletes the user's password, key,
 // salt, and parameters from storage.
 func (b *Backup) StopBackup() error {
@@ -287,5 +304,8 @@ func (b *Backup) assembleBackup() backup.Backup {
 	// Get contacts
 	bu.Contacts.Identities = b.store.E2e().GetPartners()
 
+	//add the memoized json params
+	bu.JSONParams = b.jsonParams
+
 	return bu
 }
diff --git a/backup/jsonStorage.go b/backup/jsonStorage.go
new file mode 100644
index 000000000..fe499a456
--- /dev/null
+++ b/backup/jsonStorage.go
@@ -0,0 +1,30 @@
+package backup
+
+import (
+	"gitlab.com/elixxir/client/storage/versioned"
+	"gitlab.com/xx_network/primitives/netTime"
+)
+
+const (
+	jsonStorageVersion = 0
+	jsonStorageKey     = "JsonStorage"
+)
+
+func storeJson(json string, kv *versioned.KV) error {
+	obj := &versioned.Object{
+		Version:   jsonStorageVersion,
+		Timestamp: netTime.Now(),
+		Data:      []byte(json),
+	}
+
+	return kv.Set(jsonStorageKey, jsonStorageVersion, obj)
+}
+
+func loadJson(kv *versioned.KV) string {
+	obj, err := kv.Get(passwordStorageKey, passwordStorageVersion)
+	if err != nil {
+		return ""
+	}
+
+	return string(obj.Data)
+}
diff --git a/go.mod b/go.mod
index 913291299..a2446e93a 100644
--- a/go.mod
+++ b/go.mod
@@ -13,7 +13,7 @@ require (
 	github.com/spf13/viper v1.7.1
 	gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228
 	gitlab.com/elixxir/comms v0.0.4-0.20220308183624-c2183e687a03
-	gitlab.com/elixxir/crypto v0.0.7-0.20220222221347-95c7ae58da6b
+	gitlab.com/elixxir/crypto v0.0.7-0.20220308234138-3b0743539e7d
 	gitlab.com/elixxir/ekv v0.1.6
 	gitlab.com/elixxir/primitives v0.0.3-0.20220222212109-d412a6e46623
 	gitlab.com/xx_network/comms v0.0.4-0.20220223205228-7c4974139569
diff --git a/go.sum b/go.sum
index c58adf1fd..1a8a6a22e 100644
--- a/go.sum
+++ b/go.sum
@@ -278,6 +278,8 @@ gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo
 gitlab.com/elixxir/crypto v0.0.3/go.mod h1:ZNgBOblhYToR4m8tj4cMvJ9UsJAUKq+p0gCp07WQmhA=
 gitlab.com/elixxir/crypto v0.0.7-0.20220222221347-95c7ae58da6b h1:m80Ub5mshPbMzYjRC0nXuI8vtm6e5crISczRsP2YUJ4=
 gitlab.com/elixxir/crypto v0.0.7-0.20220222221347-95c7ae58da6b/go.mod h1:tD6XjtQh87T2nKZL5I/pYPck5M2wLpkZ1Oz7H/LqO10=
+gitlab.com/elixxir/crypto v0.0.7-0.20220308234138-3b0743539e7d h1:c8GJzgdJQPm2ckd2U5uGVPkfF1qMBy8edE4LUlYJMx8=
+gitlab.com/elixxir/crypto v0.0.7-0.20220308234138-3b0743539e7d/go.mod h1:tD6XjtQh87T2nKZL5I/pYPck5M2wLpkZ1Oz7H/LqO10=
 gitlab.com/elixxir/ekv v0.1.6 h1:M2hUSNhH/ChxDd+s8xBqSEKgoPtmE6hOEBqQ73KbN6A=
 gitlab.com/elixxir/ekv v0.1.6/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4=
 gitlab.com/elixxir/primitives v0.0.0-20200731184040-494269b53b4d/go.mod h1:OQgUZq7SjnE0b+8+iIAT2eqQF+2IFHn73tOo+aV11mg=
-- 
GitLab