Skip to content

Commit

Permalink
bug fixeds
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Aug 15, 2024
1 parent 5349887 commit d704669
Show file tree
Hide file tree
Showing 131 changed files with 1,417 additions and 760 deletions.
74 changes: 41 additions & 33 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class API(
private val internalApi = InternalApi(context, defaultHttpClient)
private val configRepository = ConfigRepository(context, scope, internalApi)

@Volatile
private var cachedCountry: String? = null

val config: ConfigEntity
get() {
while (configRepository.configEntity == null) {
Expand Down Expand Up @@ -429,34 +432,35 @@ class API(
}
}

fun pushSubscribe(
suspend fun pushSubscribe(
locale: Locale,
firebaseToken: String,
deviceId: String,
accounts: List<String>
): Boolean {
return try {
val url = "${config.tonapiMainnetHost}/v1/internal/pushes/plain/subscribe"
val accountsArray = JSONArray()
for (account in accounts) {
val jsonAccount = JSONObject()
jsonAccount.put("address", account)
accountsArray.put(jsonAccount)
}
val url = "${config.tonapiMainnetHost}/v1/internal/pushes/plain/subscribe"
val accountsArray = JSONArray()
for (account in accounts) {
val jsonAccount = JSONObject()
jsonAccount.put("address", account)
accountsArray.put(jsonAccount)
}

val json = JSONObject()
json.put("locale", locale.toString())
json.put("device", deviceId)
json.put("token", firebaseToken)
json.put("accounts", accountsArray)
val json = JSONObject()
json.put("locale", locale.toString())
json.put("device", deviceId)
json.put("token", firebaseToken)
json.put("accounts", accountsArray)

return tonAPIHttpClient.postJSON(url, json.toString()).isSuccessful
} catch (e: Throwable) {
false
}
Log.d("TONKeeperLog", "json: $json")

return withRetry {
val response = tonAPIHttpClient.postJSON(url, json.toString())
response.isSuccessful
} ?: false
}

fun pushTonconnectSubscribe(
suspend fun pushTonconnectSubscribe(
token: String,
appUrl: String,
accountId: String,
Expand All @@ -465,24 +469,25 @@ class API(
commercial: Boolean = true,
silent: Boolean = true
): Boolean {
return try {
val url = "${config.tonapiMainnetHost}/v1/internal/pushes/tonconnect"
val url = "${config.tonapiMainnetHost}/v1/internal/pushes/tonconnect"

val json = JSONObject()
json.put("app_url", appUrl)
json.put("account", accountId)
json.put("firebase_token", firebaseToken)
sessionId?.let { json.put("session_id", it) }
json.put("commercial", commercial)
json.put("silent", silent)
val data = json.toString().replace("\\/", "/")
val json = JSONObject()
json.put("app_url", appUrl)
json.put("account", accountId)
json.put("firebase_token", firebaseToken)
sessionId?.let { json.put("session_id", it) }
json.put("commercial", commercial)
json.put("silent", silent)
val data = json.toString().replace("\\/", "/").trim()

Log.d("TONKeeperLog", "json: $data")
Log.d("TONKeeperLog", "token: $token")

return withRetry {
tonAPIHttpClient.postJSON(url, data, ArrayMap<String, String>().apply {
set("X-TonConnect-Auth", token)
}).isSuccessful
} catch (e: Throwable) {
false
}
} ?: false
}

fun pushTonconnectUnsubscribe(
Expand Down Expand Up @@ -566,7 +571,10 @@ class API(
}

suspend fun resolveCountry(): String? = withContext(Dispatchers.IO) {
internalApi.resolveCountry()
if (cachedCountry == null) {
cachedCountry = internalApi.resolveCountry()
}
cachedCountry
}

suspend fun reportNtfSpam(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.Log
import com.tonapps.extensions.isDebug
import com.tonapps.extensions.locale
import com.tonapps.extensions.packageInfo
import com.tonapps.extensions.withRetry
import com.tonapps.network.get
import com.tonapps.wallet.api.entity.ConfigEntity
import com.tonapps.wallet.api.entity.NotificationEntity
Expand Down Expand Up @@ -79,9 +80,15 @@ internal class InternalApi(
}
}

fun resolveCountry(): String? {
suspend fun resolveCountry(): String? {
return try {
JSONObject(okHttpClient.get("https://api.country.is/")).getString("country")
val data = withRetry { okHttpClient.get("https://api.country.is/") } ?: return null
val country = JSONObject(data).getString("country")
if (country.isNullOrBlank()) {
null
} else {
country
}
} catch (e: Throwable) {
null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonapps.wallet.data.events

import android.content.Context
import android.util.Log
import com.tonapps.extensions.MutableEffectFlow
import com.tonapps.extensions.prefs
import com.tonapps.wallet.api.API
Expand Down Expand Up @@ -66,6 +67,11 @@ class EventsRepository(
}
}

suspend fun get(
accountId: String,
testnet: Boolean
) = getLocal(accountId, testnet) ?: getRemote(accountId, testnet)

suspend fun getRemote(
accountId: String,
testnet: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object PasscodeBiometric {

fun isAvailableOnDevice(context: Context): Boolean {
val authStatus = BiometricManager.from(context).canAuthenticate(authenticators)
return authStatus == BiometricManager.BIOMETRIC_SUCCESS || authStatus == BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED
return authStatus == BiometricManager.BIOMETRIC_SUCCESS // || authStatus == BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED
}

suspend fun showPrompt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class GooglePushService: FirebaseMessagingService() {
private val recentlyReceivedMessageIds = ArrayDeque<String>(10)

private fun onPushReceived(extras: Bundle) {
Log.d("TONKeeperLog", "onPushReceived: $extras")
val firebaseMessageId = extras.getString("google.message_id") ?: return
if (alreadyReceivedMessage(firebaseMessageId)) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.tonapps.wallet.data.push
import org.koin.dsl.module

val pushModule = module {
single { PushManager(get(), get(), get(), get(), get(), get(), get()) }
single { PushManager(get(), get(), get(), get(), get(), get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.tonapps.wallet.data.push
import android.app.Notification
import android.content.Context
import android.graphics.Bitmap
import android.util.Log
import com.tonapps.blockchain.ton.extensions.toUserFriendly
import com.tonapps.extensions.locale
import com.tonapps.network.getBitmap
Expand All @@ -20,6 +21,7 @@ import com.tonapps.wallet.data.tonconnect.entities.DAppManifestEntity
import com.tonapps.wallet.data.tonconnect.entities.DConnectEntity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
Expand All @@ -35,7 +37,6 @@ class PushManager(
private val scope: CoroutineScope,
private val accountRepository: AccountRepository,
private val settingsRepository: SettingsRepository,
private val eventsRepository: EventsRepository,
private val tonConnectRepository: TonConnectRepository,
private val api: API
) {
Expand All @@ -54,9 +55,10 @@ class PushManager(
init {
combine(
settingsRepository.firebaseTokenFlow,
settingsRepository.walletPush,
::subscribe
).flowOn(Dispatchers.IO).launchIn(scope)
settingsRepository.walletPush
) { token, _ ->
subscribe(token)
}.flowOn(Dispatchers.IO).launchIn(scope)

accountRepository.selectedWalletFlow.onEach {
_dAppPushFlow.value = getRemoteDAppEvents(it)
Expand Down Expand Up @@ -158,19 +160,31 @@ class PushManager(
}

private suspend fun subscribe(
firebaseToken: String,
walletPush: Map<String, Boolean>
) {
val wallets = accountRepository.getWallets()
val accounts = wallets.filter {
!it.testnet && settingsRepository.getPushWallet(it.id)
}.map {
it.accountId.toUserFriendly(testnet = false)
firebaseToken: String
) = withContext(Dispatchers.IO) {
val tcAppsDeferred = async { tonConnectRepository.subscribePush(firebaseToken) }
val walletDeferred = async { subscribeWalletPush(firebaseToken) }

if (!tcAppsDeferred.await()) {
Log.e("TONKeeperLog", "Failed to subscribe to TC apps push")
}
val enabledAccounts = walletPush.filterValues { it }.keys

api.pushSubscribe(context.locale, firebaseToken, settingsRepository.installId, accounts)
if (!walletDeferred.await()) {
Log.e("TONKeeperLog", "Failed to subscribe to wallet push")
}
}

tonConnectRepository.updatePushToken(firebaseToken)
private suspend fun subscribeWalletPush(
firebaseToken: String
): Boolean {
val wallets = accountRepository.getWallets()
val accounts = wallets.filter { !it.testnet && settingsRepository.getPushWallet(it.id) }.map {
it.accountId.toUserFriendly(testnet = false)
}
Log.d("TONKeeperLog", "accounts: $accounts")
if (accounts.isEmpty()) {
return true
}
return api.pushSubscribe(context.locale, firebaseToken, settingsRepository.installId, accounts)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import androidx.fragment.app.FragmentActivity
import com.tonapps.wallet.data.rn.data.RNDecryptedData
import com.tonapps.wallet.data.rn.data.RNSpamTransactions
import com.tonapps.wallet.data.rn.data.RNTC
import com.tonapps.wallet.data.rn.data.RNVaultState
import com.tonapps.wallet.data.rn.data.RNWallet
Expand Down Expand Up @@ -121,6 +122,37 @@ class RNLegacy(
}
}

fun getSpamTransactions(walletId: String): RNSpamTransactions {
val key = keySpamTransactions(walletId)
val json = sql.getJSONObject(key) ?: return RNSpamTransactions(walletId)
val spam = mutableListOf<String>()
val nonSpam = mutableListOf<String>()
for (transactionId in json.keys()) {
if (json.optBoolean(transactionId)) {
spam.add(transactionId)
} else {
nonSpam.add(transactionId)
}
}
return RNSpamTransactions(walletId, spam.toList(), nonSpam.toList())
}

fun setSpamTransactions(walletId: String, data: RNSpamTransactions) {
val json = JSONObject()
for (transactionId in data.spam) {
json.put(transactionId, true)
}
for (transactionId in data.nonSpam) {
json.put(transactionId, false)
}
val key = keySpamTransactions(walletId)
sql.setJSONObject(key, json)
}

private fun keySpamTransactions(walletId: String): String {
return "${walletId}/local-scam"
}

fun getValue(key: String): String? {
return sql.getValue(key)
}
Expand All @@ -129,7 +161,10 @@ class RNLegacy(
return sql.getJSONObject(key)
}

fun setJSONValue(key: String, value: JSONObject) {
fun setJSONValue(key: String, value: JSONObject, v: Int = -1) {
if (v >= 0) {
value.put("__version", v)
}
sql.setJSONObject(key, value)
}

Expand Down Expand Up @@ -185,18 +220,32 @@ class RNLegacy(
return Pair(setupDismissed, hasOpenedTelegramChannel)
}

fun setSetupLastBackupAt(walletId: String, date: Long) {
val json = getSetupJSON(walletId)
json.put("lastBackupAt", date)
setSetupJSON(walletId, json)
}

fun setSetupDismissed(walletId: String) {
val key = "${walletId}/setup"
val json = getJSONValue(key) ?: JSONObject()
val json = getSetupJSON(walletId)
json.put("setupDismissed", true)
setJSONValue(key, json)
setSetupJSON(walletId, json)
}

fun setHasOpenedTelegramChannel(walletId: String) {
val key = "${walletId}/setup"
val json = getJSONValue(key) ?: JSONObject()
val json = getSetupJSON(walletId)
json.put("hasOpenedTelegramChannel", true)
setJSONValue(key, json)
setSetupJSON(walletId, json)
}

private fun getSetupJSON(walletId: String): JSONObject {
val key = "${walletId}/setup"
return getJSONValue(key) ?: JSONObject()
}

private fun setSetupJSON(walletId: String, json: JSONObject) {
val key = "${walletId}/setup"
setJSONValue(key, json, 1)
}

fun getNotificationsEnabled(walletId: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tonapps.wallet.data.rn.data

class RNSpamTransactions(
val walletId: String,
val spam: List<String> = emptyList(),
val nonSpam: List<String> = emptyList()
)
Loading

0 comments on commit d704669

Please sign in to comment.