diff --git a/indexedDb/init.go b/indexedDb/init.go index e34cb56f3f139eba541ab8548f671185fbdc9f4b..057f97f1c4138f59059e47f4adf74bde19d75ac0 100644 --- a/indexedDb/init.go +++ b/indexedDb/init.go @@ -10,7 +10,9 @@ package indexedDb import ( + "github.com/pkg/errors" cryptoChannel "gitlab.com/elixxir/crypto/channel" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" "github.com/hack-pad/go-indexeddb/idb" @@ -89,7 +91,17 @@ func newWASMModel(databaseName string, encryption cryptoChannel.Cipher, // Wait for database open to finish db, err := openRequest.Await(ctx) - if encryption == nil { + encryptionStatus := encryption != nil + loadedEncryptionStatus, err := utils.StoreIndexedDbEncryptionStatus( + databaseName, encryptionStatus) + if err != nil { + return nil, err + } + + if encryptionStatus != loadedEncryptionStatus { + return nil, errors.New( + "Cannot load database with different encryption status.") + } else if !encryptionStatus { jww.WARN.Printf("IndexedDb encryption disabled!") } return &wasmModel{db: db, receivedMessageCB: cb, cipher: encryption}, err diff --git a/utils/indexedDbEncryptionTrack.go b/utils/indexedDbEncryptionTrack.go new file mode 100644 index 0000000000000000000000000000000000000000..93db83884cee6144fc85c0bc9a84d7a636048e79 --- /dev/null +++ b/utils/indexedDbEncryptionTrack.go @@ -0,0 +1,35 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2022 xx foundation // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file. // +//////////////////////////////////////////////////////////////////////////////// + +package utils + +import ( + "github.com/pkg/errors" + "os" +) + +// Key to store if the database is encrypted or not +const databaseEncryptionToggleKey = "xxdkWasmDatabaseEncryptionToggle/" + +// StoreIndexedDbEncryptionStatus stores the encryption status if it has not +// been previously saved. If it has, it returns its value. +func StoreIndexedDbEncryptionStatus( + databaseName string, encryption bool) (bool, error) { + data, err := GetLocalStorage().GetItem( + databaseEncryptionToggleKey + databaseName) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + GetLocalStorage().SetItem( + databaseEncryptionToggleKey+databaseName, []byte{1}) + return encryption, nil + } else { + return false, err + } + } + + return data[0] == 1, nil +}