Skip to content

Commit

Permalink
refactor: separate eth and gateway requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennievon committed Dec 1, 2023
1 parent d2e5e03 commit 8ace8cc
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 289 deletions.
152 changes: 152 additions & 0 deletions tools/walletextension/frontend/src/api/ethRequests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import {
nativeCurrency,
tenChainIDDecimal,
tenChainIDHex,
tenscanLink,
} from "@/lib/constants";
import { getNetworkName, getRandomIntAsString, isTenChain } from "@/lib/utils";
import { requestMethods } from "@/routes";
import { ethers } from "ethers";
import { accountIsAuthenticated, authenticateUser } from "./gateway";

const { ethereum } = typeof window !== "undefined" ? window : ({} as any);

const typedData = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
],
Authentication: [{ name: "Encryption Token", type: "address" }],
},
primaryType: "Authentication",
domain: {
name: "Ten",
version: "1.0",
chainId: tenChainIDDecimal,
},
message: {
"Encryption Token": "0x",
},
};

export const switchToTenNetwork = async () => {
if (!ethereum) {
throw new Error("No ethereum object found");
}
try {
await ethereum.request({
method: requestMethods.switchNetwork,
params: [{ chainId: tenChainIDHex }],
});

return 0;
} catch (error: any) {
return error.code;
}
};

export const connectAccounts = async () => {
if (!ethereum) {
throw new Error("No ethereum object found");
}
try {
return await ethereum.request({
method: requestMethods.connectAccounts,
});
} catch (error) {
console.error(error);
throw error;
}
};

export const getSignature = async (account: string, data: any) => {
if (!ethereum) {
throw new Error("No ethereum object found");
}
return await ethereum.request({
method: requestMethods.signTypedData,
params: [account, JSON.stringify(data)],
});
};

export const getUserID = async (provider: ethers.providers.Web3Provider) => {
if (!provider) {
return null;
}

try {
if (await isTenChain()) {
const id = await provider.send(requestMethods.getStorageAt, [
"getUserID",
getRandomIntAsString(0, 1000),
null,
]);
return id;
} else {
return null;
}
} catch (e: any) {
console.error(e);
throw e;
}
};

export async function addNetworkToMetaMask(rpcUrls: string[]) {
if (!ethereum) {
throw new Error("No ethereum object found");
}
try {
await ethereum.request({
method: requestMethods.addNetwork,
params: [
{
chainId: tenChainIDHex,
chainName: getNetworkName(),
nativeCurrency,
rpcUrls,
blockExplorerUrls: [tenscanLink],
},
],
});
return true;
} catch (error) {
console.error(error);
throw error;
}
}

export async function authenticateAccountWithTenGatewayEIP712(
userID: string,
account: string
): Promise<any> {
if (!userID) {
return;
}
try {
const isAuthenticated = await accountIsAuthenticated(userID, account);
if (isAuthenticated.status) {
return {
status: true,
message: "Account already authenticated",
};
}
const data = {
...typedData,
message: {
...typedData.message,
"Encryption Token": "0x" + userID,
},
};
const signature = await getSignature(account, data);

const auth = await authenticateUser(userID, {
signature,
address: account,
});
return auth;
} catch (error) {
throw error;
}
}
114 changes: 2 additions & 112 deletions tools/walletextension/frontend/src/api/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,8 @@
import { apiRoutes, requestMethods } from "../routes";
import { apiRoutes } from "../routes";
import { httpRequest } from ".";
import { pathToUrl } from "../routes/router";
import { getNetworkName } from "../lib/utils";
import {
tenChainIDHex,
tenscanLink,
nativeCurrency,
tenChainIDDecimal,
} from "../lib/constants";
import { AuthenticationResponse } from "@/types/interfaces/GatewayInterfaces";

const typedData = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
],
Authentication: [{ name: "Encryption Token", type: "address" }],
},
primaryType: "Authentication",
domain: {
name: "Ten",
version: "1.0",
chainId: tenChainIDDecimal,
},
message: {
"Encryption Token": "0x",
},
};

const { ethereum } = typeof window !== "undefined" ? window : ({} as any);

export async function switchToTenNetwork() {
if (!ethereum) {
return;
}
try {
await (window as any).ethereum.request({
method: requestMethods.switchNetwork,
params: [{ chainId: tenChainIDHex }],
});

return 0;
} catch (switchError: any) {
return switchError.code;
}
}

export async function fetchVersion(): Promise<string> {
return await httpRequest<string>({
method: "get",
Expand All @@ -69,48 +24,7 @@ export async function accountIsAuthenticated(
});
}

const getSignature = async (account: string, data: any) => {
if (!ethereum) {
return;
}
return await ethereum.request({
method: requestMethods.signTypedData,
params: [account, JSON.stringify(data)],
});
};

export async function authenticateAccountWithTenGatewayEIP712(
userID: string,
account: string
): Promise<any> {
if (!userID) {
return;
}
try {
const isAuthenticated = await accountIsAuthenticated(userID, account);
if (isAuthenticated.status) {
return "Account is already authenticated";
}
const data = {
...typedData,
message: {
...typedData.message,
"Encryption Token": "0x" + userID,
},
};
const signature = await getSignature(account, data);

const auth = await authenticateUser(userID, {
signature,
address: account,
});
return auth;
} catch (error) {
throw error;
}
}

const authenticateUser = async (
export const authenticateUser = async (
userID: string,
authenticateFields: {
signature: string;
Expand Down Expand Up @@ -143,27 +57,3 @@ export async function joinTestnet(): Promise<string> {
url: pathToUrl(apiRoutes.join),
});
}

export async function addNetworkToMetaMask(rpcUrls: string[]) {
if (!ethereum) {
return;
}
try {
await ethereum.request({
method: requestMethods.addNetwork,
params: [
{
chainId: tenChainIDHex,
chainName: getNetworkName(),
nativeCurrency,
rpcUrls,
blockExplorerUrls: [tenscanLink],
},
],
});
return true;
} catch (error) {
console.error(error);
return error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const Connected = () => {
size={"sm"}
onClick={() => connectAccount(account.name)}
>
{account.connected ? "Disconnect" : "Connect"}
Connect
</Button>
)}
</TableCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from "../../ui/dialog";
import Copy from "../common/copy";
import { testnetUrls, tenChainIDDecimal } from "../../../lib/constants";
import Link from "next/link";

const CONNECTION_STEPS = [
"Hit Connect to Ten and start your journey",
Expand Down
Loading

0 comments on commit 8ace8cc

Please sign in to comment.