-
-
Notifications
You must be signed in to change notification settings - Fork 287
feat: Implement wallet initialization library #8838
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
29 commits
Select commit
Hold shift + click to select a range
0b94d68
feat: Implement wallet initialization library
FrederikBolding c60a904
Add KeyringController initialization
FrederikBolding cba84fb
Update README
FrederikBolding 6e45a2f
Add more tests
FrederikBolding b4bf73c
Fix Yarn constraints
FrederikBolding 9a2bb50
Add instance specific options
FrederikBolding 90b4ca7
Return instances directly
FrederikBolding 77a0013
Add CHANGELOG
FrederikBolding 465ed33
Fix tests and lint
FrederikBolding 04e70f2
Fix Node 18 tests
FrederikBolding 0f91628
Expose instances for now
FrederikBolding 48fb182
Allow passing messenger
FrederikBolding 0fd9d7f
Address a couple comments
FrederikBolding d706aa2
Address more PR comments
FrederikBolding 7eb54d2
Fix import
FrederikBolding 8b2d364
Tweak state types
FrederikBolding 23ef6e2
Export DefaultInstances
FrederikBolding 0b89138
Add overloads for getInstance
FrederikBolding 285edb4
Use camel case for instanceOptions
FrederikBolding 2bdac54
Tweak encryptor types
FrederikBolding 33fc20d
Fix cast
FrederikBolding 49b8e9e
Add MobileEncryptionResult type
FrederikBolding 559f322
Adjust messenger name, add JSDocs, cleanup
FrederikBolding a2c4a8c
Address more PR comments
FrederikBolding 5a37811
Remove unused import
FrederikBolding 681bd35
Export DefaultState
FrederikBolding 8e35748
Add CODEOWNERS entry
FrederikBolding 2bbd49c
Add a couple more tests
FrederikBolding 19bfd38
Fix tests on Node 18
FrederikBolding 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| import { Messenger } from '@metamask/messenger'; | ||
| import { Json } from '@metamask/utils'; | ||
| import { webcrypto } from 'crypto'; | ||
|
|
||
| import MockEncryptor from '../../keyring-controller/tests/mocks/mockEncryptor'; | ||
| import * as initializationModule from './initialization/initialization'; | ||
| import { importSecretRecoveryPhrase } from './utilities'; | ||
| import { Wallet } from './Wallet'; | ||
|
|
||
| const TEST_SRP = 'test test test test test test test test test test test ball'; | ||
| const TEST_PASSWORD = 'testpass'; | ||
|
|
||
| async function setupWallet(): Promise<Wallet> { | ||
| const wallet = new Wallet({}); | ||
|
|
||
| await importSecretRecoveryPhrase(wallet, TEST_PASSWORD, TEST_SRP); | ||
|
|
||
| return wallet; | ||
| } | ||
|
|
||
| describe('Wallet', () => { | ||
| beforeAll(() => { | ||
| // We can remove this once we drop Node 18 | ||
| // eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
| globalThis.crypto ??= webcrypto as typeof globalThis.crypto; | ||
|
|
||
| // eslint-disable-next-line no-restricted-syntax | ||
| if (!('CryptoKey' in globalThis)) { | ||
| Object.defineProperty(globalThis, 'CryptoKey', { | ||
| value: webcrypto.CryptoKey, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it('exposes state', async () => { | ||
| const wallet = await setupWallet(); | ||
| const { state } = wallet; | ||
|
|
||
| expect(state.KeyringController).toStrictEqual({ | ||
| isUnlocked: true, | ||
| keyrings: expect.any(Array), | ||
| encryptionKey: expect.any(String), | ||
| encryptionSalt: expect.any(String), | ||
| vault: expect.any(String), | ||
| }); | ||
| }); | ||
|
|
||
| it('exposes instances', async () => { | ||
| const wallet = await setupWallet(); | ||
|
|
||
| expect(wallet.getInstance('KeyringController').state).toStrictEqual({ | ||
| isUnlocked: true, | ||
| keyrings: expect.any(Array), | ||
| encryptionKey: expect.any(String), | ||
| encryptionSalt: expect.any(String), | ||
| vault: expect.any(String), | ||
| }); | ||
| }); | ||
|
|
||
| it('supports passing instance options', async () => { | ||
| const wallet = new Wallet({ | ||
| instanceOptions: { | ||
| keyringController: { | ||
| encryptor: new MockEncryptor(), | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| await importSecretRecoveryPhrase(wallet, TEST_PASSWORD, TEST_SRP); | ||
|
|
||
| const { state } = wallet; | ||
|
|
||
| const vault = JSON.parse(state.KeyringController.vault as string); | ||
|
|
||
| expect(vault).toStrictEqual({ | ||
| data: expect.any(String), | ||
| iv: 'iv', | ||
| salt: 'salt', | ||
| }); | ||
| }); | ||
|
|
||
| it('supports passing additional initialization configurations', async () => { | ||
| class DummyController { | ||
| state = { foo: 'bar' }; | ||
| } | ||
|
|
||
| class DummyService {} | ||
|
|
||
| const wallet = new Wallet({ | ||
| initializationConfigurations: [ | ||
| { | ||
| name: 'KeyringController', | ||
| getMessenger: (): Messenger<string> => | ||
| new Messenger({ namespace: 'KeyringController' }), | ||
| init: (): DummyController => new DummyController(), | ||
| }, | ||
| { | ||
| name: 'TestService', | ||
| getMessenger: (): Messenger<string> => | ||
| new Messenger({ namespace: 'TestService' }), | ||
| init: (): DummyService => new DummyService(), | ||
| }, | ||
| ], | ||
| }); | ||
| const { state } = wallet; | ||
|
|
||
| expect(state.KeyringController).toStrictEqual({ | ||
| foo: 'bar', | ||
| }); | ||
|
|
||
| expect((state as Record<string, Json>).TestService).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('exposes controllerMetadata for each initialized controller', async () => { | ||
| const wallet = await setupWallet(); | ||
|
|
||
| const names = Object.keys(wallet.controllerMetadata); | ||
| expect(names).toStrictEqual(Object.keys(wallet.state)); | ||
| for (const name of names) { | ||
| expect(wallet.controllerMetadata[name]).toBeDefined(); | ||
| } | ||
| }); | ||
|
|
||
| it('omits instances without a metadata property from controllerMetadata', async () => { | ||
| const fakeMetadata = { | ||
| foo: { persist: true, includeInDebugSnapshot: false }, | ||
| }; | ||
| jest.spyOn(initializationModule, 'initialize').mockReturnValueOnce({ | ||
| // @ts-expect-error Mock data. | ||
| WithMeta: { state: {}, metadata: fakeMetadata }, | ||
| NoMeta: { state: {} }, | ||
| }); | ||
|
|
||
| const wallet = new Wallet({}); | ||
|
|
||
| expect(wallet.controllerMetadata).toStrictEqual({ | ||
| WithMeta: fakeMetadata, | ||
| }); | ||
| expect(Object.keys(wallet.state)).toStrictEqual(['WithMeta', 'NoMeta']); | ||
| }); | ||
|
|
||
| it('disallows modifying the messenger', async () => { | ||
| const wallet = await setupWallet(); | ||
|
|
||
| expect(() => { | ||
| wallet.messenger = new Messenger({ namespace: 'Root' }); | ||
| }).toThrow('The messenger cannot be directly mutated.'); | ||
| }); | ||
|
|
||
| it('disallows modifying the state', async () => { | ||
| const wallet = await setupWallet(); | ||
|
|
||
| expect(() => { | ||
| wallet.state = { KeyringController: { isUnlocked: false, keyrings: [] } }; | ||
| }).toThrow('Wallet state cannot be directly mutated.'); | ||
| }); | ||
|
|
||
| it('disallows modifying the controller metadata', async () => { | ||
| const wallet = await setupWallet(); | ||
|
|
||
| expect(() => { | ||
| wallet.controllerMetadata = {}; | ||
| }).toThrow('The controller metadata cannot be directly mutated.'); | ||
| }); | ||
|
|
||
| it('calls destroy on instances exactly once', async () => { | ||
| const wallet = await setupWallet(); | ||
|
|
||
| const keyringController = wallet.getInstance('KeyringController'); | ||
|
|
||
| const spy = jest.spyOn(keyringController, 'destroy'); | ||
|
|
||
| await wallet.destroy(); | ||
| await wallet.destroy(); | ||
|
|
||
| expect(spy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| describe('KeyringController', () => { | ||
| it('can unlock and populate accounts', async () => { | ||
| const wallet = await setupWallet(); | ||
| const { messenger } = wallet; | ||
|
|
||
| expect( | ||
| await messenger.call('KeyringController:getAccounts'), | ||
| ).toStrictEqual(['0xc6d5a3c98ec9073b54fa0969957bd582e8d874bf']); | ||
| }); | ||
|
|
||
| it('can unlock a persisted vault', async () => { | ||
| const vault = | ||
| '{"data":"iOD5pIcPeRZYQ4WdEMsNYoZ3xBxWBafIU8Cr4nD0X4zBvrOA06tGen3sKQ/ValasXSweLnzH9Fk2frkPYmqeJWBtTNYFwdHPe7P970ThZwreSXN1Sqrx9Ad+YzmIN0y89Yg3KrUodPWaRgIZmgWbfDon6ADPgeEDkX0/GAEYET39O7Rx/gL+rcaTpxnpHPTgHiLbhRHWGsS3z+JVomSqoLAO5XVvrJWenO6R3Nzm62BaJaSPrf/pwstZqhSvxTq8hnQf7aR81hWfwYTxNBVG7TC/dniSQ8K5So6PvUN5nzAqvtzzHT2TagOuxQkX88Zi17P8os21jNmNdA90IGYroD+b/mppyRIgRYWtAUQZH9ji36atEuFupszbg8Qw1iaL3EQyUogC30Cpj9ko5bbqhYgqmFHF0J/kflhPHKuO6d4tgSmhYpTumydQRjxaPnlghIS5YI4W+7p9HVBpb+c6IPUz9y/x3Ngbp+ukJwOnXt2U/eZhXrJzi2z1x/nzPg4fzDJoM7k=","iv":"yrZsyC7dso/q7pQ48YX3vw==","keyMetadata":{"algorithm":"PBKDF2","params":{"iterations":600000}},"salt":"s7nIrMWK1lcZVjfdmES1DBML8Uz4ja2fpm8zUz1lWI0="}'; | ||
|
|
||
| const wallet = new Wallet({ | ||
| state: { | ||
| KeyringController: { | ||
| vault, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| await wallet.messenger.call( | ||
| 'KeyringController:submitPassword', | ||
| TEST_PASSWORD, | ||
| ); | ||
|
|
||
| expect(wallet.state.KeyringController).toStrictEqual({ | ||
| isUnlocked: true, | ||
| keyrings: expect.any(Array), | ||
| encryptionKey: expect.any(String), | ||
| encryptionSalt: expect.any(String), | ||
| vault: expect.any(String), | ||
| }); | ||
| }); | ||
|
|
||
| it('can lock', async () => { | ||
| const wallet = await setupWallet(); | ||
| const { messenger } = wallet; | ||
|
|
||
| await messenger.call('KeyringController:setLocked'); | ||
|
|
||
| expect(wallet.state.KeyringController).toStrictEqual({ | ||
| isUnlocked: false, | ||
| keyrings: [], | ||
| vault: expect.any(String), | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we're using this package in a way that works across platforms? Curious if it might be worth it at some point to extract the more platform-agnostic functions to some other library. Otherwise it might be a bit confusing. We can think about that later perhaps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@metamask/browser-passworderworks on most platforms except mobile, so it seemed fine to include. It's a good default option IMO.