Skip to main content
Last updated

Cryptography utilities

The @kadena/cryptography-utils library provides a collection of cryptographic utility functions. This library is used by the Kadena client—kadena.js—to hash your transactions. If you have secret keys available, you can also sign transactions using functions in this library.

The library includes the following utility functions for performing cryptographic operations:

Use this functionTo do this
binToHexConvert binary numbers to hexadecimal strings.
hexToBinConvert hexadecimal strings to binary numbers.
base64UrlEncodeEncode text as a Base64-encoded URL.
base64UrlDecodeConvert a Base64-encoded URL to text.
base64UrlEncodeArrEncode an array of text as Base64 values that can be used for URLs.
base64UrlDecodeArrDecode Base64 arrays into text.
strToUint8ArrayConvert a string to a Uint8Array.
uint8ArrayToStrConvert a Uint8Array to a string.
hashGenerate a cryptographic hash for a transaction.
hashBinConvert a string to a hashed binary using the BLAKE2b-256 hashing algorithm.
genKeyPairGenerate a random, cryptographically secure public and secret key using the ED25519 signature scheme.
restoreKeyPairFromSecretKeyRestore a key pair from a secret phrase.
signGenerate a hash using the BLAKE2b-256 hashing algorithm and sign a transaction with a public and secret key pair.
signHashSign a hashed message using a public and secret key pair.
verifySigVerify a signature.
uniqueConvert an array with IBase64Url values into an array with unique IBase64Url values.
toTweetNaclSecretKeyConvert a public and secret key pair into Uint8Array binary object.

You can find the generated API documentation for these utilities in crypto.api.md and cryptography-utils.api.md.

Examples

ts
import { hash, sign } from '@kadena/cryptography-utils';import { IKeyPair } from '@kadena/types'; // Create a commandlet commandPayload: string = 'Hello world!'; // Get the has of the commandlet h: string = hash(commandPayload); // Signing is normally handled by wallets, but if you have the private key// available to you, you can also sign in this way:let keyPair: IKeyPair = {  publicKey: 'ba54b224d1924dd98403f5c751abdd10de6cd81b0121800bf7bdbdcfaec7388d',  secretKey: '8693e641ae2bbe9ea802c736f42027b03f86afe63cae315e7169c9c496c17332',}; let sig: string = sign(commandPayload, keyPair);
ts
import { hash, sign } from '@kadena/cryptography-utils';import { IKeyPair } from '@kadena/types'; // Create a commandlet commandPayload: string = 'Hello world!'; // Get the has of the commandlet h: string = hash(commandPayload); // Signing is normally handled by wallets, but if you have the private key// available to you, you can also sign in this way:let keyPair: IKeyPair = {  publicKey: 'ba54b224d1924dd98403f5c751abdd10de6cd81b0121800bf7bdbdcfaec7388d',  secretKey: '8693e641ae2bbe9ea802c736f42027b03f86afe63cae315e7169c9c496c17332',}; let sig: string = sign(commandPayload, keyPair);