Skip to content
Snippets Groups Projects
Commit 3135d79f authored by Jono Wenger's avatar Jono Wenger
Browse files

Add tracking if database is encrypted and return an error if loading the wrong type of database

parent 878d7bd3
No related branches found
No related tags found
3 merge requests!60Revert "Fail a test to be sure it works",!21added optional db encryption,!20Create JS bindings for ChannelDbCipher
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
package indexedDb package indexedDb
import ( import (
"github.com/pkg/errors"
cryptoChannel "gitlab.com/elixxir/crypto/channel" cryptoChannel "gitlab.com/elixxir/crypto/channel"
"gitlab.com/elixxir/xxdk-wasm/utils"
"syscall/js" "syscall/js"
"github.com/hack-pad/go-indexeddb/idb" "github.com/hack-pad/go-indexeddb/idb"
...@@ -89,7 +91,17 @@ func newWASMModel(databaseName string, encryption cryptoChannel.Cipher, ...@@ -89,7 +91,17 @@ func newWASMModel(databaseName string, encryption cryptoChannel.Cipher,
// Wait for database open to finish // Wait for database open to finish
db, err := openRequest.Await(ctx) 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!") jww.WARN.Printf("IndexedDb encryption disabled!")
} }
return &wasmModel{db: db, receivedMessageCB: cb, cipher: encryption}, err return &wasmModel{db: db, receivedMessageCB: cb, cipher: encryption}, err
......
////////////////////////////////////////////////////////////////////////////////
// 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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment