From b240834e934bc929627be4d7b6b15da16cdd8a08 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 31 Oct 2022 15:55:21 +0000 Subject: [PATCH 01/14] add clef listaccounts to cli --- cmd/clef/main.go | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index a3e4815ed5fa..1c5004b2a38d 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -203,9 +203,8 @@ 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", @@ -213,6 +212,13 @@ which can be used in lieu of an external UI.`, 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", + Description: ` + Lists the accounts in the keystore. + `} ) var app = flags.NewApp("Manage Ethereum account operations") @@ -249,6 +255,7 @@ func init() { delCredentialCommand, newAccountCommand, gendocCommand, + listAccountsCommand, } } @@ -351,6 +358,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()) + 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") From 4682074a547c437f300c47e5e0d484fd1d51eca7 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 31 Oct 2022 16:58:35 +0000 Subject: [PATCH 02/14] add keystore flag --- cmd/clef/main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 1c5004b2a38d..45bb62eb584e 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -216,6 +216,12 @@ The gendoc generates example structures of the json-rpc communication types. 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. `} From a1ed5887de67acd6eec8c0f885a846bfa6e61fc1 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 1 Nov 2022 17:09:41 +0000 Subject: [PATCH 03/14] use internal api to call ListAccounts --- cmd/clef/main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 45bb62eb584e..143610e814d6 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -378,15 +378,17 @@ func listAccounts(c *cli.Context) error { 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()) + internalApi := core.NewUIServerAPI(api) + 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.Println(account) + fmt.Println(account.Address) } return err } From 01588bbd57cb33012b522246b63515eb6b61da4c Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 1 Nov 2022 17:22:31 +0000 Subject: [PATCH 04/14] add list-wallet command --- cmd/clef/main.go | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 143610e814d6..0523bf0efde6 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -214,7 +214,7 @@ The gendoc generates example structures of the json-rpc communication types. `} listAccountsCommand = &cli.Command{ Action: listAccounts, - Name: "listaccounts", + Name: "list-accounts", Usage: "List accounts in the keystore", Flags: []cli.Flag{ logLevelFlag, @@ -225,6 +225,19 @@ The gendoc generates example structures of the json-rpc communication types. 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") @@ -262,6 +275,7 @@ func init() { newAccountCommand, gendocCommand, listAccountsCommand, + listWalletsCommand, } } @@ -393,6 +407,32 @@ func listAccounts(c *cli.Context) error { 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 _, wallet := range wallets { + fmt.Println(wallet) + } + 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") From 65db9b1feec8f3903fb10a702603abfda24b84a3 Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 09:04:51 +0000 Subject: [PATCH 05/14] Update cmd/clef/main.go Co-authored-by: Martin Holst Swende --- cmd/clef/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 0523bf0efde6..9836889f8fa7 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -402,7 +402,7 @@ func listAccounts(c *cli.Context) error { } fmt.Println() for _, account := range accs { - fmt.Println(account.Address) + fmt.Printf("%v (%v)\n", account.Address, account.URL) } return err } From 61c2e77f7847d38ab147e98cd38eb94c484d363b Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 09:04:57 +0000 Subject: [PATCH 06/14] Update cmd/clef/main.go Co-authored-by: Martin Holst Swende --- cmd/clef/main.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 9836889f8fa7..e3267596ec0b 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -427,8 +427,11 @@ func listWallets(c *cli.Context) error{ fmt.Println("\nThere are no wallets.") } fmt.Println() - for _, wallet := range wallets { - fmt.Println(wallet) + 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 } From b8e04dd7616390b549b540e7359d5c8d30313920 Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 11:46:52 +0000 Subject: [PATCH 07/14] init internal api logic --> func --- cmd/clef/main.go | 52 ++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index e3267596ec0b..fef04939e713 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -378,11 +378,10 @@ func attestFile(ctx *cli.Context) error { return nil } -func listAccounts(c *cli.Context) error { +func initInternalApi(c *cli.Context) (*core.UIServerAPI, error){ if err := initialize(c); err != nil { - return err + return nil, err } - // listaccounts is meant for users using the CLI. var ( ui = core.NewCommandlineUI() pwStorage storage.Storage = &storage.NoStorage{} @@ -390,9 +389,13 @@ func listAccounts(c *cli.Context) error { 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) + return internalApi, nil +} + +func listAccounts(c *cli.Context) error { + internalApi, err := initInternalApi(c) accs, err := internalApi.ListAccounts(context.Background()) if err != nil { return err @@ -402,36 +405,28 @@ func listAccounts(c *cli.Context) error { } fmt.Println() for _, account := range accs { - fmt.Printf("%v (%v)\n", account.Address, account.URL) + fmt.Println(account.Address) } return err } func listWallets(c *cli.Context) error{ - if err := initialize(c); err != nil { + internalApi, err := initInternalApi(c) + if 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) + fmt.Printf("Wallet %d at %v (%v %v)\n", i, wallet.URL, wallet.Status, wallet.Failure) + fmt.Printf("Accounts in Wallet %d:\n", i) + for j, acc := range wallet.Accounts { + fmt.Printf("Account %d: %v (%v)\n", j, acc.Address, acc.URL) } + fmt.Println() } return nil } @@ -495,23 +490,10 @@ func removeCredential(ctx *cli.Context) error { } func newAccount(c *cli.Context) error { - if err := initialize(c); err != nil { + internalApi, err := initInternalApi(c) + if err != nil { return err } - // The newaccount is meant for users using the CLI, since 'real' external - // UIs can use the UI-api instead. So we'll just use the native CLI UI here. - var ( - ui = core.NewCommandlineUI() - pwStorage storage.Storage = &storage.NoStorage{} - ksLoc = c.String(keystoreFlag.Name) - lightKdf = c.Bool(utils.LightKDFFlag.Name) - ) - log.Info("Starting clef", "keystore", ksLoc, "light-kdf", lightKdf) - am := core.StartClefAccountManager(ksLoc, true, lightKdf, "") - // This gives is us access to the external API - apiImpl := core.NewSignerAPI(am, 0, true, ui, nil, false, pwStorage) - // This gives us access to the internal API - internalApi := core.NewUIServerAPI(apiImpl) addr, err := internalApi.New(context.Background()) if err == nil { fmt.Printf("Generated account %v\n", addr.String()) From aa42247e4108773eb5ee3180c32b9bafc981a3f6 Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 11:48:41 +0000 Subject: [PATCH 08/14] move listAccounts and listWallets --- cmd/clef/main.go | 74 ++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index fef04939e713..ed3a0f2cf25e 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -394,43 +394,6 @@ func initInternalApi(c *cli.Context) (*core.UIServerAPI, error){ return internalApi, nil } -func listAccounts(c *cli.Context) error { - internalApi, err := initInternalApi(c) - 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.Println(account.Address) - } - return err -} - -func listWallets(c *cli.Context) error{ - internalApi, err := initInternalApi(c) - if err != nil { - return err - } - wallets := internalApi.ListWallets() - if len(wallets) == 0 { - fmt.Println("\nThere are no wallets.") - } - fmt.Println() - for i, wallet := range wallets { - fmt.Printf("Wallet %d at %v (%v %v)\n", i, wallet.URL, wallet.Status, wallet.Failure) - fmt.Printf("Accounts in Wallet %d:\n", i) - for j, acc := range wallet.Accounts { - fmt.Printf("Account %d: %v (%v)\n", j, acc.Address, acc.URL) - } - fmt.Println() - } - 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") @@ -526,6 +489,43 @@ func initialize(c *cli.Context) error { return nil } +func listAccounts(c *cli.Context) error { + internalApi, err := initInternalApi(c) + 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.Println(account.Address) + } + return err +} + +func listWallets(c *cli.Context) error{ + internalApi, err := initInternalApi(c) + if err != nil { + return err + } + wallets := internalApi.ListWallets() + if len(wallets) == 0 { + fmt.Println("\nThere are no wallets.") + } + fmt.Println() + for i, wallet := range wallets { + fmt.Printf("Wallet %d at %v (%v %v)\n", i, wallet.URL, wallet.Status, wallet.Failure) + fmt.Printf("Accounts in Wallet %d:\n", i) + for j, acc := range wallet.Accounts { + fmt.Printf("Account %d: %v (%v)\n", j, acc.Address, acc.URL) + } + fmt.Println() + } + return nil +} + // ipcEndpoint resolves an IPC endpoint based on a configured value, taking into // account the set data folders as well as the designated platform we're currently // running on. From 4c0a16c6802eb3cb6805eeaeabd6b646413f6ea4 Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 13:34:29 +0000 Subject: [PATCH 09/14] check err --- cmd/clef/main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index ed3a0f2cf25e..eda7f7480b7c 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -491,6 +491,9 @@ func initialize(c *cli.Context) error { func listAccounts(c *cli.Context) error { internalApi, err := initInternalApi(c) + if err != nil { + return err + } accs, err := internalApi.ListAccounts(context.Background()) if err != nil { return err From 248d762cbe154afd665e96ea5f92be04ce8caa29 Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 13:34:53 +0000 Subject: [PATCH 10/14] print account.URL --- cmd/clef/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index eda7f7480b7c..06ea9ebbd9e4 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -503,7 +503,7 @@ func listAccounts(c *cli.Context) error { } fmt.Println() for _, account := range accs { - fmt.Println(account.Address) + fmt.Printf("%v (%v)\n", account.Address, account.URL) } return err } From d579c56eec78aa872cabbb8250f07941c5d69cd0 Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 13:35:16 +0000 Subject: [PATCH 11/14] output to render as markdown --- cmd/clef/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 06ea9ebbd9e4..3112a341ee65 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -522,7 +522,7 @@ func listWallets(c *cli.Context) error{ fmt.Printf("Wallet %d at %v (%v %v)\n", i, wallet.URL, wallet.Status, wallet.Failure) fmt.Printf("Accounts in Wallet %d:\n", i) for j, acc := range wallet.Accounts { - fmt.Printf("Account %d: %v (%v)\n", j, acc.Address, acc.URL) + fmt.Printf(" -Account %d: %v (%v)\n", j, acc.Address, acc.URL) } fmt.Println() } From a899f41a23cf18e475bed5e624c43a90c1d1872e Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 13:39:37 +0000 Subject: [PATCH 12/14] fix func order and wallet info --- cmd/clef/main.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 3112a341ee65..626557a9dc0c 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -452,18 +452,6 @@ func removeCredential(ctx *cli.Context) error { return nil } -func newAccount(c *cli.Context) error { - internalApi, err := initInternalApi(c) - if err != nil { - return err - } - addr, err := internalApi.New(context.Background()) - if err == nil { - fmt.Printf("Generated account %v\n", addr.String()) - } - return err -} - func initialize(c *cli.Context) error { // Set up the logger to print everything logOutput := os.Stdout @@ -489,6 +477,18 @@ func initialize(c *cli.Context) error { return nil } +func newAccount(c *cli.Context) error { + internalApi, err := initInternalApi(c) + if err != nil { + return err + } + addr, err := internalApi.New(context.Background()) + if err == nil { + fmt.Printf("Generated account %v\n", addr.String()) + } + return err +} + func listAccounts(c *cli.Context) error { internalApi, err := initInternalApi(c) if err != nil { @@ -519,8 +519,7 @@ func listWallets(c *cli.Context) error{ } fmt.Println() for i, wallet := range wallets { - fmt.Printf("Wallet %d at %v (%v %v)\n", i, wallet.URL, wallet.Status, wallet.Failure) - fmt.Printf("Accounts in Wallet %d:\n", i) + fmt.Printf("- Wallet %d at %v (%v %v)\n", i, wallet.URL, wallet.Status, wallet.Failure) for j, acc := range wallet.Accounts { fmt.Printf(" -Account %d: %v (%v)\n", j, acc.Address, acc.URL) } From 364efad2d1b6f3ddfb4a6287b4da6c4af4c918c4 Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 16:14:04 +0000 Subject: [PATCH 13/14] Update cmd/clef/main.go --- cmd/clef/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index 626557a9dc0c..decf47394807 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -378,7 +378,7 @@ func attestFile(ctx *cli.Context) error { return nil } -func initInternalApi(c *cli.Context) (*core.UIServerAPI, error){ +func initInternalApi(c *cli.Context) (*core.UIServerAPI, error) { if err := initialize(c); err != nil { return nil, err } From e20ed965c1b24b9d09e718fb769c4db18526c3ae Mon Sep 17 00:00:00 2001 From: Joseph Cook <33655003+jmcook1186@users.noreply.github.com> Date: Wed, 2 Nov 2022 16:44:00 +0000 Subject: [PATCH 14/14] fix goimports --- cmd/clef/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index decf47394807..188a11500004 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -492,7 +492,7 @@ func newAccount(c *cli.Context) error { func listAccounts(c *cli.Context) error { internalApi, err := initInternalApi(c) if err != nil { - return err + return err } accs, err := internalApi.ListAccounts(context.Background()) if err != nil { @@ -508,7 +508,7 @@ func listAccounts(c *cli.Context) error { return err } -func listWallets(c *cli.Context) error{ +func listWallets(c *cli.Context) error { internalApi, err := initInternalApi(c) if err != nil { return err