Skip to content

Commit

Permalink
Merge pull request #298 from consensusnetworks/feature/pg-users
Browse files Browse the repository at this point in the history
Connect user account methods to PG
  • Loading branch information
ccali11 authored Mar 30, 2023
2 parents 09c8324 + 9789fec commit 270a47b
Show file tree
Hide file tree
Showing 31 changed files with 425 additions and 513 deletions.
10 changes: 8 additions & 2 deletions apps/web/src/composables/auth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { LoginCredentials } from '@casimir/types'
import useEnvironment from '@/composables/environment'
import { ProviderString } from '@casimir/types'
import { LoginCredentials, ProviderString } from '@casimir/types'

const { usersBaseURL } = useEnvironment()

export default function useAuth() {
/**
* Gets a message from the server to sign
*
* @param {ProviderString} provider - The provider the user is using to sign in
* @param {string} address - The user's address
* @returns {Promise<Response>} - The response from the message request
*/
async function getMessage(provider: ProviderString, address: string) {
const requestOptions = {
method: 'GET',
Expand Down
38 changes: 19 additions & 19 deletions apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { ref } from 'vue'
import { ethers } from 'ethers'
import { BrowserProviders } from '@/interfaces/BrowserProviders'
import { EthersProvider } from '@/interfaces/EthersProvider'
import { ProviderString } from '@casimir/types'
import { TransactionInit } from '@/interfaces/TransactionInit'
import { MessageInit } from '@/interfaces/MessageInit'
import { BrowserProviders, EthersProvider, MessageInit, TransactionInit } from '@/interfaces/index'
import { Currency, ProviderString } from '@casimir/types'
import useAuth from '@/composables/auth'
import { Currency } from '@casimir/types'

const { getMessage, login } = useAuth()

Expand Down Expand Up @@ -117,26 +113,30 @@ export default function useEthers() {
async function loginWithEthers(provider: ProviderString, address: string, currency: Currency) {
const browserProvider = availableProviders.value[provider as keyof BrowserProviders]
const web3Provider: ethers.providers.Web3Provider = new ethers.providers.Web3Provider(browserProvider as EthersProvider)
const messageJson = await getMessage(provider, address)
const { message } = await messageJson.json()
const signer = web3Provider.getSigner()
const signature = await signer.signMessage(message)
const response = await login({
provider,
address,
message: message.toString(),
signedMessage: signature,
currency
})
return await response.json()
try {
const { message } = await (await getMessage(provider, address)).json()
const signer = web3Provider.getSigner()
const signature = await signer.signMessage(message)
const ethersLoginResponse = await login({
provider,
address,
message: message.toString(),
signedMessage: signature,
currency
})
return await ethersLoginResponse.json()
} catch (err) {
console.log('Error logging in: ', err)
return err
}
}

async function getEthersBrowserProviderSelectedCurrency(providerString: ProviderString) {
// IOTEX Smart Contract Address: 0x6fb3e0a217407efff7ca062d46c26e5d60a14d69
const browserProvider = availableProviders.value[providerString as keyof BrowserProviders]
const web3Provider: ethers.providers.Web3Provider = new ethers.providers.Web3Provider(browserProvider as EthersProvider)
const network = await web3Provider.getNetwork()
console.log('network.chainId :>> ', network.chainId)
// console.log('network.chainId :>> ', network.chainId)
const { currency } = currenciesByChainId[network.chainId.toString() as keyof typeof currenciesByChainId]
return currency
}
Expand Down
3 changes: 1 addition & 2 deletions apps/web/src/composables/ledger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BitcoinLedgerSigner, EthersLedgerSigner } from '@casimir/wallets'
import { ethers } from 'ethers'
import { TransactionInit } from '@/interfaces/TransactionInit'
import { MessageInit } from '@/interfaces/MessageInit'
import { MessageInit, TransactionInit } from '@/interfaces/index'
import useEnvironment from '@/composables/environment'
import useEthers from '@/composables/ethers'

Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/composables/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import {
SystemProgram,
PublicKey
} from '@solana/web3.js'
import { BrowserProviders } from '@/interfaces/BrowserProviders'
import { BrowserProviders, MessageInit, TransactionInit } from '@/interfaces/index'
import { ProviderString } from '@casimir/types'
import { TransactionInit } from '@/interfaces/TransactionInit'
import { MessageInit } from '@/interfaces/MessageInit'

export default function useSolana() {
const solanaProviderList = ['Phantom']
Expand Down
3 changes: 1 addition & 2 deletions apps/web/src/composables/ssv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import useEthers from './ethers'
import useLedger from './ledger'
import useTrezor from './trezor'
import useWalletConnect from './walletConnect'
import { ProviderString } from '@casimir/types'
import { Pool } from '@casimir/types'
import { Pool, ProviderString } from '@casimir/types'

/** SSV Manager contract */
let ssvManager: SSVManager
Expand Down
3 changes: 1 addition & 2 deletions apps/web/src/composables/trezor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { EthersTrezorSigner } from '@casimir/wallets'
import useEthers from '@/composables/ethers'
import useEnvironment from '@/composables/environment'
import { ethers } from 'ethers'
import { MessageInit } from '@/interfaces/MessageInit'
import { TransactionInit } from '@/interfaces/TransactionInit'
import { MessageInit, TransactionInit } from '@/interfaces/index'

const trezorPath = 'm/44\'/60\'/0\'/0/0'

Expand Down
69 changes: 21 additions & 48 deletions apps/web/src/composables/users.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { onMounted, ref } from 'vue'
import { AddAccountOptions, ProviderString, RemoveAccountOptions, User } from '@casimir/types'
import { ethers } from 'ethers'
import useEnvironment from '@/composables/environment'
import useSSV from '@/composables/ssv'
import useWallet from '@/composables/wallet'
import { onMounted, ref } from 'vue'
import { User } from '@casimir/types'
import { Account } from '@casimir/types'
import { Currency } from '@casimir/types'
import { ProviderString } from '@casimir/types'

const { usersBaseURL, ethereumURL } = useEnvironment()

Expand Down Expand Up @@ -125,62 +122,38 @@ export default function useUsers () {
// subscribeToUserEvents()
// })

async function addAccount(provider: ProviderString, address: string, currency: Currency): Promise<{ error: boolean, message: string, data: User | null }> {
address = address.toLowerCase()
const account = user.value?.accounts?.find((account: Account) => {
const accountAddress = account.address.toLowerCase()
const accountProvider = account.walletProvider
const accountCurrency = account.currency
const addressIsEqual = accountAddress === address
const providerIsEqual = accountProvider === provider
const currencyIsEqual = accountCurrency === currency
const isEqual = addressIsEqual && providerIsEqual && currencyIsEqual
return isEqual
}) as Account
if (account) {
return { error: false, message: `Account already exists on user: ${account}`, data: user.value }
} else {
const accountToAdd = {
address,
currency,
balance: '0', // TODO: Decide how we want to handle this
balanceSnapshots: [],
roi: 0,
walletProvider: provider
}
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
address: user.value?.address,
account: accountToAdd
})
}
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 addAccount(account: AddAccountOptions): Promise<{ error: boolean, message: string, data: User | null }> {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ 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 }
}

// TODO: Refactor this next. 2/14
async function removeAccount(provider: ProviderString, address: string, currency: Currency) {
async function removeAccount({ address, currency, ownerAddress, walletProvider }: RemoveAccountOptions) {
address = address.toLowerCase()
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
primaryAddress: user.value?.address,
provider,
address,
currency
currency,
ownerAddress,
walletProvider,
})
}
return await fetch(`${usersBaseURL}/user/remove-sub-account`, requestOptions)
const response = await fetch(`${usersBaseURL}/user/remove-sub-account`, requestOptions)
const { data: userAccount } = await response.json()
user.value = userAccount
return { error: false, message: `Account removed from user: ${userAccount}`, data: userAccount }
}

async function getMessage(address: string) {
Expand Down
Loading

0 comments on commit 270a47b

Please sign in to comment.