Skip to content

Commit

Permalink
Add Zcash address validator
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Nov 16, 2023
1 parent 928a509 commit 259765f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import io.reactivex.subjects.PublishSubject
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.runBlocking
import java.math.BigDecimal
import java.util.regex.Pattern
import kotlin.math.max

class ZcashAdapter(
Expand Down Expand Up @@ -378,3 +379,23 @@ class ZcashAdapter(
}
}
}

object ZcashAddressValidator {
fun validate(address: String): Boolean {
return isValidZcashAddress(address)
}

private fun isValidTransparentAddress(address: String): Boolean {
val transparentPattern = Pattern.compile("^t[0-9a-zA-Z]{34}$")
return transparentPattern.matcher(address).matches()
}

private fun isValidShieldedAddress(address: String): Boolean {
val shieldedPattern = Pattern.compile("^z[0-9a-zA-Z]{77}$")
return shieldedPattern.matcher(address).matches()
}

private fun isValidZcashAddress(address: String): Boolean {
return isValidTransparentAddress(address) || isValidShieldedAddress(address)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ class AddressParserFactory(
}

BlockchainType.Zcash -> {
//No validation
//todo: add zcash address handler
// addressHandlers.add(AddressHandlerPure(blockchainType))
addressHandlers.add(AddressHandlerZcash())
}

BlockchainType.Ethereum,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.horizontalsystems.bankwallet.modules.address

import com.unstoppabledomains.resolution.Resolution
import io.horizontalsystems.bankwallet.core.adapters.zcash.ZcashAddressValidator
import io.horizontalsystems.bankwallet.entities.Address
import io.horizontalsystems.binancechainkit.helpers.Crypto
import io.horizontalsystems.bitcoincore.network.Network
Expand Down Expand Up @@ -257,6 +258,19 @@ class AddressHandlerSolana : IAddressHandler {

}

class AddressHandlerZcash : IAddressHandler {
override val blockchainType = BlockchainType.Zcash

override fun isSupported(value: String): Boolean {
return ZcashAddressValidator.validate(value)
}

override fun parseAddress(value: String): Address {
return Address(value)
}

}

class AddressHandlerTron : IAddressHandler {
override val blockchainType: BlockchainType
get() = BlockchainType.Tron
Expand Down

0 comments on commit 259765f

Please sign in to comment.