-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Tink as a dependency * Serialize AddressBook to any OutputStream * Extract address book format parser * Address book key storage provider * Address book encryption finalisation * Implement AddressBook encryption * Address book encryption code cleanup * Address book reset hotfix * SDK snapshot * Documentation update * Code cleanup * Test hotfix * Error handling * Code cleanup * Unencrypted address book removed after successful encrypted file read * Code cleanup * Code cleanup * Test hotfix --------- Co-authored-by: Milan Cerovsky <[email protected]> Co-authored-by: Honza <[email protected]>
- Loading branch information
1 parent
f59add8
commit 6aee0e2
Showing
23 changed files
with
500 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...b/src/main/java/co/electriccoin/zcash/ui/common/provider/AddressBookKeyStorageProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package co.electriccoin.zcash.ui.common.provider | ||
|
||
import co.electriccoin.zcash.preference.EncryptedPreferenceProvider | ||
import co.electriccoin.zcash.preference.api.PreferenceProvider | ||
import co.electriccoin.zcash.preference.model.entry.PreferenceDefault | ||
import co.electriccoin.zcash.preference.model.entry.PreferenceKey | ||
import co.electriccoin.zcash.ui.common.serialization.addressbook.AddressBookKey | ||
import com.google.crypto.tink.InsecureSecretKeyAccess | ||
import com.google.crypto.tink.SecretKeyAccess | ||
import com.google.crypto.tink.util.SecretBytes | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
|
||
interface AddressBookKeyStorageProvider { | ||
suspend fun getAddressBookKey(): AddressBookKey? | ||
|
||
suspend fun storeAddressBookKey(addressBookKey: AddressBookKey) | ||
} | ||
|
||
class AddressBookKeyStorageProviderImpl( | ||
private val encryptedPreferenceProvider: EncryptedPreferenceProvider | ||
) : AddressBookKeyStorageProvider { | ||
private val default = AddressBookKeyPreferenceDefault() | ||
|
||
override suspend fun getAddressBookKey(): AddressBookKey? { | ||
return default.getValue(encryptedPreferenceProvider()) | ||
} | ||
|
||
override suspend fun storeAddressBookKey(addressBookKey: AddressBookKey) { | ||
default.putValue(encryptedPreferenceProvider(), addressBookKey) | ||
} | ||
} | ||
|
||
private class AddressBookKeyPreferenceDefault : PreferenceDefault<AddressBookKey?> { | ||
private val secretKeyAccess: SecretKeyAccess? | ||
get() = InsecureSecretKeyAccess.get() | ||
|
||
override val key: PreferenceKey = PreferenceKey("address_book_key") | ||
|
||
override suspend fun getValue(preferenceProvider: PreferenceProvider) = preferenceProvider.getString(key)?.decode() | ||
|
||
override suspend fun putValue( | ||
preferenceProvider: PreferenceProvider, | ||
newValue: AddressBookKey? | ||
) = preferenceProvider.putString(key, newValue?.encode()) | ||
|
||
@OptIn(ExperimentalEncodingApi::class) | ||
private fun AddressBookKey?.encode() = | ||
if (this != null) { | ||
Base64.encode(this.key.toByteArray(secretKeyAccess)) | ||
} else { | ||
null | ||
} | ||
|
||
@OptIn(ExperimentalEncodingApi::class) | ||
private fun String?.decode() = | ||
if (this != null) { | ||
AddressBookKey(SecretBytes.copyFrom(Base64.decode(this), secretKeyAccess)) | ||
} else { | ||
null | ||
} | ||
} |
Oops, something went wrong.