@cryptotaxi247 / kubo / commits / 63feb68ce

refactor(name, pub, resolve) separate files

Brian Tiger Chow committed Nov 7, 2014 at 18:52 UTC 63feb68ce77cb6e69054325f378cc91b2516f281
3 files changed +199 -188
core/commands2/name.go
+1 -188
@@ -1,27 +1,12 @@
1 package commands
2
3 -import (
4 - "errors"
5 - "fmt"
6 -
7 - cmds "github.com/jbenet/go-ipfs/commands"
8 - core "github.com/jbenet/go-ipfs/core"
9 - crypto "github.com/jbenet/go-ipfs/crypto"
10 - nsys "github.com/jbenet/go-ipfs/namesys"
11 - u "github.com/jbenet/go-ipfs/util"
12 -)
3 +import cmds "github.com/jbenet/go-ipfs/commands"
4
5 type IpnsEntry struct {
6 Name string
7 Value string
8 }
9
19 -type ResolveOutput struct {
20 - Entries []IpnsEntry
21 -}
22 -
23 -var errNotOnline = errors.New("This command must be run in online mode. Try running 'ipfs daemon' first.")
24 -
10 var nameCmd = &cmds.Command{
11 // TODO UsageLine: "name [publish | resolve]",
12 // TODO Short: "ipfs namespace (ipns) tool",
@@ -63,175 +48,3 @@ Resolve te value of another name:
48 "resolve": resolveCmd,
49 },
50 }
66 -
67 -var publishCmd = &cmds.Command{
68 - // TODO UsageLine: "publish",
69 - // TODO Short: "publish a <ref> to ipns.",
70 - Help: `ipfs publish [<name>] <ref> - publish a <ref> to ipns.
71 -
72 -IPNS is a PKI namespace, where names are the hashes of public keys, and
73 -the private key enables publishing new (signed) values. In publish, the
74 -default value of <name> is your own identity public key.
75 -
76 -Examples:
77 -
78 -Publish a <ref> to your identity name:
79 -
80 - > ipfs name publish QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
81 - published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
82 -
83 -Publish a <ref> to another public key:
84 -
85 - > ipfs name publish QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
86 - published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
87 -
88 -`,
89 - Arguments: []cmds.Argument{
90 - cmds.Argument{"name", cmds.ArgString, false, false},
91 - cmds.Argument{"object", cmds.ArgString, true, false},
92 - },
93 - Run: func(res cmds.Response, req cmds.Request) {
94 - n := req.Context().Node
95 - args := req.Arguments()
96 -
97 - if n.Network == nil {
98 - res.SetError(errNotOnline, cmds.ErrNormal)
99 - return
100 - }
101 -
102 - if n.Identity == nil {
103 - res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
104 - return
105 - }
106 -
107 - // name := ""
108 - ref := ""
109 -
110 - switch len(args) {
111 - case 2:
112 - // name = args[0]
113 - ref = args[1].(string)
114 - res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
115 - return
116 - case 1:
117 - // name = n.Identity.ID.String()
118 - ref = args[0].(string)
119 - }
120 -
121 - // TODO n.Keychain.Get(name).PrivKey
122 - k := n.Identity.PrivKey()
123 - publishOutput, err := publish(n, k, ref)
124 -
125 - if err != nil {
126 - res.SetError(err, cmds.ErrNormal)
127 - return
128 - }
129 - res.SetOutput(publishOutput)
130 - },
131 - Marshallers: map[cmds.EncodingType]cmds.Marshaller{
132 - cmds.Text: func(res cmds.Response) ([]byte, error) {
133 - v := res.Output().(*IpnsEntry)
134 - s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
135 - return []byte(s), nil
136 - },
137 - },
138 - Type: &IpnsEntry{},
139 -}
140 -
141 -var resolveCmd = &cmds.Command{
142 - // TODO UsageLine: "resolve",
143 - // TODO Short: "resolve an ipns name to a <ref>",
144 - Help: `ipfs resolve [<name>] - Resolve an ipns name to a <ref>.
145 -
146 -IPNS is a PKI namespace, where names are the hashes of public keys, and
147 -the private key enables publishing new (signed) values. In resolve, the
148 -default value of <name> is your own identity public key.
149 -
150 -
151 -Examples:
152 -
153 -Resolve the value of your identity:
154 -
155 - > ipfs name resolve
156 - QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
157 -
158 -Resolve te value of another name:
159 -
160 - > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
161 - QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
162 -
163 -`,
164 -
165 - Arguments: []cmds.Argument{
166 - cmds.Argument{"name", cmds.ArgString, false, true},
167 - },
168 - Run: func(res cmds.Response, req cmds.Request) {
169 -
170 - n := req.Context().Node
171 - var names []string
172 -
173 - if n.Network == nil {
174 - res.SetError(errNotOnline, cmds.ErrNormal)
175 - return
176 - }
177 -
178 - if len(req.Arguments()) == 0 {
179 - if n.Identity == nil {
180 - res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
181 - return
182 - }
183 - names = append(names, n.Identity.ID().String())
184 - } else {
185 - for _, arg := range req.Arguments() {
186 - name, ok := arg.(string)
187 - if !ok {
188 - res.SetError(errors.New("cast error"), cmds.ErrNormal)
189 - return
190 - }
191 - names = append(names, name)
192 - }
193 - }
194 -
195 - entries, err := resolve(n, names)
196 - if err != nil {
197 - res.SetError(err, cmds.ErrNormal)
198 - return
199 - }
200 -
201 - res.SetOutput(&ResolveOutput{entries})
202 - },
203 - Type: &ResolveOutput{},
204 -}
205 -
206 -func publish(n *core.IpfsNode, k crypto.PrivKey, ref string) (*IpnsEntry, error) {
207 - pub := nsys.NewRoutingPublisher(n.Routing)
208 - err := pub.Publish(k, ref)
209 - if err != nil {
210 - return nil, err
211 - }
212 -
213 - hash, err := k.GetPublic().Hash()
214 - if err != nil {
215 - return nil, err
216 - }
217 -
218 - return &IpnsEntry{
219 - Name: u.Key(hash).String(),
220 - Value: ref,
221 - }, nil
222 -}
223 -
224 -func resolve(n *core.IpfsNode, names []string) ([]IpnsEntry, error) {
225 - var entries []IpnsEntry
226 - for _, name := range names {
227 - resolved, err := n.Namesys.Resolve(name)
228 - if err != nil {
229 - return nil, err
230 - }
231 - entries = append(entries, IpnsEntry{
232 - Name: name,
233 - Value: resolved,
234 - })
235 - }
236 - return entries, nil
237 -}
core/commands2/publish.go new
+106
@@ -0,0 +1,106 @@
1 +package commands
2 +
3 +import (
4 + "errors"
5 + "fmt"
6 +
7 + cmds "github.com/jbenet/go-ipfs/commands"
8 + core "github.com/jbenet/go-ipfs/core"
9 + crypto "github.com/jbenet/go-ipfs/crypto"
10 + nsys "github.com/jbenet/go-ipfs/namesys"
11 + u "github.com/jbenet/go-ipfs/util"
12 +)
13 +
14 +var errNotOnline = errors.New("This command must be run in online mode. Try running 'ipfs daemon' first.")
15 +
16 +var publishCmd = &cmds.Command{
17 + // TODO UsageLine: "publish",
18 + // TODO Short: "publish a <ref> to ipns.",
19 + Help: `ipfs publish [<name>] <ref> - publish a <ref> to ipns.
20 +
21 +IPNS is a PKI namespace, where names are the hashes of public keys, and
22 +the private key enables publishing new (signed) values. In publish, the
23 +default value of <name> is your own identity public key.
24 +
25 +Examples:
26 +
27 +Publish a <ref> to your identity name:
28 +
29 + > ipfs name publish QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
30 + published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
31 +
32 +Publish a <ref> to another public key:
33 +
34 + > ipfs name publish QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
35 + published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
36 +
37 +`,
38 + Arguments: []cmds.Argument{
39 + cmds.Argument{"name", cmds.ArgString, false, false},
40 + cmds.Argument{"object", cmds.ArgString, true, false},
41 + },
42 + Run: func(res cmds.Response, req cmds.Request) {
43 + n := req.Context().Node
44 + args := req.Arguments()
45 +
46 + if n.Network == nil {
47 + res.SetError(errNotOnline, cmds.ErrNormal)
48 + return
49 + }
50 +
51 + if n.Identity == nil {
52 + res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
53 + return
54 + }
55 +
56 + // name := ""
57 + ref := ""
58 +
59 + switch len(args) {
60 + case 2:
61 + // name = args[0]
62 + ref = args[1].(string)
63 + res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
64 + return
65 + case 1:
66 + // name = n.Identity.ID.String()
67 + ref = args[0].(string)
68 + }
69 +
70 + // TODO n.Keychain.Get(name).PrivKey
71 + k := n.Identity.PrivKey()
72 + publishOutput, err := publish(n, k, ref)
73 +
74 + if err != nil {
75 + res.SetError(err, cmds.ErrNormal)
76 + return
77 + }
78 + res.SetOutput(publishOutput)
79 + },
80 + Marshallers: map[cmds.EncodingType]cmds.Marshaller{
81 + cmds.Text: func(res cmds.Response) ([]byte, error) {
82 + v := res.Output().(*IpnsEntry)
83 + s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
84 + return []byte(s), nil
85 + },
86 + },
87 + Type: &IpnsEntry{},
88 +}
89 +
90 +func publish(n *core.IpfsNode, k crypto.PrivKey, ref string) (*IpnsEntry, error) {
91 + pub := nsys.NewRoutingPublisher(n.Routing)
92 + err := pub.Publish(k, ref)
93 + if err != nil {
94 + return nil, err
95 + }
96 +
97 + hash, err := k.GetPublic().Hash()
98 + if err != nil {
99 + return nil, err
100 + }
101 +
102 + return &IpnsEntry{
103 + Name: u.Key(hash).String(),
104 + Value: ref,
105 + }, nil
106 +}
core/commands2/resolve.go new
+92
@@ -0,0 +1,92 @@
1 +package commands
2 +
3 +import (
4 + "errors"
5 +
6 + cmds "github.com/jbenet/go-ipfs/commands"
7 + core "github.com/jbenet/go-ipfs/core"
8 +)
9 +
10 +type ResolveOutput struct {
11 + Entries []IpnsEntry
12 +}
13 +
14 +var resolveCmd = &cmds.Command{
15 + // TODO UsageLine: "resolve",
16 + // TODO Short: "resolve an ipns name to a <ref>",
17 + Help: `ipfs resolve [<name>] - Resolve an ipns name to a <ref>.
18 +
19 +IPNS is a PKI namespace, where names are the hashes of public keys, and
20 +the private key enables publishing new (signed) values. In resolve, the
21 +default value of <name> is your own identity public key.
22 +
23 +
24 +Examples:
25 +
26 +Resolve the value of your identity:
27 +
28 + > ipfs name resolve
29 + QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
30 +
31 +Resolve te value of another name:
32 +
33 + > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
34 + QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
35 +
36 +`,
37 +
38 + Arguments: []cmds.Argument{
39 + cmds.Argument{"name", cmds.ArgString, false, true},
40 + },
41 + Run: func(res cmds.Response, req cmds.Request) {
42 +
43 + n := req.Context().Node
44 + var names []string
45 +
46 + if n.Network == nil {
47 + res.SetError(errNotOnline, cmds.ErrNormal)
48 + return
49 + }
50 +
51 + if len(req.Arguments()) == 0 {
52 + if n.Identity == nil {
53 + res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
54 + return
55 + }
56 + names = append(names, n.Identity.ID().String())
57 + } else {
58 + for _, arg := range req.Arguments() {
59 + name, ok := arg.(string)
60 + if !ok {
61 + res.SetError(errors.New("cast error"), cmds.ErrNormal)
62 + return
63 + }
64 + names = append(names, name)
65 + }
66 + }
67 +
68 + entries, err := resolve(n, names)
69 + if err != nil {
70 + res.SetError(err, cmds.ErrNormal)
71 + return
72 + }
73 +
74 + res.SetOutput(&ResolveOutput{entries})
75 + },
76 + Type: &ResolveOutput{},
77 +}
78 +
79 +func resolve(n *core.IpfsNode, names []string) ([]IpnsEntry, error) {
80 + var entries []IpnsEntry
81 + for _, name := range names {
82 + resolved, err := n.Namesys.Resolve(name)
83 + if err != nil {
84 + return nil, err
85 + }
86 + entries = append(entries, IpnsEntry{
87 + Name: name,
88 + Value: resolved,
89 + })
90 + }
91 + return entries, nil
92 +}