Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bip39 optional #517

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ This package requires `@multiversx/sdk-bls-wasm` for BLS (Boneh-Lynn-Shacham) cr
npm install @multiversx/sdk-bls-wasm
```

### bip39

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:

```bash
npm install bip39
```

### Building the library

In order to compile the library, run the following:
Expand Down
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-core",
"version": "13.10.0",
"version": "13.12.0",
"description": "MultiversX SDK for JavaScript and TypeScript",
"author": "MultiversX",
"homepage": "https://multiversx.com",
Expand Down Expand Up @@ -40,7 +40,6 @@
"@multiversx/sdk-transaction-decoder": "1.0.2",
"json-bigint": "1.0.0",
"bech32": "1.1.4",
"bip39": "3.1.0",
"blake2b": "2.1.3",
"buffer": "6.0.3",
"ed25519-hd-key": "1.1.2",
Expand Down Expand Up @@ -79,6 +78,7 @@
},
"optionalDependencies": {
"axios": "^1.7.4",
"@multiversx/sdk-bls-wasm": "0.3.5"
"@multiversx/sdk-bls-wasm": "0.3.5",
"bip39": "3.1.0"
}
}
30 changes: 24 additions & 6 deletions src/wallet/mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { entropyToMnemonic, generateMnemonic, mnemonicToEntropy, mnemonicToSeedSync, validateMnemonic } from "bip39";
import { derivePath } from "ed25519-hd-key";
import { ErrBadMnemonicEntropy, ErrWrongMnemonic } from "../errors";
import { UserSecretKey } from "./userKeys";

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

let bip39: any;

// Load bip39 when needed
function loadBip39() {
if (!bip39) {
try {
bip39 = require("bip39");
} catch (error) {
throw new Error("bip39 is required but not installed. Please install 'bip39' to use mnemonic features.");
}
}
}

export class Mnemonic {
private readonly text: string;

Expand All @@ -14,36 +26,41 @@ export class Mnemonic {
}

static generate(): Mnemonic {
const text = generateMnemonic(MNEMONIC_STRENGTH);
loadBip39();
const text = bip39.generateMnemonic(MNEMONIC_STRENGTH);
return new Mnemonic(text);
}

static fromString(text: string) {
loadBip39();
text = text.trim();

Mnemonic.assertTextIsValid(text);
return new Mnemonic(text);
}

static fromEntropy(entropy: Uint8Array): Mnemonic {
loadBip39();
try {
const text = entropyToMnemonic(Buffer.from(entropy));
const text = bip39.entropyToMnemonic(Buffer.from(entropy));
return new Mnemonic(text);
} catch (err: any) {
throw new ErrBadMnemonicEntropy(err);
}
}

public static assertTextIsValid(text: string) {
let isValid = validateMnemonic(text);
loadBip39();
let isValid = bip39.validateMnemonic(text);

if (!isValid) {
throw new ErrWrongMnemonic();
}
}

deriveKey(addressIndex: number = 0, password: string = ""): UserSecretKey {
let seed = mnemonicToSeedSync(this.text, password);
loadBip39();
let seed = bip39.mnemonicToSeedSync(this.text, password);
let derivationPath = `${BIP44_DERIVATION_PREFIX}/${addressIndex}'`;
let derivationResult = derivePath(derivationPath, seed.toString("hex"));
let key = derivationResult.key;
Expand All @@ -55,7 +72,8 @@ export class Mnemonic {
}

getEntropy(): Uint8Array {
const entropy = mnemonicToEntropy(this.text);
loadBip39();
const entropy = bip39.mnemonicToEntropy(this.text);
return Buffer.from(entropy, "hex");
}

Expand Down
Loading