Skip to content

Commit

Permalink
Final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsraham committed Sep 21, 2023
1 parent 98b27b5 commit 1f959fd
Show file tree
Hide file tree
Showing 22 changed files with 578 additions and 267 deletions.
5 changes: 5 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# .prettierrc or .prettierrc.yaml
printWidth: 150
singleQuote: false
semi: true
trailingComma: es5
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"editor.codeActionsOnSave": {
"source.fixAll": true
},
"eslint.workingDirectories": [
{
"directory": "web-no-modal-sdk/**/*",
Expand Down
16 changes: 8 additions & 8 deletions single-factor-auth-web/sfa-next-jwt-example/app/evm.ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default class EthereumRpc {
async getAccounts(): Promise<string> {
try {
// For ethers v5
// const provider = new ethers.providers.Web3Provider(this.provider as any);
const provider = new ethers.BrowserProvider(this.provider as any);
// const provider = new ethers.providers.Web3Provider(this.provider as IProvider);
const provider = new ethers.BrowserProvider(this.provider as IProvider);
// For ethers v5
// const signer = provider.getSigner();
const signer = await provider.getSigner();
Expand All @@ -26,8 +26,8 @@ export default class EthereumRpc {
async getBalance(): Promise<string> {
try {
// For ethers v5
// const provider = new ethers.providers.Web3Provider(this.provider as any);
const provider = new ethers.BrowserProvider(this.provider as any);
// const provider = new ethers.providers.Web3Provider(this.provider as IProvider);
const provider = new ethers.BrowserProvider(this.provider as IProvider);
// For ethers v5
// const signer = provider.getSigner();
const signer = await provider.getSigner();
Expand All @@ -50,8 +50,8 @@ export default class EthereumRpc {
async signMessage(): Promise<string> {
try {
// For ethers v5
// const provider = new ethers.providers.Web3Provider(this.provider as any);
const provider = new ethers.BrowserProvider(this.provider as any);
// const provider = new ethers.providers.Web3Provider(this.provider as IProvider);
const provider = new ethers.BrowserProvider(this.provider as IProvider);
// For ethers v5
// const signer = provider.getSigner();
const signer = await provider.getSigner();
Expand All @@ -68,8 +68,8 @@ export default class EthereumRpc {
async signAndSendTransaction(): Promise<any> {
try {
// For ethers v5
// const provider = new ethers.providers.Web3Provider(this.provider as any);
const provider = new ethers.BrowserProvider(this.provider as any);
// const provider = new ethers.providers.Web3Provider(this.provider as IProvider);
const provider = new ethers.BrowserProvider(this.provider as IProvider);
// For ethers v5
// const signer = provider.getSigner();
const signer = await provider.getSigner();
Expand Down
8 changes: 4 additions & 4 deletions single-factor-auth-web/sfa-next-jwt-example/app/evm.web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class EthereumRpc {

async getAccounts(): Promise<string[]> {
try {
const web3 = new Web3(this.provider as any);
const web3 = new Web3(this.provider as IProvider);
const accounts = await web3.eth.getAccounts();
return accounts;
} catch (error: unknown) {
Expand All @@ -20,7 +20,7 @@ export default class EthereumRpc {

async getBalance(): Promise<string> {
try {
const web3 = new Web3(this.provider as any);
const web3 = new Web3(this.provider as IProvider);
const accounts = await web3.eth.getAccounts();
const balance = await web3.eth.getBalance(accounts[0]);
return balance.toString();
Expand All @@ -31,7 +31,7 @@ export default class EthereumRpc {

async signMessage(): Promise<string | undefined> {
try {
const web3 = new Web3(this.provider as any);
const web3 = new Web3(this.provider as IProvider);
const accounts = await web3.eth.getAccounts();
const message = "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad";
(web3.currentProvider as any)?.send(
Expand All @@ -54,7 +54,7 @@ export default class EthereumRpc {

async signAndSendTransaction(): Promise<string> {
try {
const web3 = new Web3(this.provider as any);
const web3 = new Web3(this.provider as IProvider);
const accounts = await web3.eth.getAccounts();

const txRes = await web3.eth.sendTransaction({
Expand Down
68 changes: 63 additions & 5 deletions single-factor-auth-web/sfa-next-jwt-example/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const verifier = "w3a-jwt-for-sfa-web";
const clientId = "BEglQSgt4cUWcj6SKRdu5QkOXTsePmMcusG5EAoyjyOYKlVRjIF1iCNnMOTfpzCiunHRrMui8TIwQPXdkQ8Yxuk"; // get from https://dashboard.web3auth.io

const chainConfig = {
chainId: "0x1",
displayName: "Ethereum Mainnet",
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x5",
rpcTarget: "https://rpc.ankr.com/eth_goerli",
displayName: "Goerli Testnet",
blockExplorer: "https://goerli.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
ticker: "ETH",
decimals: 18,
rpcTarget: "https://rpc.ankr.com/eth",
blockExplorer: "https://etherscan.io",
};

// Initialising Web3Auth Single Factor Auth SDK
Expand Down Expand Up @@ -173,6 +174,43 @@ function App() {
uiConsole(result);
};

const authenticateUser = async () => {
try {
const userCredential = await web3authSfa.authenticateUser();
uiConsole(userCredential);
} catch (err) {
uiConsole(err);
}
};

const addChain = async () => {
try {
const newChain = {
chainId: "0x5",
displayName: "Goerli",
chainNamespace: CHAIN_NAMESPACES.EIP155,
tickerName: "Goerli",
ticker: "ETH",
decimals: 18,
rpcTarget: "https://rpc.ankr.com/eth_goerli",
blockExplorer: "https://goerli.etherscan.io",
};
await web3authSfa.addChain(newChain);
uiConsole("Chain added successfully");
} catch (err) {
uiConsole(err);
}
};

const switchChain = async () => {
try {
await web3authSfa.switchChain({ chainId: "0x5" });
uiConsole("Chain switched successfully");
} catch (err) {
uiConsole(err);
}
};

const loginView = (
<>
<div className="flex-container">
Expand All @@ -181,11 +219,31 @@ function App() {
Get User Info
</button>
</div>
<div>
<button onClick={() => uiConsole(idToken)} className="card">
Get OAuth ID Token
</button>
</div>
<div>
<button onClick={authenticateUser} className="card">
Authenticate User
</button>
</div>
<div>
<button onClick={getAccounts} className="card">
Get Accounts
</button>
</div>
<div>
<button onClick={addChain} className="card">
Add Chain
</button>
</div>
<div>
<button onClick={switchChain} className="card">
Switch Chain
</button>
</div>
<div>
<button onClick={getBalance} className="card">
Get Balance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffect, useState } from "react";
import { Web3Auth } from "@web3auth/single-factor-auth";
import { CHAIN_NAMESPACES } from "@web3auth/base";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
import { GoogleLogin, CredentialResponse, googleLogout } from '@react-oauth/google';
import { GoogleLogin, CredentialResponse, googleLogout } from "@react-oauth/google";

// RPC libraries for blockchain calls
// import RPC from "./evm.web3";
Expand All @@ -18,17 +18,17 @@ import Loading from "./Loading";

const verifier = "w3a-agg-google-auth0";

const clientId =
"BEglQSgt4cUWcj6SKRdu5QkOXTsePmMcusG5EAoyjyOYKlVRjIF1iCNnMOTfpzCiunHRrMui8TIwQPXdkQ8Yxuk"; // get from https://dashboard.web3auth.io
const clientId = "BEglQSgt4cUWcj6SKRdu5QkOXTsePmMcusG5EAoyjyOYKlVRjIF1iCNnMOTfpzCiunHRrMui8TIwQPXdkQ8Yxuk"; // get from https://dashboard.web3auth.io

const chainConfig = {
chainId: "0x1",
displayName: "Ethereum Mainnet",
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x5",
rpcTarget: "https://rpc.ankr.com/eth_goerli",
displayName: "Goerli Testnet",
blockExplorer: "https://goerli.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
ticker: "ETH",
decimals: 18,
rpcTarget: "https://rpc.ankr.com/eth",
blockExplorer: "https://etherscan.io",
};

// Initialising Web3Auth Single Factor Auth SDK
Expand Down Expand Up @@ -70,7 +70,7 @@ function App() {
return null;
}
};

const loginAuth0GitHub = async () => {
// trying logging in with the Single Factor Auth SDK
try {
Expand All @@ -88,8 +88,8 @@ function App() {
{
verifier: "w3a-auth0-github",
idToken: idToken!,
}
]
},
];
await web3authSfa.connect({
verifier,
verifierId: email,
Expand Down Expand Up @@ -122,8 +122,8 @@ function App() {
{
verifier: "w3a-google",
idToken: idToken!,
}
]
},
];
await web3authSfa.connect({
verifier,
verifierId: email,
Expand Down Expand Up @@ -210,6 +210,43 @@ function App() {
}
}

const authenticateUser = async () => {
try {
const userCredential = await web3authSfa.authenticateUser();
uiConsole(userCredential);
} catch (err) {
uiConsole(err);
}
};

const addChain = async () => {
try {
const newChain = {
chainId: "0x5",
displayName: "Goerli",
chainNamespace: CHAIN_NAMESPACES.EIP155,
tickerName: "Goerli",
ticker: "ETH",
decimals: 18,
rpcTarget: "https://rpc.ankr.com/eth_goerli",
blockExplorer: "https://goerli.etherscan.io",
};
await web3authSfa.addChain(newChain);
uiConsole("Chain added successfully");
} catch (err) {
uiConsole(err);
}
};

const switchChain = async () => {
try {
await web3authSfa.switchChain({ chainId: "0x5" });
uiConsole("Chain switched successfully");
} catch (err) {
uiConsole(err);
}
};

const loginView = (
<>
<div className="flex-container">
Expand All @@ -218,11 +255,31 @@ function App() {
Get User Info
</button>
</div>
<div>
<button onClick={() => uiConsole(parseToken(idToken))} className="card">
Get OAuth ID Token
</button>
</div>
<div>
<button onClick={authenticateUser} className="card">
Authenticate User
</button>
</div>
<div>
<button onClick={getAccounts} className="card">
Get Accounts
</button>
</div>
<div>
<button onClick={addChain} className="card">
Add Chain
</button>
</div>
<div>
<button onClick={switchChain} className="card">
Switch Chain
</button>
</div>
<div>
<button onClick={getBalance} className="card">
Get Balance
Expand Down Expand Up @@ -269,13 +326,7 @@ function App() {
SFA React Auth0 GitHub Example
</h1>

{loading ? (
<Loading />
): (
<div className="grid">
{isLoggedIn ? loginView : logoutView}
</div>
)}
{loading ? <Loading /> : <div className="grid">{isLoggedIn ? loginView : logoutView}</div>}

<footer className="footer">
<a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default class EthereumRpc {
async getAccounts(): Promise<string> {
try {
// For ethers v5
// const provider = new ethers.providers.Web3Provider(this.provider as any);
const provider = new ethers.BrowserProvider(this.provider as any);
// const provider = new ethers.providers.Web3Provider(this.provider as IProvider);
const provider = new ethers.BrowserProvider(this.provider as IProvider);
// For ethers v5
// const signer = provider.getSigner();
const signer = await provider.getSigner();
Expand All @@ -26,8 +26,8 @@ export default class EthereumRpc {
async getBalance(): Promise<string> {
try {
// For ethers v5
// const provider = new ethers.providers.Web3Provider(this.provider as any);
const provider = new ethers.BrowserProvider(this.provider as any);
// const provider = new ethers.providers.Web3Provider(this.provider as IProvider);
const provider = new ethers.BrowserProvider(this.provider as IProvider);
// For ethers v5
// const signer = provider.getSigner();
const signer = await provider.getSigner();
Expand All @@ -50,8 +50,8 @@ export default class EthereumRpc {
async signMessage(): Promise<string> {
try {
// For ethers v5
// const provider = new ethers.providers.Web3Provider(this.provider as any);
const provider = new ethers.BrowserProvider(this.provider as any);
// const provider = new ethers.providers.Web3Provider(this.provider as IProvider);
const provider = new ethers.BrowserProvider(this.provider as IProvider);
// For ethers v5
// const signer = provider.getSigner();
const signer = await provider.getSigner();
Expand All @@ -68,8 +68,8 @@ export default class EthereumRpc {
async signAndSendTransaction(): Promise<any> {
try {
// For ethers v5
// const provider = new ethers.providers.Web3Provider(this.provider as any);
const provider = new ethers.BrowserProvider(this.provider as any);
// const provider = new ethers.providers.Web3Provider(this.provider as IProvider);
const provider = new ethers.BrowserProvider(this.provider as IProvider);
// For ethers v5
// const signer = provider.getSigner();
const signer = await provider.getSigner();
Expand Down
Loading

0 comments on commit 1f959fd

Please sign in to comment.