|
| 1 | +// Copyright 2016 The go-ethereum Authors |
| 2 | +// This file is part of go-ethereum. |
| 3 | +// |
| 4 | +// go-ethereum is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// go-ethereum is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/ecdsa" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "runtime" |
| 25 | + "strconv" |
| 26 | + |
| 27 | + "github.com/ethereum/go-ethereum/accounts" |
| 28 | + "github.com/ethereum/go-ethereum/cmd/utils" |
| 29 | + "github.com/ethereum/go-ethereum/common" |
| 30 | + "github.com/ethereum/go-ethereum/console" |
| 31 | + "github.com/ethereum/go-ethereum/crypto" |
| 32 | + "github.com/ethereum/go-ethereum/ethclient" |
| 33 | + "github.com/ethereum/go-ethereum/internal/debug" |
| 34 | + "github.com/ethereum/go-ethereum/logger" |
| 35 | + "github.com/ethereum/go-ethereum/logger/glog" |
| 36 | + "github.com/ethereum/go-ethereum/node" |
| 37 | + "github.com/ethereum/go-ethereum/p2p" |
| 38 | + "github.com/ethereum/go-ethereum/p2p/discover" |
| 39 | + "github.com/ethereum/go-ethereum/swarm" |
| 40 | + bzzapi "github.com/ethereum/go-ethereum/swarm/api" |
| 41 | + "gopkg.in/urfave/cli.v1" |
| 42 | +) |
| 43 | + |
| 44 | +const clientIdentifier = "bzzd" |
| 45 | + |
| 46 | +var ( |
| 47 | + gitCommit string // Git SHA1 commit hash of the release (set via linker flags) |
| 48 | + app = utils.NewApp(gitCommit, "Ethereum Swarm server daemon") |
| 49 | +) |
| 50 | + |
| 51 | +var ( |
| 52 | + ChequebookAddrFlag = cli.StringFlag{ |
| 53 | + Name: "chequebook", |
| 54 | + Usage: "chequebook contract address", |
| 55 | + } |
| 56 | + SwarmAccountFlag = cli.StringFlag{ |
| 57 | + Name: "bzzaccount", |
| 58 | + Usage: "Swarm account key file", |
| 59 | + } |
| 60 | + SwarmPortFlag = cli.StringFlag{ |
| 61 | + Name: "bzzport", |
| 62 | + Usage: "Swarm local http api port", |
| 63 | + } |
| 64 | + SwarmConfigPathFlag = cli.StringFlag{ |
| 65 | + Name: "bzzconfig", |
| 66 | + Usage: "Swarm config file path (datadir/bzz)", |
| 67 | + } |
| 68 | + SwarmSwapDisabled = cli.BoolFlag{ |
| 69 | + Name: "bzznoswap", |
| 70 | + Usage: "Swarm SWAP disabled (default false)", |
| 71 | + } |
| 72 | + SwarmSyncDisabled = cli.BoolFlag{ |
| 73 | + Name: "bzznosync", |
| 74 | + Usage: "Swarm Syncing disabled (default false)", |
| 75 | + } |
| 76 | + EthAPI = cli.StringFlag{ |
| 77 | + Name: "ethapi", |
| 78 | + Usage: "URL of the Ethereum API provider", |
| 79 | + Value: node.DefaultIPCEndpoint("geth"), |
| 80 | + } |
| 81 | +) |
| 82 | + |
| 83 | +var defaultBootnodes = []string{} |
| 84 | + |
| 85 | +func init() { |
| 86 | + // Override flag defaults so bzzd can run alongside geth. |
| 87 | + utils.ListenPortFlag.Value = 30399 |
| 88 | + utils.IPCPathFlag.Value = utils.DirectoryString{Value: "bzzd.ipc"} |
| 89 | + |
| 90 | + // Set up the cli app. |
| 91 | + app.Commands = nil |
| 92 | + app.Action = bzzd |
| 93 | + app.Flags = []cli.Flag{ |
| 94 | + utils.IdentityFlag, |
| 95 | + utils.DataDirFlag, |
| 96 | + utils.BootnodesFlag, |
| 97 | + utils.KeyStoreDirFlag, |
| 98 | + utils.ListenPortFlag, |
| 99 | + utils.MaxPeersFlag, |
| 100 | + utils.NATFlag, |
| 101 | + utils.NodeKeyFileFlag, |
| 102 | + utils.NodeKeyHexFlag, |
| 103 | + utils.IPCDisabledFlag, |
| 104 | + utils.IPCApiFlag, |
| 105 | + utils.IPCPathFlag, |
| 106 | + // bzzd-specific flags |
| 107 | + EthAPI, |
| 108 | + SwarmConfigPathFlag, |
| 109 | + SwarmSwapDisabled, |
| 110 | + SwarmSyncDisabled, |
| 111 | + SwarmPortFlag, |
| 112 | + SwarmAccountFlag, |
| 113 | + ChequebookAddrFlag, |
| 114 | + } |
| 115 | + app.Flags = append(app.Flags, debug.Flags...) |
| 116 | + app.Before = func(ctx *cli.Context) error { |
| 117 | + runtime.GOMAXPROCS(runtime.NumCPU()) |
| 118 | + return debug.Setup(ctx) |
| 119 | + } |
| 120 | + app.After = func(ctx *cli.Context) error { |
| 121 | + debug.Exit() |
| 122 | + return nil |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func main() { |
| 127 | + if err := app.Run(os.Args); err != nil { |
| 128 | + fmt.Fprintln(os.Stderr, err) |
| 129 | + os.Exit(1) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func bzzd(ctx *cli.Context) error { |
| 134 | + stack := utils.MakeNode(ctx, clientIdentifier, gitCommit) |
| 135 | + registerBzzService(ctx, stack) |
| 136 | + utils.StartNode(stack) |
| 137 | + |
| 138 | + // Add bootnodes as initial peers. |
| 139 | + if ctx.GlobalIsSet(utils.BootnodesFlag.Name) { |
| 140 | + injectBootnodes(stack.Server(), ctx.GlobalStringSlice(utils.BootnodesFlag.Name)) |
| 141 | + } else { |
| 142 | + injectBootnodes(stack.Server(), defaultBootnodes) |
| 143 | + } |
| 144 | + |
| 145 | + stack.Wait() |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func registerBzzService(ctx *cli.Context, stack *node.Node) { |
| 150 | + prvkey := getAccount(ctx, stack) |
| 151 | + |
| 152 | + chbookaddr := common.HexToAddress(ctx.GlobalString(ChequebookAddrFlag.Name)) |
| 153 | + bzzdir := ctx.GlobalString(SwarmConfigPathFlag.Name) |
| 154 | + if bzzdir == "" { |
| 155 | + bzzdir = stack.InstanceDir() |
| 156 | + } |
| 157 | + bzzconfig, err := bzzapi.NewConfig(bzzdir, chbookaddr, prvkey) |
| 158 | + if err != nil { |
| 159 | + utils.Fatalf("unable to configure swarm: %v", err) |
| 160 | + } |
| 161 | + bzzport := ctx.GlobalString(SwarmPortFlag.Name) |
| 162 | + if len(bzzport) > 0 { |
| 163 | + bzzconfig.Port = bzzport |
| 164 | + } |
| 165 | + swapEnabled := !ctx.GlobalBool(SwarmSwapDisabled.Name) |
| 166 | + syncEnabled := !ctx.GlobalBool(SwarmSyncDisabled.Name) |
| 167 | + |
| 168 | + ethapi := ctx.GlobalString(EthAPI.Name) |
| 169 | + if ethapi == "" { |
| 170 | + utils.Fatalf("Option %q must not be empty", EthAPI.Name) |
| 171 | + } |
| 172 | + |
| 173 | + boot := func(ctx *node.ServiceContext) (node.Service, error) { |
| 174 | + client, err := ethclient.Dial(ethapi) |
| 175 | + if err != nil { |
| 176 | + utils.Fatalf("Can't connect: %v", err) |
| 177 | + } |
| 178 | + return swarm.NewSwarm(ctx, client, bzzconfig, swapEnabled, syncEnabled) |
| 179 | + } |
| 180 | + if err := stack.Register(boot); err != nil { |
| 181 | + utils.Fatalf("Failed to register the Swarm service: %v", err) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +func getAccount(ctx *cli.Context, stack *node.Node) *ecdsa.PrivateKey { |
| 186 | + keyid := ctx.GlobalString(SwarmAccountFlag.Name) |
| 187 | + if keyid == "" { |
| 188 | + utils.Fatalf("Option %q is required", SwarmAccountFlag.Name) |
| 189 | + } |
| 190 | + // Try to load the arg as a hex key file. |
| 191 | + if key, err := crypto.LoadECDSA(keyid); err == nil { |
| 192 | + glog.V(logger.Info).Infof("swarm account key loaded: %#x", crypto.PubkeyToAddress(key.PublicKey)) |
| 193 | + return key |
| 194 | + } |
| 195 | + // Otherwise try getting it from the keystore. |
| 196 | + return decryptStoreAccount(stack.AccountManager(), keyid) |
| 197 | +} |
| 198 | + |
| 199 | +func decryptStoreAccount(accman *accounts.Manager, account string) *ecdsa.PrivateKey { |
| 200 | + var a accounts.Account |
| 201 | + var err error |
| 202 | + if common.IsHexAddress(account) { |
| 203 | + a, err = accman.Find(accounts.Account{Address: common.HexToAddress(account)}) |
| 204 | + } else if ix, ixerr := strconv.Atoi(account); ixerr == nil { |
| 205 | + a, err = accman.AccountByIndex(ix) |
| 206 | + } else { |
| 207 | + utils.Fatalf("Can't find swarm account key %s", account) |
| 208 | + } |
| 209 | + if err != nil { |
| 210 | + utils.Fatalf("Can't find swarm account key: %v", err) |
| 211 | + } |
| 212 | + keyjson, err := ioutil.ReadFile(a.File) |
| 213 | + if err != nil { |
| 214 | + utils.Fatalf("Can't load swarm account key: %v", err) |
| 215 | + } |
| 216 | + for i := 1; i <= 3; i++ { |
| 217 | + passphrase := promptPassphrase(fmt.Sprintf("Unlocking swarm account %s [%d/3]", a.Address.Hex(), i)) |
| 218 | + key, err := accounts.DecryptKey(keyjson, passphrase) |
| 219 | + if err == nil { |
| 220 | + return key.PrivateKey |
| 221 | + } |
| 222 | + } |
| 223 | + utils.Fatalf("Can't decrypt swarm account key") |
| 224 | + return nil |
| 225 | +} |
| 226 | + |
| 227 | +func promptPassphrase(prompt string) string { |
| 228 | + if prompt != "" { |
| 229 | + fmt.Println(prompt) |
| 230 | + } |
| 231 | + password, err := console.Stdin.PromptPassword("Passphrase: ") |
| 232 | + if err != nil { |
| 233 | + utils.Fatalf("Failed to read passphrase: %v", err) |
| 234 | + } |
| 235 | + return password |
| 236 | +} |
| 237 | + |
| 238 | +func injectBootnodes(srv *p2p.Server, nodes []string) { |
| 239 | + for _, url := range nodes { |
| 240 | + n, err := discover.ParseNode(url) |
| 241 | + if err != nil { |
| 242 | + glog.Errorf("invalid bootnode %q", err) |
| 243 | + } |
| 244 | + srv.AddPeer(n) |
| 245 | + } |
| 246 | +} |
0 commit comments