Skip to content

Commit

Permalink
Merge pull request #3002 from tallyhowallet/fix-for-account-filter
Browse files Browse the repository at this point in the history
The Account filter for abilities
  • Loading branch information
jagodarybacka authored Feb 8, 2023
2 parents 78bc5fd + c962346 commit 9e6ac6c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
11 changes: 8 additions & 3 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ export default class Main extends BaseService<never> {
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
Expand Down Expand Up @@ -309,6 +308,11 @@ export default class Main extends BaseService<never> {

const nftsService = NFTsService.create(chainService)

const abilitiesService = AbilitiesService.create(
chainService,
ledgerService
)

const walletConnectService = isEnabled(FeatureFlags.SUPPORT_WALLET_CONNECT)
? WalletConnectService.create(
providerBridgeService,
Expand Down Expand Up @@ -1543,7 +1547,9 @@ export default class Main extends BaseService<never> {
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))
})
Expand All @@ -1554,7 +1560,6 @@ export default class Main extends BaseService<never> {
})
this.abilitiesService.emitter.on("deleteAccount", (address) => {
this.store.dispatch(deleteAccountFilter(address))
this.store.dispatch(deleteAbilitiesForAccount(address))
})
}

Expand Down
7 changes: 5 additions & 2 deletions background/redux-slices/abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
)
Expand Down
28 changes: 19 additions & 9 deletions background/services/abilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,11 +78,13 @@ interface Events extends ServiceLifecycleEvents {
newAccount: string
deleteAccount: string
initAbilities: NormalizedEVMAddress
deleteAbilities: string
}
export default class AbilitiesService extends BaseService<Events> {
constructor(
private db: AbilitiesDatabase,
private chainService: ChainService
private chainService: ChainService,
private ledgerService: LedgerService
) {
super({
abilitiesAlarm: {
Expand All @@ -99,18 +102,25 @@ export default class AbilitiesService extends BaseService<Events> {
static create: ServiceCreatorFunction<
ServiceLifecycleEvents,
AbilitiesService,
[Promise<ChainService>]
> = async (chainService) => {
return new this(await getOrCreateDB(), await chainService)
[Promise<ChainService>, Promise<LedgerService>]
> = async (chainService, ledgerService) => {
return new this(
await getOrCreateDB(),
await chainService,
await ledgerService
)
}

protected override async internalStartService(): Promise<void> {
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)
}
Expand Down Expand Up @@ -174,7 +184,6 @@ export default class AbilitiesService extends BaseService<Events> {
// 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)
}
}
Expand All @@ -193,7 +202,8 @@ export default class AbilitiesService extends BaseService<Events> {
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)
}
}
5 changes: 5 additions & 0 deletions background/services/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ export default class LedgerService extends BaseService<Events> {
await this.db.removeAccount(address)
}

async getAccountByAddress(address: HexString): Promise<LedgerAccount | null> {
const ledgerAccount = await this.db.getAccountByAddress(address)
return ledgerAccount
}

async signTransaction(
transactionRequest: TransactionRequestWithNonce,
{ deviceID, path: derivationPath }: LedgerAccountSigner
Expand Down

0 comments on commit 9e6ac6c

Please sign in to comment.