Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unstoppable Domains Resolution #1

Open
wants to merge 2 commits into
base: ryipay
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ dependencies {
implementation files('lib/AF-Android-SDK.jar')

// Room
def room_version = '2.4.1'
def room_version = '2.4.2'
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
Expand Down Expand Up @@ -234,7 +234,7 @@ dependencies {
implementation "com.github.WalletConnect:WalletConnectKotlinV2:1.0.0-beta04"

// Unstoppable Domains
implementation 'com.unstoppabledomains:resolution:4.0.0'
implementation 'com.github.unstoppabledomains:resolution-java:6.1.0'

// in case native file tor.so not loading, do full gradle clean and build.
implementation 'com.github.horizontalsystems:tor-kit-android:6c775c1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.horizontalsystems.bankwallet.modules.address

import com.unstoppabledomains.resolution.Resolution
import io.horizontalsystems.bankwallet.entities.Address
import io.horizontalsystems.ethereumkit.core.AddressValidator
import io.horizontalsystems.marketkit2.models.CoinType
import com.unstoppabledomains.resolution.Resolution
import java.net.URL

interface IAddressHandler {
fun isSupported(value: String): Boolean
Expand All @@ -12,67 +12,73 @@ interface IAddressHandler {

class AddressHandlerUdn(private val coinType: CoinType, private val coinCode: String) : IAddressHandler {
private val resolution = Resolution()
private val chain by lazy { chain(coinType) }
private val chainCoinCode by lazy { chainCoinCode(coinType) }
private val currencyVersion by lazy { version(coinType, coinCode) }

override fun isSupported(value: String): Boolean {
return value.contains(".") && resolution.isSupported(value)
}

override fun parseAddress(value: String): Address {
return Address(resolveAddress(value), value)
return if (currencyVersion != ""){
Address(resolveMultiChain(value, coinCode, currencyVersion), value)
}else {
Address(resolveSingleChain(value, coinCode), value)
}
}

private fun resolveAddress(value: String): String {
val fetchers = mutableListOf<() -> String?>()
fetchers.add {
chain?.let { resolution.getMultiChainAddress(value, coinCode, it) }
}
fetchers.add {
resolution.getAddress(value, coinCode)
}
fetchers.add {
chainCoinCode?.let { resolution.getAddress(value, it) }
private fun resolveMultiChain(domain: String, currency: String, version: String): String {
val resolvedAddress: String
try{
resolvedAddress = resolution.getMultiChainAddress(domain, currency, version)
return resolvedAddress
}catch(e: Exception){
throw Exception("Unstoppable Domains - Resolution Error: $e")
}
}

var lastError: Exception? = null
for (fetcher in fetchers) {
try {
fetcher.invoke()?.let { resolvedAddress ->
return resolvedAddress
}
} catch (e: Exception) {
lastError = e
}
private fun resolveSingleChain(domain: String, currency: String): String {
val resolvedAddress: String
if (currency == "BNB"){
return resolveMultiChain(domain, currency, "BEP20")
}
try{
resolvedAddress = resolution.getAddress(domain, currency)
return resolvedAddress
}catch(e: Exception){
throw Exception("Unstoppable Domains - Resolution Error: $e")
}

throw lastError!!
}

companion object {
private fun chainCoinCode(coinType: CoinType) = when (coinType) {
is CoinType.Ethereum,
is CoinType.Erc20,
is CoinType.BinanceSmartChain,
is CoinType.Bep20,
is CoinType.Polygon,
is CoinType.Mrc20 -> "ETH"
// is CoinType.EthereumOptimism -> "ETH"
// is CoinType.OptimismErc20 -> "ETH"
// is CoinType.EthereumArbitrumOne -> "ETH"
// is CoinType.ArbitrumOneErc20 -> "ETH"
else -> null
}

private fun chain(coinType: CoinType) = when (coinType) {
is CoinType.Erc20 -> "ERC20"
is CoinType.Bep20 -> "BEP20"
is CoinType.Polygon,
is CoinType.Mrc20 -> "MATIC"
else -> null
private fun version(coinType: CoinType, coinCode: String): String {
val url = "https://unstoppabledomains.com/api/uns-resolver-keys"
val expectedVersion = coinType.toString().split("|")[0].uppercase()
val supportedCurrencies: MutableList<String> = ArrayList()
var version = ""
try{
val jsonString = URL(url).readText()
val keys = jsonString.split("keys\":{\"crypto.")[1].split("crypto.")
for (key in keys) {
val currency = key.split("\":{\"deprecated")
supportedCurrencies.add(currency[0])
}
}catch(e: Exception){
throw Exception("Unstoppable Domains - API Error: $e")
}
for (currency in supportedCurrencies) {
if(currency.startsWith(coinCode)){
if(currency.contains(expectedVersion)){
version = expectedVersion
}
}
}
if (expectedVersion.contains("[0-9]".toRegex()) && version != expectedVersion) {
throw Exception("Unstoppable Domains - Unsupported Currency Version: $expectedVersion")
} else {
return version
}
}
}

}

class AddressHandlerEvm : IAddressHandler {
Expand Down
2 changes: 1 addition & 1 deletion marketkit2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

//Room
def room_version = '2.3.0'
def room_version = '2.4.2'
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
kapt "androidx.room:room-compiler:$room_version"
Expand Down