Skip to content

Commit

Permalink
fix(coinbase): use custom tab for login & fix coin parse error (#1259)
Browse files Browse the repository at this point in the history
* fix: coinbase login & coin parse error

* feat: replace webview auth in CoinbaseServicesFragment
  • Loading branch information
Syn-McJ authored Feb 28, 2024
1 parent a88bbc1 commit 6c706ef
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 432 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.dash.wallet.integrations.coinbase

import android.content.Context
import org.dash.wallet.integrations.coinbase.service.CoinBaseClientConstants
import java.io.File

object CoinbaseConstants {
Expand All @@ -36,6 +37,20 @@ object CoinbaseConstants {
const val MIN_USD_COINBASE_AMOUNT = "2"
const val BASE_IDS_REQUEST_URL = "v2/assets/prices?filter=holdable&resolution=latest"
const val BUY_FEE = 0.006
const val REDIRECT_URL = "dashwallet://brokers/coinbase/connect"
const val AUTH_RESULT_ACTION = "Coinbase.AUTH_RESULT"
val LINK_URL = "https://www.coinbase.com/oauth/authorize?client_id=${CoinBaseClientConstants.CLIENT_ID}" +
"&redirect_uri=${REDIRECT_URL}&response_type" +
"=code&scope=wallet:accounts:read,wallet:user:read,wallet:payment-methods:read," +
"wallet:buys:read,wallet:buys:create,wallet:transactions:transfer," +
"wallet:sells:create,wallet:sells:read,wallet:deposits:create," +
"wallet:transactions:request,wallet:transactions:read,wallet:trades:create," +
"wallet:supported-assets:read,wallet:transactions:send," +
"wallet:addresses:read,wallet:addresses:create" +
"&meta[send_limit_amount]=10" +
"&meta[send_limit_currency]=USD" +
"&meta[send_limit_period]=month" +
"&account=all"

fun getCacheDir(context: Context): File {
return File(context.cacheDir, "coinbase")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ package org.dash.wallet.integrations.coinbase.model

import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import org.bitcoinj.core.Coin
import org.dash.wallet.common.util.toCoin
import java.lang.Exception
import java.lang.IllegalArgumentException
import java.math.BigDecimal
import java.math.MathContext
import java.math.RoundingMode
import java.util.UUID

@Parcelize
Expand Down Expand Up @@ -51,6 +59,17 @@ data class CoinbaseAccount(
ready = false
)
}

fun coinBalance(): Coin = try {
Coin.parseCoin(availableBalance.value)
} catch (ex: IllegalArgumentException) {
try {
val rounded = BigDecimal(availableBalance.value).round(MathContext(8, RoundingMode.HALF_UP))
rounded.toCoin()
} catch (ex: Exception) {
Coin.ZERO
}
}
}

@Parcelize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.dash.wallet.common.data.safeApiCall
import org.dash.wallet.common.services.ExchangeRatesProvider
import org.dash.wallet.common.util.Constants
import org.dash.wallet.common.util.GenericUtils
import org.dash.wallet.common.util.toCoin
import org.dash.wallet.integrations.coinbase.*
import org.dash.wallet.integrations.coinbase.model.*
import org.dash.wallet.integrations.coinbase.service.CoinBaseAuthApi
Expand All @@ -37,6 +38,8 @@ import org.dash.wallet.integrations.coinbase.service.CoinBaseServicesApi
import org.dash.wallet.integrations.coinbase.utils.CoinbaseConfig
import org.dash.wallet.integrations.coinbase.viewmodels.toDoubleOrZero
import java.math.BigDecimal
import java.math.MathContext
import java.math.RoundingMode
import javax.inject.Inject

interface CoinBaseRepositoryInt {
Expand All @@ -60,7 +63,7 @@ interface CoinBaseRepositoryInt {
): ResponseResource<SendTransactionToWalletResponse?>
suspend fun swapTrade(tradesRequest: TradesRequest): ResponseResource<SwapTradeUIModel>
suspend fun commitSwapTrade(buyOrderId: String): ResponseResource<SwapTradeUIModel>
suspend fun completeCoinbaseAuthentication(authorizationCode: String): ResponseResource<Boolean>
suspend fun completeCoinbaseAuthentication(authorizationCode: String): Boolean
suspend fun refreshWithdrawalLimit()
suspend fun getExchangeRateFromCoinbase(): ResponseResource<CoinbaseToDashExchangeRateUIModel>
suspend fun getWithdrawalLimitInDash(): Double
Expand Down Expand Up @@ -98,10 +101,10 @@ class CoinBaseRepository @Inject constructor(
it.currency == Constants.DASH_CURRENCY
} ?: throw IllegalStateException("No DASH account found")

return userAccountData.also {
config.set(CoinbaseConfig.USER_ACCOUNT_ID, it.uuid.toString())
config.set(CoinbaseConfig.LAST_BALANCE, Coin.parseCoin(it.availableBalance.value).value)
}
config.set(CoinbaseConfig.USER_ACCOUNT_ID, userAccountData.uuid.toString())
config.set(CoinbaseConfig.LAST_BALANCE, userAccountData.coinBalance().value)

return userAccountData
}

override suspend fun getUserAccounts(exchangeCurrencyCode: String): List<CoinBaseUserAccountDataUIModel> {
Expand Down Expand Up @@ -217,15 +220,14 @@ class CoinBaseRepository @Inject constructor(
)
}

override suspend fun completeCoinbaseAuthentication(authorizationCode: String) = safeApiCall {
authApi.getToken(code = authorizationCode).also {
it?.let { tokenResponse ->
config.set(CoinbaseConfig.LAST_ACCESS_TOKEN, tokenResponse.accessToken)
config.set(CoinbaseConfig.LAST_REFRESH_TOKEN, tokenResponse.refreshToken)
getUserAccount()
}
override suspend fun completeCoinbaseAuthentication(authorizationCode: String): Boolean {
authApi.getToken(code = authorizationCode)?.let { tokenResponse ->
config.set(CoinbaseConfig.LAST_ACCESS_TOKEN, tokenResponse.accessToken)
config.set(CoinbaseConfig.LAST_REFRESH_TOKEN, tokenResponse.refreshToken)
getUserAccount()
}
!config.get(CoinbaseConfig.LAST_ACCESS_TOKEN).isNullOrEmpty()

return !config.get(CoinbaseConfig.LAST_ACCESS_TOKEN).isNullOrEmpty()
}

override suspend fun refreshWithdrawalLimit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.dash.wallet.integrations.coinbase.service

import org.dash.wallet.integrations.coinbase.CoinbaseConstants
import org.dash.wallet.integrations.coinbase.model.TokenResponse
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
Expand All @@ -26,12 +27,13 @@ interface CoinBaseAuthApi {
@POST("oauth/token")
suspend fun getToken(
@Field("client_id") clientId: String = CoinBaseClientConstants.CLIENT_ID,
@Field("redirect_uri") redirectUri: String = "authhub://oauth-callback",
@Field("grant_type") grant_type: String = "authorization_code",
@Field("client_secret") client_secret: String = CoinBaseClientConstants.CLIENT_SECRET,
@Field("redirect_uri") redirectUri: String = CoinbaseConstants.REDIRECT_URL,
@Field("grant_type") grantType: String = "authorization_code",
@Field("client_secret") clientSecret: String = CoinBaseClientConstants.CLIENT_SECRET,
@Field("code") code: String
): TokenResponse?

@FormUrlEncoded
@POST("oauth/revoke")
suspend fun revokeToken(@Field("token") token: String)
}

This file was deleted.

Loading

0 comments on commit 6c706ef

Please sign in to comment.