Skip to content

Commit

Permalink
Merge pull request #1277 from radixdlt/feature/sign-with-sargon
Browse files Browse the repository at this point in the history
Sign with sargon
  • Loading branch information
micbakos-rdx authored Jan 9, 2025
2 parents 67746ce + dd871a6 commit e26ad0d
Show file tree
Hide file tree
Showing 65 changed files with 2,354 additions and 1,797 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/babylon/wallet/android/WalletApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.babylon.wallet.android.domain.model.messages.WalletAuthorizedRequest
import com.babylon.wallet.android.domain.model.messages.WalletUnauthorizedRequest
import com.babylon.wallet.android.presentation.accessfactorsources.deriveaccounts.deriveAccounts
import com.babylon.wallet.android.presentation.accessfactorsources.derivepublickey.derivePublicKeyDialog
import com.babylon.wallet.android.presentation.accessfactorsources.derivepublickeys.derivePublicKeysDialog
import com.babylon.wallet.android.presentation.accessfactorsources.signatures.getSignatures
import com.babylon.wallet.android.presentation.account.settings.delete.success.deletedAccountSuccess
import com.babylon.wallet.android.presentation.dapp.authorized.login.dAppLoginAuthorized
Expand Down Expand Up @@ -233,6 +234,7 @@ private fun HandleAccessFactorSourcesEvents(
accessFactorSourcesEvents.collect { event ->
when (event) {
AppEvent.AccessFactorSources.DerivePublicKey -> navController.derivePublicKeyDialog()
AppEvent.AccessFactorSources.DerivePublicKeys -> navController.derivePublicKeysDialog()
is AppEvent.AccessFactorSources.DeriveAccounts -> navController.deriveAccounts()
AppEvent.AccessFactorSources.GetSignatures -> navController.getSignatures()
is AppEvent.AccessFactorSources.SelectedLedgerDevice -> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class LedgerMessengerImpl @Inject constructor(
mode = LedgerInteractionRequest.SignTransaction.Mode.Summary
)
return makeLedgerRequest(request = ledgerRequest, onError = {
RadixWalletException.LedgerCommunicationException.FailedToSignTransaction(it.code)
RadixWalletException.LedgerCommunicationException.FailedToSignTransaction(it.code, it.message)
})
}

Expand All @@ -133,7 +133,7 @@ class LedgerMessengerImpl @Inject constructor(
subintentHash = subintentHash
)
return makeLedgerRequest(request = ledgerRequest, onError = {
RadixWalletException.LedgerCommunicationException.FailedToSignTransaction(it.code)
RadixWalletException.LedgerCommunicationException.FailedToSignTransaction(it.code, it.message)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import rdx.works.core.mapError

fun DappToWalletInteractionUnvalidated.toDomainModel(remoteEntityId: RemoteEntityID) = runCatching {
val metadata = DappToWalletInteraction.RequestMetadata(
version = metadata.version,
networkId = metadata.networkId,
origin = metadata.origin,
dAppDefinitionAddress = metadata.dappDefinitionAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.babylon.wallet.android.data.dapp.model

import com.radixdlt.sargon.DerivationPath
import com.radixdlt.sargon.FactorSource
import com.radixdlt.sargon.LedgerHardwareWalletModel
import com.radixdlt.sargon.PublicKey
import com.radixdlt.sargon.Slip10Curve
import com.radixdlt.sargon.extensions.curve
import com.radixdlt.sargon.extensions.hex
import com.radixdlt.sargon.extensions.string
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator
Expand Down Expand Up @@ -121,7 +125,16 @@ sealed interface LedgerInteractionRequest {
val curve: Curve,
@SerialName("derivationPath")
val derivationPath: String
)
) {

companion object {

fun from(derivationPath: DerivationPath) = KeyParameters(
curve = Curve.from(derivationPath.curve),
derivationPath = derivationPath.string
)
}
}
}

@Serializable
Expand All @@ -137,6 +150,11 @@ enum class Curve {
is PublicKey.Ed25519 -> Curve25519
is PublicKey.Secp256k1 -> Secp256k1
}

fun from(curve: Slip10Curve): Curve = when (curve) {
Slip10Curve.CURVE25519 -> Curve25519
Slip10Curve.SECP256K1 -> Secp256k1
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.babylon.wallet.android.data.dapp.model

import com.radixdlt.sargon.AddressOfAccountOrPersona
import com.radixdlt.sargon.EmailAddress
import com.radixdlt.sargon.Exactly32Bytes
import com.radixdlt.sargon.PersonaData
Expand All @@ -15,7 +16,6 @@ import com.radixdlt.sargon.WalletToDappInteractionPersonaDataRequestResponseItem
import com.radixdlt.sargon.WalletToDappInteractionPersonaProof
import com.radixdlt.sargon.WalletToDappInteractionProofOfOwnership
import com.radixdlt.sargon.WalletToDappInteractionProofOfOwnershipRequestResponseItem
import com.radixdlt.sargon.extensions.ProfileEntity
import com.radixdlt.sargon.extensions.publicKey
import com.radixdlt.sargon.extensions.signature

Expand All @@ -41,24 +41,24 @@ fun PersonaData.toWalletToDappInteractionPersonaDataRequestResponseItem(): Walle
)
}

fun Map<ProfileEntity, SignatureWithPublicKey>.toWalletToDappInteractionProofOfOwnershipRequestResponseItem(
fun Map<AddressOfAccountOrPersona, SignatureWithPublicKey>.toWalletToDappInteractionProofOfOwnershipRequestResponseItem(
challenge: Exactly32Bytes
): WalletToDappInteractionProofOfOwnershipRequestResponseItem {
val entitiesWithProofs = this.map { (profileEntity, signatureWithPublicKey) ->
when (profileEntity) {
is ProfileEntity.PersonaEntity -> {
val entitiesWithProofs = this.map { (entityAddress, signatureWithPublicKey) ->
when (entityAddress) {
is AddressOfAccountOrPersona.Identity -> {
WalletToDappInteractionProofOfOwnership.Persona(
v1 = WalletToDappInteractionPersonaProof(
identityAddress = profileEntity.identityAddress,
identityAddress = entityAddress.v1,
proof = signatureWithPublicKey.toWalletToDappInteractionAuthProof()
)
)
}

is ProfileEntity.AccountEntity -> {
is AddressOfAccountOrPersona.Account -> {
WalletToDappInteractionProofOfOwnership.Account(
v1 = WalletToDappInteractionAccountProof(
accountAddress = profileEntity.accountAddress,
accountAddress = entityAddress.v1,
proof = signatureWithPublicKey.toWalletToDappInteractionAuthProof()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.babylon.wallet.android.data.repository.homecards.HomeCardsObserverWrapper
import com.babylon.wallet.android.data.repository.homecards.HomeCardsObserverWrapperImpl
import com.babylon.wallet.android.presentation.accessfactorsources.AccessFactorSourcesProxy
import com.babylon.wallet.android.presentation.interactor.WalletInteractor
import com.radixdlt.sargon.HomeCardsManager
import com.radixdlt.sargon.HostInteractor
import com.radixdlt.sargon.NetworkId
import com.radixdlt.sargon.RadixConnectMobile
import com.radixdlt.sargon.extensions.init
Expand Down Expand Up @@ -95,4 +98,10 @@ object ApplicationModule {
) = BiometricsHandler(
biometricsSystemDialogTitle = context.getString(com.babylon.wallet.android.R.string.biometrics_prompt_title)
)

@Provides
@Singleton
fun provideHostInteractor(
accessFactorSourcesProxy: AccessFactorSourcesProxy
): HostInteractor = WalletInteractor(accessFactorSourcesProxy)
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/babylon/wallet/android/di/UiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.babylon.wallet.android.presentation.accessfactorsources.AccessFactorS
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(ActivityRetainedComponent::class)
@InstallIn(SingletonComponent::class)
interface UiModule {

@Binds
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.babylon.wallet.android.domain

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.babylon.wallet.android.BuildConfig
import com.babylon.wallet.android.R
import com.radixdlt.sargon.CommonException
import com.radixdlt.sargon.errorCodeFromError
import com.radixdlt.sargon.errorMessageFromError

@Composable
fun CommonException.toMessage(): String = with(LocalContext.current) {
toMessage(context = this)
}

fun CommonException.toMessage(context: Context): String {
val messageBuilder = StringBuilder()

messageBuilder.appendLine(publicMessage(context))

if (BuildConfig.EXPERIMENTAL_FEATURES_ENABLED) {
messageBuilder.appendLine()
messageBuilder.appendLine("[${errorCodeFromError(error = this)}]")
messageBuilder.appendLine(errorMessageFromError(error = this))
}

return messageBuilder.toString()
}

private fun CommonException.publicMessage(context: Context) = when (this) {
is CommonException.UnableToLoadMnemonicFromSecureStorage -> "Please restore your Seed Phrase and try again"
is CommonException.SecureStorageAccessException ->
"There was an issue trying to access you Seed Phrase from secure storage. ${this.errorMessage}"
is CommonException.SecureStorageReadException -> context.getString(R.string.homePage_secureFolder_warning)
else -> context.getString(R.string.common_somethingWentWrong)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.radixdlt.sargon.ResourceAddress
import com.radixdlt.sargon.ResourceOrNonFungible
import com.radixdlt.sargon.extensions.string
import rdx.works.profile.cloudbackup.model.BackupServiceException
import rdx.works.profile.domain.ProfileException

sealed class RadixWalletException(cause: Throwable? = null) : Throwable(cause = cause) {

Expand Down Expand Up @@ -48,9 +47,7 @@ sealed class RadixWalletException(cause: Throwable? = null) : Throwable(cause =
data object InvalidPersonaOrAccounts : DappRequestException()
data object InvalidRequestChallenge : DappRequestException()
data object NotPossibleToAuthenticateAutomatically : DappRequestException()
data class FailedToSignAuthChallenge(override val cause: Throwable? = null) :
DappRequestException(cause = cause)

data object FailedToSignAuthChallenge : DappRequestException()
data class PreviewError(override val cause: Throwable?) : DappRequestException()
data object InvalidPreAuthorizationExpirationTooClose : DappRequestException()
data object InvalidPreAuthorizationExpired : DappRequestException()
Expand Down Expand Up @@ -92,8 +89,6 @@ sealed class RadixWalletException(cause: Throwable? = null) : Throwable(cause =
data class BuildTransactionHeader(override val cause: Throwable) : PrepareTransactionException(cause)
data object FailedToFindAccountWithEnoughFundsToLockFee : PrepareTransactionException()
data object CompileTransactionIntent : PrepareTransactionException()
data class SignCompiledTransactionIntent(override val cause: Throwable? = null) :
PrepareTransactionException(cause)

data class PrepareNotarizedTransaction(override val cause: Throwable? = null) :
PrepareTransactionException(cause)
Expand All @@ -114,7 +109,6 @@ sealed class RadixWalletException(cause: Throwable? = null) : Throwable(cause =
}

is CompileTransactionIntent -> DappWalletInteractionErrorType.FAILED_TO_COMPILE_TRANSACTION
is SignCompiledTransactionIntent -> DappWalletInteractionErrorType.FAILED_TO_SIGN_TRANSACTION
is ReceivingAccountDoesNotAllowDeposits -> DappWalletInteractionErrorType.FAILED_TO_PREPARE_TRANSACTION
}
}
Expand Down Expand Up @@ -186,7 +180,7 @@ sealed class RadixWalletException(cause: Throwable? = null) : Throwable(cause =
data object FailedToDerivePublicKeys : LedgerCommunicationException()
data object FailedToDeriveAndDisplayAddress : LedgerCommunicationException()
data object FailedToSignAuthChallenge : LedgerCommunicationException()
data class FailedToSignTransaction(val reason: LedgerErrorCode) : LedgerCommunicationException()
data class FailedToSignTransaction(val reason: LedgerErrorCode, override val message: String?) : LedgerCommunicationException()

override val dappWalletInteractionErrorType: DappWalletInteractionErrorType
get() = when (this) {
Expand Down Expand Up @@ -258,7 +252,7 @@ fun RadixWalletException.LedgerCommunicationException.toUserFriendlyMessage(cont
R.string.ledgerHardwareDevices_verification_requestFailed
}

RadixWalletException.LedgerCommunicationException.FailedToConnect -> R.string.common_somethingWentWrong
RadixWalletException.LedgerCommunicationException.FailedToConnect -> R.string.ledgerHardwareDevices_linkConnectorAlert_message
}
)
}
Expand Down Expand Up @@ -412,21 +406,6 @@ fun RadixWalletException.PrepareTransactionException.toUserFriendlyMessage(conte
R.string.error_transactionFailure_noFundsToApproveTransaction

is RadixWalletException.PrepareTransactionException.CompileTransactionIntent -> R.string.error_transactionFailure_prepare
is RadixWalletException.PrepareTransactionException.SignCompiledTransactionIntent ->
when (val cause = this.cause) {
is ProfileException.NoMnemonic -> {
R.string.common_noMnemonicAlert_text
}

is RadixWalletException.LedgerCommunicationException -> {
return cause.toUserFriendlyMessage(context)
}

else -> {
R.string.error_transactionFailure_prepare
}
}

RadixWalletException.PrepareTransactionException.ReceivingAccountDoesNotAllowDeposits ->
R.string.error_transactionFailure_doesNotAllowThirdPartyDeposits
RadixWalletException.PrepareTransactionException.RequestNotFound -> R.string.error_transactionFailure_prepare
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.radixdlt.sargon.Exactly32Bytes
import com.radixdlt.sargon.NetworkId
import com.radixdlt.sargon.RequestedNumberQuantifier
import com.radixdlt.sargon.WalletInteractionId
import com.radixdlt.sargon.WalletInteractionVersion
import com.radixdlt.sargon.newWalletInteractionVersionCurrent
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable
import rdx.works.core.sargon.PersonaDataField
Expand Down Expand Up @@ -32,6 +34,7 @@ sealed class DappToWalletInteraction(
}

data class RequestMetadata(
val version: WalletInteractionVersion,
val networkId: NetworkId,
val origin: String,
val dAppDefinitionAddress: String,
Expand All @@ -44,6 +47,7 @@ sealed class DappToWalletInteraction(
networkId: NetworkId,
blockUntilCompleted: Boolean = false
) = RequestMetadata(
version = newWalletInteractionVersionCurrent(),
networkId = networkId,
origin = "",
dAppDefinitionAddress = "",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit e26ad0d

Please sign in to comment.