Skip to content

Commit

Permalink
Merge pull request #360 from consensusnetworks/update/type-errors
Browse files Browse the repository at this point in the history
Update/type errors
  • Loading branch information
ccali11 authored Jun 22, 2023
2 parents 506c344 + cdeb37d commit 0399f4a
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 379 deletions.
67 changes: 39 additions & 28 deletions apps/web/src/composables/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@ export default function useAuth() {
* @returns {Promise<Response>} - The response from the message request
*/
async function createSiweMessage(address: string, statement: string) {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
address
})
try {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
address
})
}
const res = await fetch(`${usersBaseURL}/auth/nonce`, requestOptions)
const { error, message: resMessage, data: nonce } = (await res.json())
if (error) throw new Error(resMessage)
const message = {
domain,
address,
statement,
uri: origin,
version: '1',
chainId: 5,
nonce
}
return prepareMessage(message)
} catch (error: any) {
throw new Error(error.message || 'Error creating SIWE message')
}
const res = await fetch(`${usersBaseURL}/auth/nonce`, requestOptions)
const { nonce } = (await res.json())
const message = {
domain,
address,
statement,
uri: origin,
version: '1',
chainId: 5,
nonce
}
return prepareMessage(message)
}

/**
Expand All @@ -43,15 +48,21 @@ export default function useAuth() {
* @param {LoginCredentials} loginCredentials - The user's address, provider, currency, message, and signed message
* @returns {Promise<Response>} - The response from the login request
*/
async function signInWithEthereum(loginCredentials: LoginCredentials) {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(loginCredentials)
async function signInWithEthereum(loginCredentials: LoginCredentials): Promise<void> {
try {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(loginCredentials)
}
const response = await fetch(`${usersBaseURL}/auth/login`, requestOptions)
const { error, message } = await response.json()
if (error) throw new Error(message)
} catch (error: any) {
throw new Error(error.message || 'Error signing in with Ethereum')
}
return await fetch(`${usersBaseURL}/auth/login`, requestOptions)
}

return {
Expand Down
23 changes: 6 additions & 17 deletions apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function useEthers() {
method: 'wallet_addEthereumChain',
params: [network]
})
} catch(error: any) {
} catch(error) {
console.log(`Error occurred while adding network ${network.chainName}, Message: ${error.message} Code: ${error.code}`)
}
}
Expand Down Expand Up @@ -92,13 +92,6 @@ export default function useEthers() {
}
}

async function getEthersAddress (providerString: ProviderString) {
const provider = getBrowserProvider(providerString)
if (provider) {
return (await requestEthersAccount(provider as EthersProvider))
}
}

