Skip to main content
Version: 20.0.0

Taquito Utils

Description

The @taquito/utils package provides developers with utility functions in Taquito.

You can find a full list of available utility functions in Taquito here

Usage Example

To use the functions, simply import the function you need as such:

import { getPkhfromPk, b58cencode, b58cdecode } from '@taquito/utils';
const publicKeyHash = getPkhfromPk('replace_with_publickey');
const encoded = b58cencode('replace_with_publickey');

Using Validation Functions from @taquito/utils

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

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.

Live Editor
Result

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 :

Live Editor
Result

The validateContractAddress function

This function is used to validate originated addresses (KT1).

Here is a valid example with the address of an existing contract :

Live Editor
Result

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 :

Live Editor
Result

Validate a public key

The validatePublicKey is used to check if a public key is valid.

Live Editor
Result

Validate a signature

The validateSignature function is used to check if a signature is valid.

Live Editor
Result

Validate a Block Hash

The validateBlock function is used to check whether a block hash is valid.

Live Editor
Result

Validate an Operation Hash

The validateOperation function is used to check whether an operation hash is valid.

Live Editor
Result

Validate a Protocol Hash

The validateProtocol function is used to check whether a protocol hash is valid.

Live Editor
Result

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:

Live Editor
Result

Provide detailed feedback