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

Commit 243c027

Browse files
committed
wip
1 parent c38691a commit 243c027

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

index.js

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,37 +106,28 @@ class KeyringController extends EventEmitter {
106106
* creates a new encrypted store with the given password,
107107
* creates a new HD wallet from the given seed with 1 account.
108108
*
109-
* @emits KeyringController#unlock
109+
* @fires KeyringController#unlock
110110
* @param {string} password - The password to encrypt the vault with
111111
* @param {Uint8Array | string} seedPhrase - The BIP39-compliant seed phrase,
112112
* either as a string or Uint8Array.
113-
* @returns {Promise<Object>} A Promise that resolves to the state.
113+
* @returns {Promise<object>} A Promise that resolves to the state.
114114
*/
115115
createNewVaultAndRestore(password, seedPhrase) {
116-
let encodedSeedPhrase = seedPhrase;
117-
if (typeof encodedSeedPhrase === 'string') {
118-
const indices = seedPhrase
119-
.split(' ')
120-
.map((word) => wordlist.indexOf(word));
121-
encodedSeedPhrase = new Uint8Array(new Uint16Array(indices).buffer);
122-
} else if (
123-
seedPhrase instanceof Object &&
124-
!(seedPhrase instanceof Uint8Array)
125-
) {
126-
// when passed from the frontend to the background process a Uint8Array becomes a javascript object
127-
encodedSeedPhrase = Uint8Array.from(Object.values(seedPhrase));
128-
}
116+
const encodedSeedPhrase = this._mnemonicToUint8Array(seedPhrase);
129117

130118
if (typeof password !== 'string') {
131119
throw new Error('Password must be text.');
132120
}
133121

134122
if (!bip39.validateMnemonic(encodedSeedPhrase, wordlist)) {
135-
return Promise.reject(new Error('Seed phrase is invalid.'));
123+
return Promise.reject(
124+
new Error('KeyringController - Seed phrase is invalid.'),
125+
);
136126
}
137127

138128
this.password = password;
139129

130+
this.clearKeyrings();
140131
return this.persistAllKeyrings(password)
141132
.then(() => {
142133
return this.addNewKeyring(KEYRINGS_TYPE_MAP.HD_KEYRING, {
@@ -924,6 +915,52 @@ class KeyringController extends EventEmitter {
924915

925916
return keyring;
926917
}
918+
919+
/*
920+
Utility Methods
921+
*/
922+
923+
_uint8ArrayToString(mnemonic) {
924+
const recoveredIndices = Array.from(
925+
new Uint16Array(new Uint8Array(mnemonic).buffer),
926+
);
927+
return recoveredIndices.map((i) => wordlist[i]).join(' ');
928+
}
929+
930+
_stringToUint8Array(mnemonic) {
931+
const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));
932+
return new Uint8Array(new Uint16Array(indices).buffer);
933+
}
934+
935+
_mnemonicToUint8Array(mnemonic) {
936+
let mnemonicData = mnemonic;
937+
// when encrypted/decrypted, buffers get cast into js object with a property type set to buffer
938+
if (mnemonic && mnemonic.type && mnemonic.type === 'Buffer') {
939+
mnemonicData = mnemonic.data;
940+
}
941+
942+
if (
943+
// this block is for backwards compatibility with vaults that were previously stored as buffers, number arrays or plain text strings
944+
typeof mnemonicData === 'string' ||
945+
Buffer.isBuffer(mnemonicData) ||
946+
Array.isArray(mnemonicData)
947+
) {
948+
let mnemonicAsString = mnemonicData;
949+
if (Array.isArray(mnemonicData)) {
950+
mnemonicAsString = Buffer.from(mnemonicData).toString();
951+
} else if (Buffer.isBuffer(mnemonicData)) {
952+
mnemonicAsString = mnemonicData.toString();
953+
}
954+
return this._stringToUint8Array(mnemonicAsString);
955+
} else if (
956+
mnemonicData instanceof Object &&
957+
!(mnemonicData instanceof Uint8Array)
958+
) {
959+
// when encrypted/decrypted the Uint8Array becomes a js object we need to cast back to a Uint8Array
960+
return Uint8Array.from(Object.values(mnemonicData));
961+
}
962+
return mnemonicData;
963+
}
927964
}
928965

929966
/**

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe('KeyringController', function () {
180180
it('throws error if argument password is not a string', async function () {
181181
await expect(() =>
182182
keyringController.createNewVaultAndRestore(12, walletTwoSeedWords),
183-
).rejects.toThrow('Password must be text.');
183+
).toThrow('Password must be text.');
184184
});
185185

186186
it('throws error if mnemonic passed is invalid', async function () {

0 commit comments

Comments
 (0)