github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/swarm/api/config.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/swarm/network"
    30  	"github.com/ethereum/go-ethereum/swarm/services/swap"
    31  	"github.com/ethereum/go-ethereum/swarm/storage"
    32  )
    33  
    34  const (
    35  	port = "8500"
    36  )
    37  
    38  var (
    39  	ensRootAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010")
    40  )
    41  
    42  // separate bzz directories
    43  // allow several bzz nodes running in parallel
    44  type Config struct {
    45  	// serialised/persisted fields
    46  	*storage.StoreParams
    47  	*storage.ChunkerParams
    48  	*network.HiveParams
    49  	Swap *swap.SwapParams
    50  	*network.SyncParams
    51  	Path      string
    52  	Port      string
    53  	PublicKey string
    54  	BzzKey    string
    55  	EnsRoot   common.Address
    56  	NetworkId uint64
    57  }
    58  
    59  // config is agnostic to where private key is coming from
    60  // so managing accounts is outside swarm and left to wrappers
    61  func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, networkId uint64) (self *Config, err error) {
    62  	address := crypto.PubkeyToAddress(prvKey.PublicKey) // default beneficiary address
    63  	dirpath := filepath.Join(path, "bzz-"+common.Bytes2Hex(address.Bytes()))
    64  	err = os.MkdirAll(dirpath, os.ModePerm)
    65  	if err != nil {
    66  		return
    67  	}
    68  	confpath := filepath.Join(dirpath, "config.json")
    69  	var data []byte
    70  	pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
    71  	pubkeyhex := common.ToHex(pubkey)
    72  	keyhex := crypto.Keccak256Hash(pubkey).Hex()
    73  
    74  	self = &Config{
    75  		SyncParams:    network.NewSyncParams(dirpath),
    76  		HiveParams:    network.NewHiveParams(dirpath),
    77  		ChunkerParams: storage.NewChunkerParams(),
    78  		StoreParams:   storage.NewStoreParams(dirpath),
    79  		Port:          port,
    80  		Path:          dirpath,
    81  		Swap:          swap.DefaultSwapParams(contract, prvKey),
    82  		PublicKey:     pubkeyhex,
    83  		BzzKey:        keyhex,
    84  		EnsRoot:       ensRootAddress,
    85  		NetworkId:     networkId,
    86  	}
    87  	data, err = ioutil.ReadFile(confpath)
    88  	if err != nil {
    89  		if !os.IsNotExist(err) {
    90  			return
    91  		}
    92  		// file does not exist
    93  		// write out config file
    94  		err = self.Save()
    95  		if err != nil {
    96  			err = fmt.Errorf("error writing config: %v", err)
    97  		}
    98  		return
    99  	}
   100  	// file exists, deserialise
   101  	err = json.Unmarshal(data, self)
   102  	if err != nil {
   103  		return nil, fmt.Errorf("unable to parse config: %v", err)
   104  	}
   105  	// check public key
   106  	if pubkeyhex != self.PublicKey {
   107  		return nil, fmt.Errorf("public key does not match the one in the config file %v != %v", pubkeyhex, self.PublicKey)
   108  	}
   109  	if keyhex != self.BzzKey {
   110  		return nil, fmt.Errorf("bzz key does not match the one in the config file %v != %v", keyhex, self.BzzKey)
   111  	}
   112  	self.Swap.SetKey(prvKey)
   113  
   114  	if (self.EnsRoot == common.Address{}) {
   115  		self.EnsRoot = ensRootAddress
   116  	}
   117  
   118  	return
   119  }
   120  
   121  func (self *Config) Save() error {
   122  	data, err := json.MarshalIndent(self, "", "    ")
   123  	if err != nil {
   124  		return err
   125  	}
   126  	err = os.MkdirAll(self.Path, os.ModePerm)
   127  	if err != nil {
   128  		return err
   129  	}
   130  	confpath := filepath.Join(self.Path, "config.json")
   131  	return ioutil.WriteFile(confpath, data, os.ModePerm)
   132  }