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

feat: update events to accomodate accounts #7895

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e7fb493
feat: add account to routes
evavirseda Dec 22, 2023
f59e885
feat: add to analytics featureflag
evavirseda Dec 22, 2023
0705215
fix: update naming
evavirseda Jan 4, 2024
3751426
feat: update wallet state to accomodate accounts
cpl121 Jan 4, 2024
ee32362
Merge branches 'feat/update-wallet-state-to-accomodate-accounts' and …
cpl121 Jan 4, 2024
5af0ce7
feat: update events to accomodate accounts WIP
cpl121 Jan 8, 2024
8010874
feat: improve validate wallet api event
cpl121 Jan 9, 2024
1533ab0
feat: improve imports
cpl121 Jan 9, 2024
2603f28
Merge branches 'feat/update-wallet-state-to-accomodate-accounts' and …
cpl121 Jan 11, 2024
36a0c8d
fix merge
cpl121 Jan 11, 2024
f6aedaf
feat: remove duplicate implicitAccountCreationRouter
cpl121 Jan 11, 2024
81edbf1
perf: update todo comment for getDepositAddress function
cpl121 Jan 11, 2024
55b2d8e
Merge branches 'feat/update-events-to-accomodate-accounts' and 'feat/…
cpl121 Jan 12, 2024
a4aaf36
feat: update dashboard to route to dashboard or implicit flow based o…
cpl121 Jan 15, 2024
7f31e15
Merge branches 'feat/update-events-to-accomodate-accounts' and 'feat/…
cpl121 Jan 15, 2024
9d4c85b
feat: update event to walletEvent
cpl121 Jan 16, 2024
ed67128
Merge branches 'feat/update-wallet-state-to-accomodate-accounts' and …
cpl121 Jan 16, 2024
95b7648
Merge branches 'feat/update-events-to-accomodate-accounts' and 'feat/…
cpl121 Jan 16, 2024
7469703
feat: update subscribeToWalletApiEventsForActiveProfile with new Wall…
cpl121 Jan 16, 2024
270303c
Merge branches 'feat/update-wallet-state-to-accomodate-accounts' and …
cpl121 Jan 16, 2024
129b81f
Merge branches 'feat/update-events-to-accomodate-accounts' and 'feat/…
cpl121 Jan 16, 2024
4ca6d8b
fix: move onDestroy to the last view of implicit account creation
cpl121 Jan 16, 2024
ea7bba0
Merge branches 'feat/update-wallet-state-to-accomodate-accounts' and …
cpl121 Jan 16, 2024
8cce33b
Merge branch 'feat/update-wallet-state-to-accomodate-accounts' of git…
cpl121 Jan 16, 2024
91b03f4
perf: hasAccount reactive
cpl121 Jan 16, 2024
b618966
perf: rename accountsOutput and implicitAccountsOutput
cpl121 Jan 16, 2024
28e035b
Merge branches 'feat/update-events-to-accomodate-accounts' and 'feat/…
cpl121 Jan 16, 2024
ce2d047
fix: undo unsubscribeFromWalletApiEvents place
cpl121 Jan 16, 2024
e7c5da1
perf: add implicit account logic to handleNewOutputEventInternal
cpl121 Jan 16, 2024
385d62c
Merge branches 'feat/update-events-to-accomodate-accounts' and 'devel…
cpl121 Jan 16, 2024
3c4aa02
feat: imporve isImplicitAccountOutput logic
cpl121 Jan 16, 2024
9ad42eb
Merge branches 'feat/update-events-to-accomodate-accounts' and 'devel…
cpl121 Jan 19, 2024
53d30a8
feat: check if the address is implicit account address
cpl121 Jan 19, 2024
e0a1598
feat: sync implicit accounts in the background if there arent account…
cpl121 Jan 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { checkAndUpdateActiveProfileNetwork } from './checkAndUpdateActiveProfil
import { checkAndRemoveProfilePicture } from './checkAndRemoveProfilePicture'
import { checkActiveProfileAuth, getWallets } from '@core/profile'
import { setStrongholdPasswordClearInterval, startBackgroundSync } from '@core/wallet/actions'
import { selectedWallet } from 'shared/lib/core/wallet'

