main.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. package main
  17. import (
  18. "context"
  19. "crypto/ecdsa"
  20. "fmt"
  21. "io/ioutil"
  22. "math/big"
  23. "os"
  24. "os/signal"
  25. "runtime"
  26. "strconv"
  27. "strings"
  28. "syscall"
  29. "time"
  30. "github.com/ethereum/go-ethereum/accounts"
  31. "github.com/ethereum/go-ethereum/accounts/keystore"
  32. "github.com/ethereum/go-ethereum/cmd/utils"
  33. "github.com/ethereum/go-ethereum/common"
  34. "github.com/ethereum/go-ethereum/console"
  35. "github.com/ethereum/go-ethereum/contracts/ens"
  36. "github.com/ethereum/go-ethereum/crypto"
  37. "github.com/ethereum/go-ethereum/ethclient"
  38. "github.com/ethereum/go-ethereum/internal/debug"
  39. "github.com/ethereum/go-ethereum/log"
  40. "github.com/ethereum/go-ethereum/node"
  41. "github.com/ethereum/go-ethereum/p2p"
  42. "github.com/ethereum/go-ethereum/p2p/discover"
  43. "github.com/ethereum/go-ethereum/params"
  44. "github.com/ethereum/go-ethereum/rpc"
  45. "github.com/ethereum/go-ethereum/swarm"
  46. bzzapi "github.com/ethereum/go-ethereum/swarm/api"
  47. "gopkg.in/urfave/cli.v1"
  48. )
  49. const clientIdentifier = "swarm"
  50. var (
  51. gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
  52. testbetBootNodes = []string{
  53. "enode://ec8ae764f7cb0417bdfb009b9d0f18ab3818a3a4e8e7c67dd5f18971a93510a2e6f43cd0b69a27e439a9629457ea804104f37c85e41eed057d3faabbf7744cdf@13.74.157.139:30429",
  54. "enode://c2e1fceb3bf3be19dff71eec6cccf19f2dbf7567ee017d130240c670be8594bc9163353ca55dd8df7a4f161dd94b36d0615c17418b5a3cdcbb4e9d99dfa4de37@13.74.157.139:30430",
  55. "enode://fe29b82319b734ce1ec68b84657d57145fee237387e63273989d354486731e59f78858e452ef800a020559da22dcca759536e6aa5517c53930d29ce0b1029286@13.74.157.139:30431",
  56. "enode://1d7187e7bde45cf0bee489ce9852dd6d1a0d9aa67a33a6b8e6db8a4fbc6fcfa6f0f1a5419343671521b863b187d1c73bad3603bae66421d157ffef357669ddb8@13.74.157.139:30432",
  57. "enode://0e4cba800f7b1ee73673afa6a4acead4018f0149d2e3216be3f133318fd165b324cd71b81fbe1e80deac8dbf56e57a49db7be67f8b9bc81bd2b7ee496434fb5d@13.74.157.139:30433",
  58. }
  59. )
  60. var (
  61. ChequebookAddrFlag = cli.StringFlag{
  62. Name: "chequebook",
  63. Usage: "chequebook contract address",
  64. }
  65. SwarmAccountFlag = cli.StringFlag{
  66. Name: "bzzaccount",
  67. Usage: "Swarm account key file",
  68. }
  69. SwarmListenAddrFlag = cli.StringFlag{
  70. Name: "httpaddr",
  71. Usage: "Swarm HTTP API listening interface",
  72. }
  73. SwarmPortFlag = cli.StringFlag{
  74. Name: "bzzport",
  75. Usage: "Swarm local http api port",
  76. }
  77. SwarmNetworkIdFlag = cli.IntFlag{
  78. Name: "bzznetworkid",
  79. Usage: "Network identifier (integer, default 3=swarm testnet)",
  80. }
  81. SwarmConfigPathFlag = cli.StringFlag{
  82. Name: "bzzconfig",
  83. Usage: "Swarm config file path (datadir/bzz)",
  84. }
  85. SwarmSwapEnabledFlag = cli.BoolFlag{
  86. Name: "swap",
  87. Usage: "Swarm SWAP enabled (default false)",
  88. }
  89. SwarmSwapAPIFlag = cli.StringFlag{
  90. Name: "swap-api",
  91. Usage: "URL of the Ethereum API provider to use to settle SWAP payments",
  92. }
  93. SwarmSyncEnabledFlag = cli.BoolTFlag{
  94. Name: "sync",
  95. Usage: "Swarm Syncing enabled (default true)",
  96. }
  97. EnsAPIFlag = cli.StringFlag{
  98. Name: "ens-api",
  99. Usage: "URL of the Ethereum API provider to use for ENS record lookups",
  100. Value: node.DefaultIPCEndpoint("geth"),
  101. }
  102. EnsAddrFlag = cli.StringFlag{
  103. Name: "ens-addr",
  104. Usage: "ENS contract address (default is detected as testnet or mainnet using --ens-api)",
  105. }
  106. SwarmApiFlag = cli.StringFlag{
  107. Name: "bzzapi",
  108. Usage: "Swarm HTTP endpoint",
  109. Value: "http://127.0.0.1:8500",
  110. }
  111. SwarmRecursiveUploadFlag = cli.BoolFlag{
  112. Name: "recursive",
  113. Usage: "Upload directories recursively",
  114. }
  115. SwarmWantManifestFlag = cli.BoolTFlag{
  116. Name: "manifest",
  117. Usage: "Automatic manifest upload",
  118. }
  119. SwarmUploadDefaultPath = cli.StringFlag{
  120. Name: "defaultpath",
  121. Usage: "path to file served for empty url path (none)",
  122. }
  123. SwarmUpFromStdinFlag = cli.BoolFlag{
  124. Name: "stdin",
  125. Usage: "reads data to be uploaded from stdin",
  126. }
  127. SwarmUploadMimeType = cli.StringFlag{
  128. Name: "mime",
  129. Usage: "force mime type",
  130. }
  131. CorsStringFlag = cli.StringFlag{
  132. Name: "corsdomain",
  133. Usage: "Domain on which to send Access-Control-Allow-Origin header (multiple domains can be supplied separated by a ',')",
  134. }
  135. // the following flags are deprecated and should be removed in the future
  136. DeprecatedEthAPIFlag = cli.StringFlag{
  137. Name: "ethapi",
  138. Usage: "DEPRECATED: please use --ens-api and --swap-api",
  139. }
  140. )
  141. var defaultNodeConfig = node.DefaultConfig
  142. // This init function sets defaults so cmd/swarm can run alongside geth.
  143. func init() {
  144. defaultNodeConfig.Name = clientIdentifier
  145. defaultNodeConfig.Version = params.VersionWithCommit(gitCommit)
  146. defaultNodeConfig.P2P.ListenAddr = ":30399"
  147. defaultNodeConfig.IPCPath = "bzzd.ipc"
  148. // Set flag defaults for --help display.
  149. utils.ListenPortFlag.Value = 30399
  150. }
  151. var app = utils.NewApp(gitCommit, "Ethereum Swarm")
  152. // This init function creates the cli.App.
  153. func init() {
  154. app.Action = bzzd
  155. app.HideVersion = true // we have a command to print the version
  156. app.Copyright = "Copyright 2013-2016 The go-ethereum Authors"
  157. app.Commands = []cli.Command{
  158. {
  159. Action: version,
  160. Name: "version",
  161. Usage: "Print version numbers",
  162. ArgsUsage: " ",
  163. Description: `
  164. The output of this command is supposed to be machine-readable.
  165. `,
  166. },
  167. {
  168. Action: upload,
  169. Name: "up",
  170. Usage: "upload a file or directory to swarm using the HTTP API",
  171. ArgsUsage: " <file>",
  172. Description: `
  173. "upload a file or directory to swarm using the HTTP API and prints the root hash",
  174. `,
  175. },
  176. {
  177. Action: list,
  178. Name: "ls",
  179. Usage: "list files and directories contained in a manifest",
  180. ArgsUsage: " <manifest> [<prefix>]",
  181. Description: `
  182. Lists files and directories contained in a manifest.
  183. `,
  184. },
  185. {
  186. Action: hash,
  187. Name: "hash",
  188. Usage: "print the swarm hash of a file or directory",
  189. ArgsUsage: " <file>",
  190. Description: `
  191. Prints the swarm hash of file or directory.
  192. `,
  193. },
  194. {
  195. Name: "manifest",
  196. Usage: "update a MANIFEST",
  197. ArgsUsage: "manifest COMMAND",
  198. Description: `
  199. Updates a MANIFEST by adding/removing/updating the hash of a path.
  200. `,
  201. Subcommands: []cli.Command{
  202. {
  203. Action: add,
  204. Name: "add",
  205. Usage: "add a new path to the manifest",
  206. ArgsUsage: "<MANIFEST> <path> <hash> [<content-type>]",
  207. Description: `
  208. Adds a new path to the manifest
  209. `,
  210. },
  211. {
  212. Action: update,
  213. Name: "update",
  214. Usage: "update the hash for an already existing path in the manifest",
  215. ArgsUsage: "<MANIFEST> <path> <newhash> [<newcontent-type>]",
  216. Description: `
  217. Update the hash for an already existing path in the manifest
  218. `,
  219. },
  220. {
  221. Action: remove,
  222. Name: "remove",
  223. Usage: "removes a path from the manifest",
  224. ArgsUsage: "<MANIFEST> <path>",
  225. Description: `
  226. Removes a path from the manifest
  227. `,
  228. },
  229. },
  230. },
  231. {
  232. Name: "db",
  233. Usage: "manage the local chunk database",
  234. ArgsUsage: "db COMMAND",
  235. Description: `
  236. Manage the local chunk database.
  237. `,
  238. Subcommands: []cli.Command{
  239. {
  240. Action: dbExport,
  241. Name: "export",
  242. Usage: "export a local chunk database as a tar archive (use - to send to stdout)",
  243. ArgsUsage: "<chunkdb> <file>",
  244. Description: `
  245. Export a local chunk database as a tar archive (use - to send to stdout).
  246. swarm db export ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar
  247. The export may be quite large, consider piping the output through the Unix
  248. pv(1) tool to get a progress bar:
  249. swarm db export ~/.ethereum/swarm/bzz-KEY/chunks - | pv > chunks.tar
  250. `,
  251. },
  252. {
  253. Action: dbImport,
  254. Name: "import",
  255. Usage: "import chunks from a tar archive into a local chunk database (use - to read from stdin)",
  256. ArgsUsage: "<chunkdb> <file>",
  257. Description: `
  258. Import chunks from a tar archive into a local chunk database (use - to read from stdin).
  259. swarm db import ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar
  260. The import may be quite large, consider piping the input through the Unix
  261. pv(1) tool to get a progress bar:
  262. pv chunks.tar | swarm db import ~/.ethereum/swarm/bzz-KEY/chunks -
  263. `,
  264. },
  265. {
  266. Action: dbClean,
  267. Name: "clean",
  268. Usage: "remove corrupt entries from a local chunk database",
  269. ArgsUsage: "<chunkdb>",
  270. Description: `
  271. Remove corrupt entries from a local chunk database.
  272. `,
  273. },
  274. },
  275. },
  276. {
  277. Action: func(ctx *cli.Context) {
  278. utils.Fatalf("ERROR: 'swarm cleandb' has been removed, please use 'swarm db clean'.")
  279. },
  280. Name: "cleandb",
  281. Usage: "DEPRECATED: use 'swarm db clean'",
  282. ArgsUsage: " ",
  283. Description: `
  284. DEPRECATED: use 'swarm db clean'.
  285. `,
  286. },
  287. }
  288. app.Flags = []cli.Flag{
  289. utils.IdentityFlag,
  290. utils.DataDirFlag,
  291. utils.BootnodesFlag,
  292. utils.KeyStoreDirFlag,
  293. utils.ListenPortFlag,
  294. utils.NoDiscoverFlag,
  295. utils.DiscoveryV5Flag,
  296. utils.NetrestrictFlag,
  297. utils.NodeKeyFileFlag,
  298. utils.NodeKeyHexFlag,
  299. utils.MaxPeersFlag,
  300. utils.NATFlag,
  301. utils.IPCDisabledFlag,
  302. utils.IPCPathFlag,
  303. utils.PasswordFileFlag,
  304. // bzzd-specific flags
  305. CorsStringFlag,
  306. EnsAPIFlag,
  307. EnsAddrFlag,
  308. SwarmConfigPathFlag,
  309. SwarmSwapEnabledFlag,
  310. SwarmSwapAPIFlag,
  311. SwarmSyncEnabledFlag,
  312. SwarmListenAddrFlag,
  313. SwarmPortFlag,
  314. SwarmAccountFlag,
  315. SwarmNetworkIdFlag,
  316. ChequebookAddrFlag,
  317. // upload flags
  318. SwarmApiFlag,
  319. SwarmRecursiveUploadFlag,
  320. SwarmWantManifestFlag,
  321. SwarmUploadDefaultPath,
  322. SwarmUpFromStdinFlag,
  323. SwarmUploadMimeType,
  324. //deprecated flags
  325. DeprecatedEthAPIFlag,
  326. }
  327. app.Flags = append(app.Flags, debug.Flags...)
  328. app.Before = func(ctx *cli.Context) error {
  329. runtime.GOMAXPROCS(runtime.NumCPU())
  330. return debug.Setup(ctx)
  331. }
  332. app.After = func(ctx *cli.Context) error {
  333. debug.Exit()
  334. return nil
  335. }
  336. }
  337. func main() {
  338. if err := app.Run(os.Args); err != nil {
  339. fmt.Fprintln(os.Stderr, err)
  340. os.Exit(1)
  341. }
  342. }
  343. func version(ctx *cli.Context) error {
  344. fmt.Println(strings.Title(clientIdentifier))
  345. fmt.Println("Version:", params.Version)
  346. if gitCommit != "" {
  347. fmt.Println("Git Commit:", gitCommit)
  348. }
  349. fmt.Println("Network Id:", ctx.GlobalInt(utils.NetworkIdFlag.Name))
  350. fmt.Println("Go Version:", runtime.Version())
  351. fmt.Println("OS:", runtime.GOOS)
  352. fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
  353. fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
  354. return nil
  355. }
  356. func bzzd(ctx *cli.Context) error {
  357. // exit if the deprecated --ethapi flag is set
  358. if ctx.GlobalString(DeprecatedEthAPIFlag.Name) != "" {
  359. utils.Fatalf("--ethapi is no longer a valid command line flag, please use --ens-api and/or --swap-api.")
  360. }
  361. cfg := defaultNodeConfig
  362. utils.SetNodeConfig(ctx, &cfg)
  363. stack, err := node.New(&cfg)
  364. if err != nil {
  365. utils.Fatalf("can't create node: %v", err)
  366. }
  367. registerBzzService(ctx, stack)
  368. utils.StartNode(stack)
  369. go func() {
  370. sigc := make(chan os.Signal, 1)
  371. signal.Notify(sigc, syscall.SIGTERM)
  372. defer signal.Stop(sigc)
  373. <-sigc
  374. log.Info("Got sigterm, shutting swarm down...")
  375. stack.Stop()
  376. }()
  377. networkId := ctx.GlobalUint64(SwarmNetworkIdFlag.Name)
  378. // Add bootnodes as initial peers.
  379. if ctx.GlobalIsSet(utils.BootnodesFlag.Name) {
  380. bootnodes := strings.Split(ctx.GlobalString(utils.BootnodesFlag.Name), ",")
  381. injectBootnodes(stack.Server(), bootnodes)
  382. } else {
  383. if networkId == 3 {
  384. injectBootnodes(stack.Server(), testbetBootNodes)
  385. }
  386. }
  387. stack.Wait()
  388. return nil
  389. }
  390. // detectEnsAddr determines the ENS contract address by getting both the
  391. // version and genesis hash using the client and matching them to either
  392. // mainnet or testnet addresses
  393. func detectEnsAddr(client *rpc.Client) (common.Address, error) {
  394. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  395. defer cancel()
  396. var version string
  397. if err := client.CallContext(ctx, &version, "net_version"); err != nil {
  398. return common.Address{}, err
  399. }
  400. block, err := ethclient.NewClient(client).BlockByNumber(ctx, big.NewInt(0))
  401. if err != nil {
  402. return common.Address{}, err
  403. }
  404. switch {
  405. case version == "1" && block.Hash() == params.MainnetGenesisHash:
  406. log.Info("using Mainnet ENS contract address", "addr", ens.MainNetAddress)
  407. return ens.MainNetAddress, nil
  408. case version == "3" && block.Hash() == params.TestnetGenesisHash:
  409. log.Info("using Testnet ENS contract address", "addr", ens.TestNetAddress)
  410. return ens.TestNetAddress, nil
  411. default:
  412. return common.Address{}, fmt.Errorf("unknown version and genesis hash: %s %s", version, block.Hash())
  413. }
  414. }
  415. func registerBzzService(ctx *cli.Context, stack *node.Node) {
  416. prvkey := getAccount(ctx, stack)
  417. chbookaddr := common.HexToAddress(ctx.GlobalString(ChequebookAddrFlag.Name))
  418. bzzdir := ctx.GlobalString(SwarmConfigPathFlag.Name)
  419. if bzzdir == "" {
  420. bzzdir = stack.InstanceDir()
  421. }
  422. bzzconfig, err := bzzapi.NewConfig(bzzdir, chbookaddr, prvkey, ctx.GlobalUint64(SwarmNetworkIdFlag.Name))
  423. if err != nil {
  424. utils.Fatalf("unable to configure swarm: %v", err)
  425. }
  426. bzzport := ctx.GlobalString(SwarmPortFlag.Name)
  427. if len(bzzport) > 0 {
  428. bzzconfig.Port = bzzport
  429. }
  430. if bzzaddr := ctx.GlobalString(SwarmListenAddrFlag.Name); bzzaddr != "" {
  431. bzzconfig.ListenAddr = bzzaddr
  432. }
  433. swapEnabled := ctx.GlobalBool(SwarmSwapEnabledFlag.Name)
  434. syncEnabled := ctx.GlobalBoolT(SwarmSyncEnabledFlag.Name)
  435. swapapi := ctx.GlobalString(SwarmSwapAPIFlag.Name)
  436. if swapEnabled && swapapi == "" {
  437. utils.Fatalf("SWAP is enabled but --swap-api is not set")
  438. }
  439. ensapi := ctx.GlobalString(EnsAPIFlag.Name)
  440. ensAddr := ctx.GlobalString(EnsAddrFlag.Name)
  441. cors := ctx.GlobalString(CorsStringFlag.Name)
  442. boot := func(ctx *node.ServiceContext) (node.Service, error) {
  443. var swapClient *ethclient.Client
  444. if swapapi != "" {
  445. log.Info("connecting to SWAP API", "url", swapapi)
  446. swapClient, err = ethclient.Dial(swapapi)
  447. if err != nil {
  448. return nil, fmt.Errorf("error connecting to SWAP API %s: %s", swapapi, err)
  449. }
  450. }
  451. var ensClient *ethclient.Client
  452. if ensapi != "" {
  453. log.Info("connecting to ENS API", "url", ensapi)
  454. client, err := rpc.Dial(ensapi)
  455. if err != nil {
  456. return nil, fmt.Errorf("error connecting to ENS API %s: %s", ensapi, err)
  457. }
  458. ensClient = ethclient.NewClient(client)
  459. if ensAddr != "" {
  460. bzzconfig.EnsRoot = common.HexToAddress(ensAddr)
  461. } else {
  462. ensAddr, err := detectEnsAddr(client)
  463. if err == nil {
  464. bzzconfig.EnsRoot = ensAddr
  465. } else {
  466. log.Warn(fmt.Sprintf("could not determine ENS contract address, using default %s", bzzconfig.EnsRoot), "err", err)
  467. }
  468. }
  469. }
  470. return swarm.NewSwarm(ctx, swapClient, ensClient, bzzconfig, swapEnabled, syncEnabled, cors)
  471. }
  472. if err := stack.Register(boot); err != nil {
  473. utils.Fatalf("Failed to register the Swarm service: %v", err)
  474. }
  475. }
  476. func getAccount(ctx *cli.Context, stack *node.Node) *ecdsa.PrivateKey {
  477. keyid := ctx.GlobalString(SwarmAccountFlag.Name)
  478. if keyid == "" {
  479. utils.Fatalf("Option %q is required", SwarmAccountFlag.Name)
  480. }
  481. // Try to load the arg as a hex key file.
  482. if key, err := crypto.LoadECDSA(keyid); err == nil {
  483. log.Info("Swarm account key loaded", "address", crypto.PubkeyToAddress(key.PublicKey))
  484. return key
  485. }
  486. // Otherwise try getting it from the keystore.
  487. am := stack.AccountManager()
  488. ks := am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
  489. return decryptStoreAccount(ks, keyid, utils.MakePasswordList(ctx))
  490. }
  491. func decryptStoreAccount(ks *keystore.KeyStore, account string, passwords []string) *ecdsa.PrivateKey {
  492. var a accounts.Account
  493. var err error
  494. if common.IsHexAddress(account) {
  495. a, err = ks.Find(accounts.Account{Address: common.HexToAddress(account)})
  496. } else if ix, ixerr := strconv.Atoi(account); ixerr == nil && ix > 0 {
  497. if accounts := ks.Accounts(); len(accounts) > ix {
  498. a = accounts[ix]
  499. } else {
  500. err = fmt.Errorf("index %d higher than number of accounts %d", ix, len(accounts))
  501. }
  502. } else {
  503. utils.Fatalf("Can't find swarm account key %s", account)
  504. }
  505. if err != nil {
  506. utils.Fatalf("Can't find swarm account key: %v", err)
  507. }
  508. keyjson, err := ioutil.ReadFile(a.URL.Path)
  509. if err != nil {
  510. utils.Fatalf("Can't load swarm account key: %v", err)
  511. }
  512. for i := 0; i < 3; i++ {
  513. password := getPassPhrase(fmt.Sprintf("Unlocking swarm account %s [%d/3]", a.Address.Hex(), i+1), i, passwords)
  514. key, err := keystore.DecryptKey(keyjson, password)
  515. if err == nil {
  516. return key.PrivateKey
  517. }
  518. }
  519. utils.Fatalf("Can't decrypt swarm account key")
  520. return nil
  521. }
  522. // getPassPhrase retrieves the password associated with bzz account, either by fetching
  523. // from a list of pre-loaded passwords, or by requesting it interactively from user.
  524. func getPassPhrase(prompt string, i int, passwords []string) string {
  525. // non-interactive
  526. if len(passwords) > 0 {
  527. if i < len(passwords) {
  528. return passwords[i]
  529. }
  530. return passwords[len(passwords)-1]
  531. }
  532. // fallback to interactive mode
  533. if prompt != "" {
  534. fmt.Println(prompt)
  535. }
  536. password, err := console.Stdin.PromptPassword("Passphrase: ")
  537. if err != nil {
  538. utils.Fatalf("Failed to read passphrase: %v", err)
  539. }
  540. return password
  541. }
  542. func injectBootnodes(srv *p2p.Server, nodes []string) {
  543. for _, url := range nodes {
  544. n, err := discover.ParseNode(url)
  545. if err != nil {
  546. log.Error("Invalid swarm bootnode", "err", err)
  547. continue
  548. }
  549. srv.AddPeer(n)
  550. }
  551. }