github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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/atheioschain/go-atheios/common"
    28  	"github.com/atheioschain/go-atheios/crypto"
    29  	"github.com/atheioschain/go-atheios/swarm/network"
    30  	"github.com/atheioschain/go-atheios/swarm/services/swap"
    31  	"github.com/atheioschain/go-atheios/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  
    89  	// if not set in function param, then set default for swarm network, will be overwritten by config file if present
    90  	if networkId == 0 {
    91  		self.NetworkId = network.NetworkId
    92  	}
    93  
    94  	if err != nil {
    95  		if !os.IsNotExist(err) {
    96  			return
    97  		}
    98  
    99  		// file does not exist
   100  		// write out config file
   101  		err = self.Save()
   102  		if err != nil {
   103  			err = fmt.Errorf("error writing config: %v", err)
   104  		}
   105  		return
   106  	}
   107  
   108  	// file exists, deserialise
   109  	err = json.Unmarshal(data, self)
   110  	if err != nil {
   111  		return nil, fmt.Errorf("unable to parse config: %v", err)
   112  	}
   113  	// check public key
   114  	if pubkeyhex != self.PublicKey {
   115  		return nil, fmt.Errorf("public key does not match the one in the config file %v != %v", pubkeyhex, self.PublicKey)
   116  	}
   117  	if keyhex != self.BzzKey {
   118  		return nil, fmt.Errorf("bzz key does not match the one in the config file %v != %v", keyhex, self.BzzKey)
   119  	}
   120  
   121  	// if set in function param, replace id set from config file
   122  	if networkId != 0 {
   123  		self.NetworkId = networkId
   124  	}
   125  
   126  	self.Swap.SetKey(prvKey)
   127  
   128  	if (self.EnsRoot == common.Address{}) {
   129  		self.EnsRoot = ensRootAddress
   130  	}
   131  
   132  	return
   133  }
   134  
   135  func (self *Config) Save() error {
   136  	data, err := json.MarshalIndent(self, "", "    ")
   137  	if err != nil {
   138  		return err
   139  	}
   140  	err = os.MkdirAll(self.Path, os.ModePerm)
   141  	if err != nil {
   142  		return err
   143  	}
   144  	confpath := filepath.Join(self.Path, "config.json")
   145  	return ioutil.WriteFile(confpath, data, os.ModePerm)
   146  }