0
# BIP39 Mnemonic Operations
1
2
BIP39 mnemonic phrase generation, validation, and derivation functions for seed phrase handling in cryptographic applications.
3
4
## Capabilities
5
6
### Generate Mnemonic Phrase
7
8
Generates a new BIP39 mnemonic phrase with the specified number of words.
9
10
```typescript { .api }
11
/**
12
* Generates a new BIP39 mnemonic phrase
13
* @param words - Number of words in the mnemonic (12, 15, 18, 21, or 24)
14
* @returns Generated mnemonic phrase as a string
15
*/
16
function bip39Generate(words: 12 | 15 | 18 | 21 | 24): string;
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { waitReady, bip39Generate } from "@polkadot/wasm-crypto";
23
24
await waitReady();
25
26
// Generate different length mnemonics
27
const mnemonic12 = bip39Generate(12);
28
const mnemonic24 = bip39Generate(24);
29
30
console.log("12-word mnemonic:", mnemonic12);
31
console.log("24-word mnemonic:", mnemonic24);
32
```
33
34
### Validate Mnemonic Phrase
35
36
Validates whether a mnemonic phrase conforms to BIP39 standards.
37
38
```typescript { .api }
39
/**
40
* Validates a BIP39 mnemonic phrase
41
* @param phrase - The mnemonic phrase to validate
42
* @returns true if the phrase is valid, false otherwise
43
*/
44
function bip39Validate(phrase: string): boolean;
45
```
46
47
**Usage Example:**
48
49
```typescript
50
import { waitReady, bip39Generate, bip39Validate } from "@polkadot/wasm-crypto";
51
52
await waitReady();
53
54
const mnemonic = bip39Generate(12);
55
const isValid = bip39Validate(mnemonic);
56
console.log("Is valid:", isValid); // true
57
58
const invalid = bip39Validate("invalid mnemonic phrase");
59
console.log("Is invalid:", invalid); // false
60
```
61
62
### Convert Mnemonic to Seed
63
64
Converts a BIP39 mnemonic phrase to a seed using PBKDF2 derivation.
65
66
```typescript { .api }
67
/**
68
* Converts a BIP39 mnemonic phrase to seed
69
* @param phrase - The BIP39 mnemonic phrase
70
* @param password - Optional password for seed derivation (use empty string if none)
71
* @returns 64-byte seed as Uint8Array
72
*/
73
function bip39ToSeed(phrase: string, password: string): Uint8Array;
74
```
75
76
**Usage Example:**
77
78
```typescript
79
import { waitReady, bip39Generate, bip39ToSeed } from "@polkadot/wasm-crypto";
80
81
await waitReady();
82
83
const mnemonic = bip39Generate(12);
84
const seed = bip39ToSeed(mnemonic, ""); // No password
85
const seedWithPassword = bip39ToSeed(mnemonic, "my-passphrase");
86
87
console.log("Seed length:", seed.length); // 64
88
```
89
90
### Convert Mnemonic to Mini Secret
91
92
Converts a BIP39 mnemonic phrase to a mini secret using entropy derivation.
93
94
```typescript { .api }
95
/**
96
* Converts a BIP39 mnemonic phrase to mini secret
97
* @param phrase - The BIP39 mnemonic phrase
98
* @param password - Password for mini secret derivation (use empty string if none)
99
* @returns 32-byte mini secret as Uint8Array
100
*/
101
function bip39ToMiniSecret(phrase: string, password: string): Uint8Array;
102
```
103
104
**Usage Example:**
105
106
```typescript
107
import { waitReady, bip39Generate, bip39ToMiniSecret } from "@polkadot/wasm-crypto";
108
109
await waitReady();
110
111
const mnemonic = bip39Generate(12);
112
const miniSecret = bip39ToMiniSecret(mnemonic, "");
113
114
console.log("Mini secret length:", miniSecret.length); // 32
115
```
116
117
### Convert Mnemonic to Entropy
118
119
Converts a BIP39 mnemonic phrase back to its original entropy bytes.
120
121
```typescript { .api }
122
/**
123
* Converts a BIP39 mnemonic phrase to entropy
124
* @param phrase - The BIP39 mnemonic phrase
125
* @returns Entropy bytes as Uint8Array (length varies based on mnemonic length)
126
*/
127
function bip39ToEntropy(phrase: string): Uint8Array;
128
```
129
130
**Usage Example:**
131
132
```typescript
133
import { waitReady, bip39Generate, bip39ToEntropy } from "@polkadot/wasm-crypto";
134
135
await waitReady();
136
137
const mnemonic12 = bip39Generate(12);
138
const mnemonic24 = bip39Generate(24);
139
140
const entropy12 = bip39ToEntropy(mnemonic12);
141
const entropy24 = bip39ToEntropy(mnemonic24);
142
143
console.log("12-word entropy length:", entropy12.length); // 16
144
console.log("24-word entropy length:", entropy24.length); // 32
145
```
146
147
## Complete Workflow Example
148
149
```typescript
150
import {
151
waitReady,
152
bip39Generate,
153
bip39Validate,
154
bip39ToSeed,
155
bip39ToMiniSecret
156
} from "@polkadot/wasm-crypto";
157
158
async function demonstrateBip39() {
159
// Initialize WASM
160
await waitReady();
161
162
// Generate and validate mnemonic
163
const mnemonic = bip39Generate(12);
164
console.log("Generated mnemonic:", mnemonic);
165
166
const isValid = bip39Validate(mnemonic);
167
console.log("Is valid:", isValid);
168
169
// Derive different secrets
170
const seed = bip39ToSeed(mnemonic, "");
171
const miniSecret = bip39ToMiniSecret(mnemonic, "");
172
173
console.log("Seed:", seed);
174
console.log("Mini secret:", miniSecret);
175
176
return { mnemonic, seed, miniSecret };
177
}
178
179
demonstrateBip39();
180
```