Skip to content

Commit

Permalink
AND-5815 Fix detekt warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mama1emon committed Jan 16, 2024
1 parent 046a376 commit cfe93da
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class TokenBalanceERC20TokenMethod(
override val data: ByteArray
get() {
val prefixData = prefix.hexToBytes()
val addressData = address.hexToBytes().leftPadToFixedSize(32)
val addressData = address.hexToBytes().leftPadToFixedSize(fixedSize = 32)
return prefixData + addressData
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ data class TransferERC20TokenMethod(
override val data: ByteArray
get() {
val prefixData = prefix.hexToBytes()
val addressData = destination.hexToBytes().leftPadToFixedSize(32)
val amountData = amount.toByteArray().leftPadToFixedSize(32)
val addressData = destination.hexToBytes().leftPadToFixedSize(fixedSize = 32)
val amountData = amount.toByteArray().leftPadToFixedSize(fixedSize = 32)
return prefixData + addressData + amountData
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W
minimum = Fee.Vechain(
amount = Amount(
token = VechainWalletManager.VTHO_TOKEN,
value = gas.toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL)
value = gas.toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL),
),
gasPriceCoef = 0,
),
normal = Fee.Vechain(
amount = Amount(
token = VechainWalletManager.VTHO_TOKEN,
value = (gas * 1.5).toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL)
value = (gas * NORMAL_FEE_COEFFICIENT).toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL),
),
gasPriceCoef = 127,
),
priority = Fee.Vechain(
amount = Amount(
token = VechainWalletManager.VTHO_TOKEN,
value = (gas * 2).toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL)
value = (gas * PRIORITY_FEE_COEFFICIENT).toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL),
),
gasPriceCoef = 255,
),
Expand Down Expand Up @@ -85,7 +85,7 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W
fee,
transactionData.destinationAddress,
blockInfo,
nonce
nonce,
)

val publicKeys = DataVector()
Expand All @@ -95,12 +95,15 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W
signatures.add(unmarshalSignature(signature, hash, publicKey))

val compileWithSignatures = TransactionCompiler.compileWithSignatures(
coinType, inputData.toByteArray(), signatures, publicKeys
coinType,
inputData.toByteArray(),
signatures,
publicKeys,
)

val output = VeChain.SigningOutput.parseFrom(compileWithSignatures)
if (output.error != Common.SigningError.OK) {
throw IllegalStateException("something went wrong")
error("something went wrong")
}

return output.encoded.toByteArray()
Expand All @@ -119,7 +122,7 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W
.setChainTag(chainTag)
.setNonce(nonce)
.setBlockRef(blockInfo.blockRef)
.setExpiration(180)
.setExpiration(EXPIRATION_BLOCKS)
.setGas(intrinsicGas(clause))
.setGasPriceCoef(fee.gasPriceCoef)
.addClauses(clause)
Expand Down Expand Up @@ -166,14 +169,17 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W
}

private fun unmarshalSignature(signature: ByteArray, hash: ByteArray, publicKey: Wallet.PublicKey): ByteArray {
val r = BigInteger(1, signature.copyOfRange(0, 32))
val s = BigInteger(1, signature.copyOfRange(32, 64))
val r = BigInteger(1, signature.copyOfRange(fromIndex = 0, toIndex = 32))
val s = BigInteger(1, signature.copyOfRange(fromIndex = 32, toIndex = 64))

val ecdsaSignature = ECDSASignature(r, s).canonicalise()

val recId = ecdsaSignature.determineRecId(
hash,
PublicKey(publicKey.blockchainKey.toDecompressedPublicKey().sliceArray(1..64)),
PublicKey(
publicKey = publicKey.blockchainKey.toDecompressedPublicKey()
.sliceArray(1..PUBLIC_KEY_LENGTH),
),
)
val signatureData = SignatureData(ecdsaSignature.r, ecdsaSignature.s, recId.toBigInteger())

Expand All @@ -183,13 +189,20 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W
}

private companion object {
private const val TX_GAS = 5_000L
private const val CLAUSE_GAS = 16_000L
private const val VM_INVOCATION_COST = 15_000L
const val TX_GAS = 5_000L
const val CLAUSE_GAS = 16_000L
const val VM_INVOCATION_COST = 15_000L

const val Z_GAS = 4L
const val NZ_GAS = 68L

const val GAS_TO_VET_DECIMAL = 5

const val NORMAL_FEE_COEFFICIENT = 1.5
const val PRIORITY_FEE_COEFFICIENT = 2

private const val Z_GAS = 4L
private const val NZ_GAS = 68L
const val EXPIRATION_BLOCKS = 180

private const val GAS_TO_VET_DECIMAL = 5
const val PUBLIC_KEY_LENGTH = 64
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ internal class VechainNetworkProvider(
private fun mapBlockInfo(response: VechainLatestBlockResponse): VechainBlockInfo {
val blockRef = response.blockId
.removePrefix("0x")
.substring(0..15)
.substring(range = 0..BLOCK_REFERENCE_LENGTH)
.toLongOrNull(radix = 16)
return VechainBlockInfo(
blockId = response.blockId,
blockRef = blockRef ?: 0,
blockNumber = response.number,
)
}

private companion object {

const val BLOCK_REFERENCE_LENGTH = 15
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class VechainNetworkService(
accountResponse = accountInfoDeferred.await().extractResult(),
pendingTxsInfo = pendingTxsDeferred.awaitAll().map { it.extractResult() },
tokenBalances = tokenBalances,
)
),
)
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.tangem.blockchain.externallinkprovider.ExternalLinkProviderFactory
import com.tangem.common.card.EllipticCurve
import com.tangem.crypto.hdWallet.DerivationPath

@Suppress("LargeClass")
enum class Blockchain(
val id: String,
val currency: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ internal object VechainWalletManagerAssembly : WalletManagerAssembly<VechainWall
override fun make(input: WalletManagerAssemblyInput): VechainWalletManager {
return VechainWalletManager(
wallet = input.wallet,
networkProviders = VechainNetworkProvidersBuilder().build(input.wallet.blockchain.isTestnet(), input.config)
networkProviders = VechainNetworkProvidersBuilder().build(
isTestNet = input.wallet.blockchain.isTestnet(),
config = input.config,
),
)
}
}

0 comments on commit cfe93da

Please sign in to comment.