Skip to content
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

WIP: Enhancement/ Switch account action #1046

Draft
wants to merge 1 commit into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/controllers/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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)
}
Expand Down
74 changes: 63 additions & 11 deletions src/controllers/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions src/interfaces/userRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -60,7 +56,7 @@ export interface SignUserRequest {
export interface DappUserRequest {
id: string | number
action: {
kind: Exclude<string, 'calls' | 'message' | 'typedMessage' | 'benzin'>
kind: Exclude<string, 'calls' | 'message' | 'typedMessage' | 'benzin' | 'switchAccount'>
params: any
}
session: DappProviderRequest['session']
Expand Down
Loading