1
0
Форкнуть 0

Bring back destructive fallback for db

This commit is contained in:
Andreas Mikolajewski 2021-02-18 20:24:32 +01:00
Родитель f0d990e93b
Коммит 89dfdb8c61
7 изменённых файлов: 14 добавлений и 13 удалений

Просмотреть файл

@ -10,7 +10,7 @@ import com.microsoft.did.sdk.crypto.keyStore.EncryptedKeyStore
import com.microsoft.did.sdk.datasource.repository.IdentifierRepository
import com.microsoft.did.sdk.identifier.IdentifierCreator
import com.microsoft.did.sdk.identifier.models.Identifier
import com.microsoft.did.sdk.util.Constants.MASTER_IDENTIFIER_NAME
import com.microsoft.did.sdk.util.Constants.MAIN_IDENTIFIER_REFERENCE
import com.microsoft.did.sdk.util.controlflow.RepositoryException
import com.microsoft.did.sdk.util.controlflow.Result
import com.microsoft.did.sdk.util.controlflow.runResultTry
@ -30,7 +30,7 @@ class IdentifierManager @Inject constructor(
) {
suspend fun getMasterIdentifier(): Result<Identifier> {
val identifier = identifierRepository.queryByName(MASTER_IDENTIFIER_NAME)
val identifier = identifierRepository.queryByName(MAIN_IDENTIFIER_REFERENCE)
return if (identifier != null) {
Result.Success(identifier)
} else {
@ -41,9 +41,9 @@ class IdentifierManager @Inject constructor(
private suspend fun createMasterIdentifier(): Result<Identifier> {
return runResultTry {
val seed = CryptoOperations.generateSeed()
keyStore.storeKey(MASTER_IDENTIFIER_NAME, OctetSequenceKey.Builder(seed).build())
val identifier = identifierCreator.create(MASTER_IDENTIFIER_NAME)
SdkLog.i("Creating Identifier: $identifier")
keyStore.storeKey(MAIN_IDENTIFIER_REFERENCE, OctetSequenceKey.Builder(seed).build())
val identifier = identifierCreator.create(MAIN_IDENTIFIER_REFERENCE)
SdkLog.i("Created Identifier: $identifier")
identifierRepository.insert(identifier)
Result.Success(identifier)
}

Просмотреть файл

@ -17,7 +17,7 @@ import com.microsoft.did.sdk.identifier.models.Identifier
* More info:
* https://developer.android.com/topic/libraries/architecture/room
*/
@Database(entities = [Identifier::class], version = 1)
@Database(entities = [Identifier::class], version = 2)
abstract class SdkDatabase : RoomDatabase() {
abstract fun identifierDao(): IdentifierDao

Просмотреть файл

@ -87,6 +87,7 @@ class SdkModule {
@Singleton
fun sdkDatabase(context: Context): SdkDatabase {
return Room.databaseBuilder(context, SdkDatabase::class.java, "vc-sdk-db")
.fallbackToDestructiveMigration() // TODO: Remove during public preview
.build()
}

Просмотреть файл

@ -80,7 +80,7 @@ class IdentifierCreator @Inject constructor(
}
internal fun generatePersonaSeed(personaDid: String): ByteArray {
val masterSeed = keyStore.getKey(Constants.MASTER_IDENTIFIER_NAME).toOctetSequenceKey().toByteArray()
val masterSeed = keyStore.getKey(Constants.MAIN_IDENTIFIER_REFERENCE).toOctetSequenceKey().toByteArray()
return CryptoOperations.computeMac(personaDid.toByteArray(), SecretKeySpec(masterSeed, AES_KEY), MacAlgorithm.HmacSha512)
}

Просмотреть файл

@ -43,7 +43,7 @@ object Constants {
const val CORRELATION_VECTOR_IN_PREF = "correlation_vector"
//Identifier Constants
const val MASTER_IDENTIFIER_NAME = "did.main.identifier"
const val MAIN_IDENTIFIER_REFERENCE = "did.main.identifier"
const val METHOD_NAME = "ion"
const val IDENTIFIER_PUBLIC_KEY_PURPOSE = "authentication"
const val COLON = ":"

Просмотреть файл

@ -55,7 +55,7 @@ class PairwiseKeyTest {
val expectedEncodedMasterKey = "h-Z5gO1eBjY1EYXh64-f8qQF5ojeh1KVMKxmd0JI3YKScTOYjVm-h1j2pUNV8q6s8yphAR4lk5yXYiQhAOVlUw"
var persona = "persona"
val keyStore: EncryptedKeyStore = mockk()
every { keyStore.getKey(Constants.MASTER_IDENTIFIER_NAME) } returns seedKey
every { keyStore.getKey(Constants.MAIN_IDENTIFIER_REFERENCE) } returns seedKey
val creator = IdentifierCreator(mockk(), mockk(), mockk(), keyStore)
var masterKey = creator.generatePersonaSeed(persona)
var actualEncodedMasterKey = Base64URL.encode(masterKey)
@ -99,7 +99,7 @@ class PairwiseKeyTest {
val creator = IdentifierCreator(mockk(), mockk(), mockk(), keyStore)
for (i in 0..49) {
val seed ="1234567890-$i".toByteArray()
every { keyStore.getKey(Constants.MASTER_IDENTIFIER_NAME) } returns OctetSequenceKey.Builder(seed).build()
every { keyStore.getKey(Constants.MAIN_IDENTIFIER_REFERENCE) } returns OctetSequenceKey.Builder(seed).build()
val generatedSeed = creator.generatePersonaSeed(persona)
val pairwiseKey = crypto.generateKey<ECPrivateKey>(PrivateKeyFactoryAlgorithm.EcPairwise(
EcPairwisePrivateKeySpec(generatedSeed, peer)
@ -120,7 +120,7 @@ class PairwiseKeyTest {
val peer = "did:peer:1"
val keyStore: EncryptedKeyStore = mockk()
val creator = IdentifierCreator(mockk(), mockk(), mockk(), keyStore)
every { keyStore.getKey(Constants.MASTER_IDENTIFIER_NAME) } returns seedKey
every { keyStore.getKey(Constants.MAIN_IDENTIFIER_REFERENCE) } returns seedKey
val personaSeed = creator.generatePersonaSeed(persona)
for (i in 0..49) {
val suppliedPeer = "$peer-$i"
@ -145,7 +145,7 @@ class PairwiseKeyTest {
val seed = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi".toByteArray()
val keyStore: EncryptedKeyStore = mockk()
val creator = IdentifierCreator(mockk(), mockk(), mockk(), keyStore)
every { keyStore.getKey(Constants.MASTER_IDENTIFIER_NAME) } returns OctetSequenceKey.Builder(seed).build()
every { keyStore.getKey(Constants.MAIN_IDENTIFIER_REFERENCE) } returns OctetSequenceKey.Builder(seed).build()
val persona = "abcdef"
val personaSeed = creator.generatePersonaSeed(persona)
for (index in 0 until countOfIds) {

Просмотреть файл

@ -97,7 +97,7 @@ class IdentifierCreatorTest {
fun `pairwise Identifier gets created properly`() {
val masterIdentifier = identifierCreator.create(personaName)
val masterSeed = ByteArray(16, { it.toByte() })
every { keyStore.getKey(Constants.MASTER_IDENTIFIER_NAME) } returns OctetSequenceKey.Builder(masterSeed).build()
every { keyStore.getKey(Constants.MAIN_IDENTIFIER_REFERENCE) } returns OctetSequenceKey.Builder(masterSeed).build()
val actualIdentifier = identifierCreator.createPairwiseId(masterIdentifier, "randomDid")
val expectedDid = "did:ion:EiDm_INb-7RpbbbiftnuCXt033w6UCLiL7aUEZRSEtaydA:eyJkZWx0YSI6eyJwYXRjaGVzIjpbeyJhY3Rpb24iOiJyZXBsYWNlIiwiZG9jdW1lbnQiOnsicHVibGljS2V5cyI6W3siaWQiOiI2NTA2YWE2NzFhNTE0MmEwYTE4NzU0NDJiM2QxY2QyNSIsInB1YmxpY0tleUp3ayI6eyJjcnYiOiJzZWNwMjU2azEiLCJraWQiOiI2NTA2YWE2NzFhNTE0MmEwYTE4NzU0NDJiM2QxY2QyNSIsImt0eSI6IkVDIiwieCI6IkJCdkxxSkNyeWJoLUJhV1VOMDhLdG9ibUl4ME5SZUNCQTVDWDhKTzI0ZXciLCJ5IjoiaUJVM1A2TGt4VTJKb0Y3MnpFTEprUjdEZmxJLWNtN1F1aHBMZEZCTXJCYyJ9LCJwdXJwb3NlcyI6WyJhdXRoZW50aWNhdGlvbiJdLCJ0eXBlIjoiRWNkc2FTZWNwMjU2azFWZXJpZmljYXRpb25LZXkyMDE5In1dfX1dLCJ1cGRhdGVDb21taXRtZW50IjoiRWlDYllJcEJTaVU5dFVlOS1GdWd3Ti1MWmxPQS03dGlOWThHWWJUejFXY3JQdyJ9LCJzdWZmaXhEYXRhIjp7ImRlbHRhSGFzaCI6IkVpQW96Ty1JcGQ5SFFESjNOaUtVaF92UnVoc3JDX0ctbUR3QjExMjlQN1N6Z2ciLCJyZWNvdmVyeUNvbW1pdG1lbnQiOiJFaUJDYy1HQmt0NXo0Y1FXQUhHc1RFM3FocktxZG1nM0NyWDY1cEhWNVVBeDFBIn19"
val expectedPairwiseName = "q9eFTKE8MG8LO_YzDIgmGw"