Skip to content

Commit 47145ab

Browse files
committed
Make bip39 optional
1 parent cf3b92b commit 47145ab

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ This package requires `@multiversx/sdk-bls-wasm` for BLS (Boneh-Lynn-Shacham) cr
5050
npm install @multiversx/sdk-bls-wasm
5151
```
5252

53+
### bip39
54+
55+
This package provides mnemonic and seed generation functionality using `bip39`, but it is not bundled by default. If you plan to use mnemonic-related features, make sure to install this optional dependency:
56+
57+
```bash
58+
npm install bip39
59+
```
60+
5361
### Building the library
5462

5563
In order to compile the library, run the following:

package-lock.json

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@multiversx/sdk-core",
3-
"version": "13.10.0",
3+
"version": "13.11.0",
44
"description": "MultiversX SDK for JavaScript and TypeScript",
55
"author": "MultiversX",
66
"homepage": "https://multiversx.com",
@@ -40,7 +40,6 @@
4040
"@multiversx/sdk-transaction-decoder": "1.0.2",
4141
"json-bigint": "1.0.0",
4242
"bech32": "1.1.4",
43-
"bip39": "3.1.0",
4443
"blake2b": "2.1.3",
4544
"buffer": "6.0.3",
4645
"ed25519-hd-key": "1.1.2",
@@ -79,6 +78,7 @@
7978
},
8079
"optionalDependencies": {
8180
"axios": "^1.7.4",
82-
"@multiversx/sdk-bls-wasm": "0.3.5"
81+
"@multiversx/sdk-bls-wasm": "0.3.5",
82+
"bip39": "3.1.0"
8383
}
8484
}

src/wallet/mnemonic.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
import { entropyToMnemonic, generateMnemonic, mnemonicToEntropy, mnemonicToSeedSync, validateMnemonic } from "bip39";
21
import { derivePath } from "ed25519-hd-key";
32
import { ErrBadMnemonicEntropy, ErrWrongMnemonic } from "../errors";
43
import { UserSecretKey } from "./userKeys";
54

65
const MNEMONIC_STRENGTH = 256;
76
const BIP44_DERIVATION_PREFIX = "m/44'/508'/0'/0'";
87

8+
let bip39: any;
9+
10+
function loadBip39() {
11+
if (!bip39) {
12+
try {
13+
bip39 = require("bip39");
14+
} catch (error) {
15+
throw new Error("bip39 is required but not installed. Please install 'bip39' to use mnemonic features.");
16+
}
17+
}
18+
}
19+
920
export class Mnemonic {
1021
private readonly text: string;
1122

@@ -14,36 +25,41 @@ export class Mnemonic {
1425
}
1526

1627
static generate(): Mnemonic {
17-
const text = generateMnemonic(MNEMONIC_STRENGTH);
28+
loadBip39(); // Load bip39 when needed
29+
const text = bip39.generateMnemonic(MNEMONIC_STRENGTH);
1830
return new Mnemonic(text);
1931
}
2032

2133
static fromString(text: string) {
34+
loadBip39(); // Load bip39 when needed
2235
text = text.trim();
2336

2437
Mnemonic.assertTextIsValid(text);
2538
return new Mnemonic(text);
2639
}
2740

2841
static fromEntropy(entropy: Uint8Array): Mnemonic {
42+
loadBip39(); // Load bip39 when needed
2943
try {
30-
const text = entropyToMnemonic(Buffer.from(entropy));
44+
const text = bip39.entropyToMnemonic(Buffer.from(entropy));
3145
return new Mnemonic(text);
3246
} catch (err: any) {
3347
throw new ErrBadMnemonicEntropy(err);
3448
}
3549
}
3650

3751
public static assertTextIsValid(text: string) {
38-
let isValid = validateMnemonic(text);
52+
loadBip39(); // Load bip39 when needed
53+
let isValid = bip39.validateMnemonic(text);
3954

4055
if (!isValid) {
4156
throw new ErrWrongMnemonic();
4257
}
4358
}
4459

4560
deriveKey(addressIndex: number = 0, password: string = ""): UserSecretKey {
46-
let seed = mnemonicToSeedSync(this.text, password);
61+
loadBip39(); // Load bip39 when needed
62+
let seed = bip39.mnemonicToSeedSync(this.text, password);
4763
let derivationPath = `${BIP44_DERIVATION_PREFIX}/${addressIndex}'`;
4864
let derivationResult = derivePath(derivationPath, seed.toString("hex"));
4965
let key = derivationResult.key;
@@ -55,7 +71,8 @@ export class Mnemonic {
5571
}
5672

5773
getEntropy(): Uint8Array {
58-
const entropy = mnemonicToEntropy(this.text);
74+
loadBip39(); // Load bip39 when needed
75+
const entropy = bip39.mnemonicToEntropy(this.text);
5976
return Buffer.from(entropy, "hex");
6077
}
6178

0 commit comments

Comments
 (0)