-
-
Notifications
You must be signed in to change notification settings - Fork 294
feat(wallet): Add SeedlessOnboardingController and PasskeyController #9533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0781e51
feat: add PasskeyController to wallet initialization instances
lwin-kyaw d795cf6
feat: add SeedlessOnboardingController to wallet init
lwin-kyaw 3cbc158
fix: fixed build and lint
lwin-kyaw abc2e4c
chore: updated CHANGELOG
lwin-kyaw b867f45
chore: updaed CODE_OWNERS
lwin-kyaw 901744a
chore: reuse constructor options for PasskeyControllerInstanceOptions
lwin-kyaw c94fc01
fix: fixed lint
lwin-kyaw ded1aa1
feat: updated passkey-controller-init
lwin-kyaw 6b754f5
Merge remote-tracking branch 'origin/main' into feat/passkey-seedless…
lwin-kyaw 70079ce
fix: fixed tests
lwin-kyaw c60b979
fix: fixed tests
lwin-kyaw 27b20c0
chore: rebased with main
lwin-kyaw 12bb367
fix: fixed CODEOWNERS
lwin-kyaw 5566024
chore: test updates
lwin-kyaw 8018aad
chore: rebased with main
lwin-kyaw 30b4935
feat: update passkey-controller to v3
lwin-kyaw b5b7ac6
fix: fixed default encryptor for seedless-onboarding-controller
lwin-kyaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
177 changes: 177 additions & 0 deletions
177
packages/wallet/src/initialization/instances/passkey-controller/passkey-controller.test.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import { Messenger } from '@metamask/messenger'; | ||
| import { | ||
| PasskeyController, | ||
| PasskeyControllerErrorCode, | ||
| getDefaultPasskeyControllerState, | ||
| } from '@metamask/passkey-controller'; | ||
| import type { | ||
| PasskeyAuthenticationResponse, | ||
| PasskeyRecord, | ||
| PasskeyRegistrationResponse, | ||
| } from '@metamask/passkey-controller'; | ||
|
|
||
| import { defaultConfigurations } from '../../defaults.js'; | ||
| import type { | ||
| DefaultActions, | ||
| DefaultEvents, | ||
| RootMessenger, | ||
| } from '../../defaults.js'; | ||
| import { passkeyController } from './passkey-controller.js'; | ||
| import type { PasskeyControllerInstanceOptions } from './types.js'; | ||
|
|
||
| const REQUIRED_OPTIONS: PasskeyControllerInstanceOptions = { | ||
| expectedRPID: 'extension-id', | ||
| expectedOrigin: 'https://extension.origin', | ||
| rpName: 'MetaMask', | ||
| getIsOnboardingCompleted: () => false, | ||
| }; | ||
|
|
||
| /** | ||
| * Creates a root messenger for use in tests. | ||
| * | ||
| * @returns A root messenger. | ||
| */ | ||
| function getRootMessenger(): RootMessenger<DefaultActions, DefaultEvents> { | ||
| return new Messenger({ namespace: 'Root' }); | ||
| } | ||
|
|
||
| describe('passkeyController', () => { | ||
| it('is registered as a default initialization configuration', () => { | ||
| // Proves the controller is part of the default ensemble that `initialize()` | ||
| // wires, without constructing a `Wallet` (which keeps this PR independent of | ||
| // the constructor-options shape). | ||
| expect(Object.values(defaultConfigurations)).toContain(passkeyController); | ||
| }); | ||
|
|
||
| it('initializes a PasskeyController with default state', () => { | ||
| const messenger = passkeyController.getMessenger(getRootMessenger()); | ||
|
|
||
| const instance = passkeyController.init({ | ||
| state: undefined, | ||
| messenger, | ||
| options: REQUIRED_OPTIONS, | ||
| }); | ||
|
|
||
| expect(instance).toBeInstanceOf(PasskeyController); | ||
| expect(instance.state).toStrictEqual(getDefaultPasskeyControllerState()); | ||
| }); | ||
|
|
||
| it('forwards the provided state to the controller', () => { | ||
| const messenger = passkeyController.getMessenger(getRootMessenger()); | ||
|
|
||
| const passkeyRecord: PasskeyRecord = { | ||
| credential: { | ||
| id: 'credential-id', | ||
| publicKey: 'public-key', | ||
| counter: 0, | ||
| transports: ['internal'], | ||
| aaguid: '00000000-0000-0000-0000-000000000000', | ||
| }, | ||
| encryptedVaultKey: { | ||
| ciphertext: 'YQ==', | ||
| iv: 'YWFhYWFhYWFhYQ==', | ||
| }, | ||
| keyDerivation: { method: 'userHandle' }, | ||
| }; | ||
|
|
||
| const instance = passkeyController.init({ | ||
| state: { passkeyRecord }, | ||
| messenger, | ||
| options: REQUIRED_OPTIONS, | ||
| }); | ||
|
|
||
| expect(instance.state.passkeyRecord).toStrictEqual(passkeyRecord); | ||
| }); | ||
|
|
||
| it('defaults userName and userDisplayName to rpName when omitted', () => { | ||
| const messenger = passkeyController.getMessenger(getRootMessenger()); | ||
|
|
||
| const instance = passkeyController.init({ | ||
| state: undefined, | ||
| messenger, | ||
| options: REQUIRED_OPTIONS, | ||
| }); | ||
|
|
||
| const options = instance.generateRegistrationOptions({ | ||
| prfAvailable: false, | ||
| }); | ||
|
|
||
| expect(options.user).toStrictEqual({ | ||
| id: expect.any(String), | ||
| name: 'MetaMask', | ||
| displayName: 'MetaMask', | ||
| }); | ||
| }); | ||
|
|
||
| it('uses custom passkey configuration options', () => { | ||
| const messenger = passkeyController.getMessenger(getRootMessenger()); | ||
|
|
||
| const instance = passkeyController.init({ | ||
| state: undefined, | ||
| messenger, | ||
| options: { | ||
| ...REQUIRED_OPTIONS, | ||
| expectedRPID: ['extension-id', 'other-id'], | ||
| expectedOrigin: ['https://a.example', 'https://b.example'], | ||
| rpId: 'rp-id', | ||
| rpName: 'Custom RP', | ||
| userName: 'custom-user', | ||
| userDisplayName: 'Custom Display Name', | ||
| }, | ||
| }); | ||
|
|
||
| const options = instance.generateRegistrationOptions({ | ||
| prfAvailable: false, | ||
| }); | ||
|
|
||
| expect(options.rp).toStrictEqual({ | ||
| name: 'Custom RP', | ||
| id: 'rp-id', | ||
| }); | ||
| expect(options.user).toStrictEqual({ | ||
| id: expect.any(String), | ||
| name: 'custom-user', | ||
| displayName: 'Custom Display Name', | ||
| }); | ||
| }); | ||
|
|
||
| it('uses the provided getIsOnboardingCompleted callback', async () => { | ||
| const messenger = passkeyController.getMessenger(getRootMessenger()); | ||
| const getIsOnboardingCompleted = jest.fn().mockReturnValue(true); | ||
|
|
||
| const instance = passkeyController.init({ | ||
| state: undefined, | ||
| messenger, | ||
| options: { | ||
| ...REQUIRED_OPTIONS, | ||
| getIsOnboardingCompleted, | ||
| }, | ||
| }); | ||
|
|
||
| await expect( | ||
| instance.protectVaultKeyWithPasskey({ | ||
| registrationResponse: {} as PasskeyRegistrationResponse, | ||
| authenticationResponse: {} as PasskeyAuthenticationResponse, | ||
| }), | ||
| ).rejects.toMatchObject({ | ||
| code: PasskeyControllerErrorCode.EnrollmentPasswordRequired, | ||
| }); | ||
|
|
||
| expect(getIsOnboardingCompleted).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('exposes its state through the root messenger', () => { | ||
| const rootMessenger = getRootMessenger(); | ||
| const messenger = passkeyController.getMessenger(rootMessenger); | ||
|
|
||
| passkeyController.init({ | ||
| state: undefined, | ||
| messenger, | ||
| options: REQUIRED_OPTIONS, | ||
| }); | ||
|
|
||
| expect(rootMessenger.call('PasskeyController:getState')).toStrictEqual( | ||
| getDefaultPasskeyControllerState(), | ||
| ); | ||
| }); | ||
| }); |
47 changes: 47 additions & 0 deletions
47
packages/wallet/src/initialization/instances/passkey-controller/passkey-controller.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||
| import { Messenger } from '@metamask/messenger'; | ||||||
| import { | ||||||
| PasskeyController, | ||||||
| PasskeyControllerMessenger, | ||||||
| } from '@metamask/passkey-controller'; | ||||||
|
|
||||||
| import type { | ||||||
| DefaultActions, | ||||||
| DefaultEvents, | ||||||
| RootMessenger, | ||||||
| } from '../../defaults.js'; | ||||||
| import { InitializationConfiguration } from '../../types.js'; | ||||||
|
|
||||||
| export const passkeyController: InitializationConfiguration< | ||||||
| PasskeyController, | ||||||
| PasskeyControllerMessenger | ||||||
| > = { | ||||||
| name: 'PasskeyController', | ||||||
| init: ({ state, messenger, options }) => | ||||||
| new PasskeyController({ | ||||||
| ...options, | ||||||
| messenger, | ||||||
| state, | ||||||
| }), | ||||||
| getMessenger: (parent: RootMessenger<DefaultActions, DefaultEvents>) => { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: You don't need this type assertion, TypeScript can infer it:
Suggested change
|
||||||
| const passkeyControllerMessenger: PasskeyControllerMessenger = | ||||||
| new Messenger({ | ||||||
| namespace: 'PasskeyController', | ||||||
| parent, | ||||||
| }); | ||||||
|
|
||||||
| parent.delegate({ | ||||||
| messenger: passkeyControllerMessenger, | ||||||
| actions: [ | ||||||
| 'KeyringController:verifyPassword', | ||||||
| 'KeyringController:exportEncryptionKey', | ||||||
| 'KeyringController:submitEncryptionKey', | ||||||
| 'KeyringController:changePassword', | ||||||
| 'KeyringController:exportSeedPhrase', | ||||||
| 'KeyringController:exportAccount', | ||||||
| ], | ||||||
| events: [], | ||||||
| }); | ||||||
|
|
||||||
| return passkeyControllerMessenger; | ||||||
| }, | ||||||
| }; | ||||||
6 changes: 6 additions & 0 deletions
6
packages/wallet/src/initialization/instances/passkey-controller/types.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import type { PasskeyControllerOptions } from '@metamask/passkey-controller'; | ||
|
|
||
| export type PasskeyControllerInstanceOptions = Omit< | ||
| PasskeyControllerOptions, | ||
| 'messenger' | 'state' | ||
| >; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.