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