-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AND-4536 Added decimal chain support
- Loading branch information
1 parent
e8648ec
commit ded4cd8
Showing
13 changed files
with
288 additions
and
13 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalAddressService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.tangem.blockchain.blockchains.decimal | ||
|
||
import com.tangem.blockchain.blockchains.binance.client.encoding.Crypto | ||
import com.tangem.blockchain.common.address.AddressService | ||
import com.tangem.blockchain.common.address.AddressType | ||
import com.tangem.common.card.EllipticCurve | ||
import com.tangem.common.extensions.hexToBytes | ||
import com.tangem.common.extensions.toDecompressedPublicKey | ||
import org.bitcoinj.core.Bech32 | ||
import org.kethereum.crypto.toAddress | ||
import org.kethereum.erc55.hasValidERC55ChecksumOrNoChecksum | ||
import org.kethereum.erc55.withERC55Checksum | ||
import org.kethereum.model.Address | ||
import org.kethereum.model.PublicKey | ||
import org.komputing.khex.extensions.toHexString | ||
import com.tangem.blockchain.common.address.Address as SdkAddress | ||
|
||
internal class DecimalAddressService : AddressService() { | ||
|
||
override fun makeAddress(walletPublicKey: ByteArray, curve: EllipticCurve?): String { | ||
return makeErcAddress(walletPublicKey) | ||
} | ||
|
||
override fun makeAddresses( | ||
walletPublicKey: ByteArray, | ||
curve: EllipticCurve?, | ||
): Set<SdkAddress> { | ||
val ercAddress = makeErcAddress(walletPublicKey) | ||
|
||
return setOf( | ||
SdkAddress(ercAddress), | ||
SdkAddress( | ||
convertErcAddressToDscAddress(ercAddress), | ||
AddressType.Legacy, | ||
) | ||
) | ||
} | ||
|
||
private fun makeErcAddress(walletPublicKey: ByteArray): String { | ||
val decompressedPublicKey = walletPublicKey | ||
.toDecompressedPublicKey() | ||
.sliceArray(1..64) | ||
|
||
return PublicKey(decompressedPublicKey) | ||
.toAddress() | ||
.withERC55Checksum() | ||
.hex | ||
} | ||
|
||
override fun validate(address: String): Boolean { | ||
val addressToValidate = when { | ||
address.startsWith(ADDRESS_PREFIX) || address.startsWith(LEGACY_ADDRESS_PREFIX) -> { | ||
convertDscAddressToErcAddress(address) ?: return false | ||
} | ||
|
||
else -> address | ||
} | ||
|
||
return Address(addressToValidate).hasValidERC55ChecksumOrNoChecksum() | ||
} | ||
|
||
companion object { | ||
private const val ADDRESS_PREFIX = "d0" | ||
private const val LEGACY_ADDRESS_PREFIX = "dx" | ||
private const val ERC55_ADDRESS_PREFIX = "0x" | ||
|
||
fun convertDscAddressToErcAddress(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 | ||
|
||
val convertedAddressBytes = Crypto.convertBits(addressBytes, 0, addressBytes.size, 5, 8, false) | ||
|
||
return convertedAddressBytes.toHexString() | ||
} | ||
|
||
fun convertErcAddressToDscAddress(addressHex: String): String { | ||
if (addressHex.startsWith(ADDRESS_PREFIX) || addressHex.startsWith(LEGACY_ADDRESS_PREFIX)) { | ||
return addressHex | ||
} | ||
|
||
val addressBytes = addressHex.hexToBytes() | ||
val converted = Crypto.convertBits(addressBytes, 0, addressBytes.size, 8, 5, false) | ||
|
||
return Bech32.encode(ADDRESS_PREFIX, converted) | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
blockchain/src/main/java/com/tangem/blockchain/blockchains/decimal/DecimalWalletManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.tangem.blockchain.blockchains.decimal | ||
|
||
import com.tangem.blockchain.blockchains.ethereum.CompiledEthereumTransaction | ||
import com.tangem.blockchain.blockchains.ethereum.EthereumTransactionBuilder | ||
import com.tangem.blockchain.blockchains.ethereum.EthereumWalletManager | ||
import com.tangem.blockchain.blockchains.ethereum.network.EthereumNetworkProvider | ||
import com.tangem.blockchain.common.Amount | ||
import com.tangem.blockchain.common.TransactionData | ||
import com.tangem.blockchain.common.TransactionSigner | ||
import com.tangem.blockchain.common.Wallet | ||
import com.tangem.blockchain.common.transaction.TransactionFee | ||
import com.tangem.blockchain.extensions.Result | ||
import com.tangem.blockchain.extensions.SimpleResult | ||
import java.math.BigInteger | ||
|
||
internal class DecimalWalletManager( | ||
wallet: Wallet, | ||
transactionBuilder: EthereumTransactionBuilder, | ||
networkProvider: EthereumNetworkProvider, | ||
) : EthereumWalletManager(wallet, transactionBuilder, networkProvider) { | ||
|
||
override suspend fun getFee(amount: Amount, destination: String): Result<TransactionFee> { | ||
return super.getFee(amount, convertAddress(destination)) | ||
} | ||
|
||
override suspend fun getFee(amount: Amount, destination: String, data: String): Result<TransactionFee> { | ||
return super.getFee(amount, convertAddress(destination), data) | ||
} | ||
|
||
override suspend fun getFeeInternal(amount: Amount, destination: String, data: String?): Result<TransactionFee> { | ||
return super.getFeeInternal(amount, convertAddress(destination), data) | ||
} | ||
|
||
override suspend fun getGasLimit(amount: Amount, destination: String): Result<BigInteger> { | ||
return super.getGasLimit(amount, convertAddress(destination)) | ||
} | ||
|
||
override suspend fun getGasLimit(amount: Amount, destination: String, data: String): Result<BigInteger> { | ||
return super.getGasLimit(amount, convertAddress(destination), data) | ||
} | ||
|
||
override suspend fun send(transactionData: TransactionData, signer: TransactionSigner): SimpleResult { | ||
return super.send(convertTransactionDataAddress(transactionData), signer) | ||
} | ||
|
||
override suspend fun sign( | ||
transactionData: TransactionData, | ||
signer: TransactionSigner, | ||
): Result<Pair<ByteArray, CompiledEthereumTransaction>> { | ||
return super.sign(convertTransactionDataAddress(transactionData), signer) | ||
} | ||
|
||
private fun convertTransactionDataAddress(transactionData: TransactionData) = transactionData.copy( | ||
destinationAddress = convertAddress(transactionData.destinationAddress), | ||
) | ||
|
||
private fun convertAddress(destinationAddress: String): String { | ||
return DecimalAddressService.convertDscAddressToErcAddress(destinationAddress) ?: destinationAddress | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
.../src/main/java/com/tangem/blockchain/common/assembly/impl/DecimalWalletManagerAssembly.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.tangem.blockchain.common.assembly.impl | ||
|
||
import com.tangem.blockchain.blockchains.decimal.DecimalWalletManager | ||
import com.tangem.blockchain.blockchains.ethereum.EthereumTransactionBuilder | ||
import com.tangem.blockchain.blockchains.ethereum.getEthereumJsonRpcProviders | ||
import com.tangem.blockchain.blockchains.ethereum.network.EthereumNetworkService | ||
import com.tangem.blockchain.common.assembly.WalletManagerAssembly | ||
import com.tangem.blockchain.common.assembly.WalletManagerAssemblyInput | ||
|
||
internal object DecimalWalletManagerAssembly : WalletManagerAssembly<DecimalWalletManager>() { | ||
|
||
override fun make(input: WalletManagerAssemblyInput): DecimalWalletManager { | ||
return with(input.wallet) { | ||
DecimalWalletManager( | ||
wallet = this, | ||
transactionBuilder = EthereumTransactionBuilder( | ||
walletPublicKey = publicKey.blockchainKey, | ||
blockchain = blockchain | ||
), | ||
networkProvider = EthereumNetworkService( | ||
jsonRpcProviders = blockchain.getEthereumJsonRpcProviders(input.config), | ||
) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.