/
builder.go
114 lines (97 loc) · 2.42 KB
/
builder.go
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
package core
import (
"crypto/rand"
"encoding/base64"
"errors"
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
dsync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
key "github.com/ipfs/go-ipfs/blocks/key"
ci "github.com/ipfs/go-ipfs/p2p/crypto"
repo "github.com/ipfs/go-ipfs/repo"
cfg "github.com/ipfs/go-ipfs/repo/config"
)
var ErrAlreadyBuilt = errors.New("this builder has already been used")
// NodeBuilder is an object used to generate an IpfsNode
type NodeBuilder struct {
online bool
routing RoutingOption
peerhost HostOption
repo repo.Repo
built bool
nilrepo bool
}
func NewNodeBuilder() *NodeBuilder {
return &NodeBuilder{
online: false,
routing: DHTOption,
peerhost: DefaultHostOption,
}
}
func defaultRepo(dstore ds.ThreadSafeDatastore) (repo.Repo, error) {
c := cfg.Config{}
priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader)
if err != nil {
return nil, err
}
data, err := pub.Hash()
if err != nil {
return nil, err
}
privkeyb, err := priv.Bytes()
if err != nil {
return nil, err
}
c.Bootstrap = cfg.DefaultBootstrapAddresses
c.Addresses.Swarm = []string{"/ip4/0.0.0.0/tcp/4001"}
c.Identity.PeerID = key.Key(data).B58String()
c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb)
return &repo.Mock{
D: dstore,
C: c,
}, nil
}
func (nb *NodeBuilder) Online() *NodeBuilder {
nb.online = true
return nb
}
func (nb *NodeBuilder) Offline() *NodeBuilder {
nb.online = false
return nb
}
func (nb *NodeBuilder) SetRouting(ro RoutingOption) *NodeBuilder {
nb.routing = ro
return nb
}
func (nb *NodeBuilder) SetHost(ho HostOption) *NodeBuilder {
nb.peerhost = ho
return nb
}
func (nb *NodeBuilder) SetRepo(r repo.Repo) *NodeBuilder {
nb.repo = r
return nb
}
func (nb *NodeBuilder) NilRepo() *NodeBuilder {
nb.nilrepo = true
return nb
}
func (nb *NodeBuilder) Build(ctx context.Context) (*IpfsNode, error) {
if nb.built {
return nil, ErrAlreadyBuilt
}
nb.built = true
if nb.repo == nil {
var d ds.Datastore
d = ds.NewMapDatastore()
if nb.nilrepo {
d = ds.NewNullDatastore()
}
r, err := defaultRepo(dsync.MutexWrap(d))
if err != nil {
return nil, err
}
nb.repo = r
}
conf := standardWithRouting(nb.repo, nb.online, nb.routing, nb.peerhost)
return NewIPFSNode(ctx, conf)
}