generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add KeyringV2 wrapper and adapters
#398
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
Open
mathieuartu
wants to merge
22
commits into
main
Choose a base branch
from
feat/add-keyring-wrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
07162a5
feat: add keyring wrapper
mathieuartu 0c8ada7
fix: update CHANGELOG
mathieuartu 8999d5b
fix: lint issues
mathieuartu caec531
feat: add uuid to keyring-api
mathieuartu 5cda8f7
fix: eslint resolution path
mathieuartu 157125a
fix: use capabilities by default
mathieuartu 594b75e
fix: drop normalization in address resolver
mathieuartu 3df80b6
fix: lint issue
mathieuartu 7975e78
fix: optional exportAccounts
mathieuartu f88f4c4
feat: move getAccounts to being abstract
mathieuartu 4c8d7fa
feat: add HD keyring V2
mathieuartu f2f4071
fix: UTs, lint, coverage
mathieuartu b93d870
fix: lint issue
mathieuartu 66c53da
fix: increase coverage, fix readme graph, fix lint issue
mathieuartu 413c641
feat: add simple eth keyring V2 adapter
mathieuartu 972d46e
feat: add QR KeyringV2 adapter
mathieuartu 369230d
fix: lint issue
mathieuartu 0e1f72a
fix: update readme
mathieuartu 3ee7007
feat: add ledger & trezor KeyringV2 adapters
mathieuartu 45d3c9e
fix: account deletion bug for HD keyring V2
mathieuartu 4f7b828
fix: align v2 capabilities on the inner hd keyring
mathieuartu 956d921
fix: export hd keyring v2 options
mathieuartu 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
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,2 @@ | ||
| export * from './keyring-wrapper'; | ||
| export * from './keyring-address-resolver'; |
28 changes: 28 additions & 0 deletions
28
packages/keyring-api/src/api/v2/wrapper/keyring-address-resolver.test.ts
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,28 @@ | ||
| import { InMemoryKeyringAddressResolver } from './keyring-address-resolver'; | ||
|
|
||
| describe('InMemoryKeyringAddressResolver', () => { | ||
| it('registers and resolves account IDs and addresses', () => { | ||
| const resolver = new InMemoryKeyringAddressResolver(); | ||
|
|
||
| const address = '0xaBc'; | ||
| const id = resolver.register(address); | ||
|
|
||
| expect(typeof id).toBe('string'); | ||
|
|
||
| const resolvedAddress = resolver.getAddress(id); | ||
| expect(resolvedAddress).toBe(address); | ||
|
|
||
| const resolvedId = resolver.getAccountId(address); | ||
| expect(resolvedId).toBe(id); | ||
| }); | ||
|
|
||
| it('reuses the same ID when registering the same address', () => { | ||
| const resolver = new InMemoryKeyringAddressResolver(); | ||
|
|
||
| const address = '0xaBc'; | ||
| const firstId = resolver.register(address); | ||
| const secondId = resolver.register(address); | ||
|
|
||
| expect(firstId).toBe(secondId); | ||
| }); | ||
| }); |
54 changes: 54 additions & 0 deletions
54
packages/keyring-api/src/api/v2/wrapper/keyring-address-resolver.ts
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,54 @@ | ||
| import type { AccountId } from '@metamask/keyring-utils'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| /** | ||
| * Mapping between an internal AccountId and the underlying keyring address. | ||
| */ | ||
| export type KeyringAddressResolver = { | ||
| /** | ||
| * Resolve an AccountId into an underlying address. | ||
| */ | ||
| getAddress(accountId: AccountId): string | undefined; | ||
|
|
||
| /** | ||
| * Resolve an underlying address into an AccountId. | ||
| */ | ||
| getAccountId(address: string): AccountId | undefined; | ||
|
|
||
| /** | ||
| * Register a new mapping. Implementations are free to decide how the | ||
| * AccountId is generated (e.g. UUIDv4). | ||
| */ | ||
| register(address: string): AccountId; | ||
| }; | ||
|
|
||
| /** | ||
| * Simple in-memory AccountId/address resolver used by default. This is mostly | ||
| * intended for controller-managed lifecycles where wrappers live in memory. | ||
| */ | ||
| export class InMemoryKeyringAddressResolver implements KeyringAddressResolver { | ||
| readonly #idByAddress = new Map<string, AccountId>(); | ||
|
|
||
| readonly #addressById = new Map<AccountId, string>(); | ||
|
|
||
| getAddress(accountId: AccountId): string | undefined { | ||
| return this.#addressById.get(accountId); | ||
| } | ||
|
|
||
| getAccountId(address: string): AccountId | undefined { | ||
| return this.#idByAddress.get(address); | ||
| } | ||
|
|
||
| register(address: string): AccountId { | ||
| const existing = this.#idByAddress.get(address); | ||
| if (existing) { | ||
| return existing; | ||
| } | ||
| const id = uuidv4(); | ||
|
|
||
| this.#idByAddress.set(address, id); | ||
| this.#addressById.set(id, address); | ||
|
|
||
| return id; | ||
| } | ||
| } | ||
203 changes: 203 additions & 0 deletions
203
packages/keyring-api/src/api/v2/wrapper/keyring-wrapper.test.ts
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,203 @@ | ||
| import type { Keyring, AccountId } from '@metamask/keyring-utils'; | ||
| import type { Hex, Json } from '@metamask/utils'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| import { InMemoryKeyringAddressResolver } from './keyring-address-resolver'; | ||
| import { KeyringWrapper } from './keyring-wrapper'; | ||
| import type { KeyringAccount } from '../../account'; | ||
| import type { KeyringCapabilities } from '../keyring-capabilities'; | ||
| import { KeyringType } from '../keyring-type'; | ||
|
|
||
| class TestKeyringWrapper extends KeyringWrapper<TestKeyring> { | ||
| async getAccounts(): Promise<KeyringAccount[]> { | ||
| const addresses = await this.inner.getAccounts(); | ||
| const scopes = this.capabilities.scopes ?? ['eip155:1']; | ||
|
|
||
| return addresses.map((address) => { | ||
| const id = this.resolver.register(address); | ||
|
|
||
| const account: KeyringAccount = { | ||
| id, | ||
| type: 'eip155:eoa', | ||
| address, | ||
| scopes, | ||
| options: {}, | ||
| methods: [], | ||
| }; | ||
|
|
||
| return account; | ||
| }); | ||
| } | ||
|
|
||
| public deletedAccountIds: AccountId[] = []; | ||
|
|
||
| async createAccounts(): Promise<KeyringAccount[]> { | ||
| return this.getAccounts(); | ||
| } | ||
|
|
||
| async deleteAccount(accountId: AccountId): Promise<void> { | ||
| this.deletedAccountIds.push(accountId); | ||
| } | ||
|
|
||
| async submitRequest(): Promise<any> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| class TestKeyring implements Keyring { | ||
| static type = 'Test Keyring'; | ||
|
|
||
| public type = TestKeyring.type; | ||
|
|
||
| readonly #accounts: Hex[]; | ||
|
|
||
| public lastDeserializedState: Json | undefined; | ||
|
|
||
| constructor(addresses: Hex[] = []) { | ||
| this.#accounts = addresses; | ||
| } | ||
|
|
||
| async serialize(): Promise<Json> { | ||
| return { accounts: this.#accounts }; | ||
| } | ||
|
|
||
| async deserialize(state: Json): Promise<void> { | ||
| this.lastDeserializedState = state; | ||
| } | ||
|
|
||
| async getAccounts(): Promise<Hex[]> { | ||
| return this.#accounts; | ||
| } | ||
|
|
||
| async addAccounts(_numberOfAccounts = 1): Promise<Hex[]> { | ||
| return this.#accounts; | ||
| } | ||
| } | ||
|
|
||
| const capabilities: KeyringCapabilities = { | ||
| scopes: ['eip155:10'], | ||
| }; | ||
|
|
||
| const entropySourceId = 'test-entropy-source'; | ||
|
|
||
| describe('KeyringWrapper', () => { | ||
| it('serializes and deserializes via the inner keyring', async () => { | ||
| const inner = new TestKeyring(['0x1']); | ||
| const wrapper = new TestKeyringWrapper({ | ||
| inner, | ||
| type: KeyringType.Hd, | ||
| capabilities, | ||
| entropySourceId, | ||
| }); | ||
|
|
||
| const state = await wrapper.serialize(); | ||
| expect(state).toStrictEqual({ accounts: ['0x1'] }); | ||
|
|
||
| await wrapper.deserialize(state); | ||
| expect(inner.lastDeserializedState).toStrictEqual(state); | ||
| }); | ||
|
|
||
| it('returns accounts with stable IDs and correct addresses', async () => { | ||
| const addresses = ['0x1' as const, '0x2' as const]; | ||
| const inner = new TestKeyring(addresses); | ||
| const wrapper = new TestKeyringWrapper({ | ||
| inner, | ||
| type: KeyringType.Hd, | ||
| capabilities, | ||
| entropySourceId, | ||
| }); | ||
|
|
||
| const accounts = await wrapper.getAccounts(); | ||
|
|
||
| expect(accounts).toHaveLength(addresses.length); | ||
|
|
||
| const ids = new Set<string>(); | ||
| accounts.forEach((account: KeyringAccount, index) => { | ||
| expect(account.address).toBe(addresses[index]); | ||
| expect(account.type).toBe('eip155:eoa'); | ||
| expect(account.scopes).toStrictEqual(capabilities.scopes); | ||
| expect(account.options).toStrictEqual({}); | ||
| expect(account.methods).toStrictEqual([]); | ||
|
|
||
| ids.add(account.id); | ||
| }); | ||
|
|
||
| // Ensure IDs are unique | ||
| expect(ids.size).toBe(addresses.length); | ||
|
|
||
| // getAccount should resolve by ID | ||
| const [firstAccount] = accounts; | ||
| expect(firstAccount).toBeDefined(); | ||
| if (!firstAccount) { | ||
| throw new Error('Expected at least one account from the wrapper'); | ||
| } | ||
| const resolved = await wrapper.getAccount(firstAccount.id); | ||
| expect(resolved.address).toBe(firstAccount.address); | ||
| }); | ||
|
|
||
| it('throws when requesting an unknown account', async () => { | ||
| const inner = new TestKeyring([]); | ||
| const wrapper = new TestKeyringWrapper({ | ||
| inner, | ||
| type: KeyringType.Hd, | ||
| capabilities, | ||
| entropySourceId, | ||
| }); | ||
|
|
||
| await expect(wrapper.getAccount(uuidv4())).rejects.toThrow( | ||
| 'Account not found for id', | ||
| ); | ||
| }); | ||
|
|
||
| it('throws when account mapping exists but account object cannot be found', async () => { | ||
| const addresses = ['0x1' as const]; | ||
| const inner = new TestKeyring(addresses); | ||
| const resolver = new InMemoryKeyringAddressResolver(); | ||
| const wrapper = new TestKeyringWrapper({ | ||
| inner, | ||
| type: KeyringType.Hd, | ||
| capabilities, | ||
| resolver, | ||
| entropySourceId, | ||
| }); | ||
|
|
||
| // Prime the resolver by calling getAccounts once | ||
| await wrapper.getAccounts(); | ||
|
|
||
| // Now, simulate a missing account object by clearing the underlying | ||
| // accounts of the inner keyring. | ||
| const emptyInner = new TestKeyring([] as Hex[]); | ||
| const inconsistentWrapper = new TestKeyringWrapper({ | ||
| inner: emptyInner, | ||
| type: KeyringType.Hd, | ||
| capabilities, | ||
| resolver, | ||
| entropySourceId, | ||
| }); | ||
|
|
||
| const accountId = resolver.getAccountId( | ||
| addresses[0] as string, | ||
| ) as AccountId; | ||
|
|
||
| await expect(inconsistentWrapper.getAccount(accountId)).rejects.toThrow( | ||
| 'Account not found for id', | ||
| ); | ||
| }); | ||
|
|
||
| it('falls back to mainnet scope when capabilities.scopes is not set', async () => { | ||
| const addresses = ['0x1' as const]; | ||
| const inner = new TestKeyring(addresses); | ||
| const wrapper = new TestKeyringWrapper({ | ||
| inner, | ||
| type: KeyringType.Hd, | ||
| entropySourceId, | ||
| // Explicitly omit scopes to exercise the default fallback. | ||
| capabilities: {} as KeyringCapabilities, | ||
| }); | ||
|
|
||
| const accounts = await wrapper.getAccounts(); | ||
|
|
||
| expect(accounts).toHaveLength(1); | ||
| expect(accounts[0]?.scopes).toStrictEqual(['eip155:1']); | ||
| }); | ||
| }); |
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.