Skip to content
91 changes: 88 additions & 3 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,41 @@ 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: "list-accounts",
Usage: "List accounts in the keystore",
Flags: []cli.Flag{
logLevelFlag,
keystoreFlag,
utils.LightKDFFlag,
acceptFlag,
},
Description: `
Lists the accounts in the keystore.
`}
listWalletsCommand = &cli.Command{
Action: listWallets,
Name: "list-wallets",
Usage: "List wallets known to Clef",
Flags: []cli.Flag{
logLevelFlag,
keystoreFlag,
utils.LightKDFFlag,
acceptFlag,
},
Description: `
Lists the wallets known to Clef.
`}
)

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

Expand Down Expand Up @@ -351,6 +378,64 @@ 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)
internalApi := core.NewUIServerAPI(api)
Copy link
Contributor

Choose a reason for hiding this comment

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

This chunk of code is now repeated at least three times. I suggest moving this to it's own function, with the signature

func initInternal(c *cli.Context) (*core.UIServerAPI, error) {

or maybe initInternalApi.
And then shorten the three methods , e.g

func newAccount(c *cli.Context) error {
	internalApi, err := initInternal(c)
	if err != nil {

And then move these two new methods down, so they appear after newAccount within the file.

accs, err := internalApi.ListAccounts(context.Background())
if err != nil {
return err
}
if len(accs) == 0 {
fmt.Println("\nThe keystore is empty.")
}
fmt.Println()
for _, account := range accs {
fmt.Printf("%v (%v)\n", account.Address, account.URL)
}
return err
}

func listWallets(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)
internalApi := core.NewUIServerAPI(api)
wallets := internalApi.ListWallets()
if len(wallets) == 0 {
fmt.Println("\nThere are no wallets.")
}
fmt.Println()
for i, wallet := range wallets {
fmt.Printf("%d. Keystore at %v (%v %v)\n", i, wallet.URL, wallet.Status, wallet.Failure)
for _, acc := range wallet.Accounts {
fmt.Printf("\t%v (%v)\n", acc.Address, acc.URL)
}
}
return nil
}

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