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 add listAccounts to CLI #26080

Merged
merged 14 commits into from
Nov 2, 2022
46 changes: 43 additions & 3 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,28 @@ The delpw command removes a password for a given address (keyfile).
},
Description: `
The newaccount command creates a new keystore-backed account. It is a convenience-method
which can be used in lieu of an external UI.`,
}

which can be used in lieu of an external UI.
`}
gendocCommand = &cli.Command{
Action: GenDoc,
Name: "gendoc",
Usage: "Generate documentation about json-rpc format",
Description: `
The gendoc generates example structures of the json-rpc communication types.
`}
listAccountsCommand = &cli.Command{
Action: listAccounts,
Name: "listaccounts",
Usage: "List accounts in the keystore",
Flags: []cli.Flag{
logLevelFlag,
keystoreFlag,
utils.LightKDFFlag,
acceptFlag,
},
Description: `
Lists the accounts in the keystore.
`}
)

var app = flags.NewApp("Manage Ethereum account operations")
Expand Down Expand Up @@ -249,6 +261,7 @@ func init() {
delCredentialCommand,
newAccountCommand,
gendocCommand,
listAccountsCommand,
}
}

Expand Down Expand Up @@ -351,6 +364,33 @@ func attestFile(ctx *cli.Context) error {
return nil
}

func listAccounts(c *cli.Context) error {
if err := initialize(c); err != nil {
return err
}
// listaccounts is meant for users using the CLI.
var (
ui = core.NewCommandlineUI()
pwStorage storage.Storage = &storage.NoStorage{}
ksLoc = c.String(keystoreFlag.Name)
lightKdf = c.Bool(utils.LightKDFFlag.Name)
)
am := core.StartClefAccountManager(ksLoc, true, lightKdf, "")
// Access external API and call List()
api := core.NewSignerAPI(am, 0, true, ui, nil, false, pwStorage)
accs, err := api.List(context.Background())
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit wonky :)
We instantiate a cli-UI, but then we make a request via the external api, about a listing. And since it's on the external api, the user needs to approve it.
It should be possible to get a listing via the internal api, originating from the ui, and thus not requiring user-approval.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, there are

  - `clef_listWallets`
  - `clef_listAccounts`
  - `clef_listWallets`

Or, if you want to invoke them directly, use ListAccounts

	internalApi := core.NewUIServerAPI(api)
	accounts, err := internalApi.ListAccounts()

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, we could add both list-accounts and list-wallets as CLI commands, corresponding to the two internal methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok i tried to fix this according to your comments and also added clef list-wallets. Hopefully this works.

if err != nil {
return err
}
if len(accs) == 0 {
fmt.Println("\nThe keystore is empty.")
}
for _, account := range accs {
fmt.Println(account)
}
return err
}

func setCredential(ctx *cli.Context) error {
if ctx.NArg() < 1 {
utils.Fatalf("This command requires an address to be passed as an argument")
Expand Down