Skip to content

Commit

Permalink
cmd/clef: list accounts at startup (ethereum#26082)
Browse files Browse the repository at this point in the history
Reports accounts known to Clef during startup, after master seed is provided by the user.
  • Loading branch information
jmcook1186 committed Nov 7, 2022
1 parent ca948b8 commit 55a92fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
5 changes: 2 additions & 3 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,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 Down Expand Up @@ -768,7 +769,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 @@ -779,8 +779,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
29 changes: 25 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,9 +243,28 @@ func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
}
}

func (ui *CommandlineUI) showAccounts() {
accounts, err := ui.api.ListAccounts(context.Background())
if err != nil {
log.Error("Error listing accounts", "err", err)
return
}
if len(accounts) == 0 {
fmt.Print("No accounts found\n")
return
}
var out = new(strings.Builder)
fmt.Fprint(out, "\n------- Available accounts -------\n")
for i, account := range accounts {
fmt.Fprintf(out, "%d. %s at %s\n", i, account.Address, account.URL)
}
fmt.Print(out.String())
}

func (ui *CommandlineUI) OnSignerStartup(info StartupInfo) {
fmt.Printf("------- Signer info -------\n")
fmt.Print("\n------- Signer info -------\n")
for k, v := range info.Info {
fmt.Printf("* %v : %v\n", k, v)
}
go ui.showAccounts()
}

0 comments on commit 55a92fa

Please sign in to comment.