Skip to content

Commit

Permalink
Add mnemonicToEntropy and entropyToMnemonic.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Oct 1, 2024
1 parent cfd88cd commit 010c021
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 33 deletions.
74 changes: 45 additions & 29 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-wallet",
"version": "4.5.1",
"version": "4.6.0",
"description": "Wallet components for MultiversX",
"main": "out/index.js",
"types": "out/index.d.js",
Expand All @@ -22,7 +22,7 @@
"dependencies": {
"@multiversx/sdk-bls-wasm": "0.3.5",
"bech32": "1.1.4",
"bip39": "3.0.2",
"bip39": "3.1.0",
"blake2b": "2.1.3",
"ed25519-hd-key": "1.1.2",
"ed2curve": "0.3.0",
Expand Down
20 changes: 18 additions & 2 deletions src/mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { generateMnemonic, mnemonicToSeedSync, validateMnemonic } from "bip39";
import {
entropyToMnemonic,
generateMnemonic,
mnemonicToEntropy,
mnemonicToSeedSync,
validateMnemonic,
} from "bip39";
import { derivePath } from "ed25519-hd-key";
import { ErrWrongMnemonic } from "./errors";
import { UserSecretKey } from "./userKeys";
Expand All @@ -14,7 +20,7 @@ export class Mnemonic {
}

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

Expand All @@ -25,6 +31,11 @@ export class Mnemonic {
return new Mnemonic(text);
}

static fromEntropy(entropy: Uint8Array): Mnemonic {
const text = entropyToMnemonic(Buffer.from(entropy));
return new Mnemonic(text);
}

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

Expand All @@ -45,6 +56,11 @@ export class Mnemonic {
return this.text.split(" ");
}

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

toString(): string {
return this.text;
}
Expand Down
20 changes: 20 additions & 0 deletions src/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ describe("test user wallets", () => {
assert.lengthOf(words, 24);
});

it("should convert entropy to mnemonic and back", () => {
function testConversion(text: string, entropyHex: string) {
const entropyFromMnemonic = Mnemonic.fromString(text).getEntropy();
const mnemonicFromEntropy = Mnemonic.fromEntropy(Buffer.from(entropyHex, "hex"));

assert.equal(Buffer.from(entropyFromMnemonic).toString("hex"), entropyHex);
assert.equal(mnemonicFromEntropy.toString(), text);
}

testConversion(
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
"00000000000000000000000000000000",
);

testConversion(
"moral volcano peasant pass circle pen over picture flat shop clap goat never lyrics gather prepare woman film husband gravity behind test tiger improve",
"8fbeb688d0529344e77d225898d4a73209510ad81d4ffceac9bfb30149bf387b",
);
});

it("should derive keys", async () => {
let mnemonic = Mnemonic.fromString(DummyMnemonic);

Expand Down

0 comments on commit 010c021

Please sign in to comment.