Skip to content

Commit

Permalink
adding connect button
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Oct 4, 2023
1 parent 1a226b1 commit bf0177e
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions tools/walletextension/api/staticOG/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ async function isMetamaskConnected() {
let accounts;
try {
accounts = await provider.listAccounts()
console.log(accounts)
return accounts.length > 0;

} catch (error) {
Expand Down Expand Up @@ -237,33 +236,51 @@ const initialize = async () => {
let userID = await getUserID()


// load the current version
await fetchAndDisplayVersion();
function displayOnlyConnectButton(){
joinButton.style.display = "none"
addAccountButton.style.display = "none"
addAllAccountsButton.style.display = "none"
revokeUserIDButton.style.display = "none"
accountsTable.style.display = "none"
connectButton.style.display = "block"
}

// check if user already connected accounts
if (await isMetamaskConnected()) {
console.log("Connected")
} else {
console.log("Not connected")
function displayOnlyJoin() {
joinButton.style.display = "block"
addAccountButton.style.display = "none"
addAllAccountsButton.style.display = "none"
revokeUserIDButton.style.display = "none"
accountsTable.style.display = "none"
connectButton.style.display = "none"
}

// check if userID exists and has a correct type and length (is valid) and display either
// option to join or to add a new account to existing user
if (isValidUserIDFormat(userID)) {
async function displayConnectedAndJoinedSuccessfully() {
joinButton.style.display = "none"
addAccountButton.style.display = "block"
addAllAccountsButton.style.display = "block"
revokeUserIDButton.style.display = "block"
accountsTable.style.display = "block"
connectButton.style.display = "block"
connectButton.style.display = "none"
await populateAccountsTable(document, tableBody, userID)
}

// load the current version
await fetchAndDisplayVersion();

// handle which buttons should be shown to the user
if (await isMetamaskConnected()) {
// check if userID exists and has a correct type and length (is valid) and display either
// option to join or to add a new account to existing user
if (isValidUserIDFormat(userID)) {
await displayConnectedAndJoinedSuccessfully()
} else {
displayOnlyJoin()
}
} else {
joinButton.style.display = "block"
addAccountButton.style.display = "none"
revokeUserIDButton.style.display = "none"
accountsTable.style.display = "none"
displayOnlyConnectButton()
}


joinButton.addEventListener(eventClick, async () => {
// join Obscuro Gateway
const joinResp = await fetch(
Expand All @@ -279,7 +296,6 @@ const initialize = async () => {

// save userID to the localStorage and hide button that enables users to join
userID = await joinResp.text();
joinButton.style.display = "none"

// add Obscuro network to Metamask
let networkAdded = await addNetworkToMetaMask(ethereum, userID, obscuroChainIDDecimal)
Expand All @@ -289,20 +305,14 @@ const initialize = async () => {
}
statusArea.innerText = "Successfully joined Obscuro Gateway";
// show users an option to add another account and revoke userID
addAccountButton.style.display = "block"
connectButton.style.display = "block"
addAllAccountsButton.style.display = "block"
revokeUserIDButton.style.display = "block"
accountsTable.style.display = "block"
await populateAccountsTable(document, tableBody, userID)

await displayConnectedAndJoinedSuccessfully()
})

addAccountButton.addEventListener(eventClick, async () => {
// check if we have userID and it is the correct length
if (!isValidUserIDFormat(userID)) {
statusArea.innerText = "\n Please join Obscuro network first"
joinButton.style.display = "block"
addAccountButton.style.display = "none"
displayOnlyJoin()
}

// Get an account and prompt user to sign joining with a selected account
Expand All @@ -311,18 +321,14 @@ const initialize = async () => {
statusArea.innerText = "No MetaMask accounts found."
return
}
let authenticateAccountStatus = await authenticateAccountWithObscuroGateway(ethereum, account, userID)
//statusArea.innerText = "\n Authentication status: " + authenticateAccountStatus
accountsTable.style.display = "block"
let _ = await authenticateAccountWithObscuroGateway(ethereum, account, userID)
await populateAccountsTable(document, tableBody, userID)
})

addAllAccountsButton.addEventListener(eventClick, async () => {
// check if we have userID and it is the correct length
if (!isValidUserIDFormat(userID)) {
statusArea.innerText = "\n Please join Obscuro network first"
joinButton.style.display = "block"
addAccountButton.style.display = "none"
displayOnlyJoin()
}

// Get an account and prompt user to sign joining with selected account
Expand All @@ -345,12 +351,7 @@ const initialize = async () => {
await populateAccountsTable(document, tableBody, userID)

if (result) {
joinButton.style.display = "block";
revokeUserIDButton.style.display = "none";
addAllAccountsButton.style.display = "none";
statusArea.innerText = "Revoking UserID successful. Please remove current network from Metamask."
addAccountButton.style.display = "none";
accountsTable.style.display = "none"
displayOnlyJoin()
} else {
statusArea.innerText = "Revoking UserID failed";
}
Expand All @@ -359,6 +360,7 @@ const initialize = async () => {
connectButton.addEventListener(eventClick, async () => {
console.log("connect button!")
await connectAccounts();
location.reload()
})

}
Expand Down

0 comments on commit bf0177e

Please sign in to comment.