Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wrong target branch] #52

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
802d5bb
fix: warning on start
sahithyandev Aug 6, 2023
b930abe
fix: style issue in navbar
sahithyandev Aug 6, 2023
8c368b7
fix: theming issue
sahithyandev Aug 7, 2023
0c90bbc
feat: add /extension-installed page
sahithyandev Aug 7, 2023
b042c63
fix: hide nav bar on /extension-installed page
sahithyandev Aug 7, 2023
3b92d97
feat: open /extension-installed for users on install
sahithyandev Aug 7, 2023
c630e04
fix: discord invite link
sahithyandev Aug 7, 2023
b488c48
chore: extract domainInfo route handler
sahithyandev Aug 9, 2023
0ff6f6e
chore: extract /contract-info route handler
sahithyandev Aug 9, 2023
242cc34
chore: extract submitContractReport route handler
sahithyandev Aug 9, 2023
5827f62
feat: add /event endpoint
sahithyandev Aug 11, 2023
c433c45
chore: minor issues in background.js
sahithyandev Aug 11, 2023
88a59c0
feat: turn on pageview tracking
sahithyandev Aug 11, 2023
bd2aae5
feat: send events from extension
sahithyandev Aug 11, 2023
2616a86
fix: set max age for user id cookie
sahithyandev Aug 11, 2023
7fdcce8
fix: send event on /domain-info fetched
sahithyandev Aug 11, 2023
ade65f8
Merge pull request #50 from sahithyandev/add-socials
VenkatTeja Aug 13, 2023
0094a67
merged
VenkatTeja Aug 13, 2023
89efa4c
merge event improvements
VenkatTeja Aug 13, 2023
2060601
warn when MIXPANEL_TOKEN is undefined
sahithyandev Aug 14, 2023
fcdd6e4
stop using cookies in /event
sahithyandev Aug 14, 2023
39f4c82
refactor to use user id from storage
sahithyandev Aug 14, 2023
6900b40
minor refactor in contstants.js
sahithyandev Aug 14, 2023
13a699a
fix error on building content.js
sahithyandev Aug 14, 2023
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
18 changes: 16 additions & 2 deletions chrome-extension/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ const abi = [

const address = "0x68Db62ADCaADdb21cB000841f1F347A6d8bEED9b"

// for production
const API_ENDPOINT = "https://api.vigilancedao.org"
const DOMAIN = 'https://vigilancedao.org'

// for development
// const API_ENDPOINT = "http://localhost:4000";
// const DOMAIN = "http://localhost:3000";

const USER_ID_KEY = "user-id";

module.exports = {
abi, address
}
abi,
address,
API_ENDPOINT,
DOMAIN,
USER_ID_KEY,
};
9 changes: 5 additions & 4 deletions chrome-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"dev:other": "node bundle-config.mjs --watch",
"build": "concurrently npm:build:content npm:build:other npm:build:inject",
"dev": "concurrently npm:dev:other npm:dev:content npm:dev:inject",
"build:content": "browserify src/content.js -o build/content.js",
"dev:content": "nodemon --exec \"browserify src/content.js -o build/content.js\" --watch src/content.js",
"build:inject": "browserify src/inject.js -o build/inject.js",
"dev:inject": "nodemon --exec \"browserify src/inject.js -o build/inject.js\" --watch src/inject.js",
"build:content": "browserify -p esmify src/content.js -o build/content.js",
"dev:content": "nodemon --exec \"browserify -p esmify src/content.js -o build/content.js\" --watch src/content.js",
"build:inject": "browserify -p esmify src/inject.js -o build/inject.js",
"dev:inject": "nodemon --exec \"browserify -p esmify src/inject.js -o build/inject.js\" --watch src/inject.js",
"copy:assets": "cp -r assets/ build/static/",
"release": "yarn build && yarn build:content && yarn build:inject && yarn copy:assets"
},
Expand Down Expand Up @@ -58,6 +58,7 @@
"@types/mixpanel-browser": "^2.47.0",
"concurrently": "^8.2.0",
"esbuild-envfile-plugin": "^1.0.5",
"esmify": "^2.1.1",
"mkdirp": "^3.0.0",
"nodemon": "^2.0.22"
}
Expand Down
40 changes: 39 additions & 1 deletion chrome-extension/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
/// <reference types="psl" />
/// <reference lib="webworker" />

import { API_ENDPOINT, DOMAIN } from "../constants";
import { sendEvent } from "./utils";

