Skip to content

Commit

Permalink
Update sfa vue qs
Browse files Browse the repository at this point in the history
  • Loading branch information
yashovardhan committed Sep 29, 2024
1 parent 97d1962 commit 80638f9
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
"@web3auth/base": "^9.0.0",
"@web3auth/ethereum-provider": "^9.0.0",
"@web3auth/single-factor-auth": "^9.0.0",
"core-js": "^3.26.1",
"firebase": "^10.4.0",
"vue": "^3.3.4",
"web3": "^4.2.2"
"@babel/plugin-transform-private-methods": "^7.24.7",
"core-js": "^3.38.1",
"ethers": "^6.13.2",
"firebase": "^10.13.0",
"viem": "^2.19.8",
"vue": "^3.4.38",
"web3": "^4.11.1"
},
"//IMP END": "IMP END - Web3Auth Installation",
"devDependencies": {
Expand All @@ -31,6 +34,6 @@
"@vue/eslint-config-typescript": "^11.0.2",
"eslint": "^8.29.0",
"eslint-plugin-vue": "^9.8.0",
"typescript": "~4.9.4"
"typescript": "~5.5.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<Home />
<Home msg="Welcome to Your Vue.js + TypeScript App" />
</template>

<script lang="ts">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,25 @@
<div id="console">
<h3>Console Output:</h3>
<pre></pre>
<div>
<button class="card" @click="getAccounts" style="cursor: pointer">Get Accounts</button>
</div>
<div>
<button class="card" @click="getBalance" style="cursor: pointer">Get Balance</button>
</div>
<div>
<button class="card" @click="signMessage" style="cursor: pointer">Sign Message</button>
</div>
<div>
<button class="card" @click="logout" style="cursor: pointer">Logout</button>
</div>
</div>
</div>
</main>

<footer>
<a
href="https://github.com/Web3Auth/web3auth-core-kit-examples/tree/main/single-factor-auth-web/quick-starts/sfa-vue-quick-start"
target="_blank"
rel="noopener noreferrer"
>
<a href="https://github.com/Web3Auth/web3auth-core-kit-examples/tree/main/single-factor-auth-web/quick-starts/sfa-vue-quick-start"
target="_blank" rel="noopener noreferrer">
Source code
</a>
</footer>
Expand All @@ -44,7 +53,11 @@ import { Web3Auth, decodeToken } from "@web3auth/single-factor-auth";
import { CHAIN_NAMESPACES, IProvider, ADAPTER_EVENTS, WEB3AUTH_NETWORK } from "@web3auth/base";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
// IMP END - Quick Start
import Web3 from "web3";
// IMP START - Blockchain Calls
import RPC from "./ethersRPC";
// import RPC from "./viemRPC";
// import RPC from "./web3RPC";
// IMP END - Blockchain Calls

// Firebase libraries for custom authentication
import { initializeApp } from "firebase/app";
Expand All @@ -69,15 +82,20 @@ export default {
const verifier = "w3a-firebase-demo";
// IMP END - Verifier Creation

// IMP START - Chain Config
const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x1", // Please use 0x1 for Mainnet
rpcTarget: "https://rpc.ankr.com/eth",
displayName: "Ethereum Mainnet",
blockExplorer: "https://etherscan.io/",
chainId: "0xaa36a7",
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Ethereum Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
};
// IMP END - Chain Config

const privateKeyProvider = new EthereumPrivateKeyProvider({
config: { chainConfig },
Expand Down Expand Up @@ -189,10 +207,7 @@ export default {
uiConsole("provider not initialized yet");
return;
}
const web3 = new Web3(provider as any);

// Get user's Ethereum public address
const address = await web3.eth.getAccounts();
const address = await RPC.getAccounts(provider);
uiConsole(address);
};

Expand All @@ -201,16 +216,7 @@ export default {
uiConsole("provider not initialized yet");
return;
}
const web3 = new Web3(provider as any);

// 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"
);
const balance = await RPC.getBalance(provider);
uiConsole(balance);
};

Expand All @@ -219,20 +225,19 @@ export default {
uiConsole("provider not initialized yet");
return;
}
const web3 = new Web3(provider as any);

// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const signedMessage = await RPC.signMessage(provider);
uiConsole(signedMessage);
};

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);
const sendTransaction = async () => {
if (!provider) {
uiConsole("provider not initialized yet");
return;
}
uiConsole("Sending Transaction...");
const transactionReceipt = await RPC.sendTransaction(provider);
uiConsole(transactionReceipt);
};
// IMP END - Blockchain Calls

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { IProvider } from "@web3auth/base";
import { ethers } from "ethers";

const getChainId = async (provider: IProvider): Promise<any> => {
try {
const ethersProvider = new ethers.BrowserProvider(provider);
// Get the connected Chain's ID
const networkDetails = await ethersProvider.getNetwork();
return networkDetails.chainId.toString();
} catch (error) {
return error;
}
};

