From b17c83c2bde5eabad695b923e967a183a2d97a1c Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Mon, 28 Oct 2024 12:55:23 +0200 Subject: [PATCH 1/9] add(wip): switch account action logic --- src/controllers/actions/actions.ts | 22 ++++++++- src/controllers/main/main.ts | 74 +++++++++++++++++++++++++----- src/interfaces/userRequest.ts | 6 +-- 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/src/controllers/actions/actions.ts b/src/controllers/actions/actions.ts index a10fb0245..d28df59f4 100644 --- a/src/controllers/actions/actions.ts +++ b/src/controllers/actions/actions.ts @@ -12,6 +12,17 @@ import { ENTRY_POINT_AUTHORIZATION_REQUEST_ID } from '../../libs/userOperation/u import { AccountsController } from '../accounts/accounts' import EventEmitter from '../eventEmitter/eventEmitter' +export type SwitchAccountAction = { + id: UserRequest['id'] + type: 'switchAccount' + userRequest: { + meta: { + accountAddr: Account['addr'] + switchToAccountAddr: Account['addr'] + } + } +} + export type AccountOpAction = { id: SignUserRequest['id'] type: 'accountOp' @@ -36,7 +47,12 @@ export type DappRequestAction = { userRequest: DappUserRequest } -export type Action = AccountOpAction | SignMessageAction | BenzinAction | DappRequestAction +export type Action = + | AccountOpAction + | SignMessageAction + | BenzinAction + | DappRequestAction + | SwitchAccountAction /** * The ActionsController is responsible for storing the converted userRequests @@ -90,6 +106,9 @@ export class ActionsController extends EventEmitter { if (a.type === 'benzin') { return a.userRequest.meta.accountAddr === this.#accounts.selectedAccount } + if (a.type === 'switchAccount') { + return a.userRequest.meta.switchToAccountAddr !== this.#accounts.selectedAccount + } return true }) @@ -179,6 +198,7 @@ export class ActionsController extends EventEmitter { removeAction(actionId: Action['id'], shouldOpenNextAction: boolean = true) { this.actionsQueue = this.actionsQueue.filter((a) => a.id !== actionId) + console.log('New actionsQueue:', this.actionsQueue) if (shouldOpenNextAction) { this.#setCurrentAction(this.visibleActionsQueue[0] || null) } diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index c02f03a21..08adbccc4 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -79,7 +79,12 @@ import shortenAddress from '../../utils/shortenAddress' import wait from '../../utils/wait' import { AccountAdderController } from '../accountAdder/accountAdder' import { AccountsController } from '../accounts/accounts' -import { AccountOpAction, ActionsController, SignMessageAction } from '../actions/actions' +import { + AccountOpAction, + ActionsController, + SignMessageAction, + SwitchAccountAction +} from '../actions/actions' import { ActivityController } from '../activity/activity' import { AddressBookController } from '../addressBook/addressBook' import { DappsController } from '../dapps/dapps' @@ -937,13 +942,12 @@ export class MainController extends EventEmitter { throw ethErrors.rpc.invalidRequest('No msg request to sign') } const msgAddress = getAddress(msg?.[1]) - // TODO: if address is in this.accounts in theory the user should be able to sign - // e.g. if an acc from the wallet is used as a signer of another wallet - if (msgAddress !== this.accounts.selectedAccount) { + + if (!this.accounts.accounts.some((a) => a.addr === msgAddress)) { dappPromise.reject( ethErrors.provider.userRejectedRequest( // if updating, check https://github.com/AmbireTech/ambire-wallet/pull/1627 - 'the dApp is trying to sign using an address different from the currently selected account. Try re-connecting.' + 'the dApp is trying to sign using an address that is not imported in the extension.' ) ) return @@ -979,13 +983,10 @@ export class MainController extends EventEmitter { throw ethErrors.rpc.invalidRequest('No msg request to sign') } const msgAddress = getAddress(msg?.[0]) - // TODO: if address is in this.accounts in theory the user should be able to sign - // e.g. if an acc from the wallet is used as a signer of another wallet - if (msgAddress !== this.accounts.selectedAccount) { + if (!this.accounts.accounts.some((a) => a.addr === msgAddress)) { dappPromise.reject( ethErrors.provider.userRejectedRequest( - // if updating, check https://github.com/AmbireTech/ambire-wallet/pull/1627 - 'the dApp is trying to sign using an address different from the currently selected account. Try re-connecting.' + 'the dApp is trying to sign using an address that is not in the extension.' ) ) return @@ -1056,7 +1057,46 @@ export class MainController extends EventEmitter { } if (userRequest) { + const isASignOperationRequestedForAnotherAccount = + userRequest.meta.isSignAction && + userRequest.meta.accountAddr !== this.accounts.selectedAccount + + if (isASignOperationRequestedForAnotherAccount) { + const network = this.networks.networks.find( + (n) => Number(n.chainId) === Number(dapp?.chainId) + ) + + if (!network) { + throw ethErrors.provider.chainDisconnected('Transaction failed - unknown network') + } + + await this.addUserRequest(userRequest, false) + await this.addUserRequest( + { + id: new Date().getTime(), + action: { + kind: 'switchAccount', + params: {} + }, + session: request.session, + meta: { + accountAddr: this.accounts.selectedAccount, + switchToAccountAddr: userRequest.meta.accountAddr, + networkId: network.id, + isSignAction: false + }, + dappPromise: { + session: request.session, + resolve: () => {}, + reject: () => {} + } + }, + true + ) + return + } await this.addUserRequest(userRequest, withPriority) + this.emitUpdate() } } @@ -1300,7 +1340,7 @@ export class MainController extends EventEmitter { this.actions.addOrUpdateAction(accountOpAction, withPriority, executionType) } } else { - let actionType: 'dappRequest' | 'benzin' | 'signMessage' = 'dappRequest' + let actionType: 'dappRequest' | 'benzin' | 'signMessage' | 'switchAccount' = 'dappRequest' if (req.action.kind === 'typedMessage' || req.action.kind === 'message') { actionType = 'signMessage' @@ -1320,6 +1360,8 @@ export class MainController extends EventEmitter { } } if (req.action.kind === 'benzin') actionType = 'benzin' + if (req.action.kind === 'switchAccount') actionType = 'switchAccount' + this.actions.addOrUpdateAction( { id, @@ -1559,6 +1601,16 @@ export class MainController extends EventEmitter { this.emitUpdate() } + async resolveSwitchAccountAction(actionId: SwitchAccountAction['id']) { + const switchAccountAction = this.actions.actionsQueue.find((a) => a.id === actionId) + if (!switchAccountAction || switchAccountAction.type !== 'switchAccount') return + + const { userRequest } = switchAccountAction + await this.accounts.selectAccount(userRequest.meta.switchToAccountAddr) + this.removeUserRequest(actionId) + this.emitUpdate() + } + async #updateGasPrice() { await this.#initialLoadPromise diff --git a/src/interfaces/userRequest.ts b/src/interfaces/userRequest.ts index 81f824cce..f174b98b7 100644 --- a/src/interfaces/userRequest.ts +++ b/src/interfaces/userRequest.ts @@ -3,7 +3,6 @@ import { TypedDataDomain, TypedDataField } from 'ethers' import { AccountId } from './account' import { DappProviderRequest } from './dapp' -import { HumanizerFragment } from './humanizer' import { NetworkId } from './network' export interface Calls { @@ -34,9 +33,6 @@ export interface Message { networkId: NetworkId content: PlainTextMessage | TypedMessage signature: string | null - // those are the async non global data fragments that are obtained via the humanizer and stored - // in the Message so we can visualize it better and fatter later - humanizerFragments?: HumanizerFragment[] } export interface SignUserRequest { @@ -60,7 +56,7 @@ export interface SignUserRequest { export interface DappUserRequest { id: string | number action: { - kind: Exclude + kind: Exclude params: any } session: DappProviderRequest['session'] From 0c7d35e9d4f369f67eabb5f2e0da6ab74f4952c5 Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Thu, 21 Nov 2024 11:39:45 +0200 Subject: [PATCH 2/9] fix: switch account action logic --- src/controllers/main/main.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index 0d3a667a5..a57058bf9 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -1135,7 +1135,7 @@ export class MainController extends EventEmitter { if (userRequest) { const isASignOperationRequestedForAnotherAccount = userRequest.meta.isSignAction && - userRequest.meta.accountAddr !== this.accounts.selectedAccount + userRequest.meta.accountAddr !== this.selectedAccount.account?.addr if (isASignOperationRequestedForAnotherAccount) { const network = this.networks.networks.find( @@ -1156,7 +1156,7 @@ export class MainController extends EventEmitter { }, session: request.session, meta: { - accountAddr: this.accounts.selectedAccount, + accountAddr: this.selectedAccount.account?.addr, switchToAccountAddr: userRequest.meta.accountAddr, networkId: network.id, isSignAction: false @@ -1734,7 +1734,7 @@ export class MainController extends EventEmitter { if (!switchAccountAction || switchAccountAction.type !== 'switchAccount') return const { userRequest } = switchAccountAction - await this.accounts.selectAccount(userRequest.meta.switchToAccountAddr) + await this.#selectAccount(userRequest.meta.switchToAccountAddr) this.removeUserRequest(actionId) this.emitUpdate() } From 07345a1eca531ff07656156ad23469034034f668 Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Thu, 21 Nov 2024 17:46:40 +0200 Subject: [PATCH 3/9] update: switch account request reject to reject the next request too --- src/controllers/actions/actions.ts | 3 +++ src/controllers/main/main.ts | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/controllers/actions/actions.ts b/src/controllers/actions/actions.ts index a1e405199..dea234f8d 100644 --- a/src/controllers/actions/actions.ts +++ b/src/controllers/actions/actions.ts @@ -283,6 +283,9 @@ export class ActionsController extends EventEmitter { if (a.type === 'benzin') { return a.userRequest.meta.accountAddr !== address } + if (a.type === 'switchAccount') { + return a.userRequest.meta.switchToAccountAddr !== address + } return true }) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index a57058bf9..7223d4716 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -1145,6 +1145,7 @@ export class MainController extends EventEmitter { if (!network) { throw ethErrors.provider.chainDisconnected('Transaction failed - unknown network') } + const userRequestId = userRequest.id await this.addUserRequest(userRequest, false) await this.addUserRequest( @@ -1164,7 +1165,9 @@ export class MainController extends EventEmitter { dappPromise: { session: request.session, resolve: () => {}, - reject: () => {} + reject: () => { + this.rejectUserRequest('Switch account request rejected', userRequestId) + } } }, true From 99085f3b518f5270c27d489c56e3031f2f1a3196 Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Fri, 22 Nov 2024 11:05:11 +0200 Subject: [PATCH 4/9] delete: log in actions ctrl --- src/controllers/actions/actions.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/controllers/actions/actions.ts b/src/controllers/actions/actions.ts index dea234f8d..894cc6f1f 100644 --- a/src/controllers/actions/actions.ts +++ b/src/controllers/actions/actions.ts @@ -173,7 +173,6 @@ export class ActionsController extends EventEmitter { removeAction(actionId: Action['id'], shouldOpenNextAction: boolean = true) { this.actionsQueue = this.actionsQueue.filter((a) => a.id !== actionId) - console.log('New actionsQueue:', this.actionsQueue) if (shouldOpenNextAction) { this.#setCurrentAction(this.visibleActionsQueue[0] || null) } From 7935bbfcf5cf3e971a2dd59b140c5dc1eb40e242 Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Fri, 22 Nov 2024 12:46:35 +0200 Subject: [PATCH 5/9] fix: dappRequest account safeguard --- src/consts/dappCommunication.ts | 8 +++++++ src/controllers/main/main.ts | 42 +++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 src/consts/dappCommunication.ts diff --git a/src/consts/dappCommunication.ts b/src/consts/dappCommunication.ts new file mode 100644 index 000000000..bc1f5eea4 --- /dev/null +++ b/src/consts/dappCommunication.ts @@ -0,0 +1,8 @@ +const ORIGINS_WHITELISTED_TO_ALL_ACCOUNTS = [ + 'https://legends.ambire.com', + 'https://legends-staging.ambire.com', + 'http://localhost:19006', + 'http://localhost:19007' +] + +export { ORIGINS_WHITELISTED_TO_ALL_ACCOUNTS } diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index 1dc345d1a..5ed5b1035 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -5,6 +5,7 @@ import { getAddress, getBigInt, Interface, isAddress } from 'ethers' import AmbireAccount from '../../../contracts/compiled/AmbireAccount.json' import AmbireFactory from '../../../contracts/compiled/AmbireFactory.json' import EmittableError from '../../classes/EmittableError' +import { ORIGINS_WHITELISTED_TO_ALL_ACCOUNTS } from '../../consts/dappCommunication' import { AMBIRE_ACCOUNT_FACTORY, SINGLETON } from '../../consts/deploy' import { BIP44_LEDGER_DERIVATION_TEMPLATE, @@ -957,6 +958,21 @@ export class MainController extends EventEmitter { ) } + #getUserRequestAccountError(dappOrigin: string, fromAccountAddr: string): string | null { + if (ORIGINS_WHITELISTED_TO_ALL_ACCOUNTS.includes(dappOrigin)) { + const isAddressInAccounts = this.accounts.accounts.some((a) => a.addr === fromAccountAddr) + + if (isAddressInAccounts) return null + + return 'The dApp is trying to sign using an address that is not imported in the extension.' + } + const isAddressSelected = this.selectedAccount.account?.addr === fromAccountAddr + + if (isAddressSelected) return null + + return 'The dApp is trying to sign using an address that is not selected in the extension.' + } + async buildUserRequestFromDAppRequest( request: DappProviderRequest, dappPromise: { @@ -984,6 +1000,13 @@ export class MainController extends EventEmitter { (n) => Number(n.chainId) === Number(dapp?.chainId) ) + const accountError = this.#getUserRequestAccountError(dappPromise.session.origin, accountAddr) + + if (accountError) { + dappPromise.reject(ethErrors.provider.userRejectedRequest(accountError)) + return + } + if (!network) { throw ethErrors.provider.chainDisconnected('Transaction failed - unknown network') } @@ -1017,14 +1040,10 @@ export class MainController extends EventEmitter { throw ethErrors.rpc.invalidRequest('No msg request to sign') } const msgAddress = getAddress(msg?.[1]) + const accountError = this.#getUserRequestAccountError(dappPromise.session.origin, msgAddress) - if (!this.accounts.accounts.some((a) => a.addr === msgAddress)) { - dappPromise.reject( - ethErrors.provider.userRejectedRequest( - // if updating, check https://github.com/AmbireTech/ambire-wallet/pull/1627 - 'the dApp is trying to sign using an address that is not imported in the extension.' - ) - ) + if (accountError) { + dappPromise.reject(ethErrors.provider.userRejectedRequest(accountError)) return } @@ -1058,12 +1077,9 @@ export class MainController extends EventEmitter { throw ethErrors.rpc.invalidRequest('No msg request to sign') } const msgAddress = getAddress(msg?.[0]) - if (!this.accounts.accounts.some((a) => a.addr === msgAddress)) { - dappPromise.reject( - ethErrors.provider.userRejectedRequest( - 'the dApp is trying to sign using an address that is not in the extension.' - ) - ) + const accountError = this.#getUserRequestAccountError(dappPromise.session.origin, msgAddress) + if (accountError) { + dappPromise.reject(ethErrors.provider.userRejectedRequest(accountError)) return } From bf6efcdac36fe3c9c468fde6840c3e789f207d0a Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Mon, 25 Nov 2024 12:57:35 +0200 Subject: [PATCH 6/9] delete: resolveSwitchAccountAction method in main --- src/controllers/main/main.ts | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index 5ed5b1035..344661696 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -90,12 +90,7 @@ import shortenAddress from '../../utils/shortenAddress' import wait from '../../utils/wait' import { AccountAdderController } from '../accountAdder/accountAdder' import { AccountsController } from '../accounts/accounts' -import { - AccountOpAction, - ActionsController, - SignMessageAction, - SwitchAccountAction -} from '../actions/actions' +import { AccountOpAction, ActionsController, SignMessageAction } from '../actions/actions' import { ActivityController } from '../activity/activity' import { AddressBookController } from '../addressBook/addressBook' import { DappsController } from '../dapps/dapps' @@ -1747,16 +1742,6 @@ export class MainController extends EventEmitter { this.emitUpdate() } - async resolveSwitchAccountAction(actionId: SwitchAccountAction['id']) { - const switchAccountAction = this.actions.actionsQueue.find((a) => a.id === actionId) - if (!switchAccountAction || switchAccountAction.type !== 'switchAccount') return - - const { userRequest } = switchAccountAction - await this.#selectAccount(userRequest.meta.switchToAccountAddr) - this.removeUserRequest(actionId) - this.emitUpdate() - } - async #updateGasPrice() { await this.#initialLoadPromise From 83da6bcc87c610a8b2f8e6b326a65a8e2da8717a Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Mon, 25 Nov 2024 13:30:28 +0200 Subject: [PATCH 7/9] update: pass nextRequestType for switchAccountRequests --- src/controllers/main/main.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index 344661696..ad6009703 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -1169,6 +1169,7 @@ export class MainController extends EventEmitter { meta: { accountAddr: this.selectedAccount.account?.addr, switchToAccountAddr: userRequest.meta.accountAddr, + nextRequestType: userRequest.action.kind, networkId: network.id, isSignAction: false }, From 0bb8ec09fa134c44bcbd9f090b8131aa16f6e470 Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Mon, 25 Nov 2024 17:04:48 +0200 Subject: [PATCH 8/9] refactor: add switch account request --- src/controllers/main/main.ts | 96 ++++++++++++++---------------------- src/libs/main/main.ts | 41 +++++++++++++++ 2 files changed, 77 insertions(+), 60 deletions(-) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index ad6009703..b3a81b9a7 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -56,6 +56,7 @@ import { GasRecommendation, getGasPriceRecommendations } from '../../libs/gasPri import { humanizeAccountOp } from '../../libs/humanizer' import { KeyIterator } from '../../libs/keyIterator/keyIterator' import { + buildSwitchAccountUserRequest, getAccountOpsForSimulation, makeBasicAccountOpAction, makeSmartAccountOpAction @@ -995,13 +996,6 @@ export class MainController extends EventEmitter { (n) => Number(n.chainId) === Number(dapp?.chainId) ) - const accountError = this.#getUserRequestAccountError(dappPromise.session.origin, accountAddr) - - if (accountError) { - dappPromise.reject(ethErrors.provider.userRejectedRequest(accountError)) - return - } - if (!network) { throw ethErrors.provider.chainDisconnected('Transaction failed - unknown network') } @@ -1035,12 +1029,6 @@ export class MainController extends EventEmitter { throw ethErrors.rpc.invalidRequest('No msg request to sign') } const msgAddress = getAddress(msg?.[1]) - const accountError = this.#getUserRequestAccountError(dappPromise.session.origin, msgAddress) - - if (accountError) { - dappPromise.reject(ethErrors.provider.userRejectedRequest(accountError)) - return - } const network = this.networks.networks.find( (n) => Number(n.chainId) === Number(dapp?.chainId) @@ -1072,11 +1060,6 @@ export class MainController extends EventEmitter { throw ethErrors.rpc.invalidRequest('No msg request to sign') } const msgAddress = getAddress(msg?.[0]) - const accountError = this.#getUserRequestAccountError(dappPromise.session.origin, msgAddress) - if (accountError) { - dappPromise.reject(ethErrors.provider.userRejectedRequest(accountError)) - return - } const network = this.networks.networks.find( (n) => Number(n.chainId) === Number(dapp?.chainId) @@ -1142,53 +1125,46 @@ export class MainController extends EventEmitter { } } - if (userRequest) { - const isASignOperationRequestedForAnotherAccount = - userRequest.meta.isSignAction && - userRequest.meta.accountAddr !== this.selectedAccount.account?.addr - - if (isASignOperationRequestedForAnotherAccount) { - const network = this.networks.networks.find( - (n) => Number(n.chainId) === Number(dapp?.chainId) - ) + if (!userRequest) return - if (!network) { - throw ethErrors.provider.chainDisconnected('Transaction failed - unknown network') - } - const userRequestId = userRequest.id + const isASignOperationRequestedForAnotherAccount = + userRequest.meta.isSignAction && + userRequest.meta.accountAddr !== this.selectedAccount.account?.addr - await this.addUserRequest(userRequest, false) - await this.addUserRequest( - { - id: new Date().getTime(), - action: { - kind: 'switchAccount', - params: {} - }, - session: request.session, - meta: { - accountAddr: this.selectedAccount.account?.addr, - switchToAccountAddr: userRequest.meta.accountAddr, - nextRequestType: userRequest.action.kind, - networkId: network.id, - isSignAction: false - }, - dappPromise: { - session: request.session, - resolve: () => {}, - reject: () => { - this.rejectUserRequest('Switch account request rejected', userRequestId) - } - } - }, - true - ) - return - } + // We can simply add the user request if it's not a sign operation + // for another account + if (!isASignOperationRequestedForAnotherAccount) { await this.addUserRequest(userRequest, withPriority) + return + } - this.emitUpdate() + const accountError = this.#getUserRequestAccountError( + dappPromise.session.origin, + userRequest.meta.accountAddr + ) + + if (accountError) { + dappPromise.reject(ethErrors.provider.userRejectedRequest(accountError)) + return } + + const network = this.networks.networks.find((n) => Number(n.chainId) === Number(dapp?.chainId)) + + if (!network) { + throw ethErrors.provider.chainDisconnected('Transaction failed - unknown network') + } + + await this.addUserRequest(userRequest, false) + await this.addUserRequest( + buildSwitchAccountUserRequest({ + nextUserRequest: userRequest, + networkId: network.id, + selectedAccountAddr: userRequest.meta.accountAddr, + session: dappPromise.session, + rejectUserRequest: this.rejectUserRequest.bind(this) + }), + true + ) } async buildTransferUserRequest( diff --git a/src/libs/main/main.ts b/src/libs/main/main.ts index 29cc9ab78..16a2037e2 100644 --- a/src/libs/main/main.ts +++ b/src/libs/main/main.ts @@ -1,5 +1,6 @@ import { AccountOpAction, Action } from '../../controllers/actions/actions' import { Account, AccountId } from '../../interfaces/account' +import { DappProviderRequest } from '../../interfaces/dapp' import { Network, NetworkId } from '../../interfaces/network' import { Calls, SignUserRequest, UserRequest } from '../../interfaces/userRequest' import generateSpoofSig from '../../utils/generateSpoofSig' @@ -30,6 +31,46 @@ export const batchCallsFromUserRequests = ({ ) } +export const buildSwitchAccountUserRequest = ({ + nextUserRequest, + selectedAccountAddr, + networkId, + session, + rejectUserRequest +}: { + nextUserRequest: UserRequest + selectedAccountAddr: string + networkId: Network['id'] + session: DappProviderRequest['session'] + rejectUserRequest: (reason: string, userRequestId: string | number) => void +}): UserRequest => { + const userRequestId = nextUserRequest.id + + return { + id: new Date().getTime(), + action: { + kind: 'switchAccount', + params: { + accountAddr: selectedAccountAddr, + switchToAccountAddr: nextUserRequest.meta.accountAddr, + nextRequestType: nextUserRequest.action.kind, + networkId + } + }, + session, + meta: { + isSignAction: false + }, + dappPromise: { + session, + resolve: () => {}, + reject: () => { + rejectUserRequest('Switch account request rejected', userRequestId) + } + } + } +} + export const makeSmartAccountOpAction = ({ account, networkId, From 96da88e9d2f665e9e13cfaafe526aa381a980aef Mon Sep 17 00:00:00 2001 From: Petromir Petrov Date: Wed, 27 Nov 2024 11:07:27 +0200 Subject: [PATCH 9/9] change: switchAccount request before userRequest --- src/controllers/main/main.ts | 5 +++-- src/libs/main/main.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index b3a81b9a7..359b3317b 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -1154,7 +1154,6 @@ export class MainController extends EventEmitter { throw ethErrors.provider.chainDisconnected('Transaction failed - unknown network') } - await this.addUserRequest(userRequest, false) await this.addUserRequest( buildSwitchAccountUserRequest({ nextUserRequest: userRequest, @@ -1163,8 +1162,10 @@ export class MainController extends EventEmitter { session: dappPromise.session, rejectUserRequest: this.rejectUserRequest.bind(this) }), - true + true, + 'open' ) + await this.addUserRequest(userRequest, false, 'queue') } async buildTransferUserRequest( diff --git a/src/libs/main/main.ts b/src/libs/main/main.ts index 16a2037e2..58437bd0d 100644 --- a/src/libs/main/main.ts +++ b/src/libs/main/main.ts @@ -47,7 +47,7 @@ export const buildSwitchAccountUserRequest = ({ const userRequestId = nextUserRequest.id return { - id: new Date().getTime(), + id: Number(nextUserRequest.id) + 222, // Otherwise the ids will be the same action: { kind: 'switchAccount', params: {