// ! For production uncomment these lines
console.log = function(){};
console.debug = function(){};
Expand All @@ -18,7 +21,7 @@ try {
const DONT_SHOW_AGAIN_DOMAINS_KEY = "dont_show_again_domains";
const env = {
// host: "http://localhost:4000", // backend API endpoint
host: "https://api.vigilancedao.org",
host: API_ENDPOINT,
alertPeriod: 4 * 30 * 86400 * 1000,
SUBGRAPH_URL:
"https://api.thegraph.com/subgraphs/name/venkatteja/vigilancedao",
Expand Down Expand Up @@ -131,6 +134,12 @@ async function getDomainRegistrationDate(storageInfo, url) {

return new Date(itemInStorage.createdon);
} else {
sendEvent({
eventName: "fetch-domain-info",
eventData: {
url,
},
});
try {
// fetch from our backend
const rawResponse = await fetch(`${env.host}/domain-info`, {
Expand All @@ -140,6 +149,7 @@ async function getDomainRegistrationDate(storageInfo, url) {
"Content-Type": "application/json",
},
body: JSON.stringify({ domain: url }),
credentials: "include",
});
/**
* @type {import("../../important-types").DomainInfo}
Expand Down Expand Up @@ -196,6 +206,7 @@ function getUrl(tab) {
console.debug("bg current url", _url);
console.debug("bg current tab", tab);

// @ts-expect-error
var parsed = psl.parse(_url.hostname);
if (parsed.error) {
throw new Error(parsed.error.message);
Expand Down Expand Up @@ -381,6 +392,12 @@ async function fetchDomainInfo(simplifiedUrl) {
}
}

sendEvent({
eventName: "fetch-domain-info",
eventData: {
url: simplifiedUrl,
},
});
// fetch /domain-info endpoint
const response = await fetch(`${env.host}/domain-info`, {
method: "POST",
Expand All @@ -389,6 +406,7 @@ async function fetchDomainInfo(simplifiedUrl) {
"Content-Type": "application/json",
},
body: JSON.stringify({ domain: simplifiedUrl }),
credentials: "include",
});
/**
* @type {import("../../important-types").DomainInfo}
Expand Down Expand Up @@ -681,6 +699,11 @@ chrome.action.onClicked.addListener(function (tab) {
// so the below functions proxies the msg between index.html and content.js
// Look for `chrome.runtime.onMessage.addListener` in the code
// to see how the msgs are being recieved and sent
/**
* @param {{ type: string; data: unknown; }} request
* @param {chrome.runtime.MessageSender} sender
* @param {(response?: any) => void} sendResponse
*/
async function processMsg(request, sender, sendResponse) {
if (sender.tab == undefined) {
console.error("sender", sender);
Expand Down Expand Up @@ -722,6 +745,7 @@ async function processMsg(request, sender, sendResponse) {
});
chrome.storage.sync.set({
[DONT_SHOW_AGAIN_DOMAINS_KEY]: dontShowAgainDomains.concat(
// @ts-expect-error
request.data.url
),
});
Expand Down Expand Up @@ -765,3 +789,17 @@ function takeScreenshot(tab) {
);
});
}

chrome.runtime.onInstalled.addListener((details) => {
console.log('onInstalled', details);
sendEvent({
eventName: "install",
...details
});

if (details.reason == 'install') {
chrome.tabs.create({
url: `${DOMAIN}/extension-installed?reason=${details.reason}`,
});
}
});
11 changes: 9 additions & 2 deletions chrome-extension/src/content.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const createMetaMaskProvider = require("metamask-extension-provider");
const mixpanel = require("mixpanel-browser")
const { address, abi } = require("../constants");
const { address, abi, API_ENDPOINT } = require("../constants");
const { MIXPANEL_PROJECT_ID } = require("../privateenv");
const { getFonts } = require("./fonts");
const { sendEvent } = require("./utils");

// ! For production uncomment these lines
console.log = function(){};
Expand All @@ -19,7 +20,7 @@ let domain = "";
mixpanel.init(MIXPANEL_PROJECT_ID, {debug: true});

const env = {
host: "https://api.vigilancedao.org",
host: API_ENDPOINT,
alertPeriod: 4 * 30 * 86400 * 1000,
rpcs: {
polygonTestnet: 'https://polygon-mumbai.g.alchemy.com/v2/1faz4r-pcSp890xH8xfvX-ZIGTTIpG3N'
Expand Down Expand Up @@ -638,5 +639,11 @@ window.addEventListener("message", (event) => {
},
"*"
);
return;
}

if (event.data.reason == "send-event" && typeof event.data.event != "undefined") {
sendEvent(event.data.event);
return;
}
});
15 changes: 12 additions & 3 deletions chrome-extension/src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const mixpanel = require("mixpanel-browser");
const { MIXPANEL_PROJECT_ID } = require("../privateenv");
const { chromeRuntimeGetUrlWrapped } = require("./fonts");
const { sendEvent } = require("./utils");
const { API_ENDPOINT } = require("../constants");

// ! For production uncomment these lines
console.log = function(){};
Expand All @@ -12,7 +14,7 @@ console.warn = function(){};
const FortaAPIUrl = "https://api.forta.network/graphql";

const env = {
host: "https://api.vigilancedao.org"
host: API_ENDPOINT
// For developement
// host: "http://localhost:4000",
};
Expand All @@ -33,13 +35,18 @@ function isSendTransactionRequest(params) {
*/
function fetchContractInfo(basicInfo) {
console.log("fetchContractInfo", basicInfo);

sendEvent({
eventName: "fetch-contract-info",
});

const contractInfoApiFetch = fetch(ContractInfoAPIURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(basicInfo),
credentials: "include",
}).then(
/**
* @returns {Promise<import("./inject").ContractInfo>}
Expand Down Expand Up @@ -161,6 +168,8 @@ function fetchContractInfo(basicInfo) {
* @returns {Promise<string | undefined>} string --> error message, undefined --> successful
*/
function submitContractReport(report) {
sendEvent({ eventName: "submit-contract-report" });

return fetch(env.host.concat("/submit-contract-report"), {
method: "POST",
headers: {
Expand All @@ -169,6 +178,7 @@ function submitContractReport(report) {
body: JSON.stringify({
report,
}),
credentials: "include",
})
.then((response) => {
if (response.ok) {
Expand Down Expand Up @@ -628,7 +638,7 @@ const ERROR_MSG = "Transaction cancelled by user.";
*/
// @ts-expect-error
window.ethereum.request = (params) => {
return /** @type {Promise<bool>} */ (
return /** @type {Promise<boolean>} */ (
new Promise(async (continueRequest, reject) => {
if (window.ethereum == undefined || !isSendTransactionRequest(params)) {
continueRequest(false);
Expand Down Expand Up @@ -778,4 +788,3 @@ const ERROR_MSG = "Transaction cancelled by user.";
// proceedButtonClickListener: () => {},
// });
// })();

53 changes: 52 additions & 1 deletion chrome-extension/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { API_ENDPOINT, USER_ID_KEY } from "../../constants";
import axios from "axios";

export async function subgraphQuery(query) {
Expand All @@ -15,4 +16,54 @@
console.error(error);
throw new Error(`Could not query the subgraph ${error.message}`);
}
}
}

/**
* @param {import("../../../important-types").TrackingEvent} event
* @returns {Promise<void>}
*/
export async function sendEvent(event) {
if (typeof chrome == "undefined" || chrome.storage == undefined) {
// means the function is called from an injected script
// in that instance, pass the event to content script
// and it will, then, send the event
const message = {
reason: "send-event",
event,
};

window.postMessage(message, "*");

Check warning

Code scanning / CodeQL

Cross-window communication with unrestricted target origin Medium

Sensitive data
is sent to another window without origin restriction.
}

const userId = await chrome.storage.sync
.get(USER_ID_KEY)
.then((result) => result[USER_ID_KEY])
.catch(console.error);
console.log("sendEvent userId", userId);
event.userId = userId;

return fetch(`${API_ENDPOINT}/event`, {
method: "POST",
body: JSON.stringify(event),
headers: {
"Content-Type": "application/json",
},
})
.then(async (response) => {
const body = await response.text();
if (!response.ok) {
console.error("sendEvent: response not ok", response.status, body);
return;
}

if (userId != body) {
return chrome.storage.sync
.set({ [USER_ID_KEY]: body })
.then(() => console.log("saved", USER_ID_KEY, "=", body));
}
})
.catch((error) => {
console.warn("Error occured while sending event to server");
console.error(error);
});
}
7 changes: 7 additions & 0 deletions important-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ export interface DomainScamInfo {
export interface DomainInfo extends BasicDomainInfo {
scamInfo?: DomainScamInfo;
}

export interface TrackingEvent {
eventName: string;
userId?: string;
accountId?: string;
eventData?: Record<string, unknown>;
}
7 changes: 5 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
"build": "tsc"
},
"dependencies": {
"@types/cookie-parser": "^1.4.3",
"capture-website": "^2.4.0",
"discord-api-types": "^0.37.49",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"discord-api-types": "^0.37.49",
"ethers": "^6.6.2",
"express": "^4.18.1",
"helmet": "^5.1.0",
"mixpanel": "^0.17.0",
"node-fetch": "^3.0.0",
"node-telegram-bot-api": "^0.61.0",
"pg": "^8.7.3",
Expand Down Expand Up @@ -53,8 +56,8 @@
"lint-staged": "^12.5.0",
"prettier": "^2.6.2",
"rimraf": "^3.0.2",
"serverless-api-gateway-caching": "^1.10.1",
"serverless": "^3.33.0",
"serverless-api-gateway-caching": "^1.10.1",
"serverless-bundle": "^5.3.0",
"serverless-dotenv-plugin": "^4.0.1",
"serverless-offline": "^8.8.0",
Expand Down
8 changes: 8 additions & 0 deletions server/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ functions:
method: POST
cors: true

event-track:
handler: src/handler.handler
events:
- http:
path: '/event'
method: POST
cors: true

package:
individually: true

Expand Down
Loading