Skip to content

Commit

Permalink
AND-5171 Merge branch 'develop' into bugfix/AND-5171_infura
Browse files Browse the repository at this point in the history
  • Loading branch information
Sateetas committed Nov 27, 2023
2 parents 33b34c2 + 39f05b8 commit a9a440e
Show file tree
Hide file tree
Showing 64 changed files with 790 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Each line is a file pattern followed by one or more owners.

# These owners will be the default owners for everything in the repo.
* @roman-pv @iiiburnyiii @Mama1emon @kozarezvlad @Yoggam1 @Sateetas
* @roman-pv @iiiburnyiii @Mama1emon @kozarezvlad @Yoggam1 @Sateetas @iMaks99
4 changes: 2 additions & 2 deletions blockchain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ dependencies {
implementation files('libs/solanaj-0.0.10.jar')
implementation files('libs/polkaj-root-0.1.5.jar')
implementation "com.tangem:blst-android:0.0.2@aar"
implementation "com.tangem:wallet-core:3.2.4-tangem3@aar"
implementation("com.tangem:wallet-core-proto:3.2.4-tangem3") {
implementation "com.tangem:wallet-core:3.2.4-tangem4@aar"
implementation("com.tangem:wallet-core-proto:3.2.4-tangem4") {
exclude group: 'com.google.protobuf', module: 'protobuf-javalite'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ class BitcoinScriptAddress(
val script: Script,
value: String,
type: AddressType = AddressType.Default
) : Address(value, type)
) : Address(value, type)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.tangem.blockchain.blockchains.bitcoin.network.blockchaininfo.Blockcha
import com.tangem.blockchain.blockchains.ravencoin.network.RavencoinNetworkProvider
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchain.common.BlockchainSdkConfig
import com.tangem.blockchain.common.GetBlockCredentials
import com.tangem.blockchain.network.blockbook.BlockBookNetworkProvider
import com.tangem.blockchain.network.blockbook.config.BlockBookConfig
import com.tangem.blockchain.network.blockchair.BlockchairNetworkProvider
Expand All @@ -17,7 +18,7 @@ internal fun Blockchain.getBitcoinNetworkProviders(
return when (this) {
Blockchain.Bitcoin -> listOfNotNull(
getNowNodesProvider(blockchain, config),
getGetBlockProvider(blockchain, config),
getGetBlockProvider(blockchain, config.getBlockCredentials),
*getBlockchairProviders(blockchain, config),
getBlockcypherProvider(blockchain, config),
BlockchainInfoNetworkProvider() // crashes when large input
Expand All @@ -32,7 +33,7 @@ internal fun Blockchain.getBitcoinNetworkProviders(
Blockchain.Dash,
-> listOfNotNull(
getNowNodesProvider(blockchain, config),
getGetBlockProvider(blockchain, config),
getGetBlockProvider(blockchain, config.getBlockCredentials),
*getBlockchairProviders(blockchain, config),
getBlockcypherProvider(blockchain, config)
)
Expand Down Expand Up @@ -69,11 +70,23 @@ private fun getNowNodesProvider(

private fun getGetBlockProvider(
blockchain: Blockchain,
config: BlockchainSdkConfig,
getBlockCredentials: GetBlockCredentials?,
): BitcoinNetworkProvider? {
return if (config.getBlockCredentials != null && config.getBlockCredentials.apiKey.isNotBlank()) {
val credentials = getBlockCredentials ?: return null
val accessToken = when(blockchain) {
Blockchain.Bitcoin -> credentials.bitcoin
Blockchain.Dash -> credentials.dash
Blockchain.Dogecoin -> credentials.dogecoin
Blockchain.Litecoin -> credentials.litecoin
else -> null
} ?: return null

return if (!accessToken.blockBookRest.isNullOrEmpty() && !accessToken.jsonRpc.isNullOrEmpty()) {
BlockBookNetworkProvider(
config = BlockBookConfig.GetBlock(getBlockCredentials = config.getBlockCredentials),
config = BlockBookConfig.GetBlock(
blockBookToken = accessToken.blockBookRest,
jsonRpcToken = accessToken.jsonRpc,
),
blockchain = blockchain
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class BitcoinTransactionHistoryProvider(
address = address,
page = 1,
pageSize = 1, // We don't need to know all transactions to define state
filterType = filterType,
filterType = null,
)
}
if (!response.transactions.isNullOrEmpty()) {
Expand Down Expand Up @@ -150,11 +150,13 @@ internal class BitcoinTransactionHistoryProvider(
outputs + fee
} else {
val outputs = tx.vout
.find { it.addresses?.contains(walletAddress) == true}
?.value.toBigDecimalOrDefault()
.filter { it.addresses?.contains(walletAddress) == true }
.map { it.value.toBigDecimalOrDefault() }
.sumOf { it }
val inputs = tx.vin
.find { it.addresses?.contains(walletAddress) == true }
?.value.toBigDecimalOrDefault()
.filter { it.addresses?.contains(walletAddress) == true }
.map { it.value.toBigDecimalOrDefault() }
.sumOf { it }
outputs - inputs
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.tangem.blockchain.blockchains.cardano.network

sealed class RosettaNetwork(val url: String) {

data class RosettaGetblock(val apiKey: String) : RosettaNetwork(url = "https://ada.getblock.io/mainnet/${apiKey}/")
data class RosettaGetblock(val accessToken: String) : RosettaNetwork(url = "https://go.getblock.io/$accessToken/")
object RosettaTangem : RosettaNetwork(url = "https://ada.tangem.com/")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ChiaWalletManager(
val sendResult = networkProvider.sendTransaction(transactionToSend)

if (sendResult is SimpleResult.Success) {
transactionData.hash = transactionToSend.spendBundle.aggregatedSignature
wallet.addOutgoingTransaction(transactionData)
}
sendResult
Expand Down
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)
}
}
}
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
}
}
Loading

0 comments on commit a9a440e

Please sign in to comment.