Taquito provides functions that allow us to see if an address, a chain, a key hash, a contract address, a public key, or a signature is valid. Note that these validations do not rely on a node but are done based on checksums. Thus, they allow us to check if a value is valid and not if it exists on a chain. The ValidationResult
returned by these functions is an enum that can take the following values:
0 = NO_PREFIX_MATCHED,
1 = INVALID_CHECKSUM,
2 = INVALID_LENGTH,
3 = VALID
Copy Validate an address# The validateAddress
function# This function can be used to validate implicit addresses (tz1, tz2, tz3) and originated addresses (KT1).
In the following example, the function is first called with a valid public key hash (pkh). It is then called with the same pkh where one character differs (e.g. 'p' instead of 'P'), which results in an invalid checksum.
import { validateAddress } from '@taquito/utils' ;
const pkh = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx' ;
const validation = validateAddress ( pkh ) ;
println ( ` Calling the validateAddress function with ${ pkh } returns ${ validation } . ` ) ;
const invalidPkh = 'tz1L9r8mWmRpndRhuvMCWESLGSVeFzQ9NAWx' ;
const invalidValidation = validateAddress ( invalidPkh ) ;
println ( ` Calling the validateAddress function with ${ invalidPkh } returns ${ invalidValidation } . ` ) ;
Copy The validateKeyHash
function# This function is used to validate implicit addresses (tz1, tz2, tz3).
Here is a valid example with a pkh and an invalid one where the prefix is missing :
import { validateKeyHash } from '@taquito/utils' ;
const keyHash = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx' ;
const validation = validateKeyHash ( keyHash ) ;
println ( ` Calling the validateKeyHash function with ${ keyHash } returns ${ validation } . ` ) ;
const keyHashWithoutPrefix = 'L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx' ;
const invalidValidation = validateKeyHash ( keyHashWithoutPrefix ) ;
println ( ` Calling the validateKeyHash function with ${ keyHash } returns ${ invalidValidation } . ` ) ;
Copy The validateContractAddress
function# This function is used to validate originated addresses (KT1).
Here is a valid example with the address of an existing contract :
import { validateContractAddress } from '@taquito/utils' ;
const contractAddress = 'KT1F7DYSa7fVTNScSDDVVokqmmytpJBB5bs9' ;
const validation = validateContractAddress ( contractAddress ) ;
println (
` Calling the validateContractAddress function with ${ contractAddress } returns ${ validation } . `
) ;
Copy Validate a chain# The validateChain
function is used to validate a chain id.
The following example shows a valid result when using the mainnet chain id and an invalid result if the prefix is missing :
import { validateChain } from '@taquito/utils' ;
const chainId = 'NetXdQprcVkpaWU' ;
const validation = validateChain ( chainId ) ;
println ( ` Calling the validateChain function with ${ chainId } returns ${ validation } . ` ) ;
const chainIdWithoutPrefix = 'XdQprcVkpaWU' ;
const invalidValidation = validateChain ( chainIdWithoutPrefix ) ;
println (
` Calling the validateChain function with ${ chainIdWithoutPrefix } returns ${ invalidValidation } . `
) ;
Copy Validate a public key# The validatePublicKey
is used to check if a public key is valid.
import { validatePublicKey } from '@taquito/utils' ;
const publicKey = 'edpkvS5QFv7KRGfa3b87gg9DBpxSm3NpSwnjhUjNBQrRUUR66F7C9g' ;
const validation = validatePublicKey ( publicKey ) ;
println ( ` Calling the validatePublicKey function with ${ publicKey } returns ${ validation } . ` ) ;
const value = 'tz1L9r8mWmRPndRhuvMCWESLGSVeFzQ9NAWx' ;
const invalidValidation = validatePublicKey ( value ) ;
println ( ` Calling the validatePublicKey function with ${ value } returns ${ invalidValidation } . ` ) ;
Copy Validate a signature# The validateSignature
function is used to check if a signature is valid.
import { validateSignature } from '@taquito/utils' ;
const signature =
'edsigtkpiSSschcaCt9pUVrpNPf7TTcgvgDEDD6NCEHMy8NNQJCGnMfLZzYoQj74yLjo9wx6MPVV29CvVzgi7qEcEUok3k7AuMg' ;
const validation = validateSignature ( signature ) ;
println ( ` Calling the validateSignature function with ${ signature } returns ${ validation } . ` ) ;
const invalidSignature =
'edsigtkpiSSschcaCt9pUVrpNPf7TTcgvgDEDD6NCEHMy8NNQJCGnMfLZzYoQj74yLjo9wx6MPVV29CvVzgi7qEcEUok3k7AuM' ;
const invalidValidation = validateSignature ( invalidSignature ) ;
println (
` Calling the validateSignature function with ${ invalidSignature } returns ${ invalidValidation } . `
) ;
Copy