The @mavrykdynamics/taquito-utils
package provides developers with utility functions in Taquito.
You can find a full list of available utility functions in Taquito here
To use the functions, simply import the function you need as such:
import { getPkhfromPk , b58cencode , b58cdecode } from '@mavrykdynamics/taquito-utils' ;
const publicKeyHash = getPkhfromPk ( 'replace_with_publickey' ) ;
const encoded = b58cencode ( 'replace_with_publickey' ) ;
Copy 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 This function can be used to validate implicit addresses (mv1, mv2, mv3) 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 '@mavrykdynamics/taquito-utils' ;
const pkh = 'mv1ENbzDhD32LEfMzqsH3PC6SUv2y62tJCrH' ;
const validation = validateAddress ( pkh ) ;
println ( ` Calling the validateAddress function with ${ pkh } returns ${ validation } . ` ) ;
const invalidPkh = 'mv1NRLktBobVVU4Sy4Ab26DLTUAhUtxsWUHf' ;
const invalidValidation = validateAddress ( invalidPkh ) ;
println ( ` Calling the validateAddress function with ${ invalidPkh } returns ${ invalidValidation } . ` ) ;
Copy This function is used to validate implicit addresses (mv1, mv2, mv3).
Here is a valid example with a pkh and an invalid one where the prefix is missing :
import { validateKeyHash } from '@mavrykdynamics/taquito-utils' ;
const keyHash = 'mv1ENbzDhD32LEfMzqsH3PC6SUv2y62tJCrH' ;
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 This function is used to validate originated addresses (KT1).
Here is a valid example with the address of an existing contract :
import { validateContractAddress } from '@mavrykdynamics/taquito-utils' ;
const contractAddress = 'KT1AfxAKKLnEg6rQ6kHdvCWwagjSaxEwURSJ' ;
const validation = validateContractAddress ( contractAddress ) ;
println ( ` Calling the validateContractAddress function with ${ contractAddress } returns ${ validation } . ` ) ;
Copy 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 '@mavrykdynamics/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 The validatePublicKey
is used to check if a public key is valid.
import { validatePublicKey } from '@mavrykdynamics/taquito-utils' ;
const publicKey = 'edpkvS5QFv7KRGfa3b87gg9DBpxSm3NpSwnjhUjNBQrRUUR66F7C9g' ;
const validation = validatePublicKey ( publicKey ) ;
println ( ` Calling the validatePublicKey function with ${ publicKey } returns ${ validation } . ` ) ;
const value = 'mv1ENbzDhD32LEfMzqsH3PC6SUv2y62tJCrH' ;
const invalidValidation = validatePublicKey ( value ) ;
println ( ` Calling the validatePublicKey function with ${ value } returns ${ invalidValidation } . ` ) ;
Copy The validateSignature
function is used to check if a signature is valid.
import { validateSignature } from '@mavrykdynamics/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 The validateBlock
function is used to check whether a block hash is valid.
import { validateBlock } from '@mavrykdynamics/taquito-utils' ;
const block = 'BLJjnzaPtSsxykZ9pLTFLSfsKuiN3z7SjSPDPWwbE4Q68u5EpBw' ;
const validation = validateBlock ( block ) ;
println ( ` Calling the validateBlock function with ${ block } returns ${ validation } . ` ) ;
const invalidBlock = 'BMEdgRZbJJrtByoA5Jyuvy8mzp8mefbcrno82nQCAEbBCUhog' ;
const invalidValidation = validateBlock ( invalidBlock ) ;
println ( ` Calling the validateBlock function with ${ invalidBlock } returns ${ invalidValidation } . ` ) ;
Copy The validateOperation
function is used to check whether an operation hash is valid.
import { validateOperation } from '@mavrykdynamics/taquito-utils' ;
const operation = 'ood2Y1FLHH9izvYghVcDGGAkvJFo1CgSEjPfWvGsaz3qypCmeUj' ;
const validation = validateOperation ( operation ) ;
println ( ` Calling the validateOperation function with ${ operation } returns ${ validation } . ` ) ;
const invalidOperation = 'ont3n75kMA2xeoTdxkGM23h5XhWgyP51WEznc4zCDtGNz1TWSz' ;
const invalidValidation = validateOperation ( invalidOperation ) ;
println ( ` Calling the validateOperation function with ${ invalidOperation } returns ${ invalidValidation } . ` ) ;
Copy The validateProtocol
function is used to check whether a protocol hash is valid.
import { validateProtocol } from '@mavrykdynamics/taquito-utils' ;
const protocol = 'PtHangz2aRngywmSRGGvrcTyMbbdpWdpFKuS4uMWxg2RaH9i1qx' ;
const validation = validateProtocol ( protocol ) ;
println ( ` Calling the validateProtocol function with ${ protocol } returns ${ validation } . ` ) ;
const invalidProtocol = 'PsBABY5HQTSkA4297zNHfsZNKtxULfL18y95b3m53QJiXGmrbU' ;
const invalidValidation = validateProtocol ( invalidProtocol ) ;
println ( ` Calling the validateProtocol function with ${ invalidProtocol } 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 '@mavrykdynamics/taquito-utils' ;
const message = '03d0c10e3ed11d7c6e3357f6ef335bab9e8f2bd54d0ce20c482e241191a6e4b8ce6c01be917311d9ac46959750e405d57e268e2ed9e174a80794fbd504e12a4a000141eb3781afed2f69679ff2bbe1c5375950b0e40d00ff000000005e05050505050507070100000024747a32526773486e74516b72794670707352466261313652546656503539684b72654a4d07070100000024747a315a6672455263414c42776d4171776f6e525859565142445439426a4e6a42484a750001' ;
const pk = 'sppk7c7hkPj47yjYFEHX85q46sFJGw6RBrqoVSHwAJAT4e14KJwzoey' ;
const sig = 'spsig1cdLkp1RLgUHAp13aRFkZ6MQDPp7xCnjAExGL3MBSdMDmT6JgQSX8cufyDgJRM3sinFtiCzLbsyP6d365EHoNevxhT47nx'
const isValid = verifySignature ( message , pk , sig ) ;
println ( isValid ) ;
Copy