Skip to content

Commit

Permalink
Refactor to remove wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
kewbish committed Dec 31, 2022
1 parent 3afdb24 commit 48dae5f
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 438 deletions.
Binary file added build.crx
Binary file not shown.
140 changes: 10 additions & 130 deletions src/pages/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,33 @@ import { storage } from "@extend-chrome/storage";

import {
MONTAG_FOUND,
USER_SET_WALLET,
DELETE_STREAM,
BLOCK_TAG,
UPDATE_SETTING,
UPDATE_STREAM,
FETCH_BALANCE,
APPROVE_AMT,
DOWNGRADE_TOKEN,
UPGRADE_TOKEN,
NEW_TOAST,
FETCH_SIGNATURE,
CHECK_METAMASK,
APPROVE_FULL,
} from "../shared/events";
import TOKEN_MAP, { PROD_TOKEN_MAP } from "../shared/tokens";
import { PayRates, Wallet } from "../shared/types";
import { PayRates } from "../shared/types";
import createStream, { updateStream } from "./lib/createStream";
import deleteStreamByTabId from "./lib/deleteStreamByTabId";
import setNewWallet from "./lib/setNewWallet";
import { getRate } from "./lib/getRate";
import setDefaultSettings from "./lib/initializeCobweb";
import blockTag from "./lib/blockTag";
import updateRateSetting from "./lib/updateRateSetting";
import fetchAndUpdateBalance from "./lib/fetchAndUpdateBalance";
import setNewAddress from "./lib/updateAddress";
import approveAmt, { approveAll } from "./lib/approveAmt";
import { downgradeTokens, upgradeTokens } from "./lib/wrapTokens";
import setNewToast, { deleteToast } from "./lib/setNewToast";
import errorToast, { toast } from "../shared/toast";
import generateSignature from "./lib/generateSignature";
import verifySignature from "../shared/verifySignature";
import cleanUpStreams from "./lib/cleanUpStreams";
import fetchCobwebAllowance from "./lib/fetchCobwebAllowance";
import { isDev } from "./lib/isDev";

