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

keypairs.mddocs/

Key Pair Generation

Key pair generation and management for sr25519, ed25519, and secp256k1 cryptographic schemes. Each scheme offers different properties and use cases within the Polkadot ecosystem.

Capabilities

Sr25519 Key Pairs

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); // 64

Ed25519 Key Pairs

Ed25519 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 Key Pairs

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); // 32

Key Pair Properties

Sr25519 Properties

  • Public Key: 32 bytes
  • Secret Key: 64 bytes (32-byte seed + 32-byte nonce)
  • Features: Supports soft derivation, VRF capabilities
  • Use Cases: Account keys, validator signing, session keys

Ed25519 Properties

  • Public Key: 32 bytes
  • Secret Key: 32 bytes (seed) or 64 bytes (expanded)
  • Features: Fast verification, deterministic signatures
  • Use Cases: High-performance signing, identity verification

Secp256k1 Properties

  • Public Key: 33 bytes (compressed) or 65 bytes (uncompressed)
  • Secret Key: 32 bytes
  • Features: Ethereum compatibility, ECDH support
  • Use Cases: Ethereum integration, cross-chain compatibility

Security Considerations

  • Always use cryptographically secure random seeds
  • Store secret keys securely and never expose them
  • Use proper key derivation for hierarchical key management
  • Validate public keys before use in cryptographic operations
  • Consider the trade-offs between different signature schemes

Performance Notes

  • Sr25519: Moderate signing speed, fast verification
  • Ed25519: Very fast signing and verification
  • Secp256k1: Slower operations but broad compatibility
  • All schemes support batch verification for multiple signatures

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