const getAccounts = async (provider: IProvider): Promise<any> => {
try {
const ethersProvider = new ethers.BrowserProvider(provider);
const signer = await ethersProvider.getSigner();

// Get user's Ethereum public address
const address = signer.getAddress();

return await address;
} catch (error) {
return error;
}
};

const getBalance = async (provider: IProvider): Promise<string> => {
try {
const ethersProvider = new ethers.BrowserProvider(provider);
const signer = await ethersProvider.getSigner();

// Get user's Ethereum public address
const address = signer.getAddress();

// Get user's balance in ether
const balance = ethers.formatEther(
await ethersProvider.getBalance(address) // Balance is in wei
);

return balance;
} catch (error) {
return error as string;
}
};

const sendTransaction = async (provider: IProvider): Promise<any> => {
try {
const ethersProvider = new ethers.BrowserProvider(provider);
const signer = await ethersProvider.getSigner();

const destination = "0x40e1c367Eca34250cAF1bc8330E9EddfD403fC56";

const amount = ethers.parseEther("0.001");

// Submit transaction to the blockchain
const tx = await signer.sendTransaction({
to: destination,
value: amount,
maxPriorityFeePerGas: "5000000000", // Max priority fee per gas
maxFeePerGas: "6000000000000", // Max fee per gas
});

// Wait for transaction to be mined
const receipt = await tx.wait();

return receipt;
} catch (error) {
return error as string;
}
};

const signMessage = async (provider: IProvider): Promise<any> => {
try {
// For ethers v5
// const ethersProvider = new ethers.providers.Web3Provider(provider);
const ethersProvider = new ethers.BrowserProvider(provider);

// For ethers v5
// const signer = ethersProvider.getSigner();
const signer = await ethersProvider.getSigner();
const originalMessage = "YOUR_MESSAGE";

// Sign the message
const signedMessage = await signer.signMessage(originalMessage);

return signedMessage;
} catch (error) {
return error as string;
}
};

export default { getChainId, getAccounts, getBalance, sendTransaction, signMessage };
130 changes: 130 additions & 0 deletions single-factor-auth-web/quick-starts/sfa-vue-quick-start/src/viemRPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { createWalletClient, createPublicClient, custom, formatEther, parseEther } from "viem";
import { mainnet, polygonAmoy, sepolia } from "viem/chains";
import type { IProvider } from "@web3auth/base";

const getViewChain = (provider: IProvider) => {
switch (provider.chainId) {
case "1":
return mainnet;
case "0x13882":
return polygonAmoy;
case "0xaa36a7":
return sepolia;
default:
return mainnet;
}
};

const getChainId = async (provider: IProvider): Promise<any> => {
try {
const walletClient = createWalletClient({
transport: custom(provider),
});

const address = await walletClient.getAddresses();
console.log(address);

const chainId = await walletClient.getChainId();
return chainId.toString();
} catch (error) {
return error;
}
};
const getAccounts = async (provider: IProvider): Promise<any> => {
try {
const walletClient = createWalletClient({
chain: getViewChain(provider),
transport: custom(provider),
});

const address = await walletClient.getAddresses();

return address;
} catch (error) {
return error;
}
};

const getBalance = async (provider: IProvider): Promise<string> => {
try {
const publicClient = createPublicClient({
chain: getViewChain(provider),
transport: custom(provider),
});

const walletClient = createWalletClient({
chain: getViewChain(provider),
transport: custom(provider),
});

const address = await walletClient.getAddresses();

const balance = await publicClient.getBalance({ address: address[0] });
console.log(balance);
return formatEther(balance);
} catch (error) {
return error as string;
}
};

const sendTransaction = async (provider: IProvider): Promise<any> => {
try {
const publicClient = createPublicClient({
chain: getViewChain(provider),
transport: custom(provider),
});

const walletClient = createWalletClient({
chain: getViewChain(provider),
transport: custom(provider),
});

// data for the transaction
const destination = "0x40e1c367Eca34250cAF1bc8330E9EddfD403fC56";
const amount = parseEther("0.0001");
const address = await walletClient.getAddresses();

// Submit transaction to the blockchain
const hash = await walletClient.sendTransaction({
account: address[0],
to: destination,
value: amount,
});
console.log(hash);
const receipt = await publicClient.waitForTransactionReceipt({ hash });

return JSON.stringify(
receipt,
(key, value) => (typeof value === "bigint" ? value.toString() : value) // return everything else unchanged
);
} catch (error) {
return error;
}
};

const signMessage = async (provider: IProvider): Promise<any> => {
try {
const walletClient = createWalletClient({
chain: getViewChain(provider),
transport: custom(provider),
});

// data for signing
const address = await walletClient.getAddresses();
const originalMessage = "YOUR_MESSAGE";

// Sign the message
const hash = await walletClient.signMessage({
account: address[0],
message: originalMessage,
});

console.log(hash);

return hash.toString();
} catch (error) {
return error;
}
};

export default { getChainId, getAccounts, getBalance, sendTransaction, signMessage };
Loading

0 comments on commit 80638f9

Please sign in to comment.