Skip to content

Commit

Permalink
Refactor detectActiveWalletAddress to be in useAuth composable
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed Oct 5, 2023
1 parent e1eab91 commit efd124d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 14 additions & 1 deletion apps/web/src/composables/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import useWalletConnect from '@/composables/walletConnectV2'
import { Account, ApiResponse, LoginCredentials, ProviderString, UserAuthState } from '@casimir/types'

const { usersUrl } = useEnvironment()
const { ethersProviderList, detectActiveWalletAddress, loginWithEthers } = useEthers()
const { ethersProviderList, detectActiveEthersWalletAddress, loginWithEthers } = useEthers()
const { loginWithLedger } = useLedger()
const { loginWithTrezor } = useTrezor()
const { setUser, user } = useUser()
Expand Down Expand Up @@ -86,6 +86,17 @@ export default function useAuth() {
}
}

async function detectActiveWalletAddress(providerString: ProviderString) {
if (ethersProviderList.includes(providerString)) {
return await detectActiveEthersWalletAddress(providerString)
} else if (providerString === 'Ledger') {

// await loginWithLedger(loginCredentials, JSON.stringify(pathIndex))
} else {
alert('detectActiveWalletAddress not yet implemented for this wallet provider')
}
}

async function getUser() {
try {
const requestOptions = {
Expand Down Expand Up @@ -157,6 +168,8 @@ export default function useAuth() {
// TODO: Implement this for all providers
// Then check if address is the same as the one that is active in their wallet
const activeAddress = await detectActiveWalletAddress(provider as ProviderString)
console.log('activeAddress :>> ', activeAddress)
console.log('address :>> ', address)
if (activeAddress === address) {
await loginWithProvider({ provider: provider as ProviderString, address, currency: 'ETH' })
return 'Successfully logged in'
Expand Down
22 changes: 15 additions & 7 deletions apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ export default function useEthers() {
}
}

async function detectActiveWalletAddress(providerString: ProviderString) {
async function detectActiveEthersWalletAddress(providerString: ProviderString): Promise<string> {
const provider = getBrowserProvider(providerString)
if (ethersProviderList.includes(providerString)) {
const accounts = await provider.request({ method: 'eth_accounts' })
if (accounts.length > 0) return accounts[0]
} else {
alert('detectActiveWalletAddress not yet implemented for this wallet provider')
try {
if (provider) {
const accounts = await provider.request({ method: 'eth_accounts' })
if (accounts.length > 0) {
return accounts[0] as string
}
return ''
} else {
return ''
}
} catch(err) {
console.error('There was an error in detectActiveEthersWalletAddress :>> ', err)
return ''
}
}

Expand Down Expand Up @@ -241,7 +249,7 @@ export default function useEthers() {

return {
ethersProviderList,
detectActiveWalletAddress,
detectActiveEthersWalletAddress,
getEthersAddressesWithBalances,
getEthersBalance,
getEthersBrowserSigner,
Expand Down

0 comments on commit efd124d

Please sign in to comment.