Skip to content

Commit 3f10cf4

Browse files
feat: Wait for onboarding before allowing usage of SnapController
1 parent e081eb2 commit 3f10cf4

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

packages/snaps-controllers/src/snaps/SnapController.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,13 @@ type SnapControllerArgs = {
815815
* MetaMetrics event tracking hook.
816816
*/
817817
trackEvent: TrackEventHook;
818+
819+
/**
820+
* A hook that returns a promise that resolves when the onboarding has completed.
821+
*
822+
* @returns A promise that resolves when onboarding is complete.
823+
*/
824+
waitForOnboarding: () => Promise<void>;
818825
};
819826

820827
type AddSnapArgs = {
@@ -932,6 +939,8 @@ export class SnapController extends BaseController<
932939

933940
readonly #trackSnapExport: ReturnType<typeof throttleTracking>;
934941

942+
readonly #waitForOnboarding: () => Promise<void>;
943+
935944
constructor({
936945
closeAllConnections,
937946
messenger,
@@ -951,6 +960,7 @@ export class SnapController extends BaseController<
951960
getFeatureFlags = () => ({}),
952961
clientCryptography,
953962
trackEvent,
963+
waitForOnboarding,
954964
}: SnapControllerArgs) {
955965
super({
956966
messenger,
@@ -1034,6 +1044,7 @@ export class SnapController extends BaseController<
10341044
this.#rollbackSnapshots = new Map();
10351045
this.#snapsRuntimeData = new Map();
10361046
this.#trackEvent = trackEvent;
1047+
this.#waitForOnboarding = waitForOnboarding;
10371048

10381049
this.#pollForLastRequestStatus();
10391050

@@ -1467,7 +1478,7 @@ export class SnapController extends BaseController<
14671478
* Also updates any preinstalled Snaps to the latest allowlisted version.
14681479
*/
14691480
async updateRegistry(): Promise<void> {
1470-
this.#assertCanUsePlatform();
1481+
await this.#assertCanUsePlatform();
14711482
await this.messenger.call('SnapsRegistry:update');
14721483

14731484
const blockedSnaps = await this.messenger.call(
@@ -1645,9 +1656,12 @@ export class SnapController extends BaseController<
16451656
}
16461657

16471658
/**
1648-
* Asserts whether the Snaps platform is allowed to run.
1659+
* Waits for onboarding and then asserts whether the Snaps platform is allowed to run.
16491660
*/
1650-
#assertCanUsePlatform() {
1661+
async #assertCanUsePlatform() {
1662+
// Ensure the user has onboarded before allowing access to Snaps.
1663+
await this.#waitForOnboarding();
1664+
16511665
const flags = this.#getFeatureFlags();
16521666
assert(
16531667
flags.disableSnaps !== true,
@@ -1730,7 +1744,7 @@ export class SnapController extends BaseController<
17301744
* @param snapId - The id of the Snap to start.
17311745
*/
17321746
async startSnap(snapId: SnapId): Promise<void> {
1733-
this.#assertCanUsePlatform();
1747+
await this.#assertCanUsePlatform();
17341748
const snap = this.state.snaps[snapId];
17351749

17361750
if (!snap.enabled) {
@@ -2630,7 +2644,7 @@ export class SnapController extends BaseController<
26302644
origin: string,
26312645
requestedSnaps: RequestSnapsParams,
26322646
): Promise<RequestSnapsResult> {
2633-
this.#assertCanUsePlatform();
2647+
await this.#assertCanUsePlatform();
26342648

26352649
const result: RequestSnapsResult = {};
26362650

@@ -2914,7 +2928,7 @@ export class SnapController extends BaseController<
29142928
automaticUpdate?: boolean;
29152929
}): Promise<TruncatedSnap> {
29162930
this.#assertCanInstallSnaps();
2917-
this.#assertCanUsePlatform();
2931+
await this.#assertCanUsePlatform();
29182932

29192933
const snap = this.getExpect(snapId);
29202934

@@ -3549,7 +3563,7 @@ export class SnapController extends BaseController<
35493563
handler: handlerType,
35503564
request: rawRequest,
35513565
}: SnapRpcHookArgs & { snapId: SnapId }): Promise<unknown> {
3552-
this.#assertCanUsePlatform();
3566+
await this.#assertCanUsePlatform();
35533567

35543568
const snap = this.get(snapId);
35553569

0 commit comments

Comments
 (0)