-
Notifications
You must be signed in to change notification settings - Fork 5.6k
fix: Use withKeyring for accessing keyring entropy for Snaps
#40296
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
7 commits
Select commit
Hold shift + click to select a range
f921836
fix: Use withKeyring for accessing keyring entropy for Snaps
FrederikBolding d5677a2
Remove unused unit test
FrederikBolding c14c631
Remove unused import
FrederikBolding 4ba2c74
Refactor
FrederikBolding 8baea33
Add eth-hd-keyring as direct dependency
FrederikBolding 5a1c309
Update LavaMoat policies
metamaskbot b8717b6
Update app/scripts/controllers/permissions/snaps/utils.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { | ||
| KeyringControllerWithKeyringAction, | ||
| KeyringTypes, | ||
| } from '@metamask/keyring-controller'; | ||
| import type { HdKeyring } from '@metamask/eth-hd-keyring'; | ||
| import { RootMessenger } from '../../../lib/messenger'; | ||
|
|
||
| /** | ||
| * Get the mnemonic for a given entropy source. If no source is | ||
| * provided, the primary HD keyring's mnemonic will be returned. | ||
| * | ||
| * @param messenger - The messenger. | ||
| * @param source - The ID of the entropy source keyring. | ||
| * @returns The mnemonic. | ||
| */ | ||
| export async function getMnemonic( | ||
| messenger: RootMessenger<KeyringControllerWithKeyringAction, never>, | ||
| source?: string | undefined, | ||
| ): Promise<Uint8Array> { | ||
| if (!source) { | ||
| const mnemonic = (await messenger.call( | ||
| 'KeyringController:withKeyring', | ||
| { | ||
| type: KeyringTypes.hd, | ||
| index: 0, | ||
| }, | ||
| async ({ keyring }) => (keyring as HdKeyring).mnemonic, | ||
| )) as Uint8Array | null; | ||
|
|
||
| if (!mnemonic) { | ||
| throw new Error('Primary keyring mnemonic unavailable.'); | ||
| } | ||
|
|
||
| return mnemonic; | ||
| } | ||
|
|
||
| try { | ||
| const keyringData = await messenger.call( | ||
| 'KeyringController:withKeyring', | ||
| { | ||
| id: source, | ||
| }, | ||
| async ({ keyring }) => ({ | ||
| type: keyring.type, | ||
| mnemonic: (keyring as HdKeyring).mnemonic, | ||
| }), | ||
| ); | ||
|
|
||
| const { type, mnemonic } = keyringData as { | ||
| type: string; | ||
| mnemonic?: Uint8Array; | ||
| }; | ||
|
|
||
| if (type !== KeyringTypes.hd || !mnemonic) { | ||
| // The keyring isn't guaranteed to have a mnemonic (e.g., | ||
| // hardware wallets, which can't be used as entropy sources), | ||
| // so we throw an error if it doesn't. | ||
| throw new Error(`Entropy source with ID "${source}" not found.`); | ||
| } | ||
|
|
||
| return mnemonic; | ||
| } catch { | ||
| throw new Error(`Entropy source with ID "${source}" not found.`); | ||
| } | ||
| } | ||
|
FrederikBolding marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Get the mnemonic seed for a given entropy source. If no source is | ||
| * provided, the primary HD keyring's mnemonic seed will be returned. | ||
| * | ||
| * @param messenger - The messenger. | ||
| * @param source - The ID of the entropy source keyring. | ||
| * @returns The mnemonic seed. | ||
| */ | ||
| export async function getMnemonicSeed( | ||
| messenger: RootMessenger<KeyringControllerWithKeyringAction, never>, | ||
| source?: string | undefined, | ||
| ): Promise<Uint8Array> { | ||
| if (!source) { | ||
| const seed = (await messenger.call( | ||
| 'KeyringController:withKeyring', | ||
| { | ||
| type: KeyringTypes.hd, | ||
| index: 0, | ||
| }, | ||
| async ({ keyring }) => (keyring as HdKeyring).seed, | ||
| )) as Uint8Array | null; | ||
|
|
||
| if (!seed) { | ||
| throw new Error('Primary keyring mnemonic unavailable.'); | ||
| } | ||
|
|
||
| return seed; | ||
| } | ||
|
|
||
| try { | ||
| const keyringData = await messenger.call( | ||
| 'KeyringController:withKeyring', | ||
| { | ||
| id: source, | ||
| }, | ||
| async ({ keyring }) => ({ | ||
| type: keyring.type, | ||
| seed: (keyring as HdKeyring).seed, | ||
| }), | ||
| ); | ||
|
|
||
| const { type, seed } = keyringData as { type: string; seed?: Uint8Array }; | ||
|
|
||
| if (type !== KeyringTypes.hd || !seed) { | ||
| // The keyring isn't guaranteed to have a mnemonic (e.g., | ||
| // hardware wallets, which can't be used as entropy sources), | ||
| // so we throw an error if it doesn't. | ||
| throw new Error(`Entropy source with ID "${source}" not found.`); | ||
| } | ||
|
|
||
| return seed; | ||
| } catch { | ||
| throw new Error(`Entropy source with ID "${source}" not found.`); | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -5494,41 +5494,6 @@ export default class MetamaskController extends EventEmitter { | |
| await this.keyringController.verifyPassword(password); | ||
| } | ||
|
|
||
| /** | ||
| * @type Identity | ||
| * @property {string} name - The account nickname. | ||
| * @property {string} address - The account's ethereum address, in lower case. | ||
| * receiving funds from our automatic Ropsten faucet. | ||
| */ | ||
|
FrederikBolding marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Gets the mnemonic of the user's primary keyring. | ||
| */ | ||
| getPrimaryKeyringMnemonic() { | ||
|
Member
Author
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. Dead code |
||
| const [keyring] = this.keyringController.getKeyringsByType( | ||
| KeyringType.hdKeyTree, | ||
| ); | ||
| if (!keyring.mnemonic) { | ||
| throw new Error('Primary keyring mnemonic unavailable.'); | ||
| } | ||
|
|
||
| return keyring.mnemonic; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the mnemonic seed of the user's primary keyring. | ||
| */ | ||
| getPrimaryKeyringMnemonicSeed() { | ||
| const [keyring] = this.keyringController.getKeyringsByType( | ||
| KeyringType.hdKeyTree, | ||
| ); | ||
| if (!keyring.seed) { | ||
| throw new Error('Primary keyring mnemonic unavailable.'); | ||
| } | ||
|
|
||
| return keyring.seed; | ||
| } | ||
|
|
||
| // | ||
| // Hardware | ||
| // | ||
|
|
||
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.