CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-polkadot--util-crypto

A collection of useful crypto utilities for Polkadot ecosystem projects

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

address.mddocs/

Address Management

SS58 address encoding, decoding, and validation with support for multi-signature and derived addresses. The SS58 format is used across the Polkadot ecosystem for human-readable addresses.

Capabilities

Encode Address

Encodes a public key to an SS58 address format with optional network prefix.

/**
 * Encode a public key or address to SS58 format
 * @param key - The public key or address to encode
 * @param ss58Format - Network prefix (default: generic Substrate)
 * @returns SS58-encoded address string
 */
function encodeAddress(key: string | Uint8Array, ss58Format?: number): string;

Usage Example:

import { encodeAddress, sr25519PairFromSeed } from "@polkadot/util-crypto";

const seed = new Uint8Array(32); // Your seed
const pair = sr25519PairFromSeed(seed);

// Generic Substrate address (prefix 42)
const genericAddress = encodeAddress(pair.publicKey);

// Polkadot address (prefix 0)
const polkadotAddress = encodeAddress(pair.publicKey, 0);

// Kusama address (prefix 2)
const kusamaAddress = encodeAddress(pair.publicKey, 2);

Decode Address

Decodes an SS58 address to extract the public key bytes.

/**
 * Decode an SS58 address to public key bytes
 * @param address - SS58 address string to decode
 * @param ignoreChecksum - Skip checksum validation (default: false)
 * @param prefix - Expected network prefix for validation
 * @returns Public key as Uint8Array (32 bytes)
 */
function decodeAddress(address: string, ignoreChecksum?: boolean, prefix?: number): Uint8Array;

Usage Example:

import { decodeAddress } from "@polkadot/util-crypto";

const address = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
const publicKey = decodeAddress(address);
console.log(publicKey.length); // 32

Check Address

Validates an SS58 address format and checksum.

/**
 * Check if an SS58 address is valid
 * @param address - Address string to validate
 * @param prefix - Expected network prefix for validation
 * @returns Tuple of [isValid, errorMessage]
 */
function checkAddress(address: string, prefix?: number): [boolean, string | null];

Usage Example:

import { checkAddress } from "@polkadot/util-crypto";

const [isValid, error] = checkAddress("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY");
if (isValid) {
  console.log("Valid address");
} else {
  console.log("Invalid address:", error);
}

Address Equality

Compares two addresses for equality, handling different formats.

/**
 * Compare two addresses for equality
 * @param a - First address
 * @param b - Second address
 * @returns true if addresses represent the same public key
 */
function addressEq(a: string, b: string): boolean;

Is Address

Checks if a string is a valid SS58 address format.

/**
 * Check if string is a valid SS58 address
 * @param address - String to check
 * @param ignoreChecksum - Skip checksum validation
 * @param prefix - Expected network prefix
 * @returns true if valid address format
 */
function isAddress(address: string, ignoreChecksum?: boolean, prefix?: number): boolean;

Validate Address

Validates an SS58 address and returns detailed information.

/**
 * Validate SS58 address with detailed response
 * @param address - Address to validate
 * @param prefix - Expected network prefix
 * @returns Validation result with details
 */
function validateAddress(address: string, prefix?: number): boolean;

Address Checksum

Validates the checksum portion of an SS58 address.

/**
 * Check SS58 address checksum validity
 * @param address - Address to check
 * @returns true if checksum is valid
 */
function checkAddressChecksum(address: string): boolean;

Derive Address

Derives an SS58 address directly from a public key.

/**
 * Derive SS58 address from public key
 * @param publicKey - Public key bytes
 * @param prefix - Network prefix
 * @returns SS58 address string
 */
function deriveAddress(publicKey: Uint8Array, prefix?: number): string;

Multi-Signature Addresses

Creates multi-signature addresses from threshold and signatories.

/**
 * Create multi-signature key and address
 * @param threshold - Minimum required signatures
 * @param signatories - Array of public keys or addresses
 * @param prefix - Network prefix
 * @returns Multi-signature public key
 */
function createKeyMulti(threshold: number, signatories: (Uint8Array | string)[], prefix?: number): Uint8Array;

/**
 * Encode multi-signature address
 * @param who - Threshold and signatories or multi-sig public key
 * @param prefix - Network prefix
 * @returns Multi-signature address
 */
function encodeMultiAddress(who: Uint8Array | [number, (Uint8Array | string)[]], prefix?: number): string;

Derived Keys and Addresses

Creates derived keys and addresses for hierarchical key management.

/**
 * Create derived key from public key and chain code
 * @param publicKey - Parent public key
 * @param chainCode - Chain code for derivation
 * @param prefix - Network prefix
 * @returns Derived public key
 */
function createKeyDerived(publicKey: Uint8Array, chainCode: Uint8Array, prefix?: number): Uint8Array;

/**
 * Encode derived address
 * @param publicKey - Public key
 * @param chainCode - Chain code
 * @param prefix - Network prefix
 * @returns Derived address
 */
function encodeDerivedAddress(publicKey: Uint8Array, chainCode: Uint8Array, prefix?: number): string;

Address Utilities

Additional utilities for address handling.

/**
 * Sort addresses array
 * @param addresses - Array of addresses to sort
 * @param prefix - Network prefix
 * @returns Sorted addresses array
 */
function sortAddresses(addresses: string[], prefix?: number): string[];

/**
 * Set global SS58 format (deprecated)
 * @param prefix - Default network prefix
 */
function setSS58Format(prefix: number): void;

EVM Compatibility

Functions for converting between SS58 and Ethereum addresses.

/**
 * Convert SS58 address to EVM format
 * @param address - SS58 address
 * @returns EVM address bytes
 */
function addressToEvm(address: string): Uint8Array;

/**
 * Convert EVM address to SS58 format
 * @param evmAddress - EVM address bytes
 * @param prefix - SS58 network prefix
 * @param hashType - Hash algorithm to use
 * @returns SS58 address
 */
function evmToAddress(evmAddress: Uint8Array, prefix?: number, hashType?: 'blake2' | 'keccak'): string;

Common Network Prefixes

  • 0: Polkadot
  • 1: Bare Sr25519
  • 2: Kusama
  • 5: Astar
  • 42: Generic Substrate (default)
  • 1284: Moonbeam
  • 1285: Moonriver

Notes

  • All public keys should be 32 bytes for sr25519/ed25519 or 33 bytes for secp256k1
  • Invalid addresses will throw errors during decoding
  • Checksum validation helps prevent typos in addresses
  • Multi-signature addresses require careful threshold management

Install with Tessl CLI

npx tessl i tessl/npm-polkadot--util-crypto

docs

address.md

base-encoding.md

crypto-init.md

ethereum.md

hashing.md

index.md

json-encryption.md

key-derivation-pbkdf.md

key-derivation.md

keypairs.md

mnemonic.md

random.md

signatures.md

tile.json