From 8296320c023dac04922d00929aba341974883004 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Wed, 18 Jul 2018 10:11:00 +0200 Subject: [PATCH] cmd/swarm: remove unneeded clientaccountmanager.go --- cmd/swarm/clientaccountmanager.go | 103 ------------------------------ cmd/swarm/main.go | 28 ++++++-- cmd/swarm/mru.go | 2 +- 3 files changed, 23 insertions(+), 110 deletions(-) delete mode 100644 cmd/swarm/clientaccountmanager.go diff --git a/cmd/swarm/clientaccountmanager.go b/cmd/swarm/clientaccountmanager.go deleted file mode 100644 index 644be4f73f29..000000000000 --- a/cmd/swarm/clientaccountmanager.go +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2016 The go-ethereum Authors -// This file is part of go-ethereum. -// -// go-ethereum is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// go-ethereum is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with go-ethereum. If not, see . - -/* -//This module implements the bare minimum to allow client commands to access the keystore -*/ -package main - -import ( - "crypto/ecdsa" - "fmt" - "io/ioutil" - "os" - - "github.com/ethereum/go-ethereum/accounts" - "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/ethereum/go-ethereum/accounts/usbwallet" - "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/node" - cli "gopkg.in/urfave/cli.v1" - - "github.com/ethereum/go-ethereum/cmd/utils" -) - -type ClientAccountManagerProvider struct { - manager *accounts.Manager -} - -func (c *ClientAccountManagerProvider) AccountManager() *accounts.Manager { - return c.manager -} - -func getClientAccount(ctx *cli.Context) *ecdsa.PrivateKey { - bzzconfig, err := buildConfig(ctx) - if err != nil { - utils.Fatalf("Cannot read configuration: %s", err.Error()) - return nil - } - nodeconfig := defaultNodeConfig - if _, err := os.Stat(bzzconfig.Path); err == nil { - nodeconfig.DataDir = bzzconfig.Path - } - - accountManager, _, err := makeAccountManager(&nodeconfig) - if err != nil { - utils.Fatalf("Cannot create account manager: %s", err.Error()) - return nil - } - - return getAccount(bzzconfig.BzzAccount, ctx, &ClientAccountManagerProvider{ - manager: accountManager, - }) - -} - -func makeAccountManager(conf *node.Config) (*accounts.Manager, string, error) { - scryptN, scryptP, keydir, err := conf.AccountConfig() - var ephemeral string - if keydir == "" { - // There is no datadir. - keydir, err = ioutil.TempDir("", "go-ethereum-keystore") - ephemeral = keydir - } - - if err != nil { - return nil, "", err - } - if err := os.MkdirAll(keydir, 0700); err != nil { - return nil, "", err - } - // Assemble the account manager and supported backends - backends := []accounts.Backend{ - keystore.NewKeyStore(keydir, scryptN, scryptP), - } - if !conf.NoUSB { - // Start a USB hub for Ledger hardware wallets - if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil { - log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err)) - } else { - backends = append(backends, ledgerhub) - } - // Start a USB hub for Trezor hardware wallets - if trezorhub, err := usbwallet.NewTrezorHub(); err != nil { - log.Warn(fmt.Sprintf("Failed to start Trezor hub, disabling: %v", err)) - } else { - backends = append(backends, trezorhub) - } - } - return accounts.NewManager(backends...), ephemeral, nil -} diff --git a/cmd/swarm/main.go b/cmd/swarm/main.go index 0dfc298ac69e..2e64475a029d 100644 --- a/cmd/swarm/main.go +++ b/cmd/swarm/main.go @@ -211,10 +211,6 @@ var defaultSubcommandHelp = cli.Command{ Hidden: true, } -type AccountManagerProvider interface { - AccountManager() *accounts.Manager -} - var defaultNodeConfig = node.DefaultConfig // This init function sets defaults so cmd/swarm can run alongside geth. @@ -597,7 +593,7 @@ func registerBzzService(bzzconfig *bzzapi.Config, stack *node.Node) { } } -func getAccount(bzzaccount string, ctx *cli.Context, provider AccountManagerProvider) *ecdsa.PrivateKey { +func getAccount(bzzaccount string, ctx *cli.Context, stack *node.Node) *ecdsa.PrivateKey { //an account is mandatory if bzzaccount == "" { utils.Fatalf(SWARM_ERR_NO_BZZACCOUNT) @@ -608,12 +604,32 @@ func getAccount(bzzaccount string, ctx *cli.Context, provider AccountManagerProv return key } // Otherwise try getting it from the keystore. - am := provider.AccountManager() + am := stack.AccountManager() ks := am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) return decryptStoreAccount(ks, bzzaccount, utils.MakePasswordList(ctx)) } +// getPrivKey returns the private key of the specified bzzaccount +// Used only by client commands, such as `resource` +func getPrivKey(ctx *cli.Context) *ecdsa.PrivateKey { + // booting up the swarm node just as we do in bzzd action + bzzconfig, err := buildConfig(ctx) + if err != nil { + utils.Fatalf("unable to configure swarm: %v", err) + } + cfg := defaultNodeConfig + if _, err := os.Stat(bzzconfig.Path); err == nil { + cfg.DataDir = bzzconfig.Path + } + utils.SetNodeConfig(ctx, &cfg) + stack, err := node.New(&cfg) + if err != nil { + utils.Fatalf("can't create node: %v", err) + } + return getAccount(bzzconfig.BzzAccount, ctx, stack) +} + func decryptStoreAccount(ks *keystore.KeyStore, account string, passwords []string) *ecdsa.PrivateKey { var a accounts.Account var err error diff --git a/cmd/swarm/mru.go b/cmd/swarm/mru.go index 070c55ff7115..6176b6d6c806 100644 --- a/cmd/swarm/mru.go +++ b/cmd/swarm/mru.go @@ -31,7 +31,7 @@ import ( ) func NewGenericSigner(ctx *cli.Context) mru.Signer { - return mru.NewGenericSigner(getClientAccount(ctx)) + return mru.NewGenericSigner(getPrivKey(ctx)) } // swarm resource create [--name ] [--data <0x Hexdata> [--multihash=false]]