Skip to content

Commit

Permalink
support legacy keys
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Nov 6, 2024
1 parent 818430e commit 0a26887
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal class RNSeedStorage(context: Context) {
private suspend fun readState(): SeedState? {
val chunks = kv.getItemImpl("${walletsKey}_chunks")?.toIntOrNull() ?: 0
if (0 >= chunks) {
return null
return readStateLegacy()
}
var encryptedString = ""
for (i in 0 until chunks) {
Expand All @@ -126,6 +126,20 @@ internal class RNSeedStorage(context: Context) {
return SeedState(json)
}

private suspend fun readStateLegacy(): SeedState? {
val chunks = kv.getItemImpl("key_v1-${walletsKey}_chunks")?.toIntOrNull() ?: 0
if (0 >= chunks) {
return null
}
var encryptedString = ""
for (i in 0 until chunks) {
val chunk = kv.getItemImpl("key_v1-${walletsKey}_chunk_$i") ?: throw Exception("Chunk $i is null")
encryptedString += chunk
}
val json = JSONObject(encryptedString)
return SeedState(json)
}

suspend fun getWithThrow(passcode: String): RNVaultState = withContext(Dispatchers.IO) {
val state = readStateWithThrow()
val decrypted = ScryptBox.decrypt(passcode, state)
Expand All @@ -136,7 +150,7 @@ internal class RNSeedStorage(context: Context) {
private suspend fun readStateWithThrow(): SeedState {
val chunks = kv.getItemImpl("${walletsKey}_chunks")?.toIntOrNull() ?: 0
if (0 >= chunks) {
throw RNException.EmptyChunks
return readStateLegacyWithThrow()
}
val builder = StringBuilder()
for (i in 0 until chunks) {
Expand All @@ -146,4 +160,18 @@ internal class RNSeedStorage(context: Context) {
val json = JSONObject(builder.toString())
return SeedState(json)
}

private suspend fun readStateLegacyWithThrow(): SeedState {
val chunks = kv.getItemImpl("key_v1-${walletsKey}_chunks")?.toIntOrNull() ?: 0
if (0 >= chunks) {
throw RNException.EmptyChunks
}
val builder = StringBuilder()
for (i in 0 until chunks) {
val chunk = kv.getItemImpl("key_v1-${walletsKey}_chunk_$i") ?: throw RNException.NotFoundChunk(i)
builder.append(chunk)
}
val json = JSONObject(builder.toString())
return SeedState(json)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/offsetMedium"
android:text="Import DApps from RN biometry"
android:text="Import DApps from RN"
app:position="middle"/>

<uikit.widget.item.ItemTextView
Expand Down

0 comments on commit 0a26887

Please sign in to comment.