Skip to content

Commit

Permalink
fix: resets
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Dec 2, 2021
1 parent 8724907 commit b81f603
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
existsL1,
getL1,
getWallets,
resetAll,
setKeystore,
validatePassword,
} from "./keys/l1"
Expand Down Expand Up @@ -116,7 +117,7 @@ async function main() {
}

case "RESET_ALL": {
return browser.storage.local.clear()
return resetAll()
}

case "ADD_WHITELIST": {
Expand Down
10 changes: 9 additions & 1 deletion packages/extension/src/background/keys/l1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ export async function validatePassword(password: string) {
let rawWalletTimeoutPid: number | undefined
let rawWallet: ethers.Wallet | undefined

function setRawWallet(wallet: ethers.Wallet) {
function setRawWallet(wallet: ethers.Wallet | undefined) {
rawWallet = wallet
rawWalletTimeoutPid = setTimeout(() => {
rawWallet = undefined
}, 15 * 60 * 60 * 1000) as unknown as number
}

export async function setKeystore(keystore: string) {
setRawWallet(undefined)
await store.setItem("encKeystore", keystore)
}

Expand Down Expand Up @@ -67,13 +68,15 @@ async function generateL1(): Promise<ethers.Wallet> {

let recoverPromise: Promise<ethers.Wallet> | undefined
export async function getL1(password: string): Promise<ethers.Wallet> {
console.log("getL1", rawWallet, await existsL1())
if (rawWallet) {
return rawWallet
} else if (await existsL1()) {
if (!recoverPromise) recoverPromise = recoverL1(password)
const recoveredWallet = await recoverPromise
setRawWallet(recoveredWallet)
const encKeyPair = JSON.parse((await store.getItem("encKeystore")) || "{}")
console.log("Set wallets", encKeyPair)
store.setItem("wallets", encKeyPair.wallets ?? [])
if (
(await selectedWalletStore.getItem("SELECTED_WALLET")).address === "" &&
Expand Down Expand Up @@ -169,3 +172,8 @@ export async function createAccount(password: string, networkId: string) {
wallets,
}
}

export async function resetAll() {
setRawWallet(undefined)
await browser.storage.local.clear()
}

0 comments on commit b81f603

Please sign in to comment.