Skip to content

Commit

Permalink
code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Sep 20, 2023
1 parent fb7aeae commit 75b565c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
6 changes: 2 additions & 4 deletions tools/walletextension/api/staticOG/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
<head>
<title>Obscuro Gateway </title>
<link rel="icon" type="favicon-32x32" sizes="32x32" href="favicon-32x32.png">
<script src="https://cdn.ethers.io/scripts/ethers-v4.min.js"
charset="utf-8"
type="text/javascript">
</script>
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
type="application/javascript"></script>
<script type="module" type="application/javascript" src="javascript.js"></script>
<link rel="stylesheet" href="style.css">
</head>
Expand Down
30 changes: 17 additions & 13 deletions tools/walletextension/api/staticOG/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const idAddAccount = "addAccount";
const idAddAllAccounts = "addAllAccounts";
const idRevokeUserID = "revokeUserID";
const idStatus = "status";
const idUserID = "userID";
const obscuroGatewayVersion = "v1"
const pathJoin = obscuroGatewayVersion + "/join/";
const pathAuthenticate = obscuroGatewayVersion + "/authenticate/";
Expand All @@ -18,7 +17,7 @@ const jsonHeaders = {
"Accept": "application/json",
"Content-Type": "application/json"
};
const metamaskRequestAccounts = "eth_requestAccounts";

const metamaskPersonalSign = "personal_sign";

function isValidUserIDFormat(value) {
Expand All @@ -27,6 +26,8 @@ function isValidUserIDFormat(value) {

let obscuroGatewayAddress = window.location.protocol + "//" + window.location.host;

let provider = null;


async function addNetworkToMetaMask(ethereum, userID, chainIDDecimal) {
// add network to MetaMask
Expand All @@ -46,6 +47,7 @@ async function addNetworkToMetaMask(ethereum, userID, chainIDDecimal) {
},
rpcUrls: [obscuroGatewayAddress+"/"+obscuroGatewayVersion+'/?u='+userID],
blockExplorerUrls: null,
iconUrls: ['https://raw.githubusercontent.com/obscuronet/go-obscuro/main/tools/walletextension/api/staticOG/Metamask%20Network%20Icon.png']
},
],
});
Expand Down Expand Up @@ -117,15 +119,12 @@ function getRandomIntAsString(min, max) {


async function getUserID() {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const userID = await provider.send('eth_getStorageAt', ["getUserID", getRandomIntAsString(0, 1000)])
return userID
return await provider.send('eth_getStorageAt', ["getUserID", getRandomIntAsString(0, 1000)])
}

async function requestAccounts() {
async function connectAccount() {
try {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
return accounts[0]; // Currently selected account
return await window.ethereum.request({ method: 'eth_requestAccounts' });
} catch (error) {
// TODO: Display warning to user to allow it and refresh page...
console.error('User denied account access:', error);
Expand Down Expand Up @@ -159,6 +158,7 @@ function checkIfMetamaskIsLoaded() {
function handleEthereum() {
const { ethereum } = window;
if (ethereum && ethereum.isMetaMask) {
provider = new ethers.providers.Web3Provider(window.ethereum);
initialize()
} else {
const statusArea = document.getElementById(idStatus);
Expand All @@ -168,7 +168,7 @@ function handleEthereum() {

async function populateAccountsTable(document, tableBody, userID) {
tableBody.innerHTML = '';
const accounts = await ethereum.request({method: 'eth_requestAccounts'});
const accounts = await provider.listAccounts();
for (const account of accounts) {
const row = document.createElement('tr');

Expand Down Expand Up @@ -253,13 +253,15 @@ const initialize = async () => {
addAccountButton.style.display = "none"
}

await connectAccount()

// Get an account and prompt user to sign joining with a selected account
const accounts = await ethereum.request({method: metamaskRequestAccounts});
if (accounts.length === 0) {
const account = await provider.getSigner().getAddress();
if (account.length === 0) {
statusArea.innerText = "No MetaMask accounts found."
return
}
let authenticateAccountStatus = await authenticateAccountWithObscuroGateway(ethereum, accounts[0], userID)
let authenticateAccountStatus = await authenticateAccountWithObscuroGateway(ethereum, account, userID)
//statusArea.innerText = "\n Authentication status: " + authenticateAccountStatus
accountsTable.style.display = "block"
await populateAccountsTable(document, tableBody, userID)
Expand All @@ -273,8 +275,10 @@ const initialize = async () => {
addAccountButton.style.display = "none"
}

await connectAccount()

// Get an account and prompt user to sign joining with selected account
const accounts = await ethereum.request({method: metamaskRequestAccounts});
const accounts = await provider.listAccounts();
if (accounts.length === 0) {
statusArea.innerText = "No MetaMask accounts found."
return
Expand Down

0 comments on commit 75b565c

Please sign in to comment.