Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
fix _initFromMnemonic bug + get test coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Apr 18, 2022
1 parent 5a139eb commit f98c952
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class HdKeyring extends SimpleKeyring {
'Eth-Hd-Keyring: Secret recovery phrase already provided',
);
}

this.opts = opts || {};
this.opts = opts;
this.wallets = [];
this.mnemonic = null;
this.root = null;
Expand Down Expand Up @@ -116,7 +115,7 @@ class HdKeyring extends SimpleKeyring {
}

// eslint-disable-next-line node/no-sync
const seed = bip39.mnemonicToSeedSync(mnemonic);
const seed = bip39.mnemonicToSeedSync(this.mnemonic);
this.hdWallet = hdkey.fromMasterSeed(seed);
this.root = this.hdWallet.derivePath(this.hdPath);
}
Expand Down
35 changes: 33 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ const privKeyHex =

const sampleMnemonic =
'finish oppose decorate face calm tragic certain desk hour urge dinosaur mango';
const sampleBufferArrayMnemonic = [
102, 105, 110, 105, 115, 104, 32, 111, 112, 112, 111, 115, 101, 32, 100, 101,
99, 111, 114, 97, 116, 101, 32, 102, 97, 99, 101, 32, 99, 97, 108, 109, 32,
116, 114, 97, 103, 105, 99, 32, 99, 101, 114, 116, 97, 105, 110, 32, 100, 101,
115, 107, 32, 104, 111, 117, 114, 32, 117, 114, 103, 101, 32, 100, 105, 110,
111, 115, 97, 117, 114, 32, 109, 97, 110, 103, 111,
];
const firstAcct = '0x1c96099350f13d558464ec79b9be4445aa0ef579';
const secondAcct = '0x1b00aed43a693f3a957f9feb5cc08afa031e37a0';

Expand All @@ -25,7 +32,7 @@ describe('hd-keyring', () => {
});

describe('constructor', () => {
it('constructs', async () => {
it('constructs with a typeof string mnemonic', async () => {
keyring = new HdKeyring({
mnemonic: sampleMnemonic,
numberOfAccounts: 2,
Expand All @@ -36,6 +43,17 @@ describe('hd-keyring', () => {
expect(accounts[1]).toStrictEqual(secondAcct);
});

it('constructs with a typeof array mnemonic', async () => {
keyring = new HdKeyring({
mnemonic: sampleBufferArrayMnemonic,
numberOfAccounts: 2,
});

const accounts = await keyring.getAccounts();
expect(accounts[0]).toStrictEqual(firstAcct);
expect(accounts[1]).toStrictEqual(secondAcct);
});

it('throws on invalid mnemonic', () => {
expect(
() =>
Expand Down Expand Up @@ -99,12 +117,19 @@ describe('hd-keyring', () => {
});

describe('#serialize mnemonic.', () => {
it('serializes mnemonic class variable into a buffer array and does not add accounts', async () => {
it('serializes mnemonic stored as a buffer in a class variable into a buffer array and does not add accounts', async () => {
keyring.generateRandomMnemonic();
const output = await keyring.serialize();
expect(output.numberOfAccounts).toBe(0);
expect(Array.isArray(output.mnemonic)).toBe(true);
});

it('serializes mnemonic stored as a string in a class variable into a buffer array and does not add accounts', async () => {
keyring.mnemonic = sampleMnemonic;
const output = await keyring.serialize();
expect(output.numberOfAccounts).toBe(0);
expect(Array.isArray(output.mnemonic)).toBe(true);
});
});

describe('#deserialize a private key', () => {
Expand Down Expand Up @@ -133,6 +158,12 @@ describe('hd-keyring', () => {
await keyring.addAccounts();
expect(keyring.wallets).toHaveLength(1);
});

it('throws an error when no SRP has been generated yet', async () => {
expect(() => keyring.addAccounts()).toThrow(
'Eth-Hd-Keyring: No secret recovery phrase provided',
);
});
});

describe('with a numeric argument', () => {
Expand Down

0 comments on commit f98c952

Please sign in to comment.