Skip to content

Commit

Permalink
new method for initializing prefs with new account
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 14, 2024
1 parent 5f57090 commit e9ef687
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package network.xyo.client
import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import network.xyo.client.address.XyoAccount
import network.xyo.client.boundwitness.XyoBoundWitnessBuilder
import network.xyo.client.datastore.XyoAccountPrefsRepository
import network.xyo.client.settings.AccountPreferences
import network.xyo.client.witness.system.info.XyoSystemInfoWitness
Expand Down Expand Up @@ -95,4 +97,35 @@ class XyoAccountPrefsRepositoryTest {
assert(originalAddress !== refreshedAddress)
}
}

@Test
fun testAccountDeserialization() {
runBlocking {
val testAccount = XyoAccount()
val instance = XyoAccountPrefsRepository.getInstance(appContext)
// Clear previously saved accounts
instance.clearSavedAccountKey()
// Serialize the test account
instance.initializeAccount(testAccount)

// Deserialize the test account
val firstAccount = instance.getAccount()
assertEquals(firstAccount.private.hex, testAccount.private.hex)

// Sign with the test account
val firstBw = XyoBoundWitnessBuilder().witness(firstAccount, null).payloads(listOf(TestConstants.debugPayload)).build()
val firstAddress = firstBw.addresses.first()

// Deserialize the test account (Ideally we would refresh the singleton but in tests this seems to cause errors with multiple instances of the prefs DataStore)
val secondInstance = XyoAccountPrefsRepository.getInstance(appContext)
val secondAccount = secondInstance.getAccount()

// Sign with the test account
val secondBw = XyoBoundWitnessBuilder().witness(secondAccount, null).payloads(listOf(TestConstants.debugPayload)).build()
val secondAddress = secondBw.addresses.first()

// check that addresses have not changed and no errors occurred during signing
assertEquals(firstAddress, secondAddress)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network.xyo.client.datastore

import android.content.Context
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.datastore.core.DataStore
import network.xyo.data.PrefsDataStoreProtos.PrefsDataStore
Expand Down Expand Up @@ -30,6 +31,28 @@ class XyoAccountPrefsRepository(context: Context, private val _accountPreference
return XyoAccount(hexStringToByteArray(saveKeyHex))
}

@RequiresApi(Build.VERSION_CODES.M)
suspend fun initializeAccount(account: XyoAccount): XyoAccount? {
var updatedKey: String? = null
val job = xyoScope.launch {
val savedKey = prefsDataStore.data.first().accountKey
if (savedKey.isNullOrEmpty()) {
// no saved key so save the passed in one
updatedKey = null
setAccountKey(account.private.hex)
} else {
updatedKey = null
Log.w("xyoClient", "Key already exists. Clear it first before initializing prefs with new account")
}
}
job.join()
return if (updatedKey !== null) {
account
} else {
null
}
}

@RequiresApi(Build.VERSION_CODES.M)
private suspend fun getAccountKey(): String {
val savedKey = prefsDataStore.data.first().accountKey
Expand Down Expand Up @@ -82,12 +105,14 @@ class XyoAccountPrefsRepository(context: Context, private val _accountPreference
fun refresh(context: Context, accountPreferences: AccountPreferences): XyoAccountPrefsRepository {
synchronized(this) {
INSTANCE = XyoAccountPrefsRepository(context, accountPreferences)
return INSTANCE!!
}
return INSTANCE!! // Return the updated instance
}

fun resetInstance() {
INSTANCE = null
synchronized(this) {
INSTANCE = null
}
}
}
}

0 comments on commit e9ef687

Please sign in to comment.