generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keyring-snap-bridge): make account creation async (#207)
We now resume the Snap execution before persisting the account into the Snap keyring state. This allows the Snap to have an existing account (once approved or not by the user, just like before), but this delays the actual account creation on the MetaMask clients (more specifically, the `AccountsController` in this case), which allows other controllers to react to the `AccountsController:accountCreated` event and to directly fetch account informations (assets/balances) from the Snap. In the following example, the `SomeOtherController` cannot use the account because the account wasn't persisted by the Snap yet: ```ts // Snap keyring methods snap.createAccount = () => { const account = ...; await emitSnapKeyringEvent(AccountCreated, { account }); // -> This will trigger keyring.handleSnapkeyringEvent // Now persists the account (since we know MetaMask has accepted it). state.accounts.push(account); } // Snap keyring (on the MetaMask client) keyring.handleSnapKeyringEvent = (message) => { ... switch (message.method) { case 'AccountCreated': { // Show some dialogs for the user if (dialog.accepted) { // At this point, Snap execution is still pending... await keyring.saveState(); // This will trigger AccountsController:accountAdded (see addNewAccount method in this example) event } } ... }) AccountsController.addNewAccount = (account) => { this.messenger.publish(`AccountsController:accountAdded`, account); // This will trigger SomeOtherController.onAccountAdded handler } SomeOtherController.onAccountAdded = (account) => { // ! // ! Here was the problem, since the account was still not persisted onto the Snap. // ! await this.getSnapClient(account.metadata.snap.id).listAccountAssets(); } ``` --------- Co-authored-by: Daniel Rocha <[email protected]>
- Loading branch information
Showing
2 changed files
with
165 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters