Skip to content

Commit

Permalink
bug fixeds for safe mode
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Nov 13, 2024
1 parent f358f57 commit e5fb7d9
Show file tree
Hide file tree
Showing 60 changed files with 1,263 additions and 655 deletions.
34 changes: 17 additions & 17 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonapps.wallet.api

import android.content.Context
import android.os.Build
import android.util.ArrayMap
import android.util.Log
import com.squareup.moshi.JsonAdapter
Expand All @@ -11,6 +12,7 @@ import com.tonapps.blockchain.ton.extensions.base64
import com.tonapps.blockchain.ton.extensions.hex
import com.tonapps.blockchain.ton.extensions.isValidTonAddress
import com.tonapps.blockchain.ton.extensions.toRawAddress
import com.tonapps.extensions.appVersionName
import com.tonapps.extensions.locale
import com.tonapps.extensions.toUriOrNull
import com.tonapps.icu.Coins
Expand Down Expand Up @@ -75,7 +77,9 @@ class API(
private val scope: CoroutineScope
) {

val defaultHttpClient = baseOkHttpClientBuilder().build()
private val userAgent = "Tonkeeper/${context.appVersionName} (Linux; Android ${Build.VERSION.RELEASE}; ${Build.MODEL})"

val defaultHttpClient = baseOkHttpClientBuilder(userAgent).build()

private val internalApi = InternalApi(context, defaultHttpClient)
private val configRepository = ConfigRepository(context, scope, internalApi)
Expand All @@ -89,15 +93,12 @@ class API(
private val tonAPIHttpClient: OkHttpClient by lazy {
createTonAPIHttpClient(
context = context,
userAgent = userAgent,
tonApiV2Key = { config.tonApiV2Key },
allowDomains = { config.domains }
allowDomains = { config.domains }
)
}

private val batteryHttpClient: OkHttpClient by lazy {
createBatteryAPIHttpClient(context)
}

@Volatile
private var cachedCountry: String? = null

Expand Down Expand Up @@ -145,7 +146,7 @@ class API(
}

private val batteryApi by lazy {
SourceAPI(BatteryApi(config.batteryHost, batteryHttpClient), BatteryApi(config.batteryTestnetHost, batteryHttpClient))
SourceAPI(BatteryApi(config.batteryHost, tonAPIHttpClient), BatteryApi(config.batteryTestnetHost, tonAPIHttpClient))
}

private val emulationJSONAdapter: JsonAdapter<MessageConsequences> by lazy {
Expand Down Expand Up @@ -846,7 +847,7 @@ class API(

private val socketFactoryTcpNoDelay = SSLSocketFactoryTcpNoDelay()

private fun baseOkHttpClientBuilder(): OkHttpClient.Builder {
private fun baseOkHttpClientBuilder(userAgent: String): OkHttpClient.Builder {
return OkHttpClient().newBuilder()
.retryOnConnectionFailure(true)
.connectTimeout(5, TimeUnit.SECONDS)
Expand All @@ -855,30 +856,29 @@ class API(
.callTimeout(5, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)
.followSslRedirects(true)
.addInterceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("User-Agent", "TonWallet")
.build()
chain.proceed(request)
}
.followRedirects(true)
// .sslSocketFactory(socketFactoryTcpNoDelay.sslSocketFactory, socketFactoryTcpNoDelay.trustManager)
// .socketFactory(SocketFactoryTcpNoDelay())
}

private fun createTonAPIHttpClient(
context: Context,
userAgent: String,
tonApiV2Key: () -> String,
allowDomains: () -> List<String>
): OkHttpClient {
return baseOkHttpClientBuilder()
return baseOkHttpClientBuilder(userAgent)
.addInterceptor(AcceptLanguageInterceptor(context.locale))
.addInterceptor(AuthorizationInterceptor.bearer(
token = tonApiV2Key,
allowDomains = allowDomains
)).build()
}

private fun createBatteryAPIHttpClient(
context: Context,
): OkHttpClient {
return baseOkHttpClientBuilder()
.addInterceptor(AcceptLanguageInterceptor(context.locale))
.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class SettingsRepository(
if (value != field) {
prefs.edit().putBoolean(SAFE_MODE_KEY, value).apply()
field = value
tokenPrefsFolder.notifyChanged()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ object DevSettings {
}
}

var ignoreSystemFontSize: Boolean = prefs.getBoolean("ignore_system_font_size", false)
set(value) {
if (field != value) {
field = value
prefs.edit().putBoolean("ignore_system_font_size", value).apply()
}
}


fun tonConnectLog(message: String, error: Boolean = false) {
if (tonConnectLogs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class HistoryHelper(
positionExtra: Int = 0,
): List<HistoryItem> = withContext(Dispatchers.IO) {
val items = mutableListOf<HistoryItem>()

val safeMode = settingsRepository.safeMode
for (event in events) {
val pending = event.inProgress

Expand All @@ -352,9 +352,11 @@ class HistoryHelper(

val chunkItems = mutableListOf<HistoryItem>()
for ((actionIndex, action) in actions.withIndex()) {
if (safeMode && event.isScam) {
continue
}
val timestamp = if (removeDate) 0 else event.timestamp
val isScam =
event.isScam || settingsRepository.isSpamTransaction(wallet.id, event.eventId)
val isScam = event.isScam || settingsRepository.isSpamTransaction(wallet.id, event.eventId)

val item = action(
index = actionIndex,
Expand All @@ -363,7 +365,8 @@ class HistoryHelper(
action = action,
timestamp = timestamp,
isScam = isScam
)
) ?: continue

chunkItems.add(
item.copy(
pending = pending,
Expand Down Expand Up @@ -404,18 +407,21 @@ class HistoryHelper(
action: Action,
timestamp: Long,
isScam: Boolean,
): HistoryItem.Event {

): HistoryItem.Event? {
val safeMode = settingsRepository.safeMode
val simplePreview = action.simplePreview
val date = DateHelper.formatTransactionTime(timestamp, settingsRepository.getLocale())
val dateDetails =
DateHelper.formatTransactionDetailsTime(timestamp, settingsRepository.getLocale())
val dateDetails = DateHelper.formatTransactionDetailsTime(timestamp, settingsRepository.getLocale())

if (action.jettonSwap != null) {
val jettonSwap = action.jettonSwap!!
val tokenIn = jettonSwap.tokenIn
val tokenOut = jettonSwap.tokenOut

if ((!tokenIn.verified || !tokenOut.verified) && safeMode) {
return null
}

val amountIn = jettonSwap.amountCoinsIn
val amountOut = jettonSwap.amountCoinsOut

Expand Down Expand Up @@ -451,6 +457,11 @@ class HistoryHelper(
} else if (action.jettonTransfer != null) {
val jettonTransfer = action.jettonTransfer!!
val token = jettonTransfer.jetton.address

if (safeMode && jettonTransfer.jetton.verification != JettonVerificationType.whitelist) {
return null
}

val symbol = jettonTransfer.jetton.symbol
val isOut = !wallet.isMyAddress(jettonTransfer.recipient?.address ?: "")

Expand Down Expand Up @@ -629,6 +640,10 @@ class HistoryHelper(
it.with(pref)
}

if (safeMode && nftItem?.verified != true) {
return null
}

val isEncryptedComment = nftItemTransfer.encryptedComment != null

val comment = HistoryItem.Event.Comment.create(
Expand Down Expand Up @@ -708,6 +723,10 @@ class HistoryHelper(
} else if (action.jettonMint != null) {
val jettonMint = action.jettonMint!!

if (safeMode && jettonMint.jetton.verification != JettonVerificationType.whitelist) {
return null
}

val amount = jettonMint.parsedAmount

val value = CurrencyFormatter.format(jettonMint.jetton.symbol, amount, 2)
Expand Down Expand Up @@ -850,6 +869,10 @@ class HistoryHelper(
} else if (action.nftPurchase != null) {
val nftPurchase = action.nftPurchase!!

if (safeMode && !nftPurchase.nft.verified) {
return null
}

val amount = Coins.of(nftPurchase.amount.value.toLong())
val value = CurrencyFormatter.format(nftPurchase.amount.tokenName, amount, 2)

Expand Down Expand Up @@ -884,6 +907,10 @@ class HistoryHelper(
} else if (action.jettonBurn != null) {
val jettonBurn = action.jettonBurn!!

if (safeMode && jettonBurn.jetton.verification != JettonVerificationType.whitelist) {
return null
}

val amount = jettonBurn.parsedAmount
val value = CurrencyFormatter.format(jettonBurn.jetton.symbol, amount, 2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import androidx.annotation.ColorInt
import com.tonapps.extensions.containsQuery
import com.tonapps.extensions.isLocal
import com.tonapps.extensions.query
import uikit.extensions.drawable

fun Uri.isTonSite(): Boolean {
return this.host?.endsWith(".ton") ?: false
}

fun Uri.withUtmSource(source: String = "tonkeeper"): Uri {
if (containsQuery("utm_source")) {
return this
}
return this.buildUpon().appendQueryParameter("utm_source", source).build()
}

fun Uri.normalizeTONSites(): Uri {
if (!isTonSite()) {
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Browser
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import com.tonapps.extensions.activity
Expand Down Expand Up @@ -59,6 +60,7 @@ object BrowserHelper {
.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_LIGHT, colorSchemeParams)
.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, colorSchemeParams)
.build()
intent.intent.putExtra(Browser.EXTRA_APPLICATION_ID, activity.packageName)

try {
intent.launchUrl(activity, uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import com.tonapps.tonkeeper.ui.screen.wallet.picker.PickerViewModel
import com.tonapps.tonkeeper.ui.screen.settings.passcode.ChangePasscodeViewModel
import com.tonapps.tonkeeper.ui.screen.settings.security.SecurityViewModel
import com.tonapps.tonkeeper.ui.screen.settings.theme.ThemeViewModel
import com.tonapps.tonkeeper.ui.screen.w5.stories.W5StoriesViewModel
import com.tonapps.tonkeeper.ui.screen.stories.w5.W5StoriesViewModel
import com.tonapps.tonkeeper.ui.screen.tonconnect.TonConnectViewModel
import com.tonapps.tonkeeper.ui.screen.wallet.main.list.WalletAdapter
import com.tonapps.tonkeeper.usecase.emulation.EmulationUseCase
import com.tonapps.tonkeeper.usecase.sign.SignUseCase
import com.tonapps.wallet.data.settings.SettingsRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.tonapps.tonkeeper.ui.screen.token.picker.TokenPickerViewModel
import com.tonapps.tonkeeper.ui.screen.battery.settings.BatterySettingsViewModel
import com.tonapps.tonkeeper.ui.screen.battery.refill.BatteryRefillViewModel
import com.tonapps.tonkeeper.ui.screen.battery.recharge.BatteryRechargeViewModel
import com.tonapps.tonkeeper.ui.screen.card.CardViewModel
import com.tonapps.tonkeeper.ui.screen.collectibles.manage.CollectiblesManageScreen
import com.tonapps.tonkeeper.ui.screen.collectibles.manage.CollectiblesManageViewModel
import com.tonapps.tonkeeper.ui.screen.send.contacts.main.SendContactsViewModel
Expand Down Expand Up @@ -67,4 +68,5 @@ val viewModelWalletModule = module {
viewModelOf(::EditContactViewModel)
viewModelOf(::AppsViewModel)
viewModelOf(::CollectiblesManageViewModel)
viewModelOf(::CardViewModel)
}
Loading

0 comments on commit e5fb7d9

Please sign in to comment.