Skip to content

Commit

Permalink
Merge pull request #1125 from radixdlt/fix/sign-purpose-check
Browse files Browse the repository at this point in the history
Pick the right factor instance based on sign purpose
  • Loading branch information
giannis-rdx authored Aug 7, 2024
2 parents 3a53526 + 307f92e commit 8e7ffb0
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.babylon.wallet.android.data.transaction

import com.babylon.wallet.android.domain.model.signing.SignPurpose
import com.babylon.wallet.android.domain.model.signing.SignRequest
import com.babylon.wallet.android.domain.model.signing.SignType
import com.babylon.wallet.android.domain.usecases.assets.GetEntitiesOwnerKeysUseCase
import com.babylon.wallet.android.domain.usecases.transaction.GenerateAuthSigningFactorInstanceUseCase
import com.babylon.wallet.android.presentation.accessfactorsources.AccessFactorSourcesInput
Expand Down Expand Up @@ -62,7 +62,7 @@ class ROLAClient @Inject constructor(
): Result<Map<ProfileEntity, SignatureWithPublicKey>> {
return accessFactorSourcesProxy.getSignatures(
accessFactorSourcesInput = AccessFactorSourcesInput.ToGetSignatures(
signType = SignType.ProvingOwnership,
signPurpose = SignPurpose.SignAuth,
signRequest = signRequest,
signers = entities
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.babylon.wallet.android.domain.model.signing

enum class SignPurpose {
SignTransaction, SignAuth
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.babylon.wallet.android.data.transaction.ROLAClient
import com.babylon.wallet.android.domain.RadixWalletException
import com.babylon.wallet.android.domain.model.IncomingMessage
import com.babylon.wallet.android.domain.model.IncomingMessage.IncomingRequest.AuthorizedRequest
import com.babylon.wallet.android.domain.model.signing.SignPurpose
import com.babylon.wallet.android.domain.model.signing.SignRequest
import com.babylon.wallet.android.domain.model.signing.SignType
import com.babylon.wallet.android.presentation.accessfactorsources.AccessFactorSourcesInput
import com.babylon.wallet.android.presentation.accessfactorsources.AccessFactorSourcesProxy
import com.babylon.wallet.android.presentation.model.toWalletToDappInteractionPersonaDataRequestResponseItem
Expand Down Expand Up @@ -59,7 +59,7 @@ open class BuildDappResponseUseCase(private val accessFactorSourcesProxy: Access

accessFactorSourcesProxy.getSignatures(
accessFactorSourcesInput = AccessFactorSourcesInput.ToGetSignatures(
signType = SignType.ProvingOwnership,
signPurpose = SignPurpose.SignAuth,
signers = accounts.map { it.asProfileEntity() },
signRequest = signRequest
)
Expand Down Expand Up @@ -298,7 +298,7 @@ class BuildUnauthorizedDappResponseUseCase @Inject constructor(

accessFactorSourcesProxy.getSignatures(
accessFactorSourcesInput = AccessFactorSourcesInput.ToGetSignatures(
signType = SignType.ProvingOwnership,
signPurpose = SignPurpose.SignAuth,
signers = oneTimeAccounts.map { it.asProfileEntity() },
signRequest = signRequest
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.babylon.wallet.android.data.repository.transaction.TransactionReposit
import com.babylon.wallet.android.data.transaction.NotaryAndSigners
import com.babylon.wallet.android.data.transaction.TransactionConfig.EPOCH_WINDOW
import com.babylon.wallet.android.domain.RadixWalletException
import com.babylon.wallet.android.domain.model.signing.SignPurpose
import com.babylon.wallet.android.domain.model.signing.SignRequest
import com.babylon.wallet.android.domain.model.signing.SignType
import com.babylon.wallet.android.domain.usecases.NotariseTransactionUseCase
import com.babylon.wallet.android.domain.usecases.ResolveNotaryAndSignersUseCase
import com.babylon.wallet.android.presentation.accessfactorsources.AccessFactorSourcesInput
Expand Down Expand Up @@ -105,7 +105,7 @@ class SignTransactionUseCase @Inject constructor(
} else {
accessFactorSourcesProxy.getSignatures(
accessFactorSourcesInput = AccessFactorSourcesInput.ToGetSignatures(
signType = SignType.SigningTransaction,
signPurpose = SignPurpose.SignTransaction,
signers = notaryAndSigners.signers,
signRequest = signRequest
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.babylon.wallet.android.presentation.accessfactorsources

import com.babylon.wallet.android.domain.model.signing.SignPurpose
import com.babylon.wallet.android.domain.model.signing.SignRequest
import com.babylon.wallet.android.domain.model.signing.SignType
import com.radixdlt.sargon.Account
import com.radixdlt.sargon.EntityKind
import com.radixdlt.sargon.FactorSource
Expand Down Expand Up @@ -30,7 +30,7 @@ interface AccessFactorSourcesProxy {
): Result<AccessFactorSourcesOutput.DerivedAccountsWithNextDerivationPath>

/**
* This method is used for signing, and based on [SignType] it can be
* This method is used for signing, and based on [SignPurpose] it can be
* - either for signing a transaction
* - or proving ownership.
*
Expand Down Expand Up @@ -115,7 +115,7 @@ sealed interface AccessFactorSourcesInput {
}

data class ToGetSignatures(
val signType: SignType,
val signPurpose: SignPurpose,
val signers: List<ProfileEntity>,
val signRequest: SignRequest
) : AccessFactorSourcesInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import com.babylon.wallet.android.R
import com.babylon.wallet.android.designsystem.composable.RadixTextButton
import com.babylon.wallet.android.designsystem.theme.RadixTheme
import com.babylon.wallet.android.designsystem.theme.RadixWalletTheme
import com.babylon.wallet.android.domain.model.signing.SignType
import com.babylon.wallet.android.domain.model.signing.SignPurpose
import com.babylon.wallet.android.presentation.accessfactorsources.composables.RoundLedgerItem
import com.babylon.wallet.android.presentation.accessfactorsources.signatures.GetSignaturesViewModel.State
import com.babylon.wallet.android.presentation.ui.composables.BottomSheetDialogWrapper
Expand Down Expand Up @@ -73,7 +73,7 @@ fun GetSignaturesDialog(

GetSignaturesBottomSheetContent(
modifier = modifier,
signType = state.signType,
signPurpose = state.signPurpose,
showContentForFactorSource = state.showContentForFactorSource,
isRetryButtonEnabled = state.isRetryButtonEnabled,
onDismiss = viewModel::onUserDismiss,
Expand All @@ -84,7 +84,7 @@ fun GetSignaturesDialog(
@Composable
private fun GetSignaturesBottomSheetContent(
modifier: Modifier = Modifier,
signType: SignType,
signPurpose: SignPurpose,
showContentForFactorSource: State.ShowContentForFactorSource,
isRetryButtonEnabled: Boolean,
onDismiss: () -> Unit,
Expand Down Expand Up @@ -114,27 +114,27 @@ private fun GetSignaturesBottomSheetContent(
Spacer(modifier = Modifier.height(RadixTheme.dimensions.paddingDefault))
Text(
style = RadixTheme.typography.title,
text = when (signType) {
SignType.SigningTransaction -> stringResource(id = R.string.factorSourceActions_signature_title)
SignType.ProvingOwnership -> stringResource(id = R.string.factorSourceActions_proveOwnership_title)
text = when (signPurpose) {
SignPurpose.SignTransaction -> stringResource(id = R.string.factorSourceActions_signature_title)
SignPurpose.SignAuth -> stringResource(id = R.string.factorSourceActions_proveOwnership_title)
}
)
Spacer(modifier = Modifier.height(RadixTheme.dimensions.paddingLarge))
when (showContentForFactorSource) {
is State.ShowContentForFactorSource.Device -> {
Text(
style = RadixTheme.typography.body1Regular,
text = when (signType) {
SignType.SigningTransaction -> stringResource(id = R.string.factorSourceActions_device_messageSignature)
SignType.ProvingOwnership -> stringResource(id = R.string.factorSourceActions_device_message)
text = when (signPurpose) {
SignPurpose.SignTransaction -> stringResource(id = R.string.factorSourceActions_device_messageSignature)
SignPurpose.SignAuth -> stringResource(id = R.string.factorSourceActions_device_message)
}
)
}

is State.ShowContentForFactorSource.Ledger -> {
val stringRes = when (signType) {
SignType.SigningTransaction -> stringResource(id = R.string.factorSourceActions_ledger_messageSignature)
SignType.ProvingOwnership -> stringResource(id = R.string.factorSourceActions_ledger_message)
val stringRes = when (signPurpose) {
SignPurpose.SignTransaction -> stringResource(id = R.string.factorSourceActions_ledger_messageSignature)
SignPurpose.SignAuth -> stringResource(id = R.string.factorSourceActions_ledger_message)
}
Text(
text = stringRes.formattedSpans(SpanStyle(fontWeight = FontWeight.Bold)),
Expand Down Expand Up @@ -165,7 +165,7 @@ private fun GetSignaturesBottomSheetContent(
fun GetSignaturesForSigningTransactionWithDevicePreview() {
RadixWalletTheme {
GetSignaturesBottomSheetContent(
signType = SignType.SigningTransaction,
signPurpose = SignPurpose.SignTransaction,
showContentForFactorSource = State.ShowContentForFactorSource.Device,
isRetryButtonEnabled = true,
onDismiss = {},
Expand All @@ -180,7 +180,7 @@ fun GetSignaturesForSigningTransactionWithDevicePreview() {
fun GetSignaturesForSigningTransactionWithLedgerPreview() {
RadixWalletTheme {
GetSignaturesBottomSheetContent(
signType = SignType.SigningTransaction,
signPurpose = SignPurpose.SignTransaction,
showContentForFactorSource = State.ShowContentForFactorSource.Ledger(
ledgerFactorSource = FactorSource.Ledger.sample()
),
Expand All @@ -197,7 +197,7 @@ fun GetSignaturesForSigningTransactionWithLedgerPreview() {
fun GetSignaturesForProvingOwnershipWithDevicePreview() {
RadixWalletTheme {
GetSignaturesBottomSheetContent(
signType = SignType.ProvingOwnership,
signPurpose = SignPurpose.SignAuth,
showContentForFactorSource = State.ShowContentForFactorSource.Device,
isRetryButtonEnabled = true,
onDismiss = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.babylon.wallet.android.data.dapp.model.LedgerErrorCode
import com.babylon.wallet.android.di.coroutines.DefaultDispatcher
import com.babylon.wallet.android.domain.RadixWalletException
import com.babylon.wallet.android.domain.model.signing.EntityWithSignature
import com.babylon.wallet.android.domain.model.signing.SignPurpose
import com.babylon.wallet.android.domain.model.signing.SignRequest
import com.babylon.wallet.android.domain.model.signing.SignType
import com.babylon.wallet.android.domain.usecases.signing.SignWithDeviceFactorSourceUseCase
import com.babylon.wallet.android.domain.usecases.signing.SignWithLedgerFactorSourceUseCase
import com.babylon.wallet.android.presentation.accessfactorsources.AccessFactorSourcesIOHandler
Expand Down Expand Up @@ -51,7 +51,7 @@ class GetSignaturesViewModel @Inject constructor(
private var collectSignaturesWithDeviceJob: Job? = null
private var collectSignaturesWithLedgerJob: Job? = null

override fun initialState(): State = State(signType = input.signType)
override fun initialState(): State = State(signPurpose = input.signPurpose)

init {
viewModelScope.launch {
Expand Down Expand Up @@ -85,12 +85,15 @@ class GetSignaturesViewModel @Inject constructor(
is FactorSource.ArculusCard -> {
// Not implemented yet
}

is FactorSource.OffDeviceMnemonic -> {
// Not implemented yet
}

is FactorSource.SecurityQuestions -> {
// Not implemented yet
}

is FactorSource.TrustedContact -> {
// Not implemented yet
}
Expand Down Expand Up @@ -253,7 +256,11 @@ class GetSignaturesViewModel @Inject constructor(
signers.forEach { signer ->
when (val securityState = signer.securityState) {
is EntitySecurityState.Unsecured -> {
val factorSourceId = securityState.value.transactionSigning.factorSourceId.asGeneral()
val factorSourceId = when (state.value.signPurpose) {
SignPurpose.SignTransaction -> securityState.value.transactionSigning.factorSourceId.asGeneral()
SignPurpose.SignAuth -> securityState.value.authenticationSigning?.factorSourceId?.asGeneral()
?: securityState.value.transactionSigning.factorSourceId.asGeneral()
}
val factorSource = requireNotNull(profile.factorSourceById(factorSourceId))

if (factorSource.kind != FactorSourceKind.TRUSTED_CONTACT) { // trusted contact cannot sign!
Expand Down Expand Up @@ -284,7 +291,7 @@ class GetSignaturesViewModel @Inject constructor(
}

data class State(
val signType: SignType,
val signPurpose: SignPurpose,
private val signersRequests: List<FactorSourceRequest> = emptyList(),
private val selectedSignersIndex: Int = -1,
// map to keep signature for each entity (signer). This will be returned as output once all signers are done.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.babylon.wallet.android.data.dapp.model.LedgerErrorCode
import com.babylon.wallet.android.domain.RadixWalletException
import com.babylon.wallet.android.domain.model.signing.EntityWithSignature
import com.babylon.wallet.android.domain.model.signing.SignRequest
import com.babylon.wallet.android.domain.model.signing.SignType
import com.babylon.wallet.android.domain.model.signing.SignPurpose
import com.babylon.wallet.android.domain.usecases.signing.SignWithDeviceFactorSourceUseCase
import com.babylon.wallet.android.domain.usecases.signing.SignWithLedgerFactorSourceUseCase
import com.babylon.wallet.android.fakes.FakeProfileRepository
Expand Down Expand Up @@ -138,7 +138,7 @@ class GetSignaturesViewModelTest : StateViewModelTest<GetSignaturesViewModel>()
backgroundScope.launch(Dispatchers.Default) { // TransactionReviewScreen needs to access factor sources to get signatures
val result = accessFactorSourcesProxyFake.getSignatures(
accessFactorSourcesInput = AccessFactorSourcesInput.ToGetSignatures(
signType = SignType.SigningTransaction,
signPurpose = SignPurpose.SignTransaction,
signers = listOf(signerWithLedgerFactorSource, signerWithDeviceFactorSource),
signRequest = signRequest
)
Expand Down Expand Up @@ -176,7 +176,7 @@ class GetSignaturesViewModelTest : StateViewModelTest<GetSignaturesViewModel>()
backgroundScope.launch(Dispatchers.Default) { // TransactionReviewScreen needs to access factor sources to get signatures
val result = accessFactorSourcesProxyFake.getSignatures(
accessFactorSourcesInput = AccessFactorSourcesInput.ToGetSignatures(
signType = SignType.SigningTransaction,
signPurpose = SignPurpose.SignTransaction,
signers = signers,
signRequest = signRequest
)
Expand Down Expand Up @@ -235,7 +235,7 @@ class AccessFactorSourcesProxyFake : AccessFactorSourcesProxy, AccessFactorSourc

override fun getInput(): AccessFactorSourcesInput {
return AccessFactorSourcesInput.ToGetSignatures(
signType = SignType.SigningTransaction,
signPurpose = SignPurpose.SignTransaction,
signers = signers,
signRequest = signRequest
)
Expand Down

0 comments on commit 8e7ffb0

Please sign in to comment.