Skip to content

Commit

Permalink
Implement Binance Api Key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Jul 3, 2023
1 parent 4b387bb commit 416c24e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.horizontalsystems.bankwallet.core.providers

import android.os.Parcelable
import androidx.room.Entity
import com.binance.connector.client.exceptions.BinanceClientException
import com.binance.connector.client.impl.SpotClientImpl
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
Expand Down Expand Up @@ -141,9 +142,11 @@ class CoinzixCexProvider(
addressData.address != null -> {
CexAddress(addressData.address, "")
}

addressData.account != null -> {
CexAddress(addressData.account, addressData.memo ?: "")
}

else -> {
throw Exception()
}
Expand Down Expand Up @@ -281,4 +284,13 @@ class BinanceCexProvider(apiKey: String, secretKey: String, override val account
val url: String,
)

companion object {
@Throws(BinanceClientException::class)
fun validate(apiKey: String, secretKey: String) {
val client = SpotClientImpl(apiKey, secretKey)
val wallet = client.createWallet()
wallet.coinInfo(mutableMapOf())
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.binance.connector.client.exceptions.BinanceClientException
import com.binance.connector.client.exceptions.BinanceConnectorException
import com.google.gson.Gson
import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.core.providers.BinanceCexProvider
import io.horizontalsystems.bankwallet.core.providers.Translator
import io.horizontalsystems.bankwallet.entities.AccountOrigin
import io.horizontalsystems.bankwallet.entities.AccountType
import io.horizontalsystems.bankwallet.entities.CexType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class EnterCexDataBinanceViewModel : ViewModel() {
private val accountManager = App.accountManager
Expand All @@ -22,8 +29,18 @@ class EnterCexDataBinanceViewModel : ViewModel() {
private var connectEnabled = false
private var accountCreated = false
private var errorMessage: String? = null

var uiState by mutableStateOf(UiState(connectEnabled, accountCreated, apiKey, secretKey, errorMessage))
private var showSpinner = false

var uiState by mutableStateOf(
UiState(
connectEnabled = connectEnabled,
accountCreated = accountCreated,
apiKey = apiKey,
secretKey = secretKey,
errorMessage = errorMessage,
showSpinner = showSpinner
)
)
private set

fun onEnterApiKey(v: String) {
Expand Down Expand Up @@ -62,22 +79,48 @@ class EnterCexDataBinanceViewModel : ViewModel() {

private fun emitState() {
connectEnabled = !(apiKey.isNullOrBlank() || secretKey.isNullOrBlank())
uiState = UiState(connectEnabled, accountCreated, apiKey, secretKey, errorMessage)
uiState = UiState(
connectEnabled = connectEnabled,
accountCreated = accountCreated,
apiKey = apiKey,
secretKey = secretKey,
errorMessage = errorMessage,
showSpinner = showSpinner
)
}

fun onClickConnect() {
val tmpApiKey = apiKey ?: return
val tmpSecretKey = secretKey ?: return
val cexType = CexType.Binance(tmpApiKey, tmpSecretKey)
showSpinner = true
errorMessage = null
emitState()

viewModelScope.launch(Dispatchers.IO) {
try {
BinanceCexProvider.validate(tmpApiKey, tmpSecretKey)
createAccount(tmpApiKey, tmpSecretKey)
} catch (error: BinanceClientException) {
errorMessage = Translator.getString(R.string.Cex_Error_FailedToConnectApiKey)
} catch (error: BinanceConnectorException) {
errorMessage = Translator.getString(R.string.Hud_Text_NoInternet)
}
showSpinner = false
withContext(Dispatchers.Main) {
emitState()
}
}
}

private fun createAccount(binanceApiKey: String, binanceSecretKey: String) {
val cexType = CexType.Binance(binanceApiKey, binanceSecretKey)
val name = accountFactory.getNextCexAccountName(cexType)

val account = accountFactory.account(name, AccountType.Cex(cexType = cexType), AccountOrigin.Restored, true, false)

accountManager.save(account)

accountCreated = true

emitState()
}

data class BinanceCexApiCredentials(
Expand All @@ -91,6 +134,7 @@ class EnterCexDataBinanceViewModel : ViewModel() {
val accountCreated: Boolean,
val apiKey: String?,
val secretKey: String?,
val errorMessage: String?
val errorMessage: String?,
val showSpinner: Boolean
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import io.horizontalsystems.bankwallet.ui.compose.ComposeAppTheme
import io.horizontalsystems.bankwallet.ui.compose.TranslatableString
import io.horizontalsystems.bankwallet.ui.compose.components.AppBar
import io.horizontalsystems.bankwallet.ui.compose.components.ButtonPrimaryTransparent
import io.horizontalsystems.bankwallet.ui.compose.components.ButtonPrimaryYellow
import io.horizontalsystems.bankwallet.ui.compose.components.ButtonPrimaryYellowWithSpinner
import io.horizontalsystems.bankwallet.ui.compose.components.FormsInput
import io.horizontalsystems.bankwallet.ui.compose.components.FormsInputPassword
Expand Down Expand Up @@ -261,10 +260,11 @@ private fun ImportBinanceCexAccountScreen(
}
ButtonsGroupWithShade {
Column(Modifier.padding(horizontal = 24.dp)) {
ButtonPrimaryYellow(
ButtonPrimaryYellowWithSpinner(
modifier = Modifier.fillMaxWidth(),
title = stringResource(R.string.Button_Connect),
enabled = connectEnabled,
showSpinner = uiState.showSpinner,
onClick = {
viewModel.onClickConnect()
},
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,7 @@
<string name="CexDeposit_MemoWarning">Memo (tag) is required, or your will lose your coins.\n\nSend only %s to this address. Sending other types of tokens to this address will result in their ultimate loss.</string>
<string name="CexDeposit_Important">Important</string>
<string name="CexDeposit_MemoAlertText">Both a memo (tag) and the address are needed to ensure that assets are received. Otherwise your funds will be lost.</string>
<string name="Cex_Error_FailedToConnectApiKey">Failed to connect your Api Key</string>

<!--Cex Withdraw Module-->
<string name="CexWithdraw_Title">Withdraw %s</string>
Expand Down

0 comments on commit 416c24e

Please sign in to comment.