// TODO(2.0) Remove usage of profile manager
export async function login(loginOptions?: ILoginOptions): Promise<void> {
Expand Down Expand Up @@ -149,7 +150,11 @@ export async function login(loginOptions?: ILoginOptions): Promise<void> {
// Step 8: start background sync
incrementLoginProgress()
subscribeToWalletApiEventsForActiveProfile()
await startBackgroundSync({ syncIncomingTransactions: true })
if (get(selectedWallet)?.accountOutputs.length === 0) {
await startBackgroundSync({ syncIncomingTransactions: true, syncImplicitAccounts: true })
} else {
await startBackgroundSync({ syncIncomingTransactions: true })
}

// Step 9: finish login
incrementLoginProgress()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
export function subscribeToWalletApiEventsForActiveProfile(): void {
const wallet = getSelectedWallet()
const eventMap: WalletApiEventMap = {
[WalletEventType.NewOutput]: handleNewOutputEvent,
[WalletEventType.SpentOutput]: handleSpentOutputEvent,
[WalletEventType.TransactionInclusion]: handleTransactionInclusionEvent,
[WalletEventType.TransactionProgress]: handleTransactionProgressEvent,
[WalletEventType.NewOutput]: handleNewOutputEvent(wallet.id),
[WalletEventType.SpentOutput]: handleSpentOutputEvent(wallet.id),
[WalletEventType.TransactionInclusion]: handleTransactionInclusionEvent(wallet.id),
[WalletEventType.TransactionProgress]: handleTransactionProgressEvent(wallet.id),
}
subscribeToWalletApiEvents({
eventMap,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Event, NewOutputWalletEvent, OutputType, WalletEventType } from '@iota/sdk/out/types'
import {
WalletEvent,
NewOutputWalletEvent,
OutputType,
WalletEventType,
CommonOutput,
UnlockConditionType,
AddressType,
AddressUnlockCondition,
} from '@iota/sdk/out/types'

import { addNftsToDownloadQueue, addOrUpdateNftInAllAccountNfts, buildNftFromNftOutput } from '@core/nfts'
import { checkAndRemoveProfilePicture } from '@core/profile/actions'
Expand All @@ -15,15 +24,18 @@ import {
getBech32AddressFromAddressTypes,
preprocessGroupedOutputs,
addActivitiesToWalletActivitiesInAllWalletActivities,
WalletApiEventHandler,
updateSelectedWallet,
} from '@core/wallet'
import { get } from 'svelte/store'
import { activeWallets, updateActiveWallet } from '@core/profile'

export function handleNewOutputEvent(error: Error, rawEvent: Event): void {
const { walletId, payload } = validateWalletApiEvent(error, rawEvent, WalletEventType.NewOutput)
const type = payload.type
if (type === WalletEventType.NewOutput) {
void handleNewOutputEventInternal(walletId, payload as NewOutputWalletEvent)
export function handleNewOutputEvent(walletId: string): WalletApiEventHandler {
return (error: Error, rawEvent: WalletEvent) => {
validateWalletApiEvent(error, rawEvent, WalletEventType.NewOutput)
if (rawEvent.type === WalletEventType.NewOutput) {
void handleNewOutputEventInternal(walletId, rawEvent as NewOutputWalletEvent)
}
}
}

Expand All @@ -36,12 +48,31 @@ export async function handleNewOutputEventInternal(walletId: string, payload: Ne

const output = outputData.output

const isImplicitAccountOutput =
output.type === OutputType.Basic &&
(output as CommonOutput).unlockConditions.length === 1 &&
(
(output as CommonOutput).unlockConditions.find(
(cmnOutput) => cmnOutput.type === UnlockConditionType.Address
) as AddressUnlockCondition
)?.address.type === AddressType.ImplicitAccountCreation

if (isImplicitAccountOutput) {
const implicitAccounts = await wallet.implicitAccounts()
updateSelectedWallet({
implicitAccountOutputs: implicitAccounts,
})
return
}

const address = getBech32AddressFromAddressTypes(outputData.address)
const isNewAliasOutput =
output.type === OutputType.Account &&
!get(allWalletActivities)[walletId].find((_activity) => _activity.id === outputData.outputId)
const isNftOutput = output.type === OutputType.Nft

const isAccountOutput = output.type === OutputType.Account

if ((wallet?.depositAddress === address && !outputData?.remainder) || isNewAliasOutput) {
await syncBalance(wallet.id)
const addressesWithOutputs = await getAddressesWithOutputs(wallet)
Expand Down Expand Up @@ -69,4 +100,11 @@ export async function handleNewOutputEventInternal(walletId: string, payload: Ne

checkAndRemoveProfilePicture()
}

if (isAccountOutput) {
const accounts = await wallet.accounts()
updateSelectedWallet({
accountOutputs: accounts,
})
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO(2.0) Fix all of events code, blocked by https://github.com/iotaledger/iota-sdk/issues/1708

import { Event, SpentOutputWalletEvent, WalletEventType } from '@iota/sdk/out/types'
import { WalletEvent, SpentOutputWalletEvent, WalletEventType } from '@iota/sdk/out/types'

import { getNftByIdFromAllAccountNfts, updateNftInAllWalletNfts } from '@core/nfts'
import {
Expand All @@ -10,15 +10,17 @@ import {
validateWalletApiEvent,
updateAsyncDataByTransactionId,
allWalletActivities,
WalletApiEventHandler,
} from '@core/wallet'
import { get } from 'svelte/store'
import { activeWallets, updateActiveWallet } from '@core/profile'

export async function handleSpentOutputEvent(error: Error, rawEvent: Event): Promise<void> {
const { walletId, payload } = validateWalletApiEvent(error, rawEvent, WalletEventType.SpentOutput)
const type = payload.type
if (type === WalletEventType.SpentOutput) {
await handleSpentOutputEventInternal(walletId, payload as SpentOutputWalletEvent)
export function handleSpentOutputEvent(walletId: string): WalletApiEventHandler {
return async (error: Error, rawEvent: WalletEvent) => {
validateWalletApiEvent(error, rawEvent, WalletEventType.SpentOutput)
if (rawEvent.type === WalletEventType.SpentOutput) {
await handleSpentOutputEventInternal(walletId, rawEvent as SpentOutputWalletEvent)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { closePopup, openPopup, PopupId } from '@auxiliary/popup'
import { Event, TransactionInclusionWalletEvent, WalletEventType } from '@iota/sdk/out/types'
import { WalletEvent, TransactionInclusionWalletEvent, WalletEventType } from '@iota/sdk/out/types'

import { updateParticipationOverview } from '@contexts/governance/stores'
import { isWalletVoting } from 'shared/lib/contexts/governance/utils/isWalletVoting'
Expand All @@ -16,15 +16,18 @@ import {
ActivityType,
GovernanceActivity,
InclusionState,
WalletApiEventHandler,
} from '@core/wallet'
import { get } from 'svelte/store'
import { activeWallets, updateActiveWallet } from 'shared/lib/core/profile'

export function handleTransactionInclusionEvent(error: Error, rawEvent: Event): void {
const { walletId, payload } = validateWalletApiEvent(error, rawEvent, WalletEventType.TransactionInclusion)
const type = payload?.type
if (type === WalletEventType.TransactionInclusion) {
handleTransactionInclusionEventInternal(walletId, payload as TransactionInclusionWalletEvent)
export function handleTransactionInclusionEvent(walletId: string): WalletApiEventHandler {
return (error: Error, rawEvent: WalletEvent) => {
validateWalletApiEvent(error, rawEvent, WalletEventType.TransactionInclusion)
const type = rawEvent?.type
if (type === WalletEventType.TransactionInclusion) {
handleTransactionInclusionEventInternal(walletId, rawEvent as TransactionInclusionWalletEvent)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { get } from 'svelte/store'
import {
Event,
WalletEvent,
TransactionProgressWalletEvent,
PreparedTransactionSigningHashProgress,
WalletEventType,
Expand All @@ -16,13 +16,16 @@ import { deconstructLedgerVerificationProps } from '@core/ledger/helpers'
import { validateWalletApiEvent } from '../../utils'
import { selectedWalletId } from '../../stores'
import { MissingTransactionProgressEventPayloadError } from '../../errors'
import { WalletApiEventHandler } from '../../types'

export function handleTransactionProgressEvent(error: Error, rawEvent: Event): void {
const { walletId, payload } = validateWalletApiEvent(error, rawEvent, WalletEventType.TransactionProgress)
const type = payload.type
if (type === WalletEventType.TransactionProgress) {
const progress = (payload as TransactionProgressWalletEvent).progress
handleTransactionProgressEventInternal(walletId, progress)
export function handleTransactionProgressEvent(walletId: string): WalletApiEventHandler {
return (error: Error, rawEvent: WalletEvent) => {
validateWalletApiEvent(error, rawEvent, WalletEventType.TransactionProgress)
const type = rawEvent.type
if (type === WalletEventType.TransactionProgress) {
const progress = (rawEvent as TransactionProgressWalletEvent).progress
handleTransactionProgressEventInternal(walletId, progress)
}
}
}

Expand Down
36 changes: 6 additions & 30 deletions packages/shared/lib/core/wallet/utils/validateWalletApiEvent.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
import { localize } from '@core/i18n'

import { Event, WalletEventType } from '@iota/sdk/out/types'

import { WalletEvent, WalletEventType } from '@iota/sdk/out/types'
import { WalletApiEventError, WalletApiEventValidationError } from '../errors'
import { IWalletApiEventPayloadWrapper } from '../interfaces'

export function validateWalletApiEvent(
error: Error,
rawEvent: Event,
apiEvent: WalletEventType
): IWalletApiEventPayloadWrapper {
export function validateWalletApiEvent(error: Error, rawEvent: WalletEvent, apiEvent: WalletEventType): void {
if (error) {
throw new WalletApiEventError(error)
} else {
/* eslint-disable-next-line prefer-const */
const { walletId, event } = rawEvent

if (Number.isNaN(walletId)) {
throw new WalletApiEventValidationError(
localize('error.walletApiEvent.invalidAccountIndex', { values: { eventName: apiEvent } })
)
}

const payload = event.type === apiEvent ? event : undefined
if (!payload) {
throw new WalletApiEventValidationError(
localize('error.walletApiEvent.invalidPayload', { values: { eventName: apiEvent } })
)
}

return {
accountIndex,
payload,
}
} else if (rawEvent?.type !== apiEvent) {
throw new WalletApiEventValidationError(
localize('error.walletApiEvent.invalidPayload', { values: { eventName: apiEvent } })
)
}
}
Loading