Skip to content

Commit

Permalink
AND-5138 [Send screen] Add XRP XAddress decoder/encoder to Blockchain…
Browse files Browse the repository at this point in the history
… SDK

Signed-off-by: Maqsudjon Ismoilov <[email protected]>
  • Loading branch information
iMaks99 committed Oct 31, 2023
1 parent 241d16c commit d0d8dc1
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tangem.blockchain.blockchains.xrp

import android.util.Log
import com.tangem.blockchain.blockchains.xrp.network.XrpAddressResponse
import com.tangem.blockchain.blockchains.xrp.network.XrpInfoResponse
import com.tangem.blockchain.blockchains.xrp.network.XrpNetworkProvider
import com.tangem.blockchain.common.Amount
Expand Down Expand Up @@ -96,4 +97,12 @@ class XrpWalletManager(
)
}
}

suspend fun decodeAddress(address: String): Result<XrpAddressResponse> {
return networkProvider.decodeAddress(address)
}

suspend fun encodeAddress(address: String, tag: String): Result<XrpAddressResponse> {
return networkProvider.encodeAddress(address, tag)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.tangem.blockchain.blockchains.xrp.network

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import retrofit2.http.GET
import retrofit2.http.Path

/**
* Api for decoding and encoding XRP X Addresses
*
* @see [https://xrpaddress.info/](https://xrpaddress.info/) for more information
*/
interface XrpAddressInfoApi {
@GET("/api/encode/{address}/{tag}")
suspend fun encodeAddress(@Path("address") address: String, @Path("tag") tag: String): XRPAddressResult

@GET("/api/decode/{address}")
suspend fun decodeAddress(@Path("address") address: String): XRPAddressResult
}

//region XRP Address
@JsonClass(generateAdapter = true)
data class XRPAddressResult(
@Json(name = "address")
var address: String? = null,
@Json(name = "account")
var account: String? = null,
@Json(name = "tag")
var tag: String? = null,
)
//endregion





Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@ import com.tangem.blockchain.extensions.Result
import com.tangem.blockchain.extensions.SimpleResult
import java.math.BigDecimal

interface XrpNetworkProvider: NetworkProvider {
interface XrpNetworkProvider : NetworkProvider {
suspend fun getInfo(address: String): Result<XrpInfoResponse>
suspend fun sendTransaction(transaction: String): SimpleResult
suspend fun getFee(): Result<XrpFeeResponse>
suspend fun checkIsAccountCreated(address: String): Boolean
suspend fun encodeAddress(address: String, tag: String): Result<XrpAddressResponse>
suspend fun decodeAddress(address: String): Result<XrpAddressResponse>
}

data class XrpInfoResponse(
val balance: BigDecimal = BigDecimal.ZERO,
val sequence: Long = 0,
val hasUnconfirmed: Boolean = false,
val reserveBase: BigDecimal,
val accountFound: Boolean = true
val balance: BigDecimal = BigDecimal.ZERO,
val sequence: Long = 0,
val hasUnconfirmed: Boolean = false,
val reserveBase: BigDecimal,
val accountFound: Boolean = true,
)

data class XrpFeeResponse(
val minimalFee: BigDecimal,
val normalFee: BigDecimal,
val priorityFee: BigDecimal
val minimalFee: BigDecimal,
val normalFee: BigDecimal,
val priorityFee: BigDecimal,
)

data class XrpAddressResponse(
val address: String,
val tag: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ class XrpNetworkService(providers: List<XrpNetworkProvider>) : XrpNetworkProvide
override suspend fun checkIsAccountCreated(address: String): Boolean {
return multiProvider.currentProvider.checkIsAccountCreated(address)
}

override suspend fun encodeAddress(address: String, tag: String): Result<XrpAddressResponse> {
return multiProvider.currentProvider.encodeAddress(address, tag)
}

override suspend fun decodeAddress(address: String): Result<XrpAddressResponse> {
return multiProvider.performRequest(XrpNetworkProvider::decodeAddress, address)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.tangem.blockchain.blockchains.xrp.network.rippled

import com.tangem.blockchain.blockchains.xrp.network.XrpFeeResponse
import com.tangem.blockchain.blockchains.xrp.network.XrpInfoResponse
import com.tangem.blockchain.blockchains.xrp.network.XrpNetworkProvider
import com.tangem.blockchain.blockchains.xrp.network.*
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchain.common.BlockchainSdkError
import com.tangem.blockchain.common.toBlockchainSdkError
import com.tangem.blockchain.extensions.AddHeaderInterceptor
import com.tangem.blockchain.extensions.Result
import com.tangem.blockchain.extensions.SimpleResult
import com.tangem.blockchain.extensions.retryIO
import com.tangem.blockchain.network.API_XRP_ADDRESS_INFO
import com.tangem.blockchain.network.createRetrofitInstance
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
Expand All @@ -35,6 +34,18 @@ class RippledNetworkProvider(
)
).create(RippledApi::class.java)
}
private val addressApi: XrpAddressInfoApi by lazy {
createRetrofitInstance(
baseUrl = API_XRP_ADDRESS_INFO,
headerInterceptors = listOf(
AddHeaderInterceptor(
headers = buildMap {
put("Content-Type", "application/json")
},
)
)
).create(XrpAddressInfoApi::class.java)
}
private val decimals = Blockchain.XRP.decimals()

override suspend fun getInfo(address: String): Result<XrpInfoResponse> {
Expand Down Expand Up @@ -133,6 +144,33 @@ class RippledNetworkProvider(
true // or let's assume it's created? (normally it is)
}
}

override suspend fun encodeAddress(address: String, tag: String): Result<XrpAddressResponse> {
return try {
val decodedAddress = retryIO { addressApi.encodeAddress(address, tag) }
Result.Success(
XrpAddressResponse(
address = decodedAddress.address!!,
)
)
} catch (exception: Exception) {
Result.Failure(exception.toBlockchainSdkError())
}
}

override suspend fun decodeAddress(address: String): Result<XrpAddressResponse> {
return try {
val decodedAddress = retryIO { addressApi.decodeAddress(address) }
Result.Success(
XrpAddressResponse(
address = decodedAddress.account!!,
tag = decodedAddress.tag
)
)
} catch (exception: Exception) {
Result.Failure(exception.toBlockchainSdkError())
}
}
}

private fun makeAccountBody(address: String, validated: Boolean): RippledBody {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const val API_STELLAR_TESTNET = "https://horizon-testnet.stellar.org/"
const val API_BLOCKCHAIN_INFO = "https://blockchain.info/"
const val API_ADALITE = "https://explorer2.adalite.io/"
const val API_XRP_LEDGER_FOUNDATION = "https://xrplcluster.com/"
const val API_XRP_ADDRESS_INFO = "https://xrpaddress.info/"
const val API_BLOCKCHAIR = "https://api.blockchair.com/"
const val API_TEZOS_BLOCKSCALE = "https://rpc.tzbeta.net/"
const val API_TEZOS_SMARTPY = "https://mainnet.smartpy.io/"
Expand Down

0 comments on commit d0d8dc1

Please sign in to comment.