github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/swarm/access.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊的分布希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  package main
    25  
    26  import (
    27  	"crypto/rand"
    28  	"encoding/json"
    29  	"fmt"
    30  	"io"
    31  	"io/ioutil"
    32  	"strings"
    33  
    34  	"github.com/ethereum/go-ethereum/cmd/utils"
    35  	"github.com/ethereum/go-ethereum/swarm/api"
    36  	"github.com/ethereum/go-ethereum/swarm/api/client"
    37  	"gopkg.in/urfave/cli.v1"
    38  )
    39  
    40  var salt = make([]byte, 32)
    41  
    42  func init() {
    43  	if _, err := io.ReadFull(rand.Reader, salt); err != nil {
    44  		panic("reading from crypto/rand failed: " + err.Error())
    45  	}
    46  }
    47  
    48  func accessNewPass(ctx *cli.Context) {
    49  	args := ctx.Args()
    50  	if len(args) != 1 {
    51  		utils.Fatalf("Expected 1 argument - the ref")
    52  	}
    53  
    54  	var (
    55  		ae        *api.AccessEntry
    56  		accessKey []byte
    57  		err       error
    58  		ref       = args[0]
    59  		password  = getPassPhrase("", 0, makePasswordList(ctx))
    60  		dryRun    = ctx.Bool(SwarmDryRunFlag.Name)
    61  	)
    62  	accessKey, ae, err = api.DoPasswordNew(ctx, password, salt)
    63  	if err != nil {
    64  		utils.Fatalf("error getting session key: %v", err)
    65  	}
    66  	m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae)
    67  	if dryRun {
    68  		err = printManifests(m, nil)
    69  		if err != nil {
    70  			utils.Fatalf("had an error printing the manifests: %v", err)
    71  		}
    72  	} else {
    73  		utils.Fatalf("uploading manifests")
    74  		err = uploadManifests(ctx, m, nil)
    75  		if err != nil {
    76  			utils.Fatalf("had an error uploading the manifests: %v", err)
    77  		}
    78  	}
    79  }
    80  
    81  func accessNewPK(ctx *cli.Context) {
    82  	args := ctx.Args()
    83  	if len(args) != 1 {
    84  		utils.Fatalf("Expected 1 argument - the ref")
    85  	}
    86  
    87  	var (
    88  		ae               *api.AccessEntry
    89  		sessionKey       []byte
    90  		err              error
    91  		ref              = args[0]
    92  		privateKey       = getPrivKey(ctx)
    93  		granteePublicKey = ctx.String(SwarmAccessGrantKeyFlag.Name)
    94  		dryRun           = ctx.Bool(SwarmDryRunFlag.Name)
    95  	)
    96  	sessionKey, ae, err = api.DoPKNew(ctx, privateKey, granteePublicKey, salt)
    97  	if err != nil {
    98  		utils.Fatalf("error getting session key: %v", err)
    99  	}
   100  	m, err := api.GenerateAccessControlManifest(ctx, ref, sessionKey, ae)
   101  	if dryRun {
   102  		err = printManifests(m, nil)
   103  		if err != nil {
   104  			utils.Fatalf("had an error printing the manifests: %v", err)
   105  		}
   106  	} else {
   107  		err = uploadManifests(ctx, m, nil)
   108  		if err != nil {
   109  			utils.Fatalf("had an error uploading the manifests: %v", err)
   110  		}
   111  	}
   112  }
   113  
   114  func accessNewACT(ctx *cli.Context) {
   115  	args := ctx.Args()
   116  	if len(args) != 1 {
   117  		utils.Fatalf("Expected 1 argument - the ref")
   118  	}
   119  
   120  	var (
   121  		ae          *api.AccessEntry
   122  		actManifest *api.Manifest
   123  		accessKey   []byte
   124  		err         error
   125  		ref         = args[0]
   126  		grantees    = []string{}
   127  		actFilename = ctx.String(SwarmAccessGrantKeysFlag.Name)
   128  		privateKey  = getPrivKey(ctx)
   129  		dryRun      = ctx.Bool(SwarmDryRunFlag.Name)
   130  	)
   131  
   132  	bytes, err := ioutil.ReadFile(actFilename)
   133  	if err != nil {
   134  		utils.Fatalf("had an error reading the grantee public key list")
   135  	}
   136  	grantees = strings.Split(string(bytes), "\n")
   137  	accessKey, ae, actManifest, err = api.DoACTNew(ctx, privateKey, salt, grantees)
   138  	if err != nil {
   139  		utils.Fatalf("error generating ACT manifest: %v", err)
   140  	}
   141  
   142  	if err != nil {
   143  		utils.Fatalf("error getting session key: %v", err)
   144  	}
   145  	m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae)
   146  	if err != nil {
   147  		utils.Fatalf("error generating root access manifest: %v", err)
   148  	}
   149  
   150  	if dryRun {
   151  		err = printManifests(m, actManifest)
   152  		if err != nil {
   153  			utils.Fatalf("had an error printing the manifests: %v", err)
   154  		}
   155  	} else {
   156  		err = uploadManifests(ctx, m, actManifest)
   157  		if err != nil {
   158  			utils.Fatalf("had an error uploading the manifests: %v", err)
   159  		}
   160  	}
   161  }
   162  
   163  func printManifests(rootAccessManifest, actManifest *api.Manifest) error {
   164  	js, err := json.Marshal(rootAccessManifest)
   165  	if err != nil {
   166  		return err
   167  	}
   168  	fmt.Println(string(js))
   169  
   170  	if actManifest != nil {
   171  		js, err := json.Marshal(actManifest)
   172  		if err != nil {
   173  			return err
   174  		}
   175  		fmt.Println(string(js))
   176  	}
   177  	return nil
   178  }
   179  
   180  func uploadManifests(ctx *cli.Context, rootAccessManifest, actManifest *api.Manifest) error {
   181  	bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
   182  	client := client.NewClient(bzzapi)
   183  
   184  	var (
   185  		key string
   186  		err error
   187  	)
   188  	if actManifest != nil {
   189  		key, err = client.UploadManifest(actManifest, false)
   190  		if err != nil {
   191  			return err
   192  		}
   193  
   194  		rootAccessManifest.Entries[0].Access.Act = key
   195  	}
   196  	key, err = client.UploadManifest(rootAccessManifest, false)
   197  	if err != nil {
   198  		return err
   199  	}
   200  	fmt.Println(key)
   201  	return nil
   202  }
   203  
   204  //makepasswordlist从global--password标志指定的文件中读取密码行
   205  //
   206  //此函数是utils.makepasswordlist的分支,用于查找子命令的CLI上下文。
   207  //函数ctx.setglobal未设置可访问的全局标志值
   208  //
   209  func makePasswordList(ctx *cli.Context) []string {
   210  	path := ctx.GlobalString(utils.PasswordFileFlag.Name)
   211  	if path == "" {
   212  		path = ctx.String(utils.PasswordFileFlag.Name)
   213  		if path == "" {
   214  			return nil
   215  		}
   216  	}
   217  	text, err := ioutil.ReadFile(path)
   218  	if err != nil {
   219  		utils.Fatalf("Failed to read password file: %v", err)
   220  	}
   221  	lines := strings.Split(string(text), "\n")
   222  //
   223  	for i := range lines {
   224  		lines[i] = strings.TrimRight(lines[i], "\r")
   225  	}
   226  	return lines
   227  }