-
-
Notifications
You must be signed in to change notification settings - Fork 52
Refactor keyring to split bridge logic #143
Conversation
eebcb0f to
b0f5036
Compare
trezor-keyring-mv2.js
Outdated
| // here: https://github.com/trezor/connect/blob/dec4a56af8a65a6059fb5f63fa3c6690d2c37e00/src/js/iframe/builder.js#L181 | ||
| TrezorConnect.dispose(); | ||
| return Promise.resolve(); | ||
| } |
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.
It is worth mentioning I noticed that this method is only present for trezor and ledger keyring, but they go under different names (dispose and destroy).
Perhaps it would be a good idea to standardise the name so that we can call it if it exists. Same as we are doing with init.
This is where they are being called.
https://github.com/MetaMask/metamask-extension/blob/61b3d25ab343f287e409e8b4509e7e1320e118cd/app/scripts/metamask-controller.js#L4288
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.
Agreed! We've used destroy for this sort of thing elsewhere, so maybe that makes sense as the standard name. We can do that in a separate PR.
mcmire
left a comment
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.
Hey @bergarces, came across this PR in my notifications and had a few suggestions. Cheers!
trezor-keyring-mv2.js
Outdated
| TREZOR_CONNECT_MANIFEST, | ||
| } = require('./trezor-keyring'); | ||
|
|
||
| class TrezorKeyringMv2 extends TrezorKeyring { |
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.
This class doesn't appear to be the exact same as the previous TrezorKeyring class. For instance:
- The initialization logic that was in the constructor has been moved to
init disposenow returns a promise
Is that intentional in this PR? Those would be breaking changes if so, in which case we no longer have to have index.js expose a default export.
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.
Adding the init method and making the dispose method return a promise in case that other implementations of the bridge logic require it are intentional changes. I've added a PR for the KeyringController to take the init method into account, as other keyring implementations might need it as well.
What I'm not too sure about is if I should make them completely optional by not mentioning them at all in the base class, return an error or return an empty promise.
I'm starting to lean towards not defining them at all in the base class, then have a separate interface in the exported types in case they have to be added so that whoever implements them has a function signature, but would like to discuss it further.
Hey @mcmire, thanks for taking the time to review and for the suggestions, I've applied most of them and I'll make sure to use them for the ledger keyring changes too. |
|
I'd also wanted to say... would it be better to move all source files to its own folder? |
eb0fe99 to
bdaedad
Compare
|
Interesting - so the "bridge" portion is the child class, extending from the base keyring with the shared logic. Have you considered passing the bridge in as a constructor parameter? Then we could have a bridge API rather than needing the bridge to extend the keyring and override methods. Edit: Oh, perhaps that's what you meant here. |
|
^ I just thought of one more advantage to passing the bridge in: it makes it feasible to change the bridge at runtime. Not that we'd need that to be a supported feature right away, but, this relates to the idea I mentioned at one point for splitting up the Ledger Live bridge from the Ledger iframe-based bridge. That's a toggle that exists at runtime, so we'd weant the bridge to be changeable at runtime. Which... we could do if it was a child class by saving state, reconstructing, and restoring, but that would be complex to manage. It seems more sensible to make the bridge a separate entity that we could swap out without reconstructing the keyring itself. |
9e6496c to
767504f
Compare
Hey @Gudahtt, I've added the discussed changes. Instead of extending from Since deserialize also returns a promise, even though there's nothing asynchronous within the method, I've also moved it out of the constructor and replaced the https://github.com/MetaMask/KeyringController/pull/163/files |
02e7ee6 to
4c68241
Compare
a504e19 to
b1659c5
Compare
| } & Partial<ConnectSettings>, | ||
| ) { | ||
| TrezorConnect.on(DEVICE_EVENT, (event) => { | ||
| if (event.type !== DEVICE.CONNECT) { |
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.
It's possible to force typescript type inference by using the event discriminator, that saves the use of hasProperty and avoid importing @metamask/utils as a dependency.
|
|
||
| global.window = windowShim; | ||
| global.navigator = navigatorShim; | ||
| global.self = selfShim; |
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.
These shims are not needed for these tests, as we are creating stubs for TrezorConnect.
b1659c5 to
30304e1
Compare
|
👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎ This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored. Next stepsTake a deeper look at the dependencyTake a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev. Remove the packageIf you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency. Mark a package as acceptable riskTo ignore an alert, reply with a comment starting with |
|
New and updated dependency changes detected. Learn more about Socket for GitHub ↗︎ 🚮 Removed packages: @metamask/utils@4.0.0 |
Gudahtt
left a comment
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.
LGTM!
The goal of this PR is to split the logic that interacts directly with the Ledger device from the keyring logic that handles the interface with
KeyringController.TrezorKeyringandTrezorConnectBridgeclasses.TrezorBridgeinterface to use in other implementations.initmethod.