Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: enforce Snap platform check before any provider calls
  • Loading branch information
ccharly committed Nov 28, 2025
commit 6e4c4a66a40aad0b4cbd8fb3f810d45d91f39a66
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class BtcAccountProvider extends SnapAccountProvider {
);
}

async createAccounts({
async runCreateAccounts({
entropySource,
groupIndex: index,
}: {
Expand All @@ -77,7 +77,7 @@ export class BtcAccountProvider extends SnapAccountProvider {
});
}

async discoverAccounts({
async runDiscoverAccounts({
entropySource,
groupIndex,
}: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class MockSnapAccountProvider extends SnapAccountProvider {
return true;
}

async discoverAccounts(): Promise<Bip44Account<KeyringAccount>[]> {
async runDiscoverAccounts(): Promise<Bip44Account<KeyringAccount>[]> {
return [];
}

async createAccounts(options: {
async runCreateAccounts(options: {
entropySource: EntropySourceId;
groupIndex: number;
}): Promise<Bip44Account<KeyringAccount>[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ export abstract class SnapAccountProvider extends BaseBip44AccountProvider {
}
}

protected async withClient(
operation: (client: KeyringClient) => Promise<void>,
): Promise<void> {
// This will make sure the Snap platform is ready before sending any request
// to the Snap.
await this.ensureSnapPlatformIsReady();

return operation(this.client);
}

/**
* Wraps an async operation with concurrency limiting based on maxConcurrency config.
* If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.
Expand Down Expand Up @@ -201,6 +191,8 @@ export abstract class SnapAccountProvider extends BaseBip44AccountProvider {
async resyncAccounts(
accounts: Bip44Account<InternalAccount>[],
): Promise<void> {
await this.ensureSnapPlatformIsReady();

const localSnapAccounts = accounts.filter(
(account) =>
account.metadata.snap && account.metadata.snap.id === this.snapId,
Expand Down Expand Up @@ -284,6 +276,8 @@ export abstract class SnapAccountProvider extends BaseBip44AccountProvider {
metadata: KeyringMetadata;
}) => Promise<CallbackResult>,
): Promise<CallbackResult> {
await this.ensureSnapPlatformIsReady();

return this.withKeyring<SnapKeyring, CallbackResult>(
{ type: KeyringTypes.snap },
(args) => {
Expand All @@ -292,14 +286,32 @@ export abstract class SnapAccountProvider extends BaseBip44AccountProvider {
);
}

async createAccounts(options: {
entropySource: EntropySourceId;
groupIndex: number;
}): Promise<Bip44Account<KeyringAccount>[]> {
await this.ensureSnapPlatformIsReady();

return await this.runCreateAccounts(options);
}

async discoverAccounts(options: {
entropySource: EntropySourceId;
groupIndex: number;
}): Promise<Bip44Account<KeyringAccount>[]> {
await this.ensureSnapPlatformIsReady();

return await this.runDiscoverAccounts(options);
}

abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;

abstract createAccounts(options: {
abstract runCreateAccounts(options: {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now providers have to implements those run* methods, so we can automatically call this.ensureSnapPlatformIsReady for any of those actions.

entropySource: EntropySourceId;
groupIndex: number;
}): Promise<Bip44Account<KeyringAccount>[]>;

abstract discoverAccounts(options: {
abstract runDiscoverAccounts(options: {
entropySource: EntropySourceId;
groupIndex: number;
}): Promise<Bip44Account<KeyringAccount>[]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class SolAccountProvider extends SnapAccountProvider {
return account;
}

async createAccounts({
async runCreateAccounts({
entropySource,
groupIndex,
}: {
Expand All @@ -103,7 +103,7 @@ export class SolAccountProvider extends SnapAccountProvider {
});
}

async discoverAccounts({
async runDiscoverAccounts({
entropySource,
groupIndex,
}: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class TrxAccountProvider extends SnapAccountProvider {
);
}

async createAccounts({
async runCreateAccounts({
entropySource,
groupIndex: index,
}: {
Expand All @@ -78,7 +78,7 @@ export class TrxAccountProvider extends SnapAccountProvider {
});
}

async discoverAccounts({
async runDiscoverAccounts({
entropySource,
groupIndex,
}: {
Expand Down
Loading