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

cmd/clef: list accounts at startup #26082

Merged
merged 11 commits into from
Nov 7, 2022
6 changes: 3 additions & 3 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ func signer(c *cli.Context) error {
// it with the UI.
ui.RegisterUIServer(core.NewUIServerAPI(apiImpl))
api = apiImpl

// Audit logging
if logfile := c.String(auditLogFlag.Name); logfile != "" {
api, err = core.NewAuditLogger(logfile, api)
Expand All @@ -645,6 +646,7 @@ func signer(c *cli.Context) error {
}
log.Info("Audit logs configured", "file", logfile)
}

jmcook1186 marked this conversation as resolved.
Show resolved Hide resolved
jmcook1186 marked this conversation as resolved.
Show resolved Hide resolved
// register signer API with server
var (
extapiURL = "n/a"
Expand Down Expand Up @@ -698,7 +700,6 @@ func signer(c *cli.Context) error {
log.Info("IPC endpoint closed", "url", ipcapiURL)
}()
}

if c.Bool(testFlag.Name) {
log.Info("Performing UI test")
go testExternalUI(apiImpl)
Expand All @@ -709,8 +710,7 @@ func signer(c *cli.Context) error {
"extapi_version": core.ExternalAPIVersion,
"extapi_http": extapiURL,
"extapi_ipc": ipcapiURL,
},
})
}})

abortChan := make(chan os.Signal, 1)
signal.Notify(abortChan, os.Interrupt)
Expand Down
34 changes: 30 additions & 4 deletions signer/core/cliui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package core

import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"
Expand All @@ -31,16 +32,17 @@ import (
)

type CommandlineUI struct {
in *bufio.Reader
mu sync.Mutex
in *bufio.Reader
mu sync.Mutex
api *UIServerAPI
}

func NewCommandlineUI() *CommandlineUI {
return &CommandlineUI{in: bufio.NewReader(os.Stdin)}
}

func (ui *CommandlineUI) RegisterUIServer(api *UIServerAPI) {
// noop
ui.api = api
}

// readString reads a single line from stdin, trimming if from spaces, enforcing
Expand Down Expand Up @@ -241,8 +243,32 @@ func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
}
}

func (ui *CommandlineUI) showAccounts() string {
accounts, err := ui.api.ListAccounts(context.Background())
if err != nil {
fmt.Print("error listing accounts", err)
}
var resp string
if len(accounts) == 0 {
resp = "No accounts known to Clef"
} else {
// account info to string for nicer printing
var addresses string = "\nAccounts known to Clef:\n"
for i, account := range accounts {
// concat string to avoid repeating "INFO" on terminal
addresses += fmt.Sprintf("Account %v: %s at %s\n", i, account.Address, account.URL)
}
resp = addresses
}
return resp
}

func (ui *CommandlineUI) OnSignerStartup(info StartupInfo) {
fmt.Printf("------- Signer info -------\n")
go func() {
addresses := ui.showAccounts()
fmt.Printf("%s", addresses)
}()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the semantics of showAccounts so it actually displays the info by itself (and not return anything), then this can be minimized into

Suggested change
go func() {
addresses := ui.showAccounts()
fmt.Printf("%s", addresses)
}()
go ui.showAccounts()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, yes that is much nicer. Done in 4131ddd

fmt.Printf("\n------- Signer info -------\n")
for k, v := range info.Info {
fmt.Printf("* %v : %v\n", k, v)
}
Expand Down