-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement brave wallet connect and address detection
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
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
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,66 @@ | ||
import { ethers } from 'ethers' | ||
import { LoginCredentials } from '@casimir/types' | ||
import { EthersProvider } from '@/interfaces' | ||
import useAuth from '@/composables/auth' | ||
import useEthers from '@/composables/ethers' | ||
|
||
const { createSiweMessage, signInWithEthereum } = useAuth() | ||
const { getEthersBalance } = useEthers() | ||
|
||
export default function useBraveWallet() { | ||
|
||
function detectBraveWalletAndGetProvider() { | ||
const { ethereum } = window | ||
if (ethereum?.isBraveWallet) { | ||
return ethereum | ||
} else { | ||
window.open('https://brave.com/download/', '_blank') | ||
} | ||
} | ||
|
||
async function getAddress() : Promise<string | undefined> { | ||
try { | ||
await window.ethereum.enable() | ||
const selectedAddress = window.ethereum.selectedAddress | ||
return selectedAddress | ||
} catch (err) { | ||
console.log('Error connecting to Brave Wallet:', err) | ||
} | ||
} | ||
|
||
async function getBraveAddressWithBalance() { | ||
const braveWalletProvider = detectBraveWalletAndGetProvider() | ||
if (braveWalletProvider) { | ||
const address = await getAddress() | ||
const balance = await getEthersBalance(address as string) | ||
return [{ address, balance }] | ||
} | ||
} | ||
|
||
async function loginWithBraveWallet(loginCredentials: LoginCredentials) { | ||
const { provider, address, currency } = loginCredentials | ||
const trustWalletProvider = detectBraveWalletAndGetProvider() | ||
const web3Provider: ethers.providers.Web3Provider = new ethers.providers.Web3Provider(trustWalletProvider as EthersProvider) | ||
try { | ||
const message = await createSiweMessage(address, 'Sign in with Ethereum to the app.') | ||
const signer = web3Provider.getSigner() | ||
const signedMessage = await signer.signMessage(message) | ||
const ethersLoginResponse = await signInWithEthereum({ | ||
address, | ||
currency, | ||
message, | ||
provider, | ||
signedMessage | ||
}) | ||
return await ethersLoginResponse.json() | ||
} catch(err) { | ||
console.log('Error logging in: ', err) | ||
return err | ||
} | ||
} | ||
|
||
return { | ||
getBraveAddressWithBalance, | ||
loginWithBraveWallet | ||
} | ||
} |
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
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
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