Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework import logic, make onsignal error nonblocking #810

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 68 additions & 64 deletions src/features/onboarding/AccountAssignScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-shadow */
import AccountIcon from '@components/AccountIcon'
import Box from '@components/Box'
import CircleLoader from '@components/CircleLoader'
Expand All @@ -19,6 +20,7 @@ import { useTranslation } from 'react-i18next'
import { KeyboardAvoidingView, Platform, StyleSheet } from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { accountNetType } from '@utils/accountUtils'
import { ResolvedPath } from '@hooks/useDerivationAccounts'
import { RootNavigationProp } from '../../navigation/rootTypes'
import { useSolana } from '../../solana/SolanaProvider'
import { useAccountStorage } from '../../storage/AccountStorageProvider'
Expand Down Expand Up @@ -79,85 +81,87 @@ const AccountAssignScreen = () => {
return paths
}, [paths, route.params?.secretKey, route.params?.derivationPath])

const { execute: handlePress, loading } = useAsyncCallback(async () => {
const getName = (index: number): string => {
const name = `${alias} ${index + 1}`
if (!existingNames?.has(name)) {
return name
}

return getName(index + 1)
}
const storeAllSecureAccounts = useCallback(async () => {
const secureAccounts = allPaths
.slice()
.reverse()
.map((path) =>
toSecureAccount({
words,
keypair: path.keypair,
derivationPath: path.derivationPath,
}),
)

try {
let mnemonicHash: string | undefined
if (words) {
mnemonicHash = createHash('sha256')
.update(words.join(' '))
.digest('hex')
}
await Promise.all(secureAccounts.map(storeSecureAccount))
}, [words, allPaths])

const newAccounts = await Promise.all(
allPaths.map(async (p, index) => ({
alias: index === 0 ? alias : getName(index),
address: heliumAddressFromSolAddress(p.keypair.publicKey.toBase58()),
solanaAddress: p.keypair.publicKey.toBase58(),
derivationPath: p.derivationPath,
mnemonicHash,
version: 'v1' as CSAccountVersion,
balance: await connection?.getBalance(p.keypair.publicKey),
})),
)
const generateMnemonicHash = (words?: string[]): string | undefined => {
if (!words) return undefined
return createHash('sha256').update(words.join(' ')).digest('hex')
}

const highestBalanceAcc = newAccounts.reduce((acc, curr) => {
if (
curr.balance !== undefined &&
(acc.balance === undefined || curr.balance > acc.balance)
) {
return curr
}
const generateUniqueName = useCallback(
(baseAlias: string, index: number): string => {
const name = `${baseAlias} ${index + 1}`
return existingNames?.has(name)
? generateUniqueName(baseAlias, index + 1)
: name
},
[existingNames],
)

return acc
}, newAccounts[0])
const createNewAccounts = async (
paths: ResolvedPath[],
alias: string,
mnemonicHash?: string,
) => {
return Promise.all(
paths.map(async (p, index) => ({
alias: index === 0 ? alias : generateUniqueName(alias, index),
address: heliumAddressFromSolAddress(p.keypair.publicKey.toBase58()),
solanaAddress: p.keypair.publicKey.toBase58(),
derivationPath: p.derivationPath,
mnemonicHash,
version: 'v1' as CSAccountVersion,
balance: (await connection?.getBalance(p.keypair.publicKey)) ?? 0,
})),
)
}

await Promise.all(
allPaths.reverse().map(async (p) => {
await storeSecureAccount(
toSecureAccount({
words,
keypair: p.keypair,
derivationPath: p.derivationPath,
}),
)
}),
const { execute: handlePress, loading } = useAsyncCallback(async () => {
try {
await storeAllSecureAccounts()
const mnemonicHash = generateMnemonicHash(words)
const newAccounts = await createNewAccounts(allPaths, alias, mnemonicHash)
const highestBalanceAcc = newAccounts.reduce((prev, current) =>
(current.balance ?? 0) > (prev.balance ?? 0) ? current : prev,
)

await upsertAccounts(newAccounts)

setCurrentAccount({
...highestBalanceAcc,
netType: accountNetType(highestBalanceAcc.address),
})

if (setAsDefault) {
await updateDefaultAccountAddress(highestBalanceAcc.address)
}

reset()
} catch (e) {
console.error(e)
return
}
if (hasAccounts) {
rootNav.reset({
index: 0,
routes: [{ name: 'TabBarNavigator' }],
})
reset()
} else {
onboardingNav.navigate('AccountCreatePinScreen', {
pinReset: true,
})
}

if (hasAccounts) {
rootNav.reset({
index: 0,
routes: [{ name: 'TabBarNavigator' }],
})
} else {
onboardingNav.navigate('AccountCreatePinScreen', {
pinReset: true,
setCurrentAccount({
...highestBalanceAcc,
netType: accountNetType(highestBalanceAcc.address),
})
} catch (e) {
console.error(e)
}
})

Expand Down
81 changes: 31 additions & 50 deletions src/storage/AccountStorageProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import { useAppStorage } from './AppStorageProvider'
import {
CSAccount,
CSAccounts,
CSAccountVersion,
getCloudDefaultAccountAddress,
LedgerDevice,
restoreAccounts,
setCloudDefaultAccountAddress,
signoutCloudStorage,
Expand Down Expand Up @@ -221,18 +219,14 @@ const useAccountStorageHook = () => {
await storeSecureAccount(secureAccount)
}

let { mnemonicHash } = csAccount
if (secureAccount?.mnemonic && !mnemonicHash) {
mnemonicHash = createHash('sha256')
.update(secureAccount?.mnemonic.join(' '))
.digest('hex')
}

let { solanaAddress } = csAccount
const mnemonicHash = secureAccount?.mnemonic
? createHash('sha256')
.update(secureAccount.mnemonic.join(' '))
.digest('hex')
: csAccount.mnemonicHash

if (!solanaAddress) {
solanaAddress = heliumAddressToSolAddress(csAccount.address)
}
const solanaAddress =
csAccount.solanaAddress || heliumAddressToSolAddress(csAccount.address)

const nextAccount: CSAccount = {
...csAccount,
Expand All @@ -246,63 +240,50 @@ const useAccountStorageHook = () => {
...accounts,
[csAccount.address]: nextAccount,
}

setAccounts(nextAccounts)
setCurrentAccount(nextAccount)
await updateCloudAccounts(nextAccounts)
await tagAccount(csAccount.address)

await Promise.all([
updateCloudAccounts(nextAccounts),
tagAccount(csAccount.address),
])
},
[accounts],
)

const upsertAccounts = useCallback(
async (
accountBulk: {
alias: string
address: string
ledgerDevice?: LedgerDevice
ledgerIndex?: number
solanaAddress: string
derivationPath?: string
mnemonicHash?: string
version?: CSAccountVersion
}[],
accountBulk: Array<
Partial<CSAccount> & {
address: string
solanaAddress: string
ledgerIndex?: number
}
>,
) => {
if (!accountBulk.length) return

const bulkAccounts = accountBulk.reduce((prev, curr) => {
const accountIndex = curr.ledgerIndex || 0
return {
...prev,
[curr.address]: {
alias: curr.alias,
address: curr.address,
netType: accountNetType(curr.address),
ledgerDevice: curr.ledgerDevice,
accountIndex,
solanaAddress: curr.solanaAddress,
derivationPath: curr.derivationPath,
mnemonicHash: curr.mnemonicHash,
version: curr.version,
},
}
const bulkAccounts = accountBulk.reduce<CSAccounts>((acc, curr) => {
const { address, ledgerIndex, ...rest } = curr
acc[address] = {
...rest,
address,
netType: accountNetType(address),
accountIndex: ledgerIndex ?? 0,
} as CSAccount
return acc
}, {})

const nextAccounts: CSAccounts = {
...accounts,
...bulkAccounts,
}
const nextAccounts: CSAccounts = { ...accounts, ...bulkAccounts }

setAccounts(nextAccounts)
setCurrentAccount(
nextAccounts[accountBulk[accountBulk.length - 1].address],
)
await updateCloudAccounts(nextAccounts)

// eslint-disable-next-line no-restricted-syntax
for (const addr of Object.keys(bulkAccounts)) {
// eslint-disable-next-line no-await-in-loop
await tagAccount(addr)
}
await Promise.all(Object.keys(bulkAccounts).map(tagAccount))
},
[accounts],
)
Expand Down
22 changes: 16 additions & 6 deletions src/storage/oneSignalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import Bcrypt from 'bcrypt-react-native'

export const tagAccount = async (address: string) => {
if (!Config.ONE_SIGNAL_ACCOUNT_TAG_SALT) return
const salt = Config.ONE_SIGNAL_ACCOUNT_TAG_SALT
const hash = await Bcrypt.hash(salt, address)
OneSignal.OneSignal.User.addTag(hash, ' ')

try {
const salt = Config.ONE_SIGNAL_ACCOUNT_TAG_SALT
const hash = await Bcrypt.hash(salt, address)
OneSignal.OneSignal.User.addTag(hash, ' ')
} catch (err) {
console.error(err)
}
}

export const removeAccountTag = async (address: string) => {
if (!Config.ONE_SIGNAL_ACCOUNT_TAG_SALT) return
const salt = Config.ONE_SIGNAL_ACCOUNT_TAG_SALT
const hash = await Bcrypt.hash(salt, address)
OneSignal.OneSignal.User.removeTag(hash)

try {
const salt = Config.ONE_SIGNAL_ACCOUNT_TAG_SALT
const hash = await Bcrypt.hash(salt, address)
OneSignal.OneSignal.User.removeTag(hash)
} catch (err) {
console.error(err)
}
}
Loading