-
-
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 4 commits
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 |
---|---|---|
|
@@ -183,7 +183,7 @@ describe('KeyringHandler', () => { | |
index: 1, | ||
addressType: 'p2wpkh', | ||
entropySource: 'entropy2', | ||
synchronize: true, | ||
synchronize: false, | ||
}; | ||
|
||
await handler.createAccount(options); | ||
|
@@ -207,7 +207,7 @@ describe('KeyringHandler', () => { | |
index: 0, | ||
addressType, | ||
entropySource: 'm', | ||
synchronize: true, | ||
synchronize: false, | ||
}; | ||
|
||
await handler.createAccount(options); | ||
|
@@ -290,7 +290,7 @@ describe('KeyringHandler', () => { | |
index: 5, | ||
addressType: 'p2wpkh', | ||
entropySource: 'm', | ||
synchronize: true, | ||
synchronize: false, | ||
}; | ||
|
||
await handler.createAccount(options); | ||
|
@@ -359,7 +359,7 @@ describe('KeyringHandler', () => { | |
expect(discovered).toStrictEqual(expect.arrayContaining(expected)); | ||
}); | ||
|
||
it('filters out accounts that have no transaction history', async () => { | ||
it.skip('filters out accounts that have no transaction history', async () => { | ||
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. Why do we skip it? 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. So the filtering was happening when we were discovering accounts which included a full scan. Since now we don't scan the chain for activity we can't know if there were txs or not. |
||
const addressTypes = Object.values(BtcAccountType); | ||
const totalCombinations = scopes.length * addressTypes.length; | ||
|
||
|
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 { | ||||||||||
|
@@ -60,6 +61,7 @@ import { | |||||||||
mapToKeyringAccount, | ||||||||||
mapToTransaction, | ||||||||||
} from './mappings'; | ||||||||||
import { validateSelectedAccounts } from './validation'; | ||||||||||
import type { AccountUseCases } from '../use-cases/AccountUseCases'; | ||||||||||
|
||||||||||
export const CreateAccountRequest = object({ | ||||||||||
|
@@ -143,6 +145,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 +181,7 @@ export class KeyringHandler implements Keyring { | |||||||||
index, | ||||||||||
derivationPath, | ||||||||||
addressType, | ||||||||||
synchronize = true, | ||||||||||
synchronize = false, | ||||||||||
accountNameSuggestion, | ||||||||||
} = options; | ||||||||||
|
||||||||||
|
@@ -257,10 +264,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 +334,26 @@ export class KeyringHandler implements Keyring { | |||||||||
return this.#keyringRequest.route(request); | ||||||||||
} | ||||||||||
|
||||||||||
async setSelectedAccounts(accounts: string[]): Promise<void> { | ||||||||||
const accountIdSet = new Set(accounts); | ||||||||||
const allAccounts = await this.#accountsUseCases.list(); | ||||||||||
|
||||||||||
validateSelectedAccounts( | ||||||||||
accountIdSet, | ||||||||||
allAccounts.map((acc) => acc.id), | ||||||||||
); | ||||||||||
|
||||||||||
const selectedAccounts = allAccounts.filter((account) => | ||||||||||
accountIdSet.has(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 👍 |
||||||||||
const scanPromises = selectedAccounts.map(async (account) => { | ||||||||||
await this.#accountsUseCases.fullScan(account); | ||||||||||
}); | ||||||||||
|
const scanPromises = selectedAccounts.map(async (account) => { | |
await this.#accountsUseCases.fullScan(account); | |
}); | |
const scanPromises = selectedAccounts.map(async (account) => this.#accountsUseCases.fullScan(account)); |
Outdated
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.
What about this?
await Promise.all(scanPromises); | |
await Promise.allSettled(scanPromises); |
Ensures that a failing fullScan
call doesn't prevent the others to complete
orestiseth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import { | |
} from 'superstruct'; | ||
|
||
import type { BitcoinAccount, CodifiedError, Logger } from '../entities'; | ||
import { ValidationError } from '../entities'; | ||
|
||
export enum RpcMethod { | ||
StartSendTransactionFlow = 'startSendTransactionFlow', | ||
|
@@ -149,3 +150,25 @@ export function validateAccountBalance( | |
|
||
return NO_ERRORS_RESPONSE; | ||
} | ||
|
||
/** | ||
* Validates that all account IDs are part of the existing accounts. | ||
* | ||
* @param accountIds - Set of account IDs to validate | ||
* @param existingAccountIds - Array of existing account IDs | ||
* @throws {ValidationError} If any account ID is not part of existing accounts | ||
*/ | ||
export function validateSelectedAccounts( | ||
accountIds: Set<string>, | ||
existingAccountIds: string[], | ||
): void { | ||
const isSubset = (first: Set<string>, second: Set<string>): boolean => { | ||
return Array.from(first).every((element) => second.has(element)); | ||
}; | ||
Comment on lines
+165
to
+167
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. 👌 |
||
|
||
if (!isSubset(accountIds, new Set(existingAccountIds))) { | ||
throw new ValidationError( | ||
'Account IDs were not part of existing accounts.', | ||
); | ||
} | ||
} |
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.