Skip to content

Commit

Permalink
AND-5394 Improved DecimalAddressService
Browse files Browse the repository at this point in the history
  • Loading branch information
iiiburnyiii committed Nov 28, 2023
1 parent ed19c8b commit 5a1a394
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ import com.tangem.blockchain.common.address.Address as SdkAddress
internal class DecimalAddressService : AddressService() {

override fun makeAddress(walletPublicKey: ByteArray, curve: EllipticCurve?): String {
return makeErcAddress(walletPublicKey)
return makeDscAddress(walletPublicKey)
}

override fun makeAddresses(
walletPublicKey: ByteArray,
curve: EllipticCurve?,
): Set<SdkAddress> {
val ercAddress = makeErcAddress(walletPublicKey)
val dscAddress = makeDscAddress(walletPublicKey)

return setOf(
SdkAddress(ercAddress, AddressType.Legacy),
SdkAddress(convertErcAddressToDscAddress(ercAddress), AddressType.Default),
SdkAddress(dscAddress, AddressType.Legacy),
SdkAddress(convertDscAddressToDelAddress(dscAddress), AddressType.Default),
)
}

private fun makeErcAddress(walletPublicKey: ByteArray): String {
/** Same as ERC55 address */
private fun makeDscAddress(walletPublicKey: ByteArray): String {
val decompressedPublicKey = walletPublicKey
.toDecompressedPublicKey()
.sliceArray(1..64)
Expand All @@ -47,7 +48,7 @@ internal class DecimalAddressService : AddressService() {
override fun validate(address: String): Boolean {
val addressToValidate = when {
address.startsWith(ADDRESS_PREFIX) || address.startsWith(LEGACY_ADDRESS_PREFIX) -> {
convertDscAddressToErcAddress(address) ?: return false
convertDelAddressToDscAddress(address)
}

else -> address
Expand All @@ -61,20 +62,22 @@ internal class DecimalAddressService : AddressService() {
private const val LEGACY_ADDRESS_PREFIX = "dx"
private const val ERC55_ADDRESS_PREFIX = "0x"

fun convertDscAddressToErcAddress(addressHex: String): String? {
fun convertDelAddressToDscAddress(addressHex: String): String {
if (addressHex.startsWith(ERC55_ADDRESS_PREFIX)) {
return addressHex
}

val (prefix, addressBytes) = Bech32.decode(addressHex).let { it.hrp to it.data }
if (prefix == null || addressBytes == null) return null
require(value = prefix != null && addressBytes != null) {
"Unable to convert DEL address to DSC address: $addressHex"
}

val convertedAddressBytes = Crypto.convertBits(addressBytes, 0, addressBytes.size, 5, 8, false)

return convertedAddressBytes.toHexString()
}

fun convertErcAddressToDscAddress(addressHex: String): String {
fun convertDscAddressToDelAddress(addressHex: String): String {
if (addressHex.startsWith(ADDRESS_PREFIX) || addressHex.startsWith(LEGACY_ADDRESS_PREFIX)) {
return addressHex
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ internal class DecimalNetworkService(
}

private fun convertAddress(address: String): String {
return DecimalAddressService.convertDscAddressToErcAddress(address) ?: address
return DecimalAddressService.convertDelAddressToDscAddress(address)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ internal class DecimalWalletManager(
)

private fun convertAddress(destinationAddress: String): String {
return DecimalAddressService.convertDscAddressToErcAddress(destinationAddress) ?: destinationAddress
return DecimalAddressService.convertDelAddressToDscAddress(destinationAddress)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class DecimalExternalLinkProvider(isTestnet: Boolean) : ExternalLinkPro
override val testNetTopUpUrl: String = "https://testnet.console.decimalchain.com/wallet/"

override fun explorerUrl(walletAddress: String, contractAddress: String?): String {
val address = DecimalAddressService.convertErcAddressToDscAddress(walletAddress)
val address = DecimalAddressService.convertDscAddressToDelAddress(walletAddress)

return explorerBaseUrl + "address/$address"
}
Expand Down

0 comments on commit 5a1a394

Please sign in to comment.