config.go 2.09 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package config

import (
4 5 6 7
	"crypto"
	"crypto/x509"
	"encoding/base64"
	"errors"
8
	"os"
9 10

	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
13
// Identity tracks the configuration of the local node's identity.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
type Identity struct {
15
	PeerID  string
16
	PrivKey string
17
	Address string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
20
// Datastore tracks the configuration of the datastore.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
type Datastore struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
22 23
	Type string
	Path string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25
}

26 27
type SavedPeer struct {
	Address string
28
	PeerID  string // until multiaddr supports ipfs, use another field.
29 30
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
31
// Config is used to load IPFS config files.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
type Config struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
	Identity  *Identity
Brian Tiger Chow's avatar
Brian Tiger Chow committed
34
	Datastore Datastore
35
	Peers     []*SavedPeer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37
}

38 39 40
const DefaultPathRoot = "~/.go-ipfs"
const DefaultConfigFilePath = DefaultPathRoot + "/config"
const DefaultConfigFile = `{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43
  "identity": {},
  "datastore": {
    "type": "leveldb",
44
    "path": "` + DefaultPathRoot + `/datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46 47 48
  }
}
`

49 50 51 52 53 54 55 56 57 58
func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) {
	pkb, err := base64.StdEncoding.DecodeString(i.PrivKey)
	if err != nil {
		return nil, err
	}

	//pretend to actually decrypt private key
	return x509.ParsePKCS1PrivateKey(pkb)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59 60
// Filename returns the proper tilde expanded config filename.
func Filename(filename string) (string, error) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
61
	if len(filename) == 0 {
62
		filename = DefaultConfigFilePath
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
63 64
	}

65
	// tilde expansion on config file
66 67 68
	return u.TildeExpansion(filename)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70 71
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
	filename, err := Filename(filename)
72 73
	if err != nil {
		return nil, err
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
74 75
	}

76
	// if nothing is there, fail. User must run 'ipfs init'
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
77
	if _, err := os.Stat(filename); os.IsNotExist(err) {
78
		return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
79 80 81
	}

	var cfg Config
82 83 84 85 86 87 88
	err = ReadConfigFile(filename, &cfg)
	if err != nil {
		return nil, err
	}

	// tilde expansion on datastore path
	cfg.Datastore.Path, err = u.TildeExpansion(cfg.Datastore.Path)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
89 90 91 92 93
	if err != nil {
		return nil, err
	}

	return &cfg, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
}
95 96 97

// Set sets the value of a particular config key
func Set(filename, key, value string) error {
98
	return WriteConfigKey(filename, key, value)
99
}