main.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. "crypto/ecdsa"
  19. "encoding/hex"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "os/signal"
  24. "runtime"
  25. "sort"
  26. "strconv"
  27. "strings"
  28. "syscall"
  29. "github.com/ethereum/go-ethereum/accounts"
  30. "github.com/ethereum/go-ethereum/accounts/keystore"
  31. "github.com/ethereum/go-ethereum/cmd/utils"
  32. "github.com/ethereum/go-ethereum/common"
  33. "github.com/ethereum/go-ethereum/console"
  34. "github.com/ethereum/go-ethereum/crypto"
  35. "github.com/ethereum/go-ethereum/internal/debug"
  36. "github.com/ethereum/go-ethereum/log"
  37. "github.com/ethereum/go-ethereum/node"
  38. "github.com/ethereum/go-ethereum/p2p/enode"
  39. "github.com/ethereum/go-ethereum/swarm"
  40. bzzapi "github.com/ethereum/go-ethereum/swarm/api"
  41. swarmmetrics "github.com/ethereum/go-ethereum/swarm/metrics"
  42. "github.com/ethereum/go-ethereum/swarm/tracing"
  43. sv "github.com/ethereum/go-ethereum/swarm/version"
  44. "gopkg.in/urfave/cli.v1"
  45. )
  46. const clientIdentifier = "swarm"
  47. const helpTemplate = `NAME:
  48. {{.HelpName}} - {{.Usage}}
  49. USAGE:
  50. {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Category}}
  51. CATEGORY:
  52. {{.Category}}{{end}}{{if .Description}}
  53. DESCRIPTION:
  54. {{.Description}}{{end}}{{if .VisibleFlags}}
  55. OPTIONS:
  56. {{range .VisibleFlags}}{{.}}
  57. {{end}}{{end}}
  58. `
  59. var (
  60. gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
  61. )
  62. var (
  63. ChequebookAddrFlag = cli.StringFlag{
  64. Name: "chequebook",
  65. Usage: "chequebook contract address",
  66. EnvVar: SWARM_ENV_CHEQUEBOOK_ADDR,
  67. }
  68. SwarmAccountFlag = cli.StringFlag{
  69. Name: "bzzaccount",
  70. Usage: "Swarm account key file",
  71. EnvVar: SWARM_ENV_ACCOUNT,
  72. }
  73. SwarmListenAddrFlag = cli.StringFlag{
  74. Name: "httpaddr",
  75. Usage: "Swarm HTTP API listening interface",
  76. EnvVar: SWARM_ENV_LISTEN_ADDR,
  77. }
  78. SwarmPortFlag = cli.StringFlag{
  79. Name: "bzzport",
  80. Usage: "Swarm local http api port",
  81. EnvVar: SWARM_ENV_PORT,
  82. }
  83. SwarmNetworkIdFlag = cli.IntFlag{
  84. Name: "bzznetworkid",
  85. Usage: "Network identifier (integer, default 3=swarm testnet)",
  86. EnvVar: SWARM_ENV_NETWORK_ID,
  87. }
  88. SwarmSwapEnabledFlag = cli.BoolFlag{
  89. Name: "swap",
  90. Usage: "Swarm SWAP enabled (default false)",
  91. EnvVar: SWARM_ENV_SWAP_ENABLE,
  92. }
  93. SwarmSwapAPIFlag = cli.StringFlag{
  94. Name: "swap-api",
  95. Usage: "URL of the Ethereum API provider to use to settle SWAP payments",
  96. EnvVar: SWARM_ENV_SWAP_API,
  97. }
  98. SwarmSyncDisabledFlag = cli.BoolTFlag{
  99. Name: "nosync",
  100. Usage: "Disable swarm syncing",
  101. EnvVar: SWARM_ENV_SYNC_DISABLE,
  102. }
  103. SwarmSyncUpdateDelay = cli.DurationFlag{
  104. Name: "sync-update-delay",
  105. Usage: "Duration for sync subscriptions update after no new peers are added (default 15s)",
  106. EnvVar: SWARM_ENV_SYNC_UPDATE_DELAY,
  107. }
  108. SwarmMaxStreamPeerServersFlag = cli.IntFlag{
  109. Name: "max-stream-peer-servers",
  110. Usage: "Limit of Stream peer servers, 0 denotes unlimited",
  111. EnvVar: SWARM_ENV_MAX_STREAM_PEER_SERVERS,
  112. Value: 10000, // A very large default value is possible as stream servers have very small memory footprint
  113. }
  114. SwarmLightNodeEnabled = cli.BoolFlag{
  115. Name: "lightnode",
  116. Usage: "Enable Swarm LightNode (default false)",
  117. EnvVar: SWARM_ENV_LIGHT_NODE_ENABLE,
  118. }
  119. SwarmDeliverySkipCheckFlag = cli.BoolFlag{
  120. Name: "delivery-skip-check",
  121. Usage: "Skip chunk delivery check (default false)",
  122. EnvVar: SWARM_ENV_DELIVERY_SKIP_CHECK,
  123. }
  124. EnsAPIFlag = cli.StringSliceFlag{
  125. Name: "ens-api",
  126. Usage: "ENS API endpoint for a TLD and with contract address, can be repeated, format [tld:][contract-addr@]url",
  127. EnvVar: SWARM_ENV_ENS_API,
  128. }
  129. SwarmApiFlag = cli.StringFlag{
  130. Name: "bzzapi",
  131. Usage: "Swarm HTTP endpoint",
  132. Value: "http://127.0.0.1:8500",
  133. }
  134. SwarmRecursiveFlag = cli.BoolFlag{
  135. Name: "recursive",
  136. Usage: "Upload directories recursively",
  137. }
  138. SwarmWantManifestFlag = cli.BoolTFlag{
  139. Name: "manifest",
  140. Usage: "Automatic manifest upload (default true)",
  141. }
  142. SwarmUploadDefaultPath = cli.StringFlag{
  143. Name: "defaultpath",
  144. Usage: "path to file served for empty url path (none)",
  145. }
  146. SwarmAccessGrantKeyFlag = cli.StringFlag{
  147. Name: "grant-key",
  148. Usage: "grants a given public key access to an ACT",
  149. }
  150. SwarmAccessGrantKeysFlag = cli.StringFlag{
  151. Name: "grant-keys",
  152. Usage: "grants a given list of public keys in the following file (separated by line breaks) access to an ACT",
  153. }
  154. SwarmUpFromStdinFlag = cli.BoolFlag{
  155. Name: "stdin",
  156. Usage: "reads data to be uploaded from stdin",
  157. }
  158. SwarmUploadMimeType = cli.StringFlag{
  159. Name: "mime",
  160. Usage: "Manually specify MIME type",
  161. }
  162. SwarmEncryptedFlag = cli.BoolFlag{
  163. Name: "encrypt",
  164. Usage: "use encrypted upload",
  165. }
  166. SwarmAccessPasswordFlag = cli.StringFlag{
  167. Name: "password",
  168. Usage: "Password",
  169. EnvVar: SWARM_ACCESS_PASSWORD,
  170. }
  171. SwarmDryRunFlag = cli.BoolFlag{
  172. Name: "dry-run",
  173. Usage: "dry-run",
  174. }
  175. CorsStringFlag = cli.StringFlag{
  176. Name: "corsdomain",
  177. Usage: "Domain on which to send Access-Control-Allow-Origin header (multiple domains can be supplied separated by a ',')",
  178. EnvVar: SWARM_ENV_CORS,
  179. }
  180. SwarmStorePath = cli.StringFlag{
  181. Name: "store.path",
  182. Usage: "Path to leveldb chunk DB (default <$GETH_ENV_DIR>/swarm/bzz-<$BZZ_KEY>/chunks)",
  183. EnvVar: SWARM_ENV_STORE_PATH,
  184. }
  185. SwarmStoreCapacity = cli.Uint64Flag{
  186. Name: "store.size",
  187. Usage: "Number of chunks (5M is roughly 20-25GB) (default 5000000)",
  188. EnvVar: SWARM_ENV_STORE_CAPACITY,
  189. }
  190. SwarmStoreCacheCapacity = cli.UintFlag{
  191. Name: "store.cache.size",
  192. Usage: "Number of recent chunks cached in memory (default 5000)",
  193. EnvVar: SWARM_ENV_STORE_CACHE_CAPACITY,
  194. }
  195. SwarmCompressedFlag = cli.BoolFlag{
  196. Name: "compressed",
  197. Usage: "Prints encryption keys in compressed form",
  198. }
  199. SwarmFeedNameFlag = cli.StringFlag{
  200. Name: "name",
  201. Usage: "User-defined name for the new feed, limited to 32 characters. If combined with topic, it will refer to a subtopic with this name",
  202. }
  203. SwarmFeedTopicFlag = cli.StringFlag{
  204. Name: "topic",
  205. Usage: "User-defined topic this feed is tracking, hex encoded. Limited to 64 hexadecimal characters",
  206. }
  207. SwarmFeedDataOnCreateFlag = cli.StringFlag{
  208. Name: "data",
  209. Usage: "Initializes the feed with the given hex-encoded data. Data must be prefixed by 0x",
  210. }
  211. SwarmFeedManifestFlag = cli.StringFlag{
  212. Name: "manifest",
  213. Usage: "Refers to the feed through a manifest",
  214. }
  215. SwarmFeedUserFlag = cli.StringFlag{
  216. Name: "user",
  217. Usage: "Indicates the user who updates the feed",
  218. }
  219. )
  220. //declare a few constant error messages, useful for later error check comparisons in test
  221. var (
  222. SWARM_ERR_NO_BZZACCOUNT = "bzzaccount option is required but not set; check your config file, command line or environment variables"
  223. SWARM_ERR_SWAP_SET_NO_API = "SWAP is enabled but --swap-api is not set"
  224. )
  225. // this help command gets added to any subcommand that does not define it explicitly
  226. var defaultSubcommandHelp = cli.Command{
  227. Action: func(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "", 1) },
  228. CustomHelpTemplate: helpTemplate,
  229. Name: "help",
  230. Usage: "shows this help",
  231. Hidden: true,
  232. }
  233. var defaultNodeConfig = node.DefaultConfig
  234. // This init function sets defaults so cmd/swarm can run alongside geth.
  235. func init() {
  236. defaultNodeConfig.Name = clientIdentifier
  237. defaultNodeConfig.Version = sv.VersionWithCommit(gitCommit)
  238. defaultNodeConfig.P2P.ListenAddr = ":30399"
  239. defaultNodeConfig.IPCPath = "bzzd.ipc"
  240. // Set flag defaults for --help display.
  241. utils.ListenPortFlag.Value = 30399
  242. }
  243. var app = utils.NewApp("", "Ethereum Swarm")
  244. // This init function creates the cli.App.
  245. func init() {
  246. app.Action = bzzd
  247. app.Version = sv.ArchiveVersion(gitCommit)
  248. app.Copyright = "Copyright 2013-2016 The go-ethereum Authors"
  249. app.Commands = []cli.Command{
  250. {
  251. Action: version,
  252. CustomHelpTemplate: helpTemplate,
  253. Name: "version",
  254. Usage: "Print version numbers",
  255. Description: "The output of this command is supposed to be machine-readable",
  256. },
  257. {
  258. Action: keys,
  259. CustomHelpTemplate: helpTemplate,
  260. Name: "print-keys",
  261. Flags: []cli.Flag{SwarmCompressedFlag},
  262. Usage: "Print public key information",
  263. Description: "The output of this command is supposed to be machine-readable",
  264. },
  265. {
  266. Action: upload,
  267. CustomHelpTemplate: helpTemplate,
  268. Name: "up",
  269. Usage: "uploads a file or directory to swarm using the HTTP API",
  270. ArgsUsage: "<file>",
  271. Flags: []cli.Flag{SwarmEncryptedFlag},
  272. Description: "uploads a file or directory to swarm using the HTTP API and prints the root hash",
  273. },
  274. {
  275. CustomHelpTemplate: helpTemplate,
  276. Name: "access",
  277. Usage: "encrypts a reference and embeds it into a root manifest",
  278. ArgsUsage: "<ref>",
  279. Description: "encrypts a reference and embeds it into a root manifest",
  280. Subcommands: []cli.Command{
  281. {
  282. CustomHelpTemplate: helpTemplate,
  283. Name: "new",
  284. Usage: "encrypts a reference and embeds it into a root manifest",
  285. ArgsUsage: "<ref>",
  286. Description: "encrypts a reference and embeds it into a root access manifest and prints the resulting manifest",
  287. Subcommands: []cli.Command{
  288. {
  289. Action: accessNewPass,
  290. CustomHelpTemplate: helpTemplate,
  291. Flags: []cli.Flag{
  292. utils.PasswordFileFlag,
  293. SwarmDryRunFlag,
  294. },
  295. Name: "pass",
  296. Usage: "encrypts a reference with a password and embeds it into a root manifest",
  297. ArgsUsage: "<ref>",
  298. Description: "encrypts a reference and embeds it into a root access manifest and prints the resulting manifest",
  299. },
  300. {
  301. Action: accessNewPK,
  302. CustomHelpTemplate: helpTemplate,
  303. Flags: []cli.Flag{
  304. utils.PasswordFileFlag,
  305. SwarmDryRunFlag,
  306. SwarmAccessGrantKeyFlag,
  307. },
  308. Name: "pk",
  309. Usage: "encrypts a reference with the node's private key and a given grantee's public key and embeds it into a root manifest",
  310. ArgsUsage: "<ref>",
  311. Description: "encrypts a reference and embeds it into a root access manifest and prints the resulting manifest",
  312. },
  313. {
  314. Action: accessNewACT,
  315. CustomHelpTemplate: helpTemplate,
  316. Flags: []cli.Flag{
  317. SwarmAccessGrantKeysFlag,
  318. SwarmDryRunFlag,
  319. utils.PasswordFileFlag,
  320. },
  321. Name: "act",
  322. Usage: "encrypts a reference with the node's private key and a given grantee's public key and embeds it into a root manifest",
  323. ArgsUsage: "<ref>",
  324. Description: "encrypts a reference and embeds it into a root access manifest and prints the resulting manifest",
  325. },
  326. },
  327. },
  328. },
  329. },
  330. {
  331. CustomHelpTemplate: helpTemplate,
  332. Name: "feed",
  333. Usage: "(Advanced) Create and update Swarm Feeds",
  334. ArgsUsage: "<create|update|info>",
  335. Description: "Works with Swarm Feeds",
  336. Subcommands: []cli.Command{
  337. {
  338. Action: feedCreateManifest,
  339. CustomHelpTemplate: helpTemplate,
  340. Name: "create",
  341. Usage: "creates and publishes a new Feed manifest",
  342. Description: `creates and publishes a new Feed manifest pointing to a specified user's updates about a particular topic.
  343. The feed topic can be built in the following ways:
  344. * use --topic to set the topic to an arbitrary binary hex string.
  345. * use --name to set the topic to a human-readable name.
  346. For example --name could be set to "profile-picture", meaning this feed allows to get this user's current profile picture.
  347. * use both --topic and --name to create named subtopics.
  348. For example, --topic could be set to an Ethereum contract address and --name could be set to "comments", meaning
  349. this feed tracks a discussion about that contract.
  350. The --user flag allows to have this manifest refer to a user other than yourself. If not specified,
  351. it will then default to your local account (--bzzaccount)`,
  352. Flags: []cli.Flag{SwarmFeedNameFlag, SwarmFeedTopicFlag, SwarmFeedUserFlag},
  353. },
  354. {
  355. Action: feedUpdate,
  356. CustomHelpTemplate: helpTemplate,
  357. Name: "update",
  358. Usage: "updates the content of an existing Swarm Feed",
  359. ArgsUsage: "<0x Hex data>",
  360. Description: `publishes a new update on the specified topic
  361. The feed topic can be built in the following ways:
  362. * use --topic to set the topic to an arbitrary binary hex string.
  363. * use --name to set the topic to a human-readable name.
  364. For example --name could be set to "profile-picture", meaning this feed allows to get this user's current profile picture.
  365. * use both --topic and --name to create named subtopics.
  366. For example, --topic could be set to an Ethereum contract address and --name could be set to "comments", meaning
  367. this feed tracks a discussion about that contract.
  368. If you have a manifest, you can specify it with --manifest to refer to the feed,
  369. instead of using --topic / --name
  370. `,
  371. Flags: []cli.Flag{SwarmFeedManifestFlag, SwarmFeedNameFlag, SwarmFeedTopicFlag},
  372. },
  373. {
  374. Action: feedInfo,
  375. CustomHelpTemplate: helpTemplate,
  376. Name: "info",
  377. Usage: "obtains information about an existing Swarm Feed",
  378. Description: `obtains information about an existing Swarm Feed
  379. The topic can be specified directly with the --topic flag as an hex string
  380. If no topic is specified, the default topic (zero) will be used
  381. The --name flag can be used to specify subtopics with a specific name.
  382. The --user flag allows to refer to a user other than yourself. If not specified,
  383. it will then default to your local account (--bzzaccount)
  384. If you have a manifest, you can specify it with --manifest instead of --topic / --name / ---user
  385. to refer to the feed`,
  386. Flags: []cli.Flag{SwarmFeedManifestFlag, SwarmFeedNameFlag, SwarmFeedTopicFlag, SwarmFeedUserFlag},
  387. },
  388. },
  389. },
  390. {
  391. Action: list,
  392. CustomHelpTemplate: helpTemplate,
  393. Name: "ls",
  394. Usage: "list files and directories contained in a manifest",
  395. ArgsUsage: "<manifest> [<prefix>]",
  396. Description: "Lists files and directories contained in a manifest",
  397. },
  398. {
  399. Action: hash,
  400. CustomHelpTemplate: helpTemplate,
  401. Name: "hash",
  402. Usage: "print the swarm hash of a file or directory",
  403. ArgsUsage: "<file>",
  404. Description: "Prints the swarm hash of file or directory",
  405. },
  406. {
  407. Action: download,
  408. Name: "down",
  409. Flags: []cli.Flag{SwarmRecursiveFlag, SwarmAccessPasswordFlag},
  410. Usage: "downloads a swarm manifest or a file inside a manifest",
  411. ArgsUsage: " <uri> [<dir>]",
  412. Description: `Downloads a swarm bzz uri to the given dir. When no dir is provided, working directory is assumed. --recursive flag is expected when downloading a manifest with multiple entries.`,
  413. },
  414. {
  415. Name: "manifest",
  416. CustomHelpTemplate: helpTemplate,
  417. Usage: "perform operations on swarm manifests",
  418. ArgsUsage: "COMMAND",
  419. Description: "Updates a MANIFEST by adding/removing/updating the hash of a path.\nCOMMAND could be: add, update, remove",
  420. Subcommands: []cli.Command{
  421. {
  422. Action: manifestAdd,
  423. CustomHelpTemplate: helpTemplate,
  424. Name: "add",
  425. Usage: "add a new path to the manifest",
  426. ArgsUsage: "<MANIFEST> <path> <hash>",
  427. Description: "Adds a new path to the manifest",
  428. },
  429. {
  430. Action: manifestUpdate,
  431. CustomHelpTemplate: helpTemplate,
  432. Name: "update",
  433. Usage: "update the hash for an already existing path in the manifest",
  434. ArgsUsage: "<MANIFEST> <path> <newhash>",
  435. Description: "Update the hash for an already existing path in the manifest",
  436. },
  437. {
  438. Action: manifestRemove,
  439. CustomHelpTemplate: helpTemplate,
  440. Name: "remove",
  441. Usage: "removes a path from the manifest",
  442. ArgsUsage: "<MANIFEST> <path>",
  443. Description: "Removes a path from the manifest",
  444. },
  445. },
  446. },
  447. {
  448. Name: "fs",
  449. CustomHelpTemplate: helpTemplate,
  450. Usage: "perform FUSE operations",
  451. ArgsUsage: "fs COMMAND",
  452. Description: "Performs FUSE operations by mounting/unmounting/listing mount points. This assumes you already have a Swarm node running locally. For all operation you must reference the correct path to bzzd.ipc in order to communicate with the node",
  453. Subcommands: []cli.Command{
  454. {
  455. Action: mount,
  456. CustomHelpTemplate: helpTemplate,
  457. Name: "mount",
  458. Flags: []cli.Flag{utils.IPCPathFlag},
  459. Usage: "mount a swarm hash to a mount point",
  460. ArgsUsage: "swarm fs mount --ipcpath <path to bzzd.ipc> <manifest hash> <mount point>",
  461. Description: "Mounts a Swarm manifest hash to a given mount point. This assumes you already have a Swarm node running locally. You must reference the correct path to your bzzd.ipc file",
  462. },
  463. {
  464. Action: unmount,
  465. CustomHelpTemplate: helpTemplate,
  466. Name: "unmount",
  467. Flags: []cli.Flag{utils.IPCPathFlag},
  468. Usage: "unmount a swarmfs mount",
  469. ArgsUsage: "swarm fs unmount --ipcpath <path to bzzd.ipc> <mount point>",
  470. Description: "Unmounts a swarmfs mount residing at <mount point>. This assumes you already have a Swarm node running locally. You must reference the correct path to your bzzd.ipc file",
  471. },
  472. {
  473. Action: listMounts,
  474. CustomHelpTemplate: helpTemplate,
  475. Name: "list",
  476. Flags: []cli.Flag{utils.IPCPathFlag},
  477. Usage: "list swarmfs mounts",
  478. ArgsUsage: "swarm fs list --ipcpath <path to bzzd.ipc>",
  479. Description: "Lists all mounted swarmfs volumes. This assumes you already have a Swarm node running locally. You must reference the correct path to your bzzd.ipc file",
  480. },
  481. },
  482. },
  483. {
  484. Name: "db",
  485. CustomHelpTemplate: helpTemplate,
  486. Usage: "manage the local chunk database",
  487. ArgsUsage: "db COMMAND",
  488. Description: "Manage the local chunk database",
  489. Subcommands: []cli.Command{
  490. {
  491. Action: dbExport,
  492. CustomHelpTemplate: helpTemplate,
  493. Name: "export",
  494. Usage: "export a local chunk database as a tar archive (use - to send to stdout)",
  495. ArgsUsage: "<chunkdb> <file>",
  496. Description: `
  497. Export a local chunk database as a tar archive (use - to send to stdout).
  498. swarm db export ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar
  499. The export may be quite large, consider piping the output through the Unix
  500. pv(1) tool to get a progress bar:
  501. swarm db export ~/.ethereum/swarm/bzz-KEY/chunks - | pv > chunks.tar
  502. `,
  503. },
  504. {
  505. Action: dbImport,
  506. CustomHelpTemplate: helpTemplate,
  507. Name: "import",
  508. Usage: "import chunks from a tar archive into a local chunk database (use - to read from stdin)",
  509. ArgsUsage: "<chunkdb> <file>",
  510. Description: `Import chunks from a tar archive into a local chunk database (use - to read from stdin).
  511. swarm db import ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar
  512. The import may be quite large, consider piping the input through the Unix
  513. pv(1) tool to get a progress bar:
  514. pv chunks.tar | swarm db import ~/.ethereum/swarm/bzz-KEY/chunks -`,
  515. },
  516. {
  517. Action: dbClean,
  518. CustomHelpTemplate: helpTemplate,
  519. Name: "clean",
  520. Usage: "remove corrupt entries from a local chunk database",
  521. ArgsUsage: "<chunkdb>",
  522. Description: "Remove corrupt entries from a local chunk database",
  523. },
  524. },
  525. },
  526. // See config.go
  527. DumpConfigCommand,
  528. }
  529. // append a hidden help subcommand to all commands that have subcommands
  530. // if a help command was already defined above, that one will take precedence.
  531. addDefaultHelpSubcommands(app.Commands)
  532. sort.Sort(cli.CommandsByName(app.Commands))
  533. app.Flags = []cli.Flag{
  534. utils.IdentityFlag,
  535. utils.DataDirFlag,
  536. utils.BootnodesFlag,
  537. utils.KeyStoreDirFlag,
  538. utils.ListenPortFlag,
  539. utils.NoDiscoverFlag,
  540. utils.DiscoveryV5Flag,
  541. utils.NetrestrictFlag,
  542. utils.NodeKeyFileFlag,
  543. utils.NodeKeyHexFlag,
  544. utils.MaxPeersFlag,
  545. utils.NATFlag,
  546. utils.IPCDisabledFlag,
  547. utils.IPCPathFlag,
  548. utils.PasswordFileFlag,
  549. // bzzd-specific flags
  550. CorsStringFlag,
  551. EnsAPIFlag,
  552. SwarmTomlConfigPathFlag,
  553. SwarmSwapEnabledFlag,
  554. SwarmSwapAPIFlag,
  555. SwarmSyncDisabledFlag,
  556. SwarmSyncUpdateDelay,
  557. SwarmMaxStreamPeerServersFlag,
  558. SwarmLightNodeEnabled,
  559. SwarmDeliverySkipCheckFlag,
  560. SwarmListenAddrFlag,
  561. SwarmPortFlag,
  562. SwarmAccountFlag,
  563. SwarmNetworkIdFlag,
  564. ChequebookAddrFlag,
  565. // upload flags
  566. SwarmApiFlag,
  567. SwarmRecursiveFlag,
  568. SwarmWantManifestFlag,
  569. SwarmUploadDefaultPath,
  570. SwarmUpFromStdinFlag,
  571. SwarmUploadMimeType,
  572. // storage flags
  573. SwarmStorePath,
  574. SwarmStoreCapacity,
  575. SwarmStoreCacheCapacity,
  576. }
  577. rpcFlags := []cli.Flag{
  578. utils.WSEnabledFlag,
  579. utils.WSListenAddrFlag,
  580. utils.WSPortFlag,
  581. utils.WSApiFlag,
  582. utils.WSAllowedOriginsFlag,
  583. }
  584. app.Flags = append(app.Flags, rpcFlags...)
  585. app.Flags = append(app.Flags, debug.Flags...)
  586. app.Flags = append(app.Flags, swarmmetrics.Flags...)
  587. app.Flags = append(app.Flags, tracing.Flags...)
  588. app.Before = func(ctx *cli.Context) error {
  589. runtime.GOMAXPROCS(runtime.NumCPU())
  590. if err := debug.Setup(ctx, ""); err != nil {
  591. return err
  592. }
  593. swarmmetrics.Setup(ctx)
  594. tracing.Setup(ctx)
  595. return nil
  596. }
  597. app.After = func(ctx *cli.Context) error {
  598. debug.Exit()
  599. return nil
  600. }
  601. }
  602. func main() {
  603. if err := app.Run(os.Args); err != nil {
  604. fmt.Fprintln(os.Stderr, err)
  605. os.Exit(1)
  606. }
  607. }
  608. func keys(ctx *cli.Context) error {
  609. privateKey := getPrivKey(ctx)
  610. pub := hex.EncodeToString(crypto.FromECDSAPub(&privateKey.PublicKey))
  611. pubCompressed := hex.EncodeToString(crypto.CompressPubkey(&privateKey.PublicKey))
  612. if !ctx.Bool(SwarmCompressedFlag.Name) {
  613. fmt.Println(fmt.Sprintf("publicKey=%s", pub))
  614. }
  615. fmt.Println(fmt.Sprintf("publicKeyCompressed=%s", pubCompressed))
  616. return nil
  617. }
  618. func version(ctx *cli.Context) error {
  619. fmt.Println(strings.Title(clientIdentifier))
  620. fmt.Println("Version:", sv.VersionWithMeta)
  621. if gitCommit != "" {
  622. fmt.Println("Git Commit:", gitCommit)
  623. }
  624. fmt.Println("Go Version:", runtime.Version())
  625. fmt.Println("OS:", runtime.GOOS)
  626. return nil
  627. }
  628. func bzzd(ctx *cli.Context) error {
  629. //build a valid bzzapi.Config from all available sources:
  630. //default config, file config, command line and env vars
  631. bzzconfig, err := buildConfig(ctx)
  632. if err != nil {
  633. utils.Fatalf("unable to configure swarm: %v", err)
  634. }
  635. cfg := defaultNodeConfig
  636. //pss operates on ws
  637. cfg.WSModules = append(cfg.WSModules, "pss")
  638. //geth only supports --datadir via command line
  639. //in order to be consistent within swarm, if we pass --datadir via environment variable
  640. //or via config file, we get the same directory for geth and swarm
  641. if _, err := os.Stat(bzzconfig.Path); err == nil {
  642. cfg.DataDir = bzzconfig.Path
  643. }
  644. //optionally set the bootnodes before configuring the node
  645. setSwarmBootstrapNodes(ctx, &cfg)
  646. //setup the ethereum node
  647. utils.SetNodeConfig(ctx, &cfg)
  648. stack, err := node.New(&cfg)
  649. if err != nil {
  650. utils.Fatalf("can't create node: %v", err)
  651. }
  652. //a few steps need to be done after the config phase is completed,
  653. //due to overriding behavior
  654. initSwarmNode(bzzconfig, stack, ctx)
  655. //register BZZ as node.Service in the ethereum node
  656. registerBzzService(bzzconfig, stack)
  657. //start the node
  658. utils.StartNode(stack)
  659. go func() {
  660. sigc := make(chan os.Signal, 1)
  661. signal.Notify(sigc, syscall.SIGTERM)
  662. defer signal.Stop(sigc)
  663. <-sigc
  664. log.Info("Got sigterm, shutting swarm down...")
  665. stack.Stop()
  666. }()
  667. stack.Wait()
  668. return nil
  669. }
  670. func registerBzzService(bzzconfig *bzzapi.Config, stack *node.Node) {
  671. //define the swarm service boot function
  672. boot := func(_ *node.ServiceContext) (node.Service, error) {
  673. // In production, mockStore must be always nil.
  674. return swarm.NewSwarm(bzzconfig, nil)
  675. }
  676. //register within the ethereum node
  677. if err := stack.Register(boot); err != nil {
  678. utils.Fatalf("Failed to register the Swarm service: %v", err)
  679. }
  680. }
  681. func getAccount(bzzaccount string, ctx *cli.Context, stack *node.Node) *ecdsa.PrivateKey {
  682. //an account is mandatory
  683. if bzzaccount == "" {
  684. utils.Fatalf(SWARM_ERR_NO_BZZACCOUNT)
  685. }
  686. // Try to load the arg as a hex key file.
  687. if key, err := crypto.LoadECDSA(bzzaccount); err == nil {
  688. log.Info("Swarm account key loaded", "address", crypto.PubkeyToAddress(key.PublicKey))
  689. return key
  690. }
  691. // Otherwise try getting it from the keystore.
  692. am := stack.AccountManager()
  693. ks := am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
  694. return decryptStoreAccount(ks, bzzaccount, utils.MakePasswordList(ctx))
  695. }
  696. // getPrivKey returns the private key of the specified bzzaccount
  697. // Used only by client commands, such as `feed`
  698. func getPrivKey(ctx *cli.Context) *ecdsa.PrivateKey {
  699. // booting up the swarm node just as we do in bzzd action
  700. bzzconfig, err := buildConfig(ctx)
  701. if err != nil {
  702. utils.Fatalf("unable to configure swarm: %v", err)
  703. }
  704. cfg := defaultNodeConfig
  705. if _, err := os.Stat(bzzconfig.Path); err == nil {
  706. cfg.DataDir = bzzconfig.Path
  707. }
  708. utils.SetNodeConfig(ctx, &cfg)
  709. stack, err := node.New(&cfg)
  710. if err != nil {
  711. utils.Fatalf("can't create node: %v", err)
  712. }
  713. return getAccount(bzzconfig.BzzAccount, ctx, stack)
  714. }
  715. func decryptStoreAccount(ks *keystore.KeyStore, account string, passwords []string) *ecdsa.PrivateKey {
  716. var a accounts.Account
  717. var err error
  718. if common.IsHexAddress(account) {
  719. a, err = ks.Find(accounts.Account{Address: common.HexToAddress(account)})
  720. } else if ix, ixerr := strconv.Atoi(account); ixerr == nil && ix > 0 {
  721. if accounts := ks.Accounts(); len(accounts) > ix {
  722. a = accounts[ix]
  723. } else {
  724. err = fmt.Errorf("index %d higher than number of accounts %d", ix, len(accounts))
  725. }
  726. } else {
  727. utils.Fatalf("Can't find swarm account key %s", account)
  728. }
  729. if err != nil {
  730. utils.Fatalf("Can't find swarm account key: %v - Is the provided bzzaccount(%s) from the right datadir/Path?", err, account)
  731. }
  732. keyjson, err := ioutil.ReadFile(a.URL.Path)
  733. if err != nil {
  734. utils.Fatalf("Can't load swarm account key: %v", err)
  735. }
  736. for i := 0; i < 3; i++ {
  737. password := getPassPhrase(fmt.Sprintf("Unlocking swarm account %s [%d/3]", a.Address.Hex(), i+1), i, passwords)
  738. key, err := keystore.DecryptKey(keyjson, password)
  739. if err == nil {
  740. return key.PrivateKey
  741. }
  742. }
  743. utils.Fatalf("Can't decrypt swarm account key")
  744. return nil
  745. }
  746. // getPassPhrase retrieves the password associated with bzz account, either by fetching
  747. // from a list of pre-loaded passwords, or by requesting it interactively from user.
  748. func getPassPhrase(prompt string, i int, passwords []string) string {
  749. // non-interactive
  750. if len(passwords) > 0 {
  751. if i < len(passwords) {
  752. return passwords[i]
  753. }
  754. return passwords[len(passwords)-1]
  755. }
  756. // fallback to interactive mode
  757. if prompt != "" {
  758. fmt.Println(prompt)
  759. }
  760. password, err := console.Stdin.PromptPassword("Passphrase: ")
  761. if err != nil {
  762. utils.Fatalf("Failed to read passphrase: %v", err)
  763. }
  764. return password
  765. }
  766. // addDefaultHelpSubcommand scans through defined CLI commands and adds
  767. // a basic help subcommand to each
  768. // if a help command is already defined, it will take precedence over the default.
  769. func addDefaultHelpSubcommands(commands []cli.Command) {
  770. for i := range commands {
  771. cmd := &commands[i]
  772. if cmd.Subcommands != nil {
  773. cmd.Subcommands = append(cmd.Subcommands, defaultSubcommandHelp)
  774. addDefaultHelpSubcommands(cmd.Subcommands)
  775. }
  776. }
  777. }
  778. func setSwarmBootstrapNodes(ctx *cli.Context, cfg *node.Config) {
  779. if ctx.GlobalIsSet(utils.BootnodesFlag.Name) || ctx.GlobalIsSet(utils.BootnodesV4Flag.Name) {
  780. return
  781. }
  782. cfg.P2P.BootstrapNodes = []*enode.Node{}
  783. for _, url := range SwarmBootnodes {
  784. node, err := enode.ParseV4(url)
  785. if err != nil {
  786. log.Error("Bootstrap URL invalid", "enode", url, "err", err)
  787. }
  788. cfg.P2P.BootstrapNodes = append(cfg.P2P.BootstrapNodes, node)
  789. }
  790. log.Debug("added default swarm bootnodes", "length", len(cfg.P2P.BootstrapNodes))
  791. }