Skip to content

Commit

Permalink
frontend: add connected to TKeystore
Browse files Browse the repository at this point in the history
Helpful for watch-only, so the user can see which one is connected.
  • Loading branch information
benma committed Dec 11, 2023
1 parent 5afd49b commit eba6686
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
31 changes: 27 additions & 4 deletions backend/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package handlers

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -355,11 +356,16 @@ type activeToken struct {
AccountCode accountsTypes.Code `json:"accountCode"`
}

type keystoreJSON struct {
config.Keystore
Connected bool `json:"connected"`
}

type accountJSON struct {
// Multiple accounts can belong to the same keystore. For now we replicate the keystore info in
// the accounts. In the future the getAccountsHandler() could return the accounts grouped
// keystore.
Keystore config.Keystore `json:"keystore"`
Keystore keystoreJSON `json:"keystore"`
Active bool `json:"active"`
Watch bool `json:"watch"`
CoinCode coinpkg.Code `json:"coinCode"`
Expand All @@ -372,12 +378,19 @@ type accountJSON struct {
BlockExplorerTxPrefix string `json:"blockExplorerTxPrefix"`
}

func newAccountJSON(keystore config.Keystore, account accounts.Interface, activeTokens []activeToken) *accountJSON {
func newAccountJSON(
keystore config.Keystore,
account accounts.Interface,
activeTokens []activeToken,
keystoreConnected bool) *accountJSON {
eth, ok := account.Coin().(*eth.Coin)
isToken := ok && eth.ERC20Token() != nil
watch := account.Config().Config.Watch
return &accountJSON{
Keystore: keystore,
Keystore: keystoreJSON{
Keystore: keystore,
Connected: keystoreConnected,
},
Active: !account.Config().Config.Inactive,
Watch: watch != nil && *watch,
CoinCode: account.Coin().Code(),
Expand Down Expand Up @@ -591,7 +604,17 @@ func (handlers *Handlers) getAccountsHandler(_ *http.Request) interface{} {
continue
}

accounts = append(accounts, newAccountJSON(*keystore, account, activeTokens))
keystoreConnected := false
if connectedKeystore := handlers.backend.Keystore(); connectedKeystore != nil {
connectedKeystoreRootFingerprint, err := connectedKeystore.RootFingerprint()
if err != nil {
handlers.log.WithError(err).Error("Could not retrieve rootFingerprint")
} else {
keystoreConnected = bytes.Equal(rootFingerprint, connectedKeystoreRootFingerprint)
}
}

accounts = append(accounts, newAccountJSON(*keystore, account, activeTokens, keystoreConnected))
}
return accounts
}
Expand Down
1 change: 1 addition & 0 deletions frontends/web/src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type TKeystore = {
watchonly: boolean;
rootFingerprint: string;
name: string;
connected: boolean;
};

export interface IAccount {
Expand Down
2 changes: 1 addition & 1 deletion frontends/web/src/components/sidebar/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Sidebar extends Component<Props> {
accountsByKeystore.map(keystore => (<React.Fragment key={keystore.keystore.rootFingerprint}>
<div className={style.sidebarHeaderContainer}>
<span className={style.sidebarHeader} hidden={!keystore.accounts.length}>
{t('sidebar.accounts')} - {keystore.keystore.name}
{t('sidebar.accounts')} - {keystore.keystore.name} {keystore.keystore.connected ? '- CONNECTED' : null}
{ isAmbiguiousName(keystore.keystore.name, accountsByKeystore) ? (
// Disambiguate accounts group by adding the fingerprint.
// The most common case where this would happen is when adding accounts from the
Expand Down

0 comments on commit eba6686

Please sign in to comment.