Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:WalletConnect/WalletConnectKotli…
Browse files Browse the repository at this point in the history
…nV2 into sign_2.5
  • Loading branch information
jakubuid committed Mar 7, 2024
2 parents ebae134 + 70c713b commit 314b5e0
Show file tree
Hide file tree
Showing 57 changed files with 1,269 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.walletconnect.android.internal.common.di.coreJsonRpcModule
import com.walletconnect.android.internal.common.di.corePairingModule
import com.walletconnect.android.internal.common.di.explorerModule
import com.walletconnect.android.internal.common.di.keyServerModule
import com.walletconnect.android.internal.common.di.pulseModule
import com.walletconnect.android.internal.common.di.pushModule
import com.walletconnect.android.internal.common.di.web3ModalModule
import com.walletconnect.android.internal.common.explorer.ExplorerInterface
Expand Down Expand Up @@ -38,6 +39,7 @@ class CoreProtocol(private val koinApp: KoinApplication = wcKoinApp) : CoreInter
override val Pairing: PairingInterface = PairingProtocol(koinApp)
override val PairingController: PairingControllerInterface = PairingController(koinApp)
override var Relay = RelayClient(koinApp)

@Deprecated(message = "Replaced with Push")
override val Echo: PushInterface = PushClient
override val Push: PushInterface = PushClient
Expand Down Expand Up @@ -83,7 +85,8 @@ class CoreProtocol(private val koinApp: KoinApplication = wcKoinApp) : CoreInter
corePairingModule(Pairing, PairingController),
keyServerModule(keyServerUrl),
explorerModule(),
web3ModalModule()
web3ModalModule(),
pulseModule()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ enum class AndroidCommonDITags {
USER_AGENT,
RELAY_URL,
KEYSERVER_URL,
PULSE_URL,
KEYSERVER_RETROFIT,
PULSE_RETROFIT,
VERIFY_RETROFIT,
VERIFY_URL,
EXPLORER_URL,
Expand All @@ -36,5 +38,7 @@ enum class AndroidCommonDITags {
DECRYPT_SIGN_MESSAGE,
DECRYPT_AUTH_MESSAGE,
DECRYPT_NOTIFY_MESSAGE,
DECRYPT_USE_CASES
DECRYPT_USE_CASES,
BUNDLE_ID,
ENABLE_ANALYTICS
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.walletconnect.android.internal.common.adapter.JsonRpcResultAdapter
import com.walletconnect.android.internal.common.adapter.TagsAdapter
import com.walletconnect.android.internal.common.model.Expiry
import com.walletconnect.android.internal.common.model.Tags
import com.walletconnect.android.pulse.model.properties.Props
import com.walletconnect.foundation.di.FoundationDITags
import com.walletconnect.foundation.di.foundationCommonModule
import com.walletconnect.foundation.util.Logger
Expand All @@ -27,6 +28,25 @@ fun coreCommonModule() = module {
.withSubtype(JsonRpcResponse.JsonRpcError::class.java, "error")
}

single<PolymorphicJsonAdapterFactory<Props>> {
PolymorphicJsonAdapterFactory.of(Props::class.java, "type")
.withSubtype(Props.ModalCreated::class.java, "modal_created")
.withSubtype(Props.ModalLoaded::class.java, "modal_loaded")
.withSubtype(Props.ModalOpen::class.java, "modal_open")
.withSubtype(Props.ModalClose::class.java, "modal_close")
.withSubtype(Props.ClickNetworks::class.java, "click_networks")
.withSubtype(Props.ClickAllWallets::class.java, "click_all_wallets")
.withSubtype(Props.SwitchNetwork::class.java, "switch_network")
.withSubtype(Props.SelectWallet::class.java, "select_wallet")
.withSubtype(Props.ConnectSuccess::class.java, "connect_success")
.withSubtype(Props.ConnectError::class.java, "connect_error")
.withSubtype(Props.DisconnectSuccess::class.java, "disconnect_success")
.withSubtype(Props.DisconnectError::class.java, "disconnect_error")
.withSubtype(Props.ClickWalletHelp::class.java, "click_wallet_help")
.withSubtype(Props.ClickNetworkHelp::class.java, "click_network_help")
.withSubtype(Props.ClickGetWallet::class.java, "click_get_wallet")
}

single<Moshi.Builder>(named(AndroidCommonDITags.MOSHI)) {
get<Moshi>(named(FoundationDITags.MOSHI))
.newBuilder()
Expand All @@ -39,6 +59,7 @@ fun coreCommonModule() = module {
}
}
.add(get<PolymorphicJsonAdapterFactory<JsonRpcResponse>>())
.add(get<PolymorphicJsonAdapterFactory<Props>>())
}

single {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import org.koin.android.ext.koin.androidApplication
import org.koin.android.ext.koin.androidContext
import org.koin.core.qualifier.named
import org.koin.dsl.module
import java.util.*
import java.util.concurrent.TimeUnit


Expand All @@ -54,6 +53,8 @@ fun coreAndroidNetworkModule(serverUrl: String, connectionType: ConnectionType,
"""wc-2/kotlin-${sdkVersion}/android-${Build.VERSION.RELEASE}"""
}

single(named(AndroidCommonDITags.BUNDLE_ID)) { androidContext().packageName }

single {
GenerateJwtStoreClientIdUseCase(get(), get())
}
Expand All @@ -62,7 +63,7 @@ fun coreAndroidNetworkModule(serverUrl: String, connectionType: ConnectionType,
Interceptor { chain ->
val updatedRequest = chain.request().newBuilder()
.addHeader("User-Agent", get(named(AndroidCommonDITags.USER_AGENT)))
.addHeader("Origin", androidContext().packageName)
.addHeader("Origin", get(named(AndroidCommonDITags.BUNDLE_ID)))
.build()

chain.proceed(updatedRequest)
Expand All @@ -73,6 +74,7 @@ fun coreAndroidNetworkModule(serverUrl: String, connectionType: ConnectionType,
HttpLoggingInterceptor().apply { setLevel(HttpLoggingInterceptor.Level.BODY) }
}

//TODO: make this more scalable
single(named(AndroidCommonDITags.FAIL_OVER_INTERCEPTOR)) {
Interceptor { chain ->
val request = chain.request()
Expand All @@ -82,6 +84,7 @@ fun coreAndroidNetworkModule(serverUrl: String, connectionType: ConnectionType,
shouldFallbackRelay(host) -> chain.proceed(request.newBuilder().url(get<String>(named(AndroidCommonDITags.RELAY_URL))).build())
shouldFallbackPush(host) -> chain.proceed(request.newBuilder().url(getFallbackPushUrl(request.url.toString())).build())
shouldFallbackVerify(host) -> chain.proceed(request.newBuilder().url(getFallbackVerifyUrl(request.url.toString())).build())
shouldFallbackPulse(host) -> chain.proceed(request.newBuilder().url(getFallbackPulseUrl()).build())
else -> chain.proceed(request)
}
} catch (e: Exception) {
Expand All @@ -90,6 +93,7 @@ fun coreAndroidNetworkModule(serverUrl: String, connectionType: ConnectionType,
DEFAULT_RELAY_URL.host -> fallbackRelay(request, chain)
DEFAULT_PUSH_URL.host -> fallbackPush(request, chain)
DEFAULT_VERIFY_URL.host -> fallbackVerify(request, chain)
DEFAULT_PULSE_URL.host -> fallbackPulse(request, chain)
else -> chain.proceed(request)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,28 @@ internal var VERIFY_URL: String = "https://verify.walletconnect.com/"
internal const val DEFAULT_VERIFY_URL: String = "https://verify.walletconnect.com/"
internal const val FAIL_OVER_VERIFY_URL: String = "https://verify.walletconnect.org"

internal var PULSE_URL: String = "https://pulse.walletconnect.com"
internal const val DEFAULT_PULSE_URL: String = "https://pulse.walletconnect.com"
internal const val FAIL_OVER_PULSE_URL: String = "https://pulse.walletconnect.org"

internal var wasRelayFailOvered = false
internal var wasEchoFailOvered = false
internal var wasVerifyFailOvered = false
internal var wasPulseFailOvered = false

internal fun shouldFallbackRelay(host: String): Boolean = wasRelayFailOvered && host == DEFAULT_RELAY_URL.host
internal fun shouldFallbackPush(host: String): Boolean = wasEchoFailOvered && host == DEFAULT_PUSH_URL.host
internal fun shouldFallbackVerify(host: String): Boolean = wasVerifyFailOvered && host == DEFAULT_VERIFY_URL.host
internal fun shouldFallbackPulse(host: String): Boolean = wasPulseFailOvered && host == DEFAULT_PULSE_URL.host
internal fun getFallbackPushUrl(url: String): String = with(Uri.parse(url)) {
val (path, query) = Pair(this.path, this.query)
return@with "$FAIL_OVER_PUSH_URL$path?$query}"
}

internal fun getFallbackVerifyUrl(url: String): String = "$FAIL_OVER_VERIFY_URL/attestation/${Uri.parse(url).lastPathSegment}"

internal fun getFallbackPulseUrl(): String = "$FAIL_OVER_PULSE_URL/e"

internal fun isFailOverException(e: Exception) = (e is SocketException || e is IOException)
internal val String.host: String? get() = Uri.parse(this).host

Expand All @@ -53,4 +61,10 @@ internal fun Scope.fallbackRelay(request: Request, chain: Interceptor.Chain): Re
SERVER_URL = "$FAIL_OVER_RELAY_URL?projectId=${Uri.parse(SERVER_URL).getQueryParameter("projectId")}"
wasRelayFailOvered = true
return chain.proceed(request.newBuilder().url(get<String>(named(AndroidCommonDITags.RELAY_URL))).build())
}

internal fun fallbackPulse(request: Request, chain: Interceptor.Chain): Response {
PULSE_URL = FAIL_OVER_PULSE_URL
wasPulseFailOvered = true
return chain.proceed(request.newBuilder().url(getFallbackPulseUrl()).build())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package com.walletconnect.android.internal.common.di

import com.walletconnect.android.pulse.data.PulseService
import com.walletconnect.android.pulse.domain.SendClickAllWalletsUseCase
import com.walletconnect.android.pulse.domain.SendClickGetWalletUseCase
import com.walletconnect.android.pulse.domain.SendClickNetworkHelpUseCase
import com.walletconnect.android.pulse.domain.SendClickNetworksUseCase
import com.walletconnect.android.pulse.domain.SendClickWalletHelpUseCase
import com.walletconnect.android.pulse.domain.SendConnectErrorUseCase
import com.walletconnect.android.pulse.domain.SendConnectSuccessUseCase
import com.walletconnect.android.pulse.domain.SendDisconnectErrorUseCase
import com.walletconnect.android.pulse.domain.SendDisconnectSuccessUseCase
import com.walletconnect.android.pulse.domain.SendModalCloseUseCase
import com.walletconnect.android.pulse.domain.SendModalCreatedUseCase
import com.walletconnect.android.pulse.domain.SendModalLoadedUseCase
import com.walletconnect.android.pulse.domain.SendModalLoadedUseCaseInterface
import com.walletconnect.android.pulse.domain.SendModalOpenUseCase
import com.walletconnect.android.pulse.domain.SendSelectWalletUseCase
import com.walletconnect.android.pulse.domain.SendSwitchNetworkUseCase
import org.koin.core.qualifier.named
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

@JvmSynthetic
fun pulseModule() = module {
single(named(AndroidCommonDITags.PULSE_URL)) { "https://pulse.walletconnect.com" }

single(named(AndroidCommonDITags.PULSE_RETROFIT)) {
Retrofit.Builder()
.baseUrl(get<String>(named(AndroidCommonDITags.PULSE_URL)))
.client(get(named(AndroidCommonDITags.WEB3MODAL_OKHTTP)))
.addConverterFactory(MoshiConverterFactory.create(get(named(AndroidCommonDITags.MOSHI))))
.build()
}

single { get<Retrofit>(named(AndroidCommonDITags.PULSE_RETROFIT)).create(PulseService::class.java) }

single {
SendModalCreatedUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendClickAllWalletsUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendClickGetWalletUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendClickWalletHelpUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendClickNetworkHelpUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendClickNetworksUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendConnectErrorUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendConnectSuccessUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendDisconnectErrorUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendDisconnectSuccessUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendModalCloseUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single<SendModalLoadedUseCaseInterface> {
SendModalLoadedUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendModalOpenUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendSelectWalletUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}

single {
SendSwitchNetworkUseCase(
pulseService = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
bundleId = get(named(AndroidCommonDITags.BUNDLE_ID))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.walletconnect.android.internal.common.di
import com.walletconnect.android.BuildConfig
import com.walletconnect.android.internal.common.modal.Web3ModalApiRepository
import com.walletconnect.android.internal.common.modal.data.network.Web3ModalService
import com.walletconnect.android.internal.common.modal.domain.usecase.EnableAnalyticsUseCase
import com.walletconnect.android.internal.common.modal.domain.usecase.EnableAnalyticsUseCaseInterface
import com.walletconnect.android.internal.common.modal.domain.usecase.GetInstalledWalletsIdsUseCase
import com.walletconnect.android.internal.common.modal.domain.usecase.GetInstalledWalletsIdsUseCaseInterface
import com.walletconnect.android.internal.common.modal.domain.usecase.GetSampleWalletsUseCase
Expand Down Expand Up @@ -60,4 +62,5 @@ internal fun web3ModalModule() = module {
single<GetInstalledWalletsIdsUseCaseInterface> { GetInstalledWalletsIdsUseCase(web3ModalApiRepository = get()) }
single<GetWalletsUseCaseInterface> { GetWalletsUseCase(web3ModalApiRepository = get()) }
single<GetSampleWalletsUseCaseInterface> { GetSampleWalletsUseCase(context = get()) }
single<EnableAnalyticsUseCaseInterface> { EnableAnalyticsUseCase(repository = get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ internal class Web3ModalApiRepository(
response.body()!!.data.toWalletsAppData().filter { it.isInstalled }
}

suspend fun getAnalyticsConfig(sdkType: String = "w3m") = runCatching {
web3ModalService.getAnalyticsConfig(sdkType = sdkType)
}.mapCatching { response ->
response.body()!!.isAnalyticsEnabled
}

suspend fun getWallets(
sdkType: String,
page: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.walletconnect.android.internal.common.modal.data.network

import com.walletconnect.android.internal.common.modal.data.network.model.EnableAnalyticsDTO
import com.walletconnect.android.internal.common.modal.data.network.model.GetAndroidDataDTO
import com.walletconnect.android.internal.common.modal.data.network.model.GetWalletsDTO
import retrofit2.Response
Expand All @@ -23,4 +24,9 @@ internal interface Web3ModalService {
suspend fun getAndroidData(
@Header("x-sdk-type") sdkType: String,
): Response<GetAndroidDataDTO>

@GET("getAnalyticsConfig")
suspend fun getAnalyticsConfig(
@Header("x-sdk-type") sdkType: String,
): Response<EnableAnalyticsDTO>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.walletconnect.android.internal.common.modal.data.network.model

import com.squareup.moshi.Json

data class EnableAnalyticsDTO(
@Json(name = "isAnalyticsEnabled")
val isAnalyticsEnabled: Boolean
)
Loading

0 comments on commit 314b5e0

Please sign in to comment.