A collection of useful crypto utilities for Polkadot ecosystem projects
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Key pair generation and management for sr25519, ed25519, and secp256k1 cryptographic schemes. Each scheme offers different properties and use cases within the Polkadot ecosystem.
Sr25519 is the primary signature scheme used in Polkadot for account keys and validator signing.
/**
* Generate sr25519 key pair from seed
* @param seed - 32-byte seed for key generation
* @returns Keypair with public and secret keys
*/
function sr25519PairFromSeed(seed: Uint8Array): Keypair;Usage Example:
import { sr25519PairFromSeed, mnemonicToMiniSecret, mnemonicGenerate } from "@polkadot/util-crypto";
// From mnemonic
const mnemonic = mnemonicGenerate();
const seed = mnemonicToMiniSecret(mnemonic);
const pair = sr25519PairFromSeed(seed);
// From custom seed
const customSeed = new Uint8Array(32);
crypto.getRandomValues(customSeed);
const customPair = sr25519PairFromSeed(customSeed);
console.log(pair.publicKey.length); // 32
console.log(pair.secretKey.length); // 64Ed25519 provides fast signing and verification with smaller signatures.
/**
* Generate ed25519 key pair from seed
* @param seed - 32-byte seed for key generation
* @returns Keypair with public and secret keys
*/
function ed25519PairFromSeed(seed: Uint8Array): Keypair;
/**
* Generate ed25519 key pair from secret key
* @param secretKey - 32-byte secret key
* @returns Keypair with public and secret keys
*/
function ed25519PairFromSecret(secretKey: Uint8Array): Keypair;
/**
* Generate random ed25519 key pair
* @returns Keypair with random keys
*/
function ed25519PairFromRandom(): Keypair;
/**
* Generate ed25519 key pair from string with optional passphrase
* @param value - String value to derive from
* @param passphrase - Optional passphrase
* @returns Keypair derived from string
*/
function ed25519PairFromString(value: string, passphrase?: string): Keypair;Usage Example:
import {
ed25519PairFromSeed,
ed25519PairFromRandom,
ed25519PairFromString
} from "@polkadot/util-crypto";
// From seed
const seed = new Uint8Array(32);
const pair = ed25519PairFromSeed(seed);
// Random generation
const randomPair = ed25519PairFromRandom();
// From string
const stringPair = ed25519PairFromString("Alice", "optional-passphrase");Secp256k1 is used for Ethereum compatibility and Bitcoin-style signatures.
/**
* Generate secp256k1 key pair from seed
* @param seed - 32-byte seed for key generation
* @returns Keypair with public and secret keys
*/
function secp256k1PairFromSeed(seed: Uint8Array): Keypair;Usage Example:
import { secp256k1PairFromSeed } from "@polkadot/util-crypto";
const seed = new Uint8Array(32);
const pair = secp256k1PairFromSeed(seed);
console.log(pair.publicKey.length); // 33 (compressed) or 65 (uncompressed)
console.log(pair.secretKey.length); // 32Install with Tessl CLI
npx tessl i tessl/npm-polkadot--util-crypto