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 = 'KT1AfxAKKLnEg6rQ6kHdvCWwagjSaxEwURSJ' ;
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 Verification of a signature Taquito provides a function named verifySignature
that allows verifying signatures of payloads. The function takes a message, a public key, and a signature as parameters and returns a boolean indicating if the signature matches.
Here is an example of a successful verification:
import { verifySignature } from '@taquito/utils' ;
const message = '03d0c10e3ed11d7c6e3357f6ef335bab9e8f2bd54d0ce20c482e241191a6e4b8ce6c01be917311d9ac46959750e405d57e268e2ed9e174a80794fbd504e12a4a000141eb3781afed2f69679ff2bbe1c5375950b0e40d00ff000000005e05050505050507070100000024747a32526773486e74516b72794670707352466261313652546656503539684b72654a4d07070100000024747a315a6672455263414c42776d4171776f6e525859565142445439426a4e6a42484a750001' ;
const pk = 'sppk7c7hkPj47yjYFEHX85q46sFJGw6RBrqoVSHwAJAT4e14KJwzoey' ;
const sig = 'spsig1cdLkp1RLgUHAp13aRFkZ6MQDPp7xCnjAExGL3MBSdMDmT6JgQSX8cufyDgJRM3sinFtiCzLbsyP6d365EHoNevxhT47nx'
const isValid = verifySignature ( message , pk , sig ) ;
println ( isValid ) ;
;
Copy