2016-08-29 14:18:00 -05:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-06-17 17:25:39 -05:00
|
|
|
"github.com/ethereum/go-ethereum/contracts/ens"
|
2016-08-29 14:18:00 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-12-11 15:56:06 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2016-08-29 14:18:00 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/network"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/services/swap"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-05-22 01:56:40 -05:00
|
|
|
DefaultHTTPListenAddr = "127.0.0.1"
|
|
|
|
DefaultHTTPPort = "8500"
|
2016-08-29 14:18:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// separate bzz directories
|
|
|
|
// allow several bzz nodes running in parallel
|
|
|
|
type Config struct {
|
|
|
|
// serialised/persisted fields
|
|
|
|
*storage.StoreParams
|
|
|
|
*storage.ChunkerParams
|
|
|
|
*network.HiveParams
|
|
|
|
Swap *swap.SwapParams
|
|
|
|
*network.SyncParams
|
2017-12-11 15:56:06 -06:00
|
|
|
Contract common.Address
|
|
|
|
EnsRoot common.Address
|
|
|
|
EnsApi string
|
|
|
|
Path string
|
|
|
|
ListenAddr string
|
|
|
|
Port string
|
|
|
|
PublicKey string
|
|
|
|
BzzKey string
|
|
|
|
NetworkId uint64
|
|
|
|
SwapEnabled bool
|
|
|
|
SyncEnabled bool
|
|
|
|
SwapApi string
|
|
|
|
Cors string
|
|
|
|
BzzAccount string
|
|
|
|
BootNodes string
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
2017-12-11 15:56:06 -06:00
|
|
|
//create a default config with all parameters to set to defaults
|
|
|
|
func NewDefaultConfig() (self *Config) {
|
2016-08-29 14:18:00 -05:00
|
|
|
|
|
|
|
self = &Config{
|
2017-12-11 15:56:06 -06:00
|
|
|
StoreParams: storage.NewDefaultStoreParams(),
|
2016-08-29 14:18:00 -05:00
|
|
|
ChunkerParams: storage.NewChunkerParams(),
|
2017-12-11 15:56:06 -06:00
|
|
|
HiveParams: network.NewDefaultHiveParams(),
|
|
|
|
SyncParams: network.NewDefaultSyncParams(),
|
|
|
|
Swap: swap.NewDefaultSwapParams(),
|
2017-05-22 01:56:40 -05:00
|
|
|
ListenAddr: DefaultHTTPListenAddr,
|
|
|
|
Port: DefaultHTTPPort,
|
2017-12-11 15:56:06 -06:00
|
|
|
Path: node.DefaultDataDir(),
|
|
|
|
EnsApi: node.DefaultIPCEndpoint("geth"),
|
2017-06-17 17:25:39 -05:00
|
|
|
EnsRoot: ens.TestNetAddress,
|
2017-12-11 15:56:06 -06:00
|
|
|
NetworkId: network.NetworkId,
|
|
|
|
SwapEnabled: false,
|
|
|
|
SyncEnabled: true,
|
|
|
|
SwapApi: "",
|
|
|
|
BootNodes: "",
|
2017-01-26 13:33:39 -06:00
|
|
|
}
|
|
|
|
|
2017-12-11 15:56:06 -06:00
|
|
|
return
|
|
|
|
}
|
2017-01-26 13:33:39 -06:00
|
|
|
|
2017-12-11 15:56:06 -06:00
|
|
|
//some config params need to be initialized after the complete
|
|
|
|
//config building phase is completed (e.g. due to overriding flags)
|
|
|
|
func (self *Config) Init(prvKey *ecdsa.PrivateKey) {
|
2017-01-26 13:33:39 -06:00
|
|
|
|
2017-12-11 15:56:06 -06:00
|
|
|
address := crypto.PubkeyToAddress(prvKey.PublicKey)
|
|
|
|
self.Path = filepath.Join(self.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
|
|
|
|
err := os.MkdirAll(self.Path, os.ModePerm)
|
2016-08-29 14:18:00 -05:00
|
|
|
if err != nil {
|
2017-12-11 15:56:06 -06:00
|
|
|
log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
|
|
|
|
return
|
2017-01-26 13:33:39 -06:00
|
|
|
}
|
|
|
|
|
2017-12-11 15:56:06 -06:00
|
|
|
pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
|
|
|
|
pubkeyhex := common.ToHex(pubkey)
|
|
|
|
keyhex := crypto.Keccak256Hash(pubkey).Hex()
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2017-12-11 15:56:06 -06:00
|
|
|
self.PublicKey = pubkeyhex
|
|
|
|
self.BzzKey = keyhex
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2017-12-11 15:56:06 -06:00
|
|
|
self.Swap.Init(self.Contract, prvKey)
|
|
|
|
self.SyncParams.Init(self.Path)
|
|
|
|
self.HiveParams.Init(self.Path)
|
|
|
|
self.StoreParams.Init(self.Path)
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|