Skip to content

Commit

Permalink
bug fixeds
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Oct 28, 2024
1 parent 2e47427 commit 1df89c6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tonapps.wallet.data.account

import android.app.KeyguardManager
import android.content.Context
import android.util.Log
import com.tonapps.blockchain.ton.contract.BaseWalletContract
Expand Down Expand Up @@ -45,7 +46,7 @@ import org.ton.contract.wallet.WalletTransfer
import java.util.UUID

class AccountRepository(
context: Context,
private val context: Context,
private val api: API,
private val rnLegacy: RNLegacy,
) {
Expand All @@ -62,6 +63,10 @@ class AccountRepository(
data class Wallet(val wallet: WalletEntity) : SelectedState()
}

private val keyguardManager by lazy {
context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
}

private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private val database = DatabaseSource(context, scope)
private val storageSource: StorageSource by lazy { StorageSource(context) }
Expand Down Expand Up @@ -182,6 +187,10 @@ class AccountRepository(
}

suspend fun requestTonProofToken(wallet: WalletEntity): String? = withContext(scope.coroutineContext) {
if (keyguardManager.isDeviceLocked) {
return@withContext null
}

if (!wallet.hasPrivateKey) {
return@withContext null
}
Expand All @@ -194,6 +203,7 @@ class AccountRepository(
tonProofToken
}


private suspend fun saveTonProof(wallet: WalletEntity, token: String) = withContext(Dispatchers.IO) {
storageSource.setTonProofToken(wallet.publicKey, token)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ class HistoryHelper(
itemAction = ActionType.JettonBurn
accountAddress = jettonTransfer.recipient
value = value.withMinus
} else if (isOut) {
} else if (isOut || wallet.isMyAddress(jettonTransfer.sender?.address ?: "")) {
itemAction = ActionType.Send
accountAddress = jettonTransfer.recipient
value = value.withMinus
Expand Down Expand Up @@ -521,7 +521,7 @@ class HistoryHelper(
val amount = Coins.of(tonTransfer.amount)
var value = CurrencyFormatter.format("TON", amount, 2)

if (isOut) {
if (isOut || wallet.isMyAddress(tonTransfer.sender.address)) {
itemAction = ActionType.Send
accountAddress = tonTransfer.recipient
value = value.withMinus
Expand Down Expand Up @@ -605,7 +605,7 @@ class HistoryHelper(
itemAction = ActionType.JettonBurn
iconURL = nftItemTransfer.recipient?.iconURL
subtitle = api.getBurnAddress()
} else if (isOut) {
} else if (isOut || wallet.isMyAddress(nftItemTransfer.sender?.address ?: "")) {
itemAction = ActionType.NftSend
iconURL = nftItemTransfer.recipient?.iconURL
subtitle = sender?.getNameOrAddress(wallet.testnet, true) ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tonapps.tonkeeper.ui.screen.browser.dapp

import android.app.Application
import android.net.Uri
import android.util.Log
import com.tonapps.extensions.appVersionName
import com.tonapps.extensions.filterList
import com.tonapps.tonkeeper.manager.tonconnect.TonConnectManager
Expand All @@ -11,9 +12,16 @@ import com.tonapps.tonkeeper.ui.base.BaseWalletVM
import com.tonapps.tonkeeper.worker.DAppPushToggleWorker
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.dapps.entities.AppConnectEntity
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
import org.json.JSONObject
import kotlin.time.Duration.Companion.seconds

class DAppViewModel(
app: Application,
Expand All @@ -23,7 +31,7 @@ class DAppViewModel(
): BaseWalletVM(app) {

val connectionFlow = tonConnectManager.walletConnectionsFlow(wallet).filterList { connection ->
connection.appUrl == url && connection.type == AppConnectEntity.Type.Internal
connection.type == AppConnectEntity.Type.Internal && connection.appUrl == url
}.map { it.firstOrNull() }

fun mute() {
Expand All @@ -40,11 +48,22 @@ class DAppViewModel(
}

suspend fun restoreConnection(): JSONObject {
val connection = connectionFlow.firstOrNull()
val connection = loadConnection()
return if (connection == null) {
JsonBuilder.connectEventError(BridgeError.unknownApp())
} else {
JsonBuilder.connectEventSuccess(wallet, null, null, context.appVersionName)
}
}

private suspend fun loadConnection(): AppConnectEntity? = withTimeoutOrNull(2.seconds) {
while (isActive) {
val connection = connectionFlow.first()
if (connection != null) {
return@withTimeoutOrNull connection
}
delay(100)
}
return@withTimeoutOrNull null
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.tonapps.tonkeeper.ui.screen.send.contacts.add

import android.app.Application
import android.util.Log
import androidx.lifecycle.viewModelScope
import com.tonapps.blockchain.ton.extensions.isTestnetAddress
import com.tonapps.extensions.bestMessage
import com.tonapps.tonkeeper.ui.base.BaseWalletVM
import com.tonapps.wallet.api.API
Expand Down Expand Up @@ -63,11 +65,15 @@ class AddContactViewModel(
userInputAddressFlow.collectFlow { address ->
if (address.isBlank()) {
_accountFlow.value = AddressAccount.Empty
} else if (address.contains(":")) {
_accountFlow.value = AddressAccount.Error
} else if (address.isTestnetAddress() && !wallet.testnet) {
_accountFlow.value = AddressAccount.Error
} else {
_accountFlow.value = AddressAccount.Loading

val account = api.resolveAccount(address, wallet.testnet)
if (account == null || !account.isWallet || account.status != AccountStatus.active) {
if (account == null || !account.isWallet || account.status == AccountStatus.nonexist) {
_accountFlow.value = AddressAccount.Error
} else {
_accountFlow.value = AddressAccount.Success(account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.util.Log
import android.view.View
import android.widget.Button
import com.tonapps.extensions.getParcelableCompat
import com.tonapps.tonkeeper.extensions.copyToClipboard
import com.tonapps.tonkeeper.extensions.hideKeyboard
import com.tonapps.tonkeeper.koin.walletViewModel
import com.tonapps.tonkeeper.ui.base.WalletContextScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ fun AddrStd.toAccountId(): String {
).lowercase()
}

fun String.isTestnetAddress(): Boolean {
return startsWith("0Q") || startsWith("kQ")
}

fun String.toUserFriendly(
wallet: Boolean = true,
testnet: Boolean,
Expand Down

0 comments on commit 1df89c6

Please sign in to comment.