Skip to content
Snippets Groups Projects
Commit 3fbb5c16 authored by Jake Taylor's avatar Jake Taylor
Browse files

added optional db encryption

parent dfdaa0f4
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
......@@ -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)
......
......@@ -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.
......
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