hash.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // Command bzzhash computes a swarm tree hash.
  17. package main
  18. import (
  19. "context"
  20. "encoding/hex"
  21. "fmt"
  22. "os"
  23. "github.com/ethereum/go-ethereum/cmd/utils"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/contracts/ens"
  26. "github.com/ethereum/go-ethereum/swarm/chunk"
  27. "github.com/ethereum/go-ethereum/swarm/storage"
  28. "gopkg.in/urfave/cli.v1"
  29. )
  30. var hashCommand = cli.Command{
  31. Action: hash,
  32. CustomHelpTemplate: helpTemplate,
  33. Name: "hash",
  34. Usage: "print the swarm hash of a file or directory",
  35. ArgsUsage: "<file>",
  36. Description: "Prints the swarm hash of file or directory",
  37. Subcommands: []cli.Command{
  38. {
  39. CustomHelpTemplate: helpTemplate,
  40. Name: "ens",
  41. Usage: "converts a swarm hash to an ens EIP1577 compatible CIDv1 hash",
  42. ArgsUsage: "<ref>",
  43. Description: "",
  44. Subcommands: []cli.Command{
  45. {
  46. Action: encodeEipHash,
  47. CustomHelpTemplate: helpTemplate,
  48. Name: "contenthash",
  49. Usage: "converts a swarm hash to an ens EIP1577 compatible CIDv1 hash",
  50. ArgsUsage: "<ref>",
  51. Description: "",
  52. },
  53. {
  54. Action: ensNodeHash,
  55. CustomHelpTemplate: helpTemplate,
  56. Name: "node",
  57. Usage: "converts an ens name to an ENS node hash",
  58. ArgsUsage: "<ref>",
  59. Description: "",
  60. },
  61. },
  62. },
  63. }}
  64. func hash(ctx *cli.Context) {
  65. args := ctx.Args()
  66. if len(args) < 1 {
  67. utils.Fatalf("Usage: swarm hash <file name>")
  68. }
  69. f, err := os.Open(args[0])
  70. if err != nil {
  71. utils.Fatalf("Error opening file " + args[1])
  72. }
  73. defer f.Close()
  74. stat, _ := f.Stat()
  75. fileStore := storage.NewFileStore(&storage.FakeChunkStore{}, storage.NewFileStoreParams(), chunk.NewTags())
  76. addr, _, err := fileStore.Store(context.TODO(), f, stat.Size(), false)
  77. if err != nil {
  78. utils.Fatalf("%v\n", err)
  79. } else {
  80. fmt.Printf("%v\n", addr)
  81. }
  82. }
  83. func ensNodeHash(ctx *cli.Context) {
  84. args := ctx.Args()
  85. if len(args) < 1 {
  86. utils.Fatalf("Usage: swarm hash ens node <ens name>")
  87. }
  88. ensName := args[0]
  89. hash := ens.EnsNode(ensName)
  90. stringHex := hex.EncodeToString(hash[:])
  91. fmt.Println(stringHex)
  92. }
  93. func encodeEipHash(ctx *cli.Context) {
  94. args := ctx.Args()
  95. if len(args) < 1 {
  96. utils.Fatalf("Usage: swarm hash ens <swarm hash>")
  97. }
  98. swarmHash := args[0]
  99. hash := common.HexToHash(swarmHash)
  100. ensHash, err := ens.EncodeSwarmHash(hash)
  101. if err != nil {
  102. utils.Fatalf("error converting swarm hash", err)
  103. }
  104. stringHex := hex.EncodeToString(ensHash)
  105. fmt.Println(stringHex)
  106. }