commonMain.dev.icerock.moko.web3.EthereumAddress.kt Maven / Gradle / Ivy
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/
package dev.icerock.moko.web3
import com.ionspin.kotlin.bignum.integer.toBigInteger
import dev.icerock.moko.web3.crypto.createChecksummedAddress
import dev.icerock.moko.web3.hex.Hex20String
import dev.icerock.moko.web3.hex.ParametrizedHexStringSerializer
import kotlinx.serialization.Serializable
object EthereumAddressSerializer : ParametrizedHexStringSerializer(EthereumAddress)
@Serializable(with = EthereumAddressSerializer::class)
open class EthereumAddress(value: String) : Hex20String(value) {
companion object : SizedFactory {
override val size: Int = 20
override fun createInstance(value: String): EthereumAddress = EthereumAddress(value)
val AddressZero = EthereumAddress.createInstance(0.toBigInteger())
}
open fun toChecksummedAddress(): EthereumAddress =
createChecksummedAddress(sourceAddress = this, factoryTypeclass = EthereumAddress)
val isChecksummed: Boolean get() = withoutPrefix == toChecksummedAddress().withoutPrefix
val isValid: Boolean get() =
withoutPrefix.uppercase() == withoutPrefix
|| withoutPrefix.lowercase() == withoutPrefix
|| isChecksummed
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy