@cryptotaxi247 / kubo / commits / ab805f338

add in a default file hash and cleaned up init functiona bit

Jeromy committed Nov 4, 2014 at 10:55 UTC ab805f33827fef4fcbe130fec3aadb6823dbe9fc
2 files changed +100 -42
cmd/ipfs/init.go
+95 -42
@@ -1,16 +1,22 @@
1 package main
2
3 import (
4 + "bytes"
5 "encoding/base64"
6 "errors"
7 + "fmt"
8 "os"
9 "path/filepath"
10
11 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
12 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
13 config "github.com/jbenet/go-ipfs/config"
14 + core "github.com/jbenet/go-ipfs/core"
15 ci "github.com/jbenet/go-ipfs/crypto"
16 + imp "github.com/jbenet/go-ipfs/importer"
17 + chunk "github.com/jbenet/go-ipfs/importer/chunk"
18 peer "github.com/jbenet/go-ipfs/peer"
19 + updates "github.com/jbenet/go-ipfs/updates"
20 u "github.com/jbenet/go-ipfs/util"
21 )
22
@@ -33,6 +39,66 @@ func init() {
39 cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
40 }
41
42 +var defaultPeers = []*config.BootstrapPeer{
43 + &config.BootstrapPeer{
44 + // mars.i.ipfs.io
45 + PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
46 + Address: "/ip4/104.131.131.82/tcp/4001",
47 + },
48 +}
49 +
50 +func datastoreConfig(dspath string) (config.Datastore, error) {
51 + ds := config.Datastore{}
52 + if len(dspath) == 0 {
53 + var err error
54 + dspath, err = config.DataStorePath("")
55 + if err != nil {
56 + return ds, err
57 + }
58 + }
59 + ds.Path = dspath
60 + ds.Type = "leveldb"
61 +
62 + // Construct the data store if missing
63 + if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
64 + return ds, err
65 + }
66 +
67 + // Check the directory is writeable
68 + if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
69 + os.Remove(f.Name())
70 + } else {
71 + return ds, errors.New("Datastore '" + dspath + "' is not writeable")
72 + }
73 +
74 + return ds, nil
75 +}
76 +
77 +func identityConfig(nbits int) (config.Identity, error) {
78 + ident := config.Identity{}
79 + fmt.Println("generating key pair...")
80 + sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
81 + if err != nil {
82 + return ident, err
83 + }
84 +
85 + // currently storing key unencrypted. in the future we need to encrypt it.
86 + // TODO(security)
87 + skbytes, err := sk.Bytes()
88 + if err != nil {
89 + return ident, err
90 + }
91 + ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
92 +
93 + id, err := peer.IDFromPubKey(pk)
94 + if err != nil {
95 + return ident, err
96 + }
97 + ident.PeerID = id.Pretty()
98 +
99 + return ident, nil
100 +}
101 +
102 func initCmd(c *commander.Command, inp []string) error {
103 configpath, err := getConfigDir(c.Parent)
104 if err != nil {
@@ -62,30 +128,12 @@ func initCmd(c *commander.Command, inp []string) error {
128 }
129 cfg := new(config.Config)
130
65 - cfg.Datastore = config.Datastore{}
66 - if len(dspath) == 0 {
67 - dspath, err = config.DataStorePath("")
68 - if err != nil {
69 - return err
70 - }
71 - }
72 - cfg.Datastore.Path = dspath
73 - cfg.Datastore.Type = "leveldb"
74 -
75 - // Construct the data store if missing
76 - if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
131 + // setup the datastore
132 + cfg.Datastore, err = datastoreConfig(dspath)
133 + if err != nil {
134 return err
135 }
136
80 - // Check the directory is writeable
81 - if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
82 - os.Remove(f.Name())
83 - } else {
84 - return errors.New("Datastore '" + dspath + "' is not writeable")
85 - }
86 -
87 - cfg.Identity = config.Identity{}
88 -
137 // setup the node addresses.
138 cfg.Addresses = config.Addresses{
139 Swarm: "/ip4/0.0.0.0/tcp/4001",
@@ -106,42 +154,47 @@ func initCmd(c *commander.Command, inp []string) error {
154 return errors.New("Bitsize less than 1024 is considered unsafe.")
155 }
156
109 - u.POut("generating key pair\n")
110 - sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
157 + cfg.Identity, err = identityConfig(nbits)
158 if err != nil {
159 return err
160 }
161
115 - // currently storing key unencrypted. in the future we need to encrypt it.
116 - // TODO(security)
117 - skbytes, err := sk.Bytes()
118 - if err != nil {
119 - return err
162 + // Use these hardcoded bootstrap peers for now.
163 + cfg.Bootstrap = defaultPeers
164 +
165 + // tracking ipfs version used to generate the init folder
166 + // and adding update checker default setting.
167 + cfg.Version = config.Version{
168 + Check: "error",
169 + Current: updates.Version,
170 }
121 - cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
171
123 - id, err := peer.IDFromPubKey(pk)
172 + err = config.WriteConfigFile(filename, cfg)
173 if err != nil {
174 return err
175 }
127 - cfg.Identity.PeerID = id.Pretty()
128 - u.POut("peer identity: %s\n", id.Pretty())
176
130 - // Use these hardcoded bootstrap peers for now.
131 - cfg.Bootstrap = []*config.BootstrapPeer{
132 - &config.BootstrapPeer{
133 - // mars.i.ipfs.io
134 - PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
135 - Address: "/ip4/104.131.131.82/tcp/4001",
136 - },
177 + nd, err := core.NewIpfsNode(cfg, false)
178 + if err != nil {
179 + return err
180 }
181 + defer nd.Close()
182
139 - // tracking ipfs version used to generate the init folder and adding update checker default setting.
140 - cfg.Version = config.VersionDefaultValue()
183 + // Set up default file
184 + msg := `Hello and Welcome to IPFS!
185 +If you're seeing this, that means that you have successfully
186 +installed ipfs and are now interfacing with the wonderful
187 +world of DAGs and hashes!
188 +`
189 + reader := bytes.NewBufferString(msg)
190
142 - err = config.WriteConfigFile(filename, cfg)
191 + defnd, err := imp.BuildDagFromReader(reader, nd.DAG, nd.Pinning.GetManual(), chunk.DefaultSplitter)
192 if err != nil {
193 return err
194 }
195 +
196 + k, _ := defnd.Key()
197 + fmt.Printf("Default file key: %s\n", k)
198 +
199 return nil
200 }
pin/pin.go
+5
@@ -32,6 +32,7 @@ type Pinner interface {
32 Pin(*mdag.Node, bool) error
33 Unpin(util.Key, bool) error
34 Flush() error
35 + GetManual() ManualPinner
36 }
37
38 // ManualPinner is for manually editing the pin structure
@@ -263,3 +264,7 @@ func (p *pinner) PinWithMode(k util.Key, mode PinMode) {
264 p.indirPin.Increment(k)
265 }
266 }
267 +
268 +func (p *pinner) GetManual() ManualPinner {
269 + return p
270 +}