From 5a1a3947b047dbdd721b52296a0e3e45b640c261 Mon Sep 17 00:00:00 2001 From: Anton Tkachev Date: Tue, 28 Nov 2023 20:20:56 +0400 Subject: [PATCH] AND-5394 Improved DecimalAddressService --- .../decimal/DecimalAddressService.kt | 21 +++++++++++-------- .../decimal/DecimalNetworkService.kt | 2 +- .../decimal/DecimalWalletManager.kt | 2 +- .../providers/DecimalExternalLinkProvider.kt | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalAddressService.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalAddressService.kt index e0a3f8837..a63c020f6 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalAddressService.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalAddressService.kt @@ -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 { - 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) @@ -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 @@ -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 } diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalNetworkService.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalNetworkService.kt index 41812a56f..d9e168187 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalNetworkService.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalNetworkService.kt @@ -38,6 +38,6 @@ internal class DecimalNetworkService( } private fun convertAddress(address: String): String { - return DecimalAddressService.convertDscAddressToErcAddress(address) ?: address + return DecimalAddressService.convertDelAddressToDscAddress(address) } } diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalWalletManager.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalWalletManager.kt index 95db45e90..a1646a79a 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalWalletManager.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalWalletManager.kt @@ -55,6 +55,6 @@ internal class DecimalWalletManager( ) private fun convertAddress(destinationAddress: String): String { - return DecimalAddressService.convertDscAddressToErcAddress(destinationAddress) ?: destinationAddress + return DecimalAddressService.convertDelAddressToDscAddress(destinationAddress) } } diff --git a/blockchain/src/main/java/com/tangem/blockchain/externallinkprovider/providers/DecimalExternalLinkProvider.kt b/blockchain/src/main/java/com/tangem/blockchain/externallinkprovider/providers/DecimalExternalLinkProvider.kt index 87a29e8d2..88214b9c2 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/externallinkprovider/providers/DecimalExternalLinkProvider.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/externallinkprovider/providers/DecimalExternalLinkProvider.kt @@ -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" }