diff --git a/wallets/client/context/hooks.js b/wallets/client/context/hooks.js index c9abc6ed..e64e3832 100644 --- a/wallets/client/context/hooks.js +++ b/wallets/client/context/hooks.js @@ -152,16 +152,11 @@ export function useKeyInit () { try { // TODO(wallet-v2): remove migration code // and delete the old IndexedDB after wallet v2 has been released for some time - const oldKeyAndHash = await loadOldKey() - if (oldKeyAndHash) { - // return key found in old db and save it to new db - await setKey(oldKeyAndHash) - return - } - // create random key before opening transaction in case we need it - // and because we can't run async code in a transaction because it will close the transaction + // load old key and create random key before opening transaction in case we need them + // because we can't run async code in a transaction because it will close the transaction // see https://javascript.info/indexeddb#transactions-autocommit + const oldKeyAndHash = await loadOldKey() const { key: randomKey, hash: randomHash } = await generateRandomKey() // run read and write in one transaction to avoid race conditions @@ -179,6 +174,11 @@ export function useKeyInit () { return resolve(read.result) } + if (oldKeyAndHash) { + // return key+hash found in old db + return resolve(oldKeyAndHash) + } + // no key found, write and return generated random key const write = tx.objectStore('vault').put({ key: randomKey, hash: randomHash }, 'key')