Skip to content

Commit

Permalink
Merge branch 'feat/unify' into TOOL-276-implement-facade
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Oct 23, 2024
2 parents af35e6a + 78b94b3 commit d4fbf59
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
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.11.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"
}
}
2 changes: 0 additions & 2 deletions src/networkProviders/providers.dev.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,6 @@ describe("test network providers on devnet: Proxy and API", function () {
guardian: "",
guardianSignature: new Uint8Array(),
options: 0,
relayer: "",
innerTransactions: [],
};

const apiLegacyTxHash = await apiProvider.sendTransaction(transaction);
Expand Down
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

0 comments on commit d4fbf59

Please sign in to comment.