async function getEthersAddressWithBalance (providerString: ProviderString) {
const provider = getBrowserProvider(providerString)

Expand Down Expand Up @@ -151,25 +144,23 @@ export default function useEthers() {
return maxAfterFees
}

async function loginWithEthers(loginCredentials: LoginCredentials) {
async function loginWithEthers(loginCredentials: LoginCredentials): Promise<void>{
const { provider, address, currency } = loginCredentials
const browserProvider = getBrowserProvider(provider)
const web3Provider: ethers.providers.Web3Provider = new ethers.providers.Web3Provider(browserProvider 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({
await signInWithEthereum({
address,
currency,
message,
provider,
signedMessage
})
return await ethersLoginResponse.json()
} catch (err) {
console.log('Error logging in: ', err)
return err
throw new Error(err.message)
}
}

Expand Down Expand Up @@ -205,8 +196,7 @@ export default function useEthers() {
async function signEthersMessage(messageRequest: MessageRequest): Promise<string> {
const { providerString, message } = messageRequest
const browserProvider = getBrowserProvider(providerString)
const web3Provider: ethers.providers.Web3Provider =
new ethers.providers.Web3Provider(browserProvider as EthersProvider)
const web3Provider: ethers.providers.Web3Provider = new ethers.providers.Web3Provider(browserProvider as EthersProvider)
const signer = web3Provider.getSigner()
const signature = await signer.signMessage(message)
return signature
Expand All @@ -226,7 +216,7 @@ export default function useEthers() {
method:'wallet_switchEthereumChain',
params: [{chainId: chainId}]
})
} catch(err: any){
} catch (err) {
console.log(`Error occurred while switching chain to chainId ${chainId}, err: ${err.message} code: ${err.code}`)
if (err.code === 4902){
if (chainId === '5') {
Expand All @@ -245,7 +235,6 @@ export default function useEthers() {
estimateLegacyGasFee,
ethersProviderList,
getMaxETHAfterFees,
getEthersAddress,
getEthersAddressWithBalance,
getEthersBalance,
getEthersBrowserSigner,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/composables/trezor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function useTrezor() {
signedMessage
})
return await loginResponse.json()
} catch (error: any) {
} catch (error) {
console.log(error)
throw new Error(error)
}
Expand Down
109 changes: 57 additions & 52 deletions apps/web/src/composables/users.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,65 @@
import { onMounted, ref } from 'vue'
import { AddAccountOptions, ProviderString, RemoveAccountOptions, UserWithAccounts, Account, ExistingUserCheck } from '@casimir/types'
import { ethers } from 'ethers'
import { ref } from 'vue'
import { AddAccountOptions, ProviderString, RemoveAccountOptions, UserWithAccounts, ExistingUserCheck, ApiResponse } from '@casimir/types'
import useEnvironment from '@/composables/environment'
import useContracts from '@/composables/contracts'
import useWallet from '@/composables/wallet'
import * as Session from 'supertokens-web-js/recipe/session'

const { usersBaseURL, ethereumURL } = useEnvironment()
const { usersBaseURL } = useEnvironment()

// 0xd557a5745d4560B24D36A68b52351ffF9c86A212
const session = ref<boolean>(false)
const user = ref<UserWithAccounts>()

export default function useUsers () {

async function addAccount(account: AddAccountOptions): Promise<{ error: boolean, message: string, data: UserWithAccounts | null }> {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ account })
async function addAccount(account: AddAccountOptions): Promise<ApiResponse> {
try {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ account })
}
const response = await fetch(`${usersBaseURL}/user/add-sub-account`, requestOptions)
const { error, message, data: userAccount } = await response.json()
user.value = userAccount
return { error, message, data: userAccount }
} catch (error: any) {
throw new Error(error.message || 'Error adding account')
}
const response = await fetch(`${usersBaseURL}/user/add-sub-account`, requestOptions)
const { data: userAccount } = await response.json()
user.value = userAccount
return { error: false, message: `Account added to user: ${userAccount}`, data: userAccount }
}

async function checkIfPrimaryUserExists(provider: ProviderString, address: string): Promise<ExistingUserCheck> {
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
async function checkIfPrimaryUserExists(provider: ProviderString, address: string): Promise<ApiResponse> {
try {
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
const response = await fetch(`${usersBaseURL}/user/check-if-primary-address-exists/${provider}/${address}`, requestOptions)
const { error, message, data } = await response.json()
if (error) throw new Error(message)
return { error, message, data }
} catch (error: any) {
throw new Error(error.message || 'Error checking if primary user exists')
}
const response = await fetch(`${usersBaseURL}/auth/check-if-primary-address-exists/${provider}/${address}`, requestOptions)
const { sameAddress, sameProvider } = await response.json()
return { sameAddress, sameProvider }
}

async function checkIfSecondaryAddress(address: string) : Promise<Account[]> {
async function checkIfSecondaryAddress(address: string) : Promise<ApiResponse> {
try {
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
const response = await fetch(`${usersBaseURL}/auth/check-secondary-address/${address}`, requestOptions)
const json = await response.json()
const { users } = json
return users
} catch (error) {
console.log('Error in checkIfSecondaryAddress in wallet.ts :>> ', error)
return [] as Account[]
const response = await fetch(`${usersBaseURL}/user/check-secondary-address/${address}`, requestOptions)
const { error, message, data } = await response.json()
if (error) throw new Error(message)
return { error, message, data }
} catch (error: any) {
throw new Error(error.message || 'Error checking if secondary address')
}
}

Expand All @@ -67,7 +72,7 @@ export default function useUsers () {
try {
session.value = await Session.doesSessionExist()
if (session.value) {
const user = await getUser()
const { data: user } = await getUser()
if (user) {
setUser(user)
return true
Expand All @@ -76,7 +81,7 @@ export default function useUsers () {
}
}
return false
} catch (error) {
} catch (error: any) {
console.log('Error in checkUserSessionExists in wallet.ts :>> ', error)
return false
}
Expand All @@ -89,26 +94,26 @@ export default function useUsers () {
return message
}

async function getUser() {
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json'
async function getUser() : Promise<ApiResponse> {
try {
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
const response = await fetch(`${usersBaseURL}/user`, requestOptions)
const { user, error, message } = await response.json()
return {
error,
message,
data: user
}
} catch (error: any) {
throw new Error('Error getting user from API route')
}
const response = await fetch(`${usersBaseURL}/user`, requestOptions)
const { user } = await response.json()
return user
}

// onMounted(async () => {
// const { getUserBalance } = useWallet()
// // Just get pools for primary account for demo
// user.value.balance = ethers.utils.formatEther(await getUserBalance(user.value.id))
// user.value.pools = await getPools(user.value.id)
// subscribeToUserEvents()
// })

async function removeAccount({ address, currency, ownerAddress, walletProvider }: RemoveAccountOptions) {
address = address.toLowerCase()
const requestOptions = {
Expand Down
Loading

0 comments on commit 0399f4a

Please sign in to comment.