// Package badgerstore is the Badger-backed implementation of // libsignal.Store: one bridged chat's OMEMO identity, prekeys, and its // remote peers' sessions/trust state. // // This is a dedicated database, separate from telegabber's existing // id-mapping Badger DB (badger.IdsDB) - it holds private key material, so // it gets its own lifecycle (optionally encrypted at rest) rather than // sharing a file with the purely-operational id-mapping store. package badgerstore import ( badger "github.com/dgraph-io/badger/v4" "dev.narayana.im/narayana/telegabber/e2ee/omemo/libsignal" ) // DB is the OMEMO secrets database, shared across every bridged chat's // identity (each gets its own key namespace within it - see Store). type DB struct { db *badger.DB } // Open opens (creating if necessary) the OMEMO secrets database at path. // If encryptionKey is non-empty, the database is encrypted at rest // (badger.Options.WithEncryptionKey); its default BlockCacheSize (256MB) // is left as-is, which badger requires to be non-zero whenever encryption // is enabled. func Open(path string, encryptionKey []byte) (*DB, error) { opts := badger.DefaultOptions(path) if len(encryptionKey) > 0 { opts = opts.WithEncryptionKey(encryptionKey) } bdb, err := badger.Open(opts) if err != nil { return nil, err } return &DB{db: bdb}, nil } // Store returns the libsignal.Store view of one bridged chat's OMEMO // identity, scoped by (login, ownerPeerJID). ctx is used to split a stored // combined identity key pair record into the separate public/private // buffers libsignal.IdentityStore.GetIdentityKeyPair must return. func (db *DB) Store(ctx *libsignal.Context, login, ownerPeerJID string) *Store { return &Store{db: db.db, ctx: ctx, login: login, owner: ownerPeerJID} } // Gc compacts the value log. func (db *DB) Gc() { db.db.RunValueLogGC(0.7) } // Close closes the database. func (db *DB) Close() error { return db.db.Close() }