forked from wanderwallet/Wander
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: background modules implementation wip
- Loading branch information
1 parent
a49d3fd
commit 5eb1979
Showing
11 changed files
with
211 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import type { OnMessageCallback } from "webext-bridge"; | ||
import { checkTypes, getAppURL } from "~utils/format"; | ||
import type { ApiCall, ApiResponse } from "shim"; | ||
import { pushEvent } from "~utils/events"; | ||
import { getTab } from "~applications"; | ||
import Application from "~applications/application"; | ||
import modules from "./background"; | ||
|
||
const handleApiCalls: OnMessageCallback< | ||
// @ts-expect-error | ||
ApiCall<{ params: any[] }>, | ||
ApiResponse | ||
> = async ({ data, sender }) => { | ||
// contruct base message to extend and return | ||
const baseMessage: ApiResponse = { | ||
type: data.type + "_result", | ||
callID: data.callID | ||
}; | ||
|
||
try { | ||
// check if the call is from the content-script | ||
if (sender.context !== "content-script") { | ||
throw new Error( | ||
"API calls are only accepted from the injected-script -> content-script" | ||
); | ||
} | ||
|
||
// grab the tab where the API call came from | ||
const tab = await getTab(sender.tabId); | ||
|
||
// if the tab is not found, reject the call | ||
if (!tab || !tab.url) { | ||
throw new Error("Call coming from invalid tab"); | ||
} | ||
|
||
// check data types | ||
checkTypes([data.callID, "string"], [data.type, "string"]); | ||
|
||
// find module to execute | ||
const functionName = data.type.replace("api_", ""); | ||
const mod = modules.find((mod) => mod.functionName === functionName); | ||
|
||
// if we cannot find the module, we return with an error | ||
if (!mod) { | ||
throw new Error(`API function "${functionName}" not found`); | ||
} | ||
|
||
// grab app info | ||
const app = new Application(getAppURL(tab.url)); | ||
|
||
// check permissions | ||
const permissionCheck = await app.hasPermissions(mod.permissions); | ||
|
||
if (!permissionCheck.result) { | ||
throw new Error( | ||
`Missing permission(s) for "${functionName}": ${permissionCheck.missing.join( | ||
", " | ||
)}` | ||
); | ||
} | ||
|
||
// check if site is blocked | ||
if (await app.isBlocked()) { | ||
throw new Error(`${app.url} is blocked from interacting with ArConnect`); | ||
} | ||
|
||
// update events | ||
await pushEvent({ | ||
type: data.type, | ||
app: app.url, | ||
date: Date.now() | ||
}); | ||
|
||
// handle function | ||
const functionResult = await mod.function(tab, ...(data.data.params || [])); | ||
|
||
// return result | ||
return { | ||
...baseMessage, | ||
data: functionResult | ||
}; | ||
} catch (e) { | ||
// return error | ||
return { | ||
...baseMessage, | ||
error: true, | ||
data: e?.message || e | ||
}; | ||
} | ||
}; | ||
|
||
export default handleApiCalls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getStorageConfig } from "./storage"; | ||
import { Storage } from "@plasmohq/storage"; | ||
|
||
interface SecurityEvent { | ||
type: string; | ||
app: string; | ||
date: number; | ||
} | ||
|
||
const storage = new Storage(getStorageConfig()); | ||
|
||
/** | ||
* Push an event to the stored events array | ||
* | ||
* @param event Event to push | ||
*/ | ||
export async function pushEvent(event: SecurityEvent) { | ||
let events = (await storage.get<SecurityEvent[]>("events")) || []; | ||
|
||
// only allow the last 99 events | ||
events = events.filter((_, i) => i < 98); | ||
events.push(event); | ||
|
||
await storage.set("events", events); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters