123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package api
- import (
- "crypto/ecdsa"
- "fmt"
- "os"
- "path/filepath"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/contracts/ens"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/log"
- "github.com/ethereum/go-ethereum/node"
- "github.com/ethereum/go-ethereum/swarm/network"
- "github.com/ethereum/go-ethereum/swarm/services/swap"
- "github.com/ethereum/go-ethereum/swarm/storage"
- )
- const (
- DefaultHTTPListenAddr = "127.0.0.1"
- DefaultHTTPPort = "8500"
- )
- type Config struct {
-
- *storage.StoreParams
- *storage.ChunkerParams
- *network.HiveParams
- Swap *swap.SwapParams
- *network.SyncParams
- Contract common.Address
- EnsRoot common.Address
- EnsAPIs []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
- }
- func NewDefaultConfig() (self *Config) {
- self = &Config{
- StoreParams: storage.NewDefaultStoreParams(),
- ChunkerParams: storage.NewChunkerParams(),
- HiveParams: network.NewDefaultHiveParams(),
- SyncParams: network.NewDefaultSyncParams(),
- Swap: swap.NewDefaultSwapParams(),
- ListenAddr: DefaultHTTPListenAddr,
- Port: DefaultHTTPPort,
- Path: node.DefaultDataDir(),
- EnsAPIs: nil,
- EnsRoot: ens.TestNetAddress,
- NetworkId: network.NetworkId,
- SwapEnabled: false,
- SyncEnabled: true,
- SwapApi: "",
- BootNodes: "",
- }
- return
- }
- func (self *Config) Init(prvKey *ecdsa.PrivateKey) {
- address := crypto.PubkeyToAddress(prvKey.PublicKey)
- self.Path = filepath.Join(self.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
- err := os.MkdirAll(self.Path, os.ModePerm)
- if err != nil {
- log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
- return
- }
- pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
- pubkeyhex := common.ToHex(pubkey)
- keyhex := crypto.Keccak256Hash(pubkey).Hex()
- self.PublicKey = pubkeyhex
- self.BzzKey = keyhex
- self.Swap.Init(self.Contract, prvKey)
- self.SyncParams.Init(self.Path)
- self.HiveParams.Init(self.Path)
- self.StoreParams.Init(self.Path)
- }
|