Skip to content

Update eth-keyring-controller#2041

Merged
mikesposito merged 3 commits intomainfrom
chore/update-eth-keyring-controller
Nov 16, 2023
Merged

Update eth-keyring-controller#2041
mikesposito merged 3 commits intomainfrom
chore/update-eth-keyring-controller

Conversation

@mikesposito
Copy link
Copy Markdown
Member

@mikesposito mikesposito commented Nov 15, 2023

Explanation

This PR updates @metamask/eth-keyring-controller in KeyringController (packages/keyring-controller) dependencies, to ^15.0.0.

The major change that this update brings in is typings for the encryptor.

References

Changelog

@metamask/keyring-controller

  • BREAKING Change encryptor constructor option property type to GenericEncryptor | ExportableKeyEncryptor | undefined
    • When the controller is instantiated with cacheEncryptionKey: true, encryptor may no longer be of type GenericEncryptor.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@mikesposito mikesposito force-pushed the chore/update-eth-keyring-controller branch 2 times, most recently from e4f7d3f to a4fae98 Compare November 15, 2023 13:02
@socket-security
Copy link
Copy Markdown

Updated dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
@metamask/eth-keyring-controller 14.0.0...15.0.0 None +0/-0 169 kB metamaskbot

Comment on lines 319 to 322
// @ts-expect-error Types are enforced in `KeyringControllerOptions`
// constructor options, but typescript is not able to correctly infer the type of the
// `encryptor` property based on `cacheEncryptionKey` value here.
this.#keyring = new EthKeyringController({
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if KeyringControllerOptions correctly forbid cacheEncryptionKey: true with an encryptor that doesn't support key export, Typescript is unable to correctly infer that cacheEncryptionKey is of type true instead of the more generic boolean.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I feel like this is happening because of the way that we're working with the options. It knows that in the argument that the constructor takes, if cacheEncryptionKey is true then encryptor may be an ExportableKeyEncryptor, and if cacheEncryptionKey is false then encryptor may be a GenericEncryptor or a ExportableKeyEncryptor. However, the destructuring kind of screws this up and divorces these options from each other, so from TypeScript's perspective, cacheEncryptionKey is just a boolean and encryptor can be GenericEncryptor | ExportableKeyEncryptor | undefined. Because options can be in two different forms, we need to preserve it. We also need to take TypeScript down one of two code paths depending on which one it is.

In other words we could do something like this:

  constructor(options: KeyringControllerOptions) {
    const {
      syncIdentities,
      updateIdentities,
      setSelectedAddress,
      setAccountLabel,
      keyringBuilders,
      messenger,
      state,
    } = options;

    // ...

    if (options.cacheEncryptionKey) {
      this.#keyring = new EthKeyringController({
        initState: state,
        encryptor: options.encryptor,
        keyringBuilders,
        cacheEncryptionKey: options.cacheEncryptionKey,
      });
    } else {
      this.#keyring = new EthKeyringController({
        initState: state,
        encryptor: options.encryptor,
        keyringBuilders,
        cacheEncryptionKey: options.cacheEncryptionKey ?? false,
      });
    }

    // ...
  }

The downside to this approach is that we now need to remove some of the JSDoc for the constructor. But that would get you what you want.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the destructuring is definitely behind us losing this type information. That seems like a good suggestion to me.

There is no need to remove the TSDoc breakdown of the properties though. The linter only requires that the properties be defined if they are destructured, but they're always allowed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I applied the suggestion

@mikesposito mikesposito marked this pull request as ready for review November 15, 2023 13:07
@mikesposito mikesposito requested a review from a team as a code owner November 15, 2023 13:07
@mikesposito mikesposito requested a review from Gudahtt November 15, 2023 13:10
@mikesposito mikesposito force-pushed the chore/update-eth-keyring-controller branch from 53ed8ec to 3dbf970 Compare November 16, 2023 10:00
@mikesposito mikesposito requested a review from mcmire November 16, 2023 10:06
@mikesposito mikesposito force-pushed the chore/update-eth-keyring-controller branch from 3dbf970 to c3d4d5c Compare November 16, 2023 14:28
Copy link
Copy Markdown
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mikesposito mikesposito force-pushed the chore/update-eth-keyring-controller branch from c3d4d5c to bbeebb2 Compare November 16, 2023 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants