aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/config.go
blob: 3a87488cccbe2e76512c3a4373f713a728029805 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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"
    "time"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/hexutil"
    "github.com/ethereum/go-ethereum/contracts/ens"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/node"
    "github.com/ethereum/go-ethereum/p2p/enode"
    "github.com/ethereum/go-ethereum/swarm/network"
    "github.com/ethereum/go-ethereum/swarm/pss"
    "github.com/ethereum/go-ethereum/swarm/services/swap"
    "github.com/ethereum/go-ethereum/swarm/storage"
)

const (
    DefaultHTTPListenAddr = "127.0.0.1"
    DefaultHTTPPort       = "8500"
)

// separate bzz directories
// allow several bzz nodes running in parallel
type Config struct {
    // serialised/persisted fields
    *storage.FileStoreParams

    // LocalStore
    ChunkDbPath   string
    DbCapacity    uint64
    CacheCapacity uint
    BaseKey       []byte

    *network.HiveParams
    Swap                 *swap.LocalProfile
    Pss                  *pss.PssParams
    Contract             common.Address
    EnsRoot              common.Address
    EnsAPIs              []string
    Path                 string
    ListenAddr           string
    Port                 string
    PublicKey            string
    BzzKey               string
    Enode                *enode.Node `toml:"-"`
    NetworkID            uint64
    SwapEnabled          bool
    SyncEnabled          bool
    SyncingSkipCheck     bool
    DeliverySkipCheck    bool
    MaxStreamPeerServers int
    LightNodeEnabled     bool
    BootnodeMode         bool
    SyncUpdateDelay      time.Duration
    SwapAPI              string
    Cors                 string
    BzzAccount           string
    GlobalStoreAPI       string
    privateKey           *ecdsa.PrivateKey
}

//create a default config with all parameters to set to defaults
func NewConfig() (c *Config) {

    c = &Config{
        FileStoreParams:      storage.NewFileStoreParams(),
        HiveParams:           network.NewHiveParams(),
        Swap:                 swap.NewDefaultSwapParams(),
        Pss:                  pss.NewPssParams(),
        ListenAddr:           DefaultHTTPListenAddr,
        Port:                 DefaultHTTPPort,
        Path:                 node.DefaultDataDir(),
        EnsAPIs:              nil,
        EnsRoot:              ens.TestNetAddress,
        NetworkID:            network.DefaultNetworkID,
        SwapEnabled:          false,
        SyncEnabled:          true,
        SyncingSkipCheck:     false,
        MaxStreamPeerServers: 10000,
        DeliverySkipCheck:    true,
        SyncUpdateDelay:      15 * time.Second,
        SwapAPI:              "",
    }

    return
}

//some config params need to be initialized after the complete
//config building phase is completed (e.g. due to overriding flags)
func (c *Config) Init(prvKey *ecdsa.PrivateKey, nodeKey *ecdsa.PrivateKey) error {

    // create swarm dir and record key
    err := c.createAndSetPath(c.Path, prvKey)
    if err != nil {
        return fmt.Errorf("Error creating root swarm data directory: %v", err)
    }
    c.setKey(prvKey)

    // create the new enode record
    // signed with the ephemeral node key
    enodeParams := &network.EnodeParams{
        PrivateKey: prvKey,
        EnodeKey:   nodeKey,
        Lightnode:  c.LightNodeEnabled,
        Bootnode:   c.BootnodeMode,
    }
    c.Enode, err = network.NewEnode(enodeParams)
    if err != nil {
        return fmt.Errorf("Error creating enode: %v", err)
    }

    // initialize components that depend on the swarm instance's private key
    if c.SwapEnabled {
        c.Swap.Init(c.Contract, prvKey)
    }

    c.privateKey = prvKey
    c.ChunkDbPath = filepath.Join(c.Path, "chunks")
    c.BaseKey = common.FromHex(c.BzzKey)

    c.Pss = c.Pss.WithPrivateKey(c.privateKey)
    return nil
}

func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
    if c.privateKey != nil {
        privKey = c.privateKey
        c.privateKey = nil
    }
    return privKey
}

func (c *Config) setKey(prvKey *ecdsa.PrivateKey) {
    bzzkeybytes := network.PrivateKeyToBzzKey(prvKey)
    pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
    pubkeyhex := hexutil.Encode(pubkey)
    keyhex := hexutil.Encode(bzzkeybytes)

    c.privateKey = prvKey
    c.PublicKey = pubkeyhex
    c.BzzKey = keyhex
}

func (c *Config) createAndSetPath(datadirPath string, prvKey *ecdsa.PrivateKey) error {
    address := crypto.PubkeyToAddress(prvKey.PublicKey)
    bzzdirPath := filepath.Join(datadirPath, "bzz-"+common.Bytes2Hex(address.Bytes()))
    err := os.MkdirAll(bzzdirPath, os.ModePerm)
    if err != nil {
        return err
    }
    c.Path = bzzdirPath
    return nil
}