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

improve multi-acct logic #1793

Merged
merged 2 commits into from
Feb 14, 2024
Merged
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
46 changes: 35 additions & 11 deletions tools/walletextension/accountmanager/account_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ func (m *AccountManager) createClientsForAccounts(accounts []wecommon.AccountDB,
return clients, nil
}

// todo - better way
const notAuthorised = "not authorised"

var platformAuthorisedCalls = map[string]bool{
rpc.GetBalance: true,
// rpc.GetCode, //todo
rpc.GetTransactionCount: true,
rpc.GetTransactionReceipt: true,
rpc.GetLogs: true,
}

func (m *AccountManager) executeCall(rpcReq *wecommon.RPCRequest, rpcResp *interface{}) error {
m.accountsMutex.RLock()
defer m.accountsMutex.RUnlock()
Expand All @@ -217,6 +228,11 @@ func (m *AccountManager) executeCall(rpcReq *wecommon.RPCRequest, rpcResp *inter
// request didn't fail, we don't need to continue trying the other clients
return nil
}
// platform calls return a standard error for calls that are not authorised.
// any other error can be returned early
if platformAuthorisedCalls[rpcReq.Method] && err.Error() != notAuthorised {
return err
}
}
// every attempt errored
return err
Expand All @@ -237,20 +253,28 @@ func (m *AccountManager) suggestAccountClient(req *wecommon.RPCRequest, accClien
return client
}
}

// todo - more calls
if req.Method == rpc.Call {
switch req.Method {
case rpc.Call, rpc.EstimateGas:
return m.handleEthCall(req, accClients)
} else if req.Method == rpc.GetBalance {
if len(req.Params) == 0 {
return nil
}
requestedAddress, err := gethencoding.ExtractAddress(req.Params[0])
if err == nil {
return accClients[*requestedAddress]
}
case rpc.GetBalance:
return extractAddress(0, req.Params, accClients)
case rpc.GetLogs:
return extractAddress(1, req.Params, accClients)
case rpc.GetTransactionCount:
return extractAddress(0, req.Params, accClients)
default:
return nil
}
}

func extractAddress(pos int, params []interface{}, accClients map[gethcommon.Address]*rpc.EncRPCClient) *rpc.EncRPCClient {
if len(params) < pos+1 {
return nil
}
requestedAddress, err := gethencoding.ExtractAddress(params[pos])
if err == nil {
return accClients[*requestedAddress]
}
return nil
}

Expand Down
Loading