chrome.runtime.onInstalled.addListener((details) => {
Expand Down Expand Up @@ -126,45 +120,6 @@ try {
throw e;
}

const getWalletAndSigner = async (): Promise<{
wallet: ethers.Wallet | null;
signer: Signer | null;
}> => {
let walletRes: ethers.Wallet | null = null;
try {
const { wallet } = (await storage.local.get("wallet")) as {
wallet: Wallet | null;
};
if (wallet) {
walletRes = new ethers.Wallet(
new ethers.utils.SigningKey(wallet.pkey),
mmProvider
);
}
} catch {
// silently fail
// throw new Error("Wallet not found, redirecting to onboarding");
return { wallet: null, signer: null };
}
let sfSigner: Signer | null = null;
try {
if (!walletRes || !sf || !infuraProvider) {
// toast("Error - expected wallet.");
// throw new Error("Error - expected wallet. (H)");
return { wallet: null, signer: null };
} else {
sfSigner = sf.createSigner({
privateKey: (walletRes as ethers.Wallet).privateKey,
provider: infuraProvider as InfuraProvider,
});
}
} catch (e) {
errorToast(e as Error);
// throw e;
}
return { wallet: walletRes, signer: sfSigner };
};

const montagFound = async ({
request,
sender,
Expand All @@ -177,9 +132,8 @@ const montagFound = async ({
if (!sf || !sfToken || !infuraProvider || !sender.tab) {
return;
}
const { wallet: walletRes, signer: sfSigner } = await getWalletAndSigner();
let address = verifySignature(request.options.address);
if (!address || !walletRes || !sfSigner || !sf) {
if (!address || !sf) {
return;
}
const tabId = sender.tab.id ?? 0;
Expand All @@ -198,34 +152,27 @@ const montagFound = async ({
url: sender.tab.url ?? "",
rateAmount: rate.rateAmount,
sf,
sfSigner,
sfToken,
infuraProvider: infuraProvider as InfuraProvider,
mmSigner: mmProvider.getSigner(),
});
cleanUpStreams({ sfSigner, sfToken, sf, mmSigner: mmProvider.getSigner() });
cleanUpStreams({ sfToken, sf, mmSigner: mmProvider.getSigner() });
};

const deleteStream = async ({ request }: { request: any }) => {
const { signer: sfSigner } = await getWalletAndSigner();
if (!sf || !sfSigner || !sfToken) {
if (!sf || !sfToken) {
return;
}
if (request.options.tabId) {
deleteStreamByTabId({
tabId: request.options.tabId,
sf,
sfSigner,
sfToken,
mmSigner: mmProvider.getSigner(),
});
}
};

const setUserWallet = async ({ request }: { request: any }) => {
setNewWallet(request.options.wallet);
};

const setBlockTag = async ({ request }: { request: any }) => {
blockTag({
address: request.options.address,
Expand All @@ -242,8 +189,7 @@ const updateSetting = async ({ request }: { request: any }) => {
};

const editCurrentStream = async ({ request }: { request: any }) => {
const { signer: sfSigner } = await getWalletAndSigner();
if (!sf || !sfSigner || !sfToken) {
if (!sf || !sfToken) {
return;
}
updateStream({
Expand All @@ -252,46 +198,16 @@ const editCurrentStream = async ({ request }: { request: any }) => {
to: request.options.to,
tabId: request.options.tabId,
sf,
sfSigner,
sfToken,
mmSigner: mmProvider.getSigner(),
});
};

const fetchBalance = async () => {
const { signer: sfSigner } = await getWalletAndSigner();
const { cwInitialized } = (await storage.local.get("cwInitialized")) as {
cwInitialized: boolean | null;
};
if (!sfToken || !mmProvider || !sf || (cwInitialized && !sfSigner)) {
return;
}
fetchAndUpdateBalance({ sfToken, mmProvider, sfSigner, sf });
};

const approveAmount = async ({ request }: { request: any }) => {
const sfSigner = mmProvider.getSigner();
if (!sf || !sfToken || !sfSigner) {
if (!sfToken || !mmProvider || !sf) {
return;
}
await approveAmt({
depositAmt: request.options.depositAmt,
sf,
sfSigner,
sfToken,
});
};

const approveFull = async () => {
const sfSigner = mmProvider.getSigner();
if (!sf || !sfToken || !sfSigner) {
return;
}
await approveAll({
sf,
sfSigner,
sfToken,
});
fetchAndUpdateBalance({ sfToken, mmProvider, sf });
};

const downgradeTokenAmount = async ({ request }: { request: any }) => {
Expand Down Expand Up @@ -354,11 +270,6 @@ const handleMessaging = async (
sendResponse();
return;
}
case USER_SET_WALLET: {
setUserWallet({ request });
sendResponse();
return;
}
case BLOCK_TAG: {
setBlockTag({ request });
sendResponse();
Expand All @@ -379,11 +290,6 @@ const handleMessaging = async (
sendResponse();
return;
}
case APPROVE_AMT: {
approveAmount({ request });
sendResponse();
return;
}
case DOWNGRADE_TOKEN: {
downgradeTokenAmount({ request });
sendResponse();
Expand All @@ -408,10 +314,6 @@ const handleMessaging = async (
storage.local.set({ mmNotFound: false }); // optimistically reset
sendResponse();
return;
case APPROVE_FULL:
approveFull();
sendResponse();
return;
default:
sendResponse();
return;
Expand All @@ -420,12 +322,10 @@ const handleMessaging = async (
chrome.runtime.onMessage.addListener(handleMessaging);

chrome.tabs.onRemoved.addListener(async (tabId, _) => {
const { signer: sfSigner } = await getWalletAndSigner();
if (sf && sfSigner && sfToken) {
if (sf && sfToken) {
deleteStreamByTabId({
tabId,
sf,
sfSigner,
sfToken,
mmSigner: mmProvider.getSigner(),
});
Expand All @@ -436,12 +336,10 @@ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, _) => {
if (changeInfo.status !== "complete") {
return;
}
const { signer: sfSigner } = await getWalletAndSigner();
if (changeInfo.url && sf && sfSigner && sfToken) {
if (changeInfo.url && sf && sfToken) {
deleteStreamByTabId({
tabId,
sf,
sfSigner,
sfToken,
checkFocus: true,
mmSigner: mmProvider.getSigner(),
Expand All @@ -456,9 +354,6 @@ chrome.runtime.onStartup.addListener(() => {
chrome.alarms.create("cobwebStreamCleanup" + alarmSuffix, {
delayInMinutes: 5,
});
chrome.alarms.create("cobwebAllowanceCheck" + alarmSuffix, {
delayInMinutes: 5,
});
chrome.alarms.onAlarm.addListener(async (alarm) => {
try {
var parsedName = alarm.name.match(/^([\S\s]*?)(\d+)$/);
Expand All @@ -471,30 +366,15 @@ chrome.alarms.onAlarm.addListener(async (alarm) => {
if (suffix !== alarmSuffix) {
return;
}
const { wallet, signer: sfSigner } = await getWalletAndSigner();
if (!sf || !sfSigner || !sfToken) {
if (!sf || !sfToken) {
return;
}
if (name === "cobwebStreamCleanup") {
cleanUpStreams({
sfSigner,
sfToken,
sf,
mmSigner: mmProvider.getSigner(),
});
} else if (name === "cobwebAllowanceCheck") {
const { address } = await storage.local.get("address");
if (!wallet || !address) {
return;
}
fetchBalance();
fetchCobwebAllowance({
sfToken,
sfSigner,
sf,
walletAddress: wallet.address,
mmAddress: address,
});
}
} catch {}
});
Expand Down
76 changes: 0 additions & 76 deletions src/pages/Background/lib/approveAmt.ts

This file was deleted.

Loading

0 comments on commit 48dae5f

Please sign in to comment.