Skip to content

Commit 5d1a25b

Browse files
authored
feat: convert capabilities to getter for subclass overrides (#447)
<!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? Are there any issues or other links reviewers should consult to understand this pull request better? For instance: * Fixes #12345 * See: #67890 --> ## Examples <!-- Are there any examples of this change being used in another repository? When considering changes to the MetaMask module template, it's strongly preferred that the change be experimented with in another repository first. This gives reviewers a better sense of how the change works, making it less likely the change will need to be reverted or adjusted later. --> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Makes keyring capabilities dynamically overridable. > > - Change `KeyringWrapper.capabilities` from a readonly property to a getter, enabling subclasses to compute capabilities at runtime > - Update `QrKeyringV2` to override `capabilities` and return mode-specific capabilities (ACCOUNT vs HD) instead of redefining the property via `Object.defineProperty` > - Update changelogs for `keyring-api` and `keyring-eth-qr` to document the change > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 32aa0bd. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 3186dae commit 5d1a25b

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

packages/keyring-api/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
### Changed
2121

22+
- Change `KeyringWrapper.capabilities` from a readonly property to a getter ([#447](https://github.com/MetaMask/accounts/pull/447))
23+
- Allows subclasses to override and return capabilities dynamically based on runtime state.
2224
- Refine `EthAddressStruct` in order to make it compatible with the `Hex` type from `@metamask/utils` ([#405](https://github.com/MetaMask/accounts/pull/405))
2325

2426
## [21.3.0]

packages/keyring-api/src/api/v2/wrapper/keyring-wrapper.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export abstract class KeyringWrapper<
5151
{
5252
readonly type: `${KeyringType}`;
5353

54-
readonly capabilities: KeyringCapabilities;
54+
readonly #capabilities: KeyringCapabilities;
5555

5656
protected readonly inner: InnerKeyring;
5757

@@ -74,7 +74,19 @@ export abstract class KeyringWrapper<
7474
constructor(options: KeyringWrapperOptions<InnerKeyring>) {
7575
this.inner = options.inner;
7676
this.type = `${options.type}`;
77-
this.capabilities = options.capabilities;
77+
this.#capabilities = options.capabilities;
78+
}
79+
80+
/**
81+
* Get the capabilities of this keyring.
82+
*
83+
* Subclasses can override this getter to return capabilities dynamically
84+
* based on runtime state.
85+
*
86+
* @returns The keyring's capabilities.
87+
*/
88+
get capabilities(): KeyringCapabilities {
89+
return this.#capabilities;
7890
}
7991

8092
/**

packages/keyring-eth-qr/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- Add `QrKeyringV2` class implementing `KeyringV2` interface ([#411](https://github.com/MetaMask/accounts/pull/411))
12+
- Add `QrKeyringV2` class implementing `KeyringV2` interface ([#411](https://github.com/MetaMask/accounts/pull/411)), ([#447](https://github.com/MetaMask/accounts/pull/447))
1313
- Wraps legacy `QrKeyring` to expose accounts via the unified `KeyringV2` API and the `KeyringAccount` type.
1414
- Extends `EthKeyringWrapper` for common Ethereum logic.
1515

packages/keyring-eth-qr/src/qr-keyring-v2.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,24 @@ export class QrKeyringV2
104104
super({
105105
type: KeyringType.Qr,
106106
inner: options.legacyKeyring,
107-
// Placeholder - will be overridden by getter below
107+
// Default capabilities - overridden by getter below
108108
capabilities: HD_MODE_CAPABILITIES,
109109
});
110110
this.entropySource = options.entropySource;
111+
}
111112

112-
// Override the readonly capabilities property with a dynamic getter
113-
// that returns capabilities based on the current device mode.
114-
Object.defineProperty(this, 'capabilities', {
115-
get: (): KeyringCapabilities => {
116-
return this.inner.getMode() === DeviceMode.ACCOUNT
117-
? ACCOUNT_MODE_CAPABILITIES
118-
: HD_MODE_CAPABILITIES;
119-
},
120-
enumerable: true,
121-
configurable: false,
122-
});
113+
/**
114+
* Get the capabilities of this keyring.
115+
*
116+
* Overrides the base class getter to return capabilities dynamically
117+
* based on the current device mode.
118+
*
119+
* @returns The keyring's capabilities.
120+
*/
121+
override get capabilities(): KeyringCapabilities {
122+
return this.inner.getMode() === DeviceMode.ACCOUNT
123+
? ACCOUNT_MODE_CAPABILITIES
124+
: HD_MODE_CAPABILITIES;
123125
}
124126

125127
/**

0 commit comments

Comments
 (0)