Skip to content

Commit

Permalink
for legacy storage debug
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Nov 6, 2024
1 parent db28532 commit 1c011d0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import androidx.fragment.app.FragmentActivity
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.extensions.bestMessage
import com.tonapps.extensions.map
import com.tonapps.wallet.data.rn.data.RNDecryptedData
import com.tonapps.wallet.data.rn.data.RNFavorites
Expand Down Expand Up @@ -56,6 +57,10 @@ class RNLegacy(
}
}

fun getAllKeyValuesForDebug(): JSONObject {
return seedStorage.getAllKeyValuesForDebug()
}

fun isRequestMainMigration(): Boolean {
while (requestMigration == null) {
Thread.sleep(16)
Expand Down Expand Up @@ -112,12 +117,12 @@ class RNLegacy(
}

suspend fun addMnemonics(passcode: String, walletIds: List<String>, mnemonic: List<String>) {
val vaultState = getVaultState(passcode)
/*val vaultState = getVaultState(passcode)
for (walletId in walletIds) {
vaultState.keys[walletId] = RNDecryptedData(walletId, mnemonic.joinToString(" "))
}
seedStorage.save(passcode, vaultState)
seedStorage.save(passcode, vaultState)*/
}

suspend fun changePasscode(oldPasscode: String, newPasscode: String) {
Expand All @@ -130,7 +135,7 @@ class RNLegacy(
seedStorage.get(passcode)
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
RNVaultState()
RNVaultState(original = e.bestMessage)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ internal class RNSeedStorage(context: Context) {
kv.setActivity(activity)
}

fun getAllKeyValuesForDebug(): JSONObject {
return kv.getAllKeyValuesForDebug()
}

suspend fun setTonProof(id: String, token: String) = withContext(Dispatchers.IO) {
val key = keyTonProof(id)
kv.setItemImpl(key, token)
Expand Down Expand Up @@ -85,7 +89,7 @@ internal class RNSeedStorage(context: Context) {
}

suspend fun get(passcode: String): RNVaultState = withContext(Dispatchers.IO) {
val state = readState() ?: return@withContext RNVaultState()
val state = readState() ?: return@withContext RNVaultState(original = "Count chunks: ${kv.getItemImpl("${walletsKey}_chunks")}")
val json = JSONObject(ScryptBox.decrypt(passcode, state))
RNVaultState.of(json)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import androidx.collection.ArrayMap
import org.json.JSONObject

data class RNVaultState(
val keys: ArrayMap<String, RNDecryptedData> = ArrayMap()
val keys: ArrayMap<String, RNDecryptedData> = ArrayMap(),
val original: String,
) {

companion object {
Expand All @@ -14,22 +15,21 @@ data class RNVaultState(
for (m in list) {
keys[m.identifier] = m
}
return RNVaultState(keys)
return RNVaultState(keys, "{none}")
}

fun of(json: JSONObject): RNVaultState {
val keys = ArrayMap<String, RNDecryptedData>()
for (key in json.keys()) {
keys[key] = RNDecryptedData(json.getJSONObject(key))
}
return RNVaultState(keys)
return RNVaultState(keys, json.toString())
}
}

val string: String
get() = toJSON().toString()


fun getDecryptedData(walletId: String): RNDecryptedData? {
return keys[walletId]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ internal class SecureStoreModule(
return context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
}

fun getAllKeyValuesForDebug(): JSONObject {
val prefs = getSharedPreferences()
val result = JSONObject()
for ((key, value) in prefs.all) {
result.put(key, value.toString())
}
return result
}

/**
* Adds the keychain service as a prefix to the key in order to avoid conflicts in shared preferences
* when there are two identical keys but saved with different keychains.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,22 @@ class DevScreen: BaseWalletScreen<ScreenContext.None>(R.layout.fragment_dev, Scr
logView.visibility = View.GONE
}

view.findViewById<View>(R.id.import_legacy).setOnClickListener {
valuesFromLegacy()
}

logCopy = view.findViewById(R.id.log_copy)

}

private fun valuesFromLegacy() {
navigation?.migrationLoader(true)
viewModel.getLegacyStorage {
showLog(it)
navigation?.migrationLoader(false)
}
}

private fun importDApps() {
navigation?.migrationLoader(true)
viewModel.importApps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class DevViewModel(
private val dAppsRepository: DAppsRepository,
): BaseWalletVM(app) {

fun getLegacyStorage(callback: (result: String) -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
val json = rnLegacy.getAllKeyValuesForDebug().toString()
withContext(Dispatchers.Main) {
callback(json)
}
}
}

fun importApps(callback: (result: String) -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
val lines = mutableListOf<String>()
Expand Down
8 changes: 8 additions & 0 deletions apps/wallet/instance/app/src/main/res/layout/fragment_dev.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@
android:text="Import PIN from RN biometry"
app:position="middle"/>

<uikit.widget.item.ItemTextView
android:id="@+id/import_legacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/offsetMedium"
android:text="Values from legacy"
app:position="middle"/>

<uikit.widget.item.ItemTextView
android:id="@+id/import_mnemonic_again"
android:layout_width="match_parent"
Expand Down

0 comments on commit 1c011d0

Please sign in to comment.