-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0d03ee
commit dd127ae
Showing
2 changed files
with
16 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,9 @@ <h1 class="title"> | |
<button id="get-balance" class="btn">Get Balance</button> | ||
<button id="sign-message" class="btn">Sign Message</button> | ||
<button id="logout" class="btn">Logout</button> | ||
<div class="console" id="console"> | ||
<p id="code" class="code"></p> | ||
</div> | ||
</div> | ||
<div class="console" id="console"> | ||
<p id="code" class="code"></p> | ||
</div> | ||
|
||
<!-- Logged Logout --> | ||
|
@@ -41,125 +41,6 @@ <h1 class="title"> | |
<script src="https://cdn.jsdelivr.net/npm/@web3auth/single-factor-auth"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/@web3auth/ethereum-provider"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script> | ||
<script> | ||
let web3auth = null; | ||
let provider = null; | ||
|
||
// // (async function init() { | ||
// $(".btn-logged-in").hide(); | ||
// $("#sign-tx").hide(); | ||
|
||
// const clientId = | ||
// "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"; // get your clientId from https://dashboard.web3auth.io | ||
|
||
|
||
// const chainConfig = { | ||
// chainNamespace: "eip155", | ||
// chainId: "0x1", // Please use 0x1 for Mainnet | ||
// rpcTarget: "https://rpc.ankr.com/eth", | ||
// displayName: "Ethereum Mainnet", | ||
// blockExplorer: "https://etherscan.io/", | ||
// ticker: "ETH", | ||
// tickerName: "Ethereum", | ||
// }; | ||
|
||
// // web3auth = new window.Modal.Web3Auth({ | ||
// // clientId, | ||
// // chainConfig, | ||
// // web3AuthNetwork: "sapphire_mainnet", | ||
// // }); | ||
|
||
// // await web3auth.initModal(); | ||
|
||
// if (web3auth.connected) { | ||
// $(".btn-logged-in").show(); | ||
// $(".btn-logged-out").hide(); | ||
// if (web3auth.connected === "openlogin") { | ||
// $("#sign-tx").show(); | ||
// } | ||
// } else { | ||
// $(".btn-logged-out").show(); | ||
// $(".btn-logged-in").hide(); | ||
// } | ||
// })(); | ||
|
||
$("#get-user-info").click(async function (event) { | ||
try { | ||
const user = await web3auth.getUserInfo(); | ||
uiConsole(user); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
}); | ||
|
||
$("#get-accounts").click(async function (event) { | ||
try { | ||
const web3 = new Web3(web3auth.provider); | ||
|
||
// Get user's Ethereum public address | ||
const address = await web3.eth.getAccounts(); | ||
uiConsole(address); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
}); | ||
|
||
$("#get-balance").click(async function (event) { | ||
try { | ||
const web3 = new Web3(web3auth.provider); | ||
|
||
// Get user's Ethereum public address | ||
const address = (await web3.eth.getAccounts())[0]; | ||
|
||
// Get user's balance in ether | ||
const balance = web3.utils.fromWei( | ||
await web3.eth.getBalance(address), // Balance is in wei | ||
"ether" | ||
); | ||
uiConsole(balance); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
}); | ||
|
||
$("#sign-message").click(async function (event) { | ||
try { | ||
const web3 = new Web3(web3auth.provider); | ||
// Get user's Ethereum public address | ||
const fromAddress = (await web3.eth.getAccounts())[0]; | ||
|
||
const originalMessage = "YOUR_MESSAGE"; | ||
|
||
// Sign the message | ||
const signedMessage = await web3.eth.personal.sign( | ||
originalMessage, | ||
fromAddress, | ||
"test password!" // configure your own password here. | ||
); | ||
uiConsole(signedMessage); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
}); | ||
|
||
$("#logout").click(async function (event) { | ||
try { | ||
await web3auth.logout(); | ||
$(".btn-logged-in").hide(); | ||
$(".btn-logged-out").show(); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
}); | ||
|
||
function uiConsole(...args) { | ||
const el = document.querySelector("#console>p"); | ||
if (el) { | ||
el.innerHTML = JSON.stringify(args || {}, null, 2); | ||
console.log(...args); | ||
} | ||
} | ||
</script> | ||
<script type = "module" src = "script.js"></script> | ||
<script type = "module" src = "./script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ let web3auth = null; | |
tickerName: "Ethereum", | ||
}; | ||
|
||
console.log(window.SingleFactorAuth.Web3Auth); | ||
uiConsole(window.SingleFactorAuth.Web3Auth); | ||
web3auth = new window.SingleFactorAuth.Web3Auth({ | ||
clientId, | ||
web3AuthNetwork: "sapphire_mainnet", // Get your Network from Web3Auth Dashboard | ||
|
@@ -65,30 +65,32 @@ let web3auth = null; | |
|
||
loginButton.addEventListener("click", async function() { | ||
email = '[email protected]'; | ||
console.log(email); | ||
password = 'Testing@123'; | ||
console.log(password); | ||
try{ | ||
uiConsole("Signing in with email and password in firebase"); | ||
response = await signInWithEmailAndPassword(auth, email , password); | ||
console.log(response.user); | ||
uiConsole(response.user); | ||
idToken = await response.user.getIdToken(true); | ||
console.log(idToken); | ||
uiConsole(idToken); | ||
await web3auth.connect({ | ||
verifier: "w3a-firebase-demo", | ||
verifierId: response.user.uid, | ||
idToken: idToken, | ||
}); | ||
$(".btn-logged-out").hide(); | ||
$(".btn-logged-in").show(); | ||
if(web3auth.status === "connected"){ | ||
uiConsole("Connected to Web3Auth"); | ||
$(".btn-logged-out").hide(); | ||
$(".btn-logged-in").show(); | ||
} | ||
} | ||
catch(error){ | ||
console.log(error); | ||
uiConsole(error); | ||
} | ||
}); | ||
|
||
$("#get-user-info").click(async function (event) { | ||
try { | ||
console.log(response); | ||
uiConsole(response); | ||
const userInfo = await response.user.getIdTokenResult(); | ||
uiConsole(userInfo); | ||
} catch (error) { | ||
|
@@ -160,8 +162,8 @@ function uiConsole(...args) { | |
const el = document.querySelector("#console>p"); | ||
if (el) { | ||
el.innerHTML = JSON.stringify(args || {}, null, 2); | ||
console.log(...args); | ||
} | ||
console.log(...args); | ||
} | ||
|
||
|