Fix old key overwrites new key (#2290)

This commit is contained in:
ekzyis 2025-07-17 19:36:38 +02:00 committed by GitHub
parent f7be43d3af
commit 075934e20b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -152,16 +152,11 @@ export function useKeyInit () {
try { try {
// TODO(wallet-v2): remove migration code // TODO(wallet-v2): remove migration code
// and delete the old IndexedDB after wallet v2 has been released for some time // 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 // load old key and create random key before opening transaction in case we need them
// and because we can't run async code in a transaction because it will close the transaction // because we can't run async code in a transaction because it will close the transaction
// see https://javascript.info/indexeddb#transactions-autocommit // see https://javascript.info/indexeddb#transactions-autocommit
const oldKeyAndHash = await loadOldKey()
const { key: randomKey, hash: randomHash } = await generateRandomKey() const { key: randomKey, hash: randomHash } = await generateRandomKey()
// run read and write in one transaction to avoid race conditions // run read and write in one transaction to avoid race conditions
@ -179,6 +174,11 @@ export function useKeyInit () {
return resolve(read.result) 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 // no key found, write and return generated random key
const write = tx.objectStore('vault').put({ key: randomKey, hash: randomHash }, 'key') const write = tx.objectStore('vault').put({ key: randomKey, hash: randomHash }, 'key')