-
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add support for setSelectedAccounts
#543
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
bf99101
dd19e37
576d098
5a6d17f
8fd8a3f
d0f5244
741cddf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { | |
ListAccountsRequestStruct, | ||
ListAccountTransactionsRequestStruct, | ||
MetaMaskOptionsStruct, | ||
SetSelectedAccountsRequestStruct, | ||
SubmitRequestRequestStruct, | ||
} from '@metamask/keyring-api'; | ||
import type { | ||
|
@@ -143,6 +144,11 @@ export class KeyringHandler implements Keyring { | |
assert(request, SubmitRequestRequestStruct); | ||
return this.submitRequest(request.params); | ||
} | ||
case `${KeyringRpcMethod.SetSelectedAccounts}`: { | ||
assert(request, SetSelectedAccountsRequestStruct); | ||
await this.setSelectedAccounts((request as any).params.accountIds); | ||
return null; | ||
} | ||
|
||
default: { | ||
throw new InexistentMethodError('Keyring method not supported', { | ||
|
@@ -174,7 +180,7 @@ export class KeyringHandler implements Keyring { | |
index, | ||
derivationPath, | ||
addressType, | ||
synchronize = true, | ||
synchronize = false, | ||
accountNameSuggestion, | ||
} = options; | ||
|
||
|
@@ -257,10 +263,7 @@ export class KeyringHandler implements Keyring { | |
), | ||
); | ||
|
||
// Return only accounts with history. | ||
return accounts | ||
.filter((account) => account.listTransactions().length > 0) | ||
.map(mapToDiscoveredAccount); | ||
return accounts.map(mapToDiscoveredAccount); | ||
} | ||
|
||
async getAccountBalances( | ||
|
@@ -330,6 +333,22 @@ export class KeyringHandler implements Keyring { | |
return this.#keyringRequest.route(request); | ||
} | ||
|
||
async setSelectedAccounts(accounts: string[]): Promise<void> { | ||
const isSubset = (first: Set<string>, second: Set<string>): boolean => { | ||
return Array.from(first).every((element) => second.has(element)); | ||
}; | ||
|
||
const currentAccounts = new Set( | ||
(await this.#accountsUseCases.list()).map((account) => account.id), | ||
); | ||
|
||
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. BTC snap has 2 methods for updating accounts. One is a full scan which previously was used when an account was created/discovered and the other one is a lighter synchronize which identified updates. Since now we don't do any scanning on discovery we don't really know when it is the first time we see an account (without storing anything on snaps state). Thus, on this PR we are going to run a full scan everytime the keyring method gets called and a synchronize on the cron job running every 30 seconds. Feel free to add your thoughts. We can always store "seen" accounts on the snap state but I am not sure how efficient is going to be if a user creates loads of them. 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. Makes a lot of sense to me 👍 |
||
if (!isSubset(new Set(accounts), currentAccounts)) { | ||
throw new Error(`Accounts ids were not part of existing accounts.`); | ||
aganglada marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
await this.#accountsUseCases.setSelectedAccounts(accounts); | ||
} | ||
|
||
#extractAddressType(path: string): AddressType { | ||
const segments = path.split('/'); | ||
if (segments.length < 4) { | ||
|
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.
I don't think the
as any
is needed here.On line 149,
assert
narrows the type ofrequest
, so that on line 150request
is of type inferred from the struct.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.
Actually it should be
await this.setSelectedAccounts(request.params.accounts);
. I have the change locally but haven't pushed it yet. Will do in a bit.