-
Notifications
You must be signed in to change notification settings - Fork 21
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
97d1962
commit 80638f9
Showing
6 changed files
with
382 additions
and
42 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
2 changes: 1 addition & 1 deletion
2
single-factor-auth-web/quick-starts/sfa-vue-quick-start/src/App.vue
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<template> | ||
<Home /> | ||
<Home msg="Welcome to Your Vue.js + TypeScript App" /> | ||
</template> | ||
|
||
<script lang="ts"> | ||
|
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
95 changes: 95 additions & 0 deletions
95
single-factor-auth-web/quick-starts/sfa-vue-quick-start/src/ethersRPC.ts
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 |
---|---|---|
@@ -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
130
single-factor-auth-web/quick-starts/sfa-vue-quick-start/src/viemRPC.ts
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 |
---|---|---|
@@ -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 }; |
Oops, something went wrong.