Skip to content

Commit

Permalink
AND-5509 Moved Near address validation to BlockchainSDK
Browse files Browse the repository at this point in the history
Signed-off-by: Maqsudjon Ismoilov <[email protected]>
  • Loading branch information
iMaks99 committed Dec 14, 2023
1 parent 005e0a3 commit ffc3b55
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,32 @@ class NearWalletManager(
}
}

suspend fun validateAddress(address: String): Boolean {
// implicit address validation
if (address.length == NEAR_IMPLICIT_ADDRESS_LENGTH && hexRegex.matches(address)) {
return wallet.blockchain.validateAddress(address)
}

// named address validation
if (address.length in NEAR_MIN_ADDRESS_LENGTH until NEAR_IMPLICIT_ADDRESS_LENGTH &&
nearAddressRegex.matches(address)
) {
val result = getAccount(address)
return result is Result.Success && result.data is NearAccount.Full
}

return false
}

suspend fun getAccount(address: String): Result<NearAccount> {
return networkService.getAccount(address)
}

companion object {
private const val IMPLICIT_ACCOUNT_ADDRESS_LENGTH = 64
private const val NEAR_MIN_ADDRESS_LENGTH = 2
private const val NEAR_IMPLICIT_ADDRESS_LENGTH = 64
private val hexRegex = Regex("^[0-9a-f]+$")
private val nearAddressRegex = Regex("^(([a-z\\d]+[\\-_])*[a-z\\d]+\\.)*([a-z\\d]+[\\-_])*[a-z\\d]+\$")
}
}

0 comments on commit ffc3b55

Please sign in to comment.