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

bip39.mddocs/

BIP39 Mnemonic Operations

BIP39 mnemonic phrase generation, validation, and derivation functions for seed phrase handling in cryptographic applications.

Capabilities

Generate Mnemonic Phrase

Generates a new BIP39 mnemonic phrase with the specified number of words.

/**
 * Generates a new BIP39 mnemonic phrase
 * @param words - Number of words in the mnemonic (12, 15, 18, 21, or 24)
 * @returns Generated mnemonic phrase as a string
 */
function bip39Generate(words: 12 | 15 | 18 | 21 | 24): string;

Usage Example:

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

await waitReady();

// Generate different length mnemonics
const mnemonic12 = bip39Generate(12);
const mnemonic24 = bip39Generate(24);

console.log("12-word mnemonic:", mnemonic12);
console.log("24-word mnemonic:", mnemonic24);

Validate Mnemonic Phrase

Validates whether a mnemonic phrase conforms to BIP39 standards.

/**
 * Validates a BIP39 mnemonic phrase
 * @param phrase - The mnemonic phrase to validate
 * @returns true if the phrase is valid, false otherwise
 */
function bip39Validate(phrase: string): boolean;

Usage Example:

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

await waitReady();

const mnemonic = bip39Generate(12);
const isValid = bip39Validate(mnemonic);
console.log("Is valid:", isValid); // true

const invalid = bip39Validate("invalid mnemonic phrase");
console.log("Is invalid:", invalid); // false

Convert Mnemonic to Seed

Converts a BIP39 mnemonic phrase to a seed using PBKDF2 derivation.

/**
 * Converts a BIP39 mnemonic phrase to seed
 * @param phrase - The BIP39 mnemonic phrase
 * @param password - Optional password for seed derivation (use empty string if none)
 * @returns 64-byte seed as Uint8Array
 */
function bip39ToSeed(phrase: string, password: string): Uint8Array;

Usage Example:

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

await waitReady();

const mnemonic = bip39Generate(12);
const seed = bip39ToSeed(mnemonic, ""); // No password
const seedWithPassword = bip39ToSeed(mnemonic, "my-passphrase");

console.log("Seed length:", seed.length); // 64

Convert Mnemonic to Mini Secret

Converts a BIP39 mnemonic phrase to a mini secret using entropy derivation.

/**
 * Converts a BIP39 mnemonic phrase to mini secret
 * @param phrase - The BIP39 mnemonic phrase
 * @param password - Password for mini secret derivation (use empty string if none)
 * @returns 32-byte mini secret as Uint8Array
 */
function bip39ToMiniSecret(phrase: string, password: string): Uint8Array;

Usage Example:

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

await waitReady();

const mnemonic = bip39Generate(12);
const miniSecret = bip39ToMiniSecret(mnemonic, "");

console.log("Mini secret length:", miniSecret.length); // 32

Convert Mnemonic to Entropy

Converts a BIP39 mnemonic phrase back to its original entropy bytes.

/**
 * Converts a BIP39 mnemonic phrase to entropy
 * @param phrase - The BIP39 mnemonic phrase
 * @returns Entropy bytes as Uint8Array (length varies based on mnemonic length)
 */
function bip39ToEntropy(phrase: string): Uint8Array;

Usage Example:

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

await waitReady();

const mnemonic12 = bip39Generate(12);
const mnemonic24 = bip39Generate(24);

const entropy12 = bip39ToEntropy(mnemonic12);
const entropy24 = bip39ToEntropy(mnemonic24);

console.log("12-word entropy length:", entropy12.length); // 16
console.log("24-word entropy length:", entropy24.length); // 32

Complete Workflow Example

import { 
  waitReady, 
  bip39Generate, 
  bip39Validate, 
  bip39ToSeed, 
  bip39ToMiniSecret 
} from "@polkadot/wasm-crypto";

async function demonstrateBip39() {
  // Initialize WASM
  await waitReady();
  
  // Generate and validate mnemonic
  const mnemonic = bip39Generate(12);
  console.log("Generated mnemonic:", mnemonic);
  
  const isValid = bip39Validate(mnemonic);
  console.log("Is valid:", isValid);
  
  // Derive different secrets
  const seed = bip39ToSeed(mnemonic, "");
  const miniSecret = bip39ToMiniSecret(mnemonic, "");
  
  console.log("Seed:", seed);
  console.log("Mini secret:", miniSecret);
  
  return { mnemonic, seed, miniSecret };
}

demonstrateBip39();

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