Skip to content
Merged
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
16 changes: 8 additions & 8 deletions wallets/client/context/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')

Expand Down