From c9a119c61f8266643ab50066937811902ddba9d5 Mon Sep 17 00:00:00 2001 From: Karolina Kosiorowska Date: Tue, 7 Feb 2023 18:23:07 +0100 Subject: [PATCH 1/3] Fix the issue with the account filter for abilities --- background/main.ts | 11 +++++++--- background/redux-slices/abilities.ts | 7 +++++-- background/services/abilities/index.ts | 28 +++++++++++++++++--------- background/services/ledger/index.ts | 5 +++++ 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/background/main.ts b/background/main.ts index 40817e305f..402529bee9 100644 --- a/background/main.ts +++ b/background/main.ts @@ -273,7 +273,6 @@ export default class Main extends BaseService { const preferenceService = PreferenceService.create() const keyringService = KeyringService.create() const chainService = ChainService.create(preferenceService, keyringService) - const abilitiesService = AbilitiesService.create(chainService) const indexingService = IndexingService.create( preferenceService, chainService @@ -309,6 +308,11 @@ export default class Main extends BaseService { const nftsService = NFTsService.create(chainService) + const abilitiesService = AbilitiesService.create( + chainService, + ledgerService + ) + const walletConnectService = isEnabled(FeatureFlags.SUPPORT_WALLET_CONNECT) ? WalletConnectService.create( providerBridgeService, @@ -1543,7 +1547,9 @@ export default class Main extends BaseService { this.abilitiesService.emitter.on("newAbilities", (newAbilities) => { this.store.dispatch(addAbilities(newAbilities)) }) - + this.abilitiesService.emitter.on("deleteAbilities", (address) => { + this.store.dispatch(deleteAbilitiesForAccount(address)) + }) this.abilitiesService.emitter.on("updatedAbility", (ability) => { this.store.dispatch(updateAbility(ability)) }) @@ -1554,7 +1560,6 @@ export default class Main extends BaseService { }) this.abilitiesService.emitter.on("deleteAccount", (address) => { this.store.dispatch(deleteAccountFilter(address)) - this.store.dispatch(deleteAbilitiesForAccount(address)) }) } diff --git a/background/redux-slices/abilities.ts b/background/redux-slices/abilities.ts index 59284d9b9e..175c5732e1 100644 --- a/background/redux-slices/abilities.ts +++ b/background/redux-slices/abilities.ts @@ -165,17 +165,20 @@ export const reportAndRemoveAbility = createBackgroundAsyncThunk( export const initAbilities = createBackgroundAsyncThunk( "abilities/initAbilities", - async (address: NormalizedEVMAddress, { getState, extra: { main } }) => { + async ( + address: NormalizedEVMAddress, + { dispatch, getState, extra: { main } } + ) => { const { ledger, keyrings } = getState() as { ledger: LedgerState keyrings: KeyringsState } - if ( isImportOrInternalAccount(keyrings, address) || isLedgerAccount(ledger, address) ) { await main.pollForAbilities(address) + dispatch(addAccount(address)) } } ) diff --git a/background/services/abilities/index.ts b/background/services/abilities/index.ts index 88449375f6..91307309d0 100644 --- a/background/services/abilities/index.ts +++ b/background/services/abilities/index.ts @@ -12,6 +12,7 @@ import ChainService from "../chain" import { FeatureFlags, isEnabled } from "../../features" import { normalizeEVMAddress } from "../../lib/utils" import { Ability, AbilityRequirement } from "../../abilities" +import LedgerService from "../ledger" const normalizeDaylightRequirements = ( requirement: DaylightAbilityRequirement @@ -77,11 +78,13 @@ interface Events extends ServiceLifecycleEvents { newAccount: string deleteAccount: string initAbilities: NormalizedEVMAddress + deleteAbilities: string } export default class AbilitiesService extends BaseService { constructor( private db: AbilitiesDatabase, - private chainService: ChainService + private chainService: ChainService, + private ledgerService: LedgerService ) { super({ abilitiesAlarm: { @@ -99,18 +102,25 @@ export default class AbilitiesService extends BaseService { static create: ServiceCreatorFunction< ServiceLifecycleEvents, AbilitiesService, - [Promise] - > = async (chainService) => { - return new this(await getOrCreateDB(), await chainService) + [Promise, Promise] + > = async (chainService, ledgerService) => { + return new this( + await getOrCreateDB(), + await chainService, + await ledgerService + ) } protected override async internalStartService(): Promise { await super.internalStartService() this.chainService.emitter.on( "newAccountToTrack", - ({ addressOnNetwork, source }) => { - if (source) { - const { address } = addressOnNetwork + async ({ addressOnNetwork, source }) => { + const { address } = addressOnNetwork + const ledgerAccount = await this.ledgerService.getAccountByAddress( + address + ) + if (source ?? ledgerAccount) { this.pollForAbilities(address) this.emitter.emit("newAccount", address) } @@ -174,7 +184,6 @@ export default class AbilitiesService extends BaseService { // 1-by-1 decreases likelihood of hitting rate limit // eslint-disable-next-line no-restricted-syntax for (const address of addresses) { - this.emitter.emit("newAccount", address) this.emitter.emit("initAbilities", address as NormalizedEVMAddress) } } @@ -193,7 +202,8 @@ export default class AbilitiesService extends BaseService { const deletedRecords = await this.db.deleteAbilitiesForAccount(address) if (deletedRecords > 0) { - this.emitter.emit("deleteAccount", address) + this.emitter.emit("deleteAbilities", address) } + this.emitter.emit("deleteAccount", address) } } diff --git a/background/services/ledger/index.ts b/background/services/ledger/index.ts index fa44dbbcf9..3226bac3df 100644 --- a/background/services/ledger/index.ts +++ b/background/services/ledger/index.ts @@ -362,6 +362,11 @@ export default class LedgerService extends BaseService { await this.db.removeAccount(address) } + async getAccountByAddress(address: HexString): Promise { + const ledgerAccount = await this.db.getAccountByAddress(address) + return ledgerAccount + } + async signTransaction( transactionRequest: TransactionRequestWithNonce, { deviceID, path: derivationPath }: LedgerAccountSigner From f3caf88878fac35f54bd07e93e1dcaeb5b74e73e Mon Sep 17 00:00:00 2001 From: Karolina Kosiorowska Date: Wed, 8 Feb 2023 08:20:12 +0100 Subject: [PATCH 2/3] Keep the filter settings for abilities after reloading --- background/redux-slices/abilities.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/background/redux-slices/abilities.ts b/background/redux-slices/abilities.ts index 175c5732e1..174df1da95 100644 --- a/background/redux-slices/abilities.ts +++ b/background/redux-slices/abilities.ts @@ -165,10 +165,7 @@ export const reportAndRemoveAbility = createBackgroundAsyncThunk( export const initAbilities = createBackgroundAsyncThunk( "abilities/initAbilities", - async ( - address: NormalizedEVMAddress, - { dispatch, getState, extra: { main } } - ) => { + async (address: NormalizedEVMAddress, { getState, extra: { main } }) => { const { ledger, keyrings } = getState() as { ledger: LedgerState keyrings: KeyringsState @@ -178,7 +175,6 @@ export const initAbilities = createBackgroundAsyncThunk( isLedgerAccount(ledger, address) ) { await main.pollForAbilities(address) - dispatch(addAccount(address)) } } ) From c962346ba8097753045287b36a4ed14bdb495561 Mon Sep 17 00:00:00 2001 From: Karolina Kosiorowska Date: Wed, 8 Feb 2023 11:37:19 +0100 Subject: [PATCH 3/3] Make an account filter for abilities enabled by default --- background/redux-slices/abilities.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/background/redux-slices/abilities.ts b/background/redux-slices/abilities.ts index 174df1da95..175c5732e1 100644 --- a/background/redux-slices/abilities.ts +++ b/background/redux-slices/abilities.ts @@ -165,7 +165,10 @@ export const reportAndRemoveAbility = createBackgroundAsyncThunk( export const initAbilities = createBackgroundAsyncThunk( "abilities/initAbilities", - async (address: NormalizedEVMAddress, { getState, extra: { main } }) => { + async ( + address: NormalizedEVMAddress, + { dispatch, getState, extra: { main } } + ) => { const { ledger, keyrings } = getState() as { ledger: LedgerState keyrings: KeyringsState @@ -175,6 +178,7 @@ export const initAbilities = createBackgroundAsyncThunk( isLedgerAccount(ledger, address) ) { await main.pollForAbilities(address) + dispatch(addAccount(address)) } } )