CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-polkadot--wasm-crypto

WebAssembly interface layer providing high-performance cryptographic functions for blockchain applications in the Polkadot ecosystem.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

index.mddocs/

@polkadot/wasm-crypto

@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).

Package Information

  • Package Name: @polkadot/wasm-crypto
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @polkadot/wasm-crypto

Core Imports

import { waitReady, isReady, bip39Generate, sr25519Sign } from "@polkadot/wasm-crypto";

For CommonJS:

const { waitReady, isReady, bip39Generate, sr25519Sign } = require("@polkadot/wasm-crypto");

Specialized Initialization Imports

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";

Basic Usage

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

Architecture

@polkadot/wasm-crypto is built around several key components:

  • Bridge System: Global bridge interface (bridge) managing communication between JavaScript and WebAssembly
  • WASM Wrapper Functions: All cryptographic functions use a withWasm wrapper that ensures WASM initialization
  • Initialization Strategies: Multiple initialization modules supporting different deployment scenarios (WASM-only, ASM.js fallback, automatic detection)
  • Type Safety: Full TypeScript integration with proper type definitions for all cryptographic operations
  • Error Handling: Consistent error handling for uninitialized WASM interface

Capabilities

Initialization and Status

Core initialization functions and status checking for the WASM interface.

function waitReady(): Promise<boolean>;
function isReady(): boolean;

BIP39 Mnemonic Operations

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;

BIP39 Operations

Sr25519 Digital Signatures

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;

Sr25519 Signatures

Ed25519 Digital Signatures

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;

Ed25519 Signatures

Secp256k1 ECDSA Operations

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;

Secp256k1 Operations

Cryptographic Hashing

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;

Hashing Functions

Key Derivation Functions

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;

Key Derivation

VRF (Verifiable Random Functions)

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;

VRF Functions

Core Types

interface Bridge {
  readonly wasm: WasmCryptoInstance | null;
  readonly type: string;
}

interface PackageInfo {
  readonly name: string;
  readonly version: string;
  readonly path: string;
  readonly type: string;
}

Global Objects

const bridge: Bridge;
const packageInfo: PackageInfo;

Error Handling

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.

Install with Tessl CLI

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

docs

bip39.md

ed25519.md

hashing.md

index.md

key-derivation.md

secp256k1.md

sr25519.md

vrf.md

tile.json