Skip to content

Commit

Permalink
Merge pull request #154 from alephium/add-connection-expiry
Browse files Browse the repository at this point in the history
Add connection expiry
  • Loading branch information
h0ngcha0 authored Sep 26, 2023
2 parents 6a3afd7 + 7f7b560 commit 58c89a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { differenceWith, isEqual } from "lodash-es"

import { PreAuthorisationMessage } from "../shared/messages/PreAuthorisationMessage"
import { getPreAuthorized, isPreAuthorized, preAuthorizeStore, removePreAuthorization } from "../shared/preAuthorizations"
import { getPreAuthorized, isPreAuthorized, preAuthorizeStore, removePreAuthorization, setConnectionExpiryAlarm } from "../shared/preAuthorizations"
import { withNetwork } from "../shared/wallet.service"
import { addTab, sendMessageToHost } from "./activeTabs"
import { UnhandledMessage } from "./background"
Expand Down Expand Up @@ -45,6 +45,7 @@ export const handlePreAuthorizationMessage: HandleMessage<
}

if (authorized) {
await setConnectionExpiryAlarm(msg.data.host)
const authorizedAccount = await wallet.getAccount(authorized.account)
const walletAccountWithNetwork = await withNetwork(authorizedAccount)
return respond({
Expand Down
28 changes: 28 additions & 0 deletions packages/extension/src/shared/preAuthorizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { useArrayStorage } from "./storage/hooks"
import { BaseWalletAccount } from "./wallet.model"
import { accountsEqual } from "./wallet.service"

const CONNECTION_EXPIRY = 7 * 24 * 60 // 7 days in minutes

interface PreAuthorization {
account: BaseWalletAccount
host: string
Expand Down Expand Up @@ -53,6 +55,29 @@ export const migratePreAuthorizations = async () => {
}
}

const getConnectionAlarmName = (host: string) => {
return `alph:connection:${host}`
}

const connectionExpiryListner = (alarm: browser.alarms.Alarm) => {
if (alarm.name.startsWith('alph:connection:')) {
const host = alarm.name.split(':')[2]
removePreAuthorization(host)
}
}

export const setConnectionExpiryAlarm = async (host: string) => {
if (!browser.alarms.onAlarm.hasListener(connectionExpiryListner)) {
browser.alarms.onAlarm.addListener(connectionExpiryListner)
}

const alarmName = getConnectionAlarmName(host)
await browser.alarms.clear(alarmName)
browser.alarms.create(alarmName, {
delayInMinutes: CONNECTION_EXPIRY,
})
}

export const preAuthorize = async (
account: BaseWalletAccount,
host: string,
Expand All @@ -61,6 +86,8 @@ export const preAuthorize = async (
account,
host,
})

await setConnectionExpiryAlarm(host)
}

export const removePreAuthorization = async (
Expand All @@ -73,6 +100,7 @@ export const removePreAuthorization = async (
}
return x.host === host
})
await browser.alarms.clear(getConnectionAlarmName(host))
}

export const getPreAuthorizations = () => {
Expand Down

0 comments on commit 58c89a0

Please sign in to comment.