Skip to content

Commit

Permalink
move to preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 25, 2024
1 parent 1e84e4c commit 4da7d38
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,17 @@ class Client() {
dbDir.mkdir()
val dbPath: String = dbDir.absolutePath + "/$alias.db3"

val keyStore = KeyStore.getInstance("AndroidKeyStore")
withContext(Dispatchers.IO) {
keyStore.load(null)
}
val entry = keyStore.getEntry(alias, null)
val retrievedKey = if (entry is KeyStore.SecretKeyEntry) {
entry.secretKey
val sharedPreferences =
options.appContext.getSharedPreferences("XMTPPreferences", Context.MODE_PRIVATE)
val dbEncryptionKey = sharedPreferences.getString(alias, null)

val retrievedKey = if (dbEncryptionKey != null) {
Base64.decode(dbEncryptionKey)
} else {
val keyGenerator = KeyGenerator.getInstance("AES")
keyGenerator.init(256)
val key: SecretKey = keyGenerator.generateKey()
val secretKeyEntry = KeyStore.SecretKeyEntry(key)
keyStore.setEntry(alias, secretKeyEntry, null)
val editor = sharedPreferences.edit()
val key: ByteArray = SecureRandom().generateSeed(32)
editor.putString(alias, Base64.encode(key))
editor.apply()
key
}

Expand All @@ -241,7 +239,7 @@ class Client() {
host = "http://10.0.2.2:5556",
isSecure = false,
db = dbPath,
encryptionKey = retrievedKey.encoded
encryptionKey = retrievedKey
)
} else {
null
Expand Down

0 comments on commit 4da7d38

Please sign in to comment.