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

Make new internal methods private #76

Merged
merged 1 commit into from
Dec 13, 2022
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
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ class HdKeyring {
this._initFromMnemonic(bip39.generateMnemonic(wordlist));
}

uint8ArrayToString(mnemonic) {
_uint8ArrayToString(mnemonic) {
const recoveredIndices = Array.from(
new Uint16Array(new Uint8Array(mnemonic).buffer),
);
return recoveredIndices.map((i) => wordlist[i]).join(' ');
}

stringToUint8Array(mnemonic) {
_stringToUint8Array(mnemonic) {
const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));
return new Uint8Array(new Uint16Array(indices).buffer);
}

mnemonicToUint8Array(mnemonic) {
_mnemonicToUint8Array(mnemonic) {
let mnemonicData = mnemonic;
// when encrypted/decrypted, buffers get cast into js object with a property type set to buffer
if (mnemonic && mnemonic.type && mnemonic.type === 'Buffer') {
Expand All @@ -68,7 +68,7 @@ class HdKeyring {
} else if (Buffer.isBuffer(mnemonicData)) {
mnemonicAsString = mnemonicData.toString();
}
return this.stringToUint8Array(mnemonicAsString);
return this._stringToUint8Array(mnemonicAsString);
} else if (
mnemonicData instanceof Object &&
!(mnemonicData instanceof Uint8Array)
Expand All @@ -81,7 +81,7 @@ class HdKeyring {

serialize() {
return Promise.resolve({
mnemonic: this.mnemonicToUint8Array(this.mnemonic),
mnemonic: this._mnemonicToUint8Array(this.mnemonic),
numberOfAccounts: this._wallets.length,
hdPath: this.hdPath,
});
Expand Down Expand Up @@ -278,7 +278,7 @@ class HdKeyring {
);
}

this.mnemonic = this.mnemonicToUint8Array(mnemonic);
this.mnemonic = this._mnemonicToUint8Array(mnemonic);

// validate before initializing
const isValid = bip39.validateMnemonic(this.mnemonic, wordlist);
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('hd-keyring', () => {
describe('#serialize mnemonic.', () => {
it('serializes mnemonic stored as a buffer to a Uint8Array', async () => {
keyring.mnemonic = oldMMForkBIP39.generateMnemonic();
const mnemonicAsUint8Array = keyring.stringToUint8Array(
const mnemonicAsUint8Array = keyring._stringToUint8Array(
keyring.mnemonic.toString(),
);
const output = await keyring.serialize();
Expand All @@ -208,7 +208,7 @@ describe('hd-keyring', () => {
const output = await keyring.serialize();
expect(output.numberOfAccounts).toBe(0);
expect(output.mnemonic).toStrictEqual(
keyring.stringToUint8Array(sampleMnemonic),
keyring._stringToUint8Array(sampleMnemonic),
);
});
});
Expand All @@ -228,7 +228,7 @@ describe('hd-keyring', () => {
expect(accountsSecondCheck[1]).toStrictEqual(secondAcct);
expect(accountsSecondCheck).toHaveLength(2);
const serialized = await keyring.serialize();
expect(keyring.uint8ArrayToString(serialized.mnemonic)).toStrictEqual(
expect(keyring._uint8ArrayToString(serialized.mnemonic)).toStrictEqual(
sampleMnemonic,
);
});
Expand Down