github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/swarm/access.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:31</date> 10 //</624342604904927232> 11 12 package main 13 14 import ( 15 "crypto/rand" 16 "encoding/json" 17 "fmt" 18 "io" 19 "io/ioutil" 20 "strings" 21 22 "github.com/ethereum/go-ethereum/cmd/utils" 23 "github.com/ethereum/go-ethereum/swarm/api" 24 "github.com/ethereum/go-ethereum/swarm/api/client" 25 "gopkg.in/urfave/cli.v1" 26 ) 27 28 var salt = make([]byte, 32) 29 30 func init() { 31 if _, err := io.ReadFull(rand.Reader, salt); err != nil { 32 panic("reading from crypto/rand failed: " + err.Error()) 33 } 34 } 35 36 func accessNewPass(ctx *cli.Context) { 37 args := ctx.Args() 38 if len(args) != 1 { 39 utils.Fatalf("Expected 1 argument - the ref") 40 } 41 42 var ( 43 ae *api.AccessEntry 44 accessKey []byte 45 err error 46 ref = args[0] 47 password = getPassPhrase("", 0, makePasswordList(ctx)) 48 dryRun = ctx.Bool(SwarmDryRunFlag.Name) 49 ) 50 accessKey, ae, err = api.DoPasswordNew(ctx, password, salt) 51 if err != nil { 52 utils.Fatalf("error getting session key: %v", err) 53 } 54 m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae) 55 if dryRun { 56 err = printManifests(m, nil) 57 if err != nil { 58 utils.Fatalf("had an error printing the manifests: %v", err) 59 } 60 } else { 61 utils.Fatalf("uploading manifests") 62 err = uploadManifests(ctx, m, nil) 63 if err != nil { 64 utils.Fatalf("had an error uploading the manifests: %v", err) 65 } 66 } 67 } 68 69 func accessNewPK(ctx *cli.Context) { 70 args := ctx.Args() 71 if len(args) != 1 { 72 utils.Fatalf("Expected 1 argument - the ref") 73 } 74 75 var ( 76 ae *api.AccessEntry 77 sessionKey []byte 78 err error 79 ref = args[0] 80 privateKey = getPrivKey(ctx) 81 granteePublicKey = ctx.String(SwarmAccessGrantKeyFlag.Name) 82 dryRun = ctx.Bool(SwarmDryRunFlag.Name) 83 ) 84 sessionKey, ae, err = api.DoPKNew(ctx, privateKey, granteePublicKey, salt) 85 if err != nil { 86 utils.Fatalf("error getting session key: %v", err) 87 } 88 m, err := api.GenerateAccessControlManifest(ctx, ref, sessionKey, ae) 89 if dryRun { 90 err = printManifests(m, nil) 91 if err != nil { 92 utils.Fatalf("had an error printing the manifests: %v", err) 93 } 94 } else { 95 err = uploadManifests(ctx, m, nil) 96 if err != nil { 97 utils.Fatalf("had an error uploading the manifests: %v", err) 98 } 99 } 100 } 101 102 func accessNewACT(ctx *cli.Context) { 103 args := ctx.Args() 104 if len(args) != 1 { 105 utils.Fatalf("Expected 1 argument - the ref") 106 } 107 108 var ( 109 ae *api.AccessEntry 110 actManifest *api.Manifest 111 accessKey []byte 112 err error 113 ref = args[0] 114 grantees = []string{} 115 actFilename = ctx.String(SwarmAccessGrantKeysFlag.Name) 116 privateKey = getPrivKey(ctx) 117 dryRun = ctx.Bool(SwarmDryRunFlag.Name) 118 ) 119 120 bytes, err := ioutil.ReadFile(actFilename) 121 if err != nil { 122 utils.Fatalf("had an error reading the grantee public key list") 123 } 124 grantees = strings.Split(string(bytes), "\n") 125 accessKey, ae, actManifest, err = api.DoACTNew(ctx, privateKey, salt, grantees) 126 if err != nil { 127 utils.Fatalf("error generating ACT manifest: %v", err) 128 } 129 130 if err != nil { 131 utils.Fatalf("error getting session key: %v", err) 132 } 133 m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae) 134 if err != nil { 135 utils.Fatalf("error generating root access manifest: %v", err) 136 } 137 138 if dryRun { 139 err = printManifests(m, actManifest) 140 if err != nil { 141 utils.Fatalf("had an error printing the manifests: %v", err) 142 } 143 } else { 144 err = uploadManifests(ctx, m, actManifest) 145 if err != nil { 146 utils.Fatalf("had an error uploading the manifests: %v", err) 147 } 148 } 149 } 150 151 func printManifests(rootAccessManifest, actManifest *api.Manifest) error { 152 js, err := json.Marshal(rootAccessManifest) 153 if err != nil { 154 return err 155 } 156 fmt.Println(string(js)) 157 158 if actManifest != nil { 159 js, err := json.Marshal(actManifest) 160 if err != nil { 161 return err 162 } 163 fmt.Println(string(js)) 164 } 165 return nil 166 } 167 168 func uploadManifests(ctx *cli.Context, rootAccessManifest, actManifest *api.Manifest) error { 169 bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") 170 client := client.NewClient(bzzapi) 171 172 var ( 173 key string 174 err error 175 ) 176 if actManifest != nil { 177 key, err = client.UploadManifest(actManifest, false) 178 if err != nil { 179 return err 180 } 181 182 rootAccessManifest.Entries[0].Access.Act = key 183 } 184 key, err = client.UploadManifest(rootAccessManifest, false) 185 if err != nil { 186 return err 187 } 188 fmt.Println(key) 189 return nil 190 } 191 192 //makepasswordlist从global--password标志指定的文件中读取密码行 193 // 194 //此函数是utils.makepasswordlist的分支,用于查找子命令的CLI上下文。 195 //函数ctx.setglobal未设置可访问的全局标志值 196 // 197 func makePasswordList(ctx *cli.Context) []string { 198 path := ctx.GlobalString(utils.PasswordFileFlag.Name) 199 if path == "" { 200 path = ctx.String(utils.PasswordFileFlag.Name) 201 if path == "" { 202 return nil 203 } 204 } 205 text, err := ioutil.ReadFile(path) 206 if err != nil { 207 utils.Fatalf("Failed to read password file: %v", err) 208 } 209 lines := strings.Split(string(text), "\n") 210 // 211 for i := range lines { 212 lines[i] = strings.TrimRight(lines[i], "\r") 213 } 214 return lines 215 } 216