WebAssembly interface layer providing high-performance cryptographic functions for blockchain applications in the Polkadot ecosystem.
npx @tessl/cli install tessl/npm-polkadot--wasm-crypto@7.5.0@polkadot/wasm-crypto is a WebAssembly interface layer that provides high-performance cryptographic functions for blockchain applications in the Polkadot ecosystem. It wraps Rust-based cryptographic implementations compiled to WebAssembly, offering superior performance for cryptographic operations that are essential for blockchain applications but may not have pure JavaScript implementations (particularly sr25519).
npm install @polkadot/wasm-cryptoimport { waitReady, isReady, bip39Generate, sr25519Sign } from "@polkadot/wasm-crypto";For CommonJS:
const { waitReady, isReady, bip39Generate, sr25519Sign } = require("@polkadot/wasm-crypto");import { initWasm } from "@polkadot/wasm-crypto/initOnlyWasm";
import { initWasm } from "@polkadot/wasm-crypto/initWasmAsm";
import { initWasm } from "@polkadot/wasm-crypto/initOnlyAsm";
import { initWasm } from "@polkadot/wasm-crypto/initNone";import { waitReady, bip39Generate, sr25519KeypairFromSeed, sr25519Sign } from "@polkadot/wasm-crypto";
// Initialize WASM interface
await waitReady();
// Generate BIP39 mnemonic
const mnemonic = bip39Generate(12);
console.log("Mnemonic:", mnemonic);
// Create sr25519 keypair
const seed = new Uint8Array(32).fill(1); // Example seed
const keypair = sr25519KeypairFromSeed(seed);
// Sign a message
const message = new TextEncoder().encode("Hello world");
const signature = sr25519Sign(keypair.slice(32), keypair.slice(0, 32), message);@polkadot/wasm-crypto is built around several key components:
bridge) managing communication between JavaScript and WebAssemblywithWasm wrapper that ensures WASM initializationCore initialization functions and status checking for the WASM interface.
function waitReady(): Promise<boolean>;
function isReady(): boolean;BIP39 mnemonic phrase generation, validation, and derivation functions for seed phrase handling.
function bip39Generate(words: 12 | 15 | 18 | 21 | 24): string;
function bip39Validate(phrase: string): boolean;
function bip39ToSeed(phrase: string, password: string): Uint8Array;
function bip39ToMiniSecret(phrase: string, password: string): Uint8Array;
function bip39ToEntropy(phrase: string): Uint8Array;Sr25519 signature scheme implementation with keypair generation, signing, verification, and key derivation.
function sr25519KeypairFromSeed(seed: Uint8Array): Uint8Array;
function sr25519Sign(pubkey: Uint8Array, secret: Uint8Array, message: Uint8Array): Uint8Array;
function sr25519Verify(signature: Uint8Array, message: Uint8Array, pubkey: Uint8Array): boolean;
function sr25519DeriveKeypairHard(pair: Uint8Array, cc: Uint8Array): Uint8Array;
function sr25519DeriveKeypairSoft(pair: Uint8Array, cc: Uint8Array): Uint8Array;
function sr25519DerivePublicSoft(pubkey: Uint8Array, cc: Uint8Array): Uint8Array;
function sr25519Agree(pubkey: Uint8Array, secret: Uint8Array): Uint8Array;Ed25519 signature scheme implementation with keypair generation, signing, and verification.
function ed25519KeypairFromSeed(seed: Uint8Array): Uint8Array;
function ed25519Sign(pubkey: Uint8Array, seckey: Uint8Array, message: Uint8Array): Uint8Array;
function ed25519Verify(signature: Uint8Array, message: Uint8Array, pubkey: Uint8Array): boolean;Secp256k1 elliptic curve cryptography functions including key generation, signing, recovery, and key compression.
function secp256k1FromSeed(seckey: Uint8Array): Uint8Array;
function secp256k1Sign(msgHash: Uint8Array, seckey: Uint8Array): Uint8Array;
function secp256k1Recover(msgHash: Uint8Array, sig: Uint8Array, recovery: number): Uint8Array;
function secp256k1Compress(pubkey: Uint8Array): Uint8Array;
function secp256k1Expand(pubkey: Uint8Array): Uint8Array;Various cryptographic hash functions including SHA, BLAKE2b, Keccak, and HMAC variants.
function blake2b(data: Uint8Array, key: Uint8Array, size: number): Uint8Array;
function sha256(data: Uint8Array): Uint8Array;
function sha512(data: Uint8Array): Uint8Array;
function keccak256(data: Uint8Array): Uint8Array;
function keccak512(data: Uint8Array): Uint8Array;
function hmacSha256(key: Uint8Array, data: Uint8Array): Uint8Array;
function hmacSha512(key: Uint8Array, data: Uint8Array): Uint8Array;
function twox(data: Uint8Array, rounds: number): Uint8Array;Password-based key derivation functions for generating cryptographic keys from passwords.
function pbkdf2(data: Uint8Array, salt: Uint8Array, rounds: number): Uint8Array;
function scrypt(password: Uint8Array, salt: Uint8Array, log2n: number, r: number, p: number): Uint8Array;Verifiable Random Function implementation for generating and verifying cryptographically secure random values.
function vrfSign(secret: Uint8Array, context: Uint8Array, message: Uint8Array, extra: Uint8Array): Uint8Array;
function vrfVerify(pubkey: Uint8Array, context: Uint8Array, message: Uint8Array, extra: Uint8Array, outAndProof: Uint8Array): boolean;interface Bridge {
readonly wasm: WasmCryptoInstance | null;
readonly type: string;
}
interface PackageInfo {
readonly name: string;
readonly version: string;
readonly path: string;
readonly type: string;
}const bridge: Bridge;
const packageInfo: PackageInfo;All cryptographic functions throw an Error if the WASM interface has not been initialized:
"The WASM interface has not been initialized. Ensure that you wait for the initialization Promise with waitReady() from @polkadot/wasm-crypto (or cryptoWaitReady() from @polkadot/util-crypto) before attempting to use WASM-only interfaces."Always call await waitReady() before using any cryptographic functions to ensure proper initialization.