@cryptotaxi247 / kubo / commits / 39ece8648

add PeerID lookup

enhance key lookup logic and enable PeerID lookup in addition to the key name License: MIT Signed-off-by: Kerem Gocen <keremgocen@gmail.com>

Kerem committed Apr 27, 2017 at 19:45 UTC 39ece86484909a6fdb29d7b8e0abedff1e521856
1 file changed +40 -2
core/commands/publish.go
+40 -2
@@ -48,6 +48,11 @@ Publish an <ipfs-path> with another name, added by an 'ipfs key' command:
48 > ipfs name publish --key=mykey /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
49 Published to QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n: /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
50
51 +Alternatively, publish an <ipfs-path> using a valid PeerID(as listed by 'ipfs key list -l'):
52 +
53 + > ipfs name publish --key=QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
54 + Published to QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n: /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
55 +
56 `,
57 },
58
@@ -61,7 +66,7 @@ Publish an <ipfs-path> with another name, added by an 'ipfs key' command:
66 This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are
67 "ns", "us" (or "µs"), "ms", "s", "m", "h".`).Default("24h"),
68 cmds.StringOption("ttl", "Time duration this record should be cached for (caution: experimental)."),
64 - cmds.StringOption("key", "k", "Name of the key to be used, as listed by 'ipfs key list'. Default: <<default>>.").Default("self"),
69 + cmds.StringOption("key", "k", "Name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'. Default: <<default>>.").Default("self"),
70 },
71 Run: func(req cmds.Request, res cmds.Response) {
72 log.Debug("begin publish")
@@ -116,7 +121,7 @@ Publish an <ipfs-path> with another name, added by an 'ipfs key' command:
121 }
122
123 kname, _, _ := req.Option("key").String()
119 - k, err := n.GetKey(kname)
124 + k, err := keylookup(kname, n)
125 if err != nil {
126 res.SetError(err, cmds.ErrNormal)
127 return
@@ -176,3 +181,36 @@ func publish(ctx context.Context, n *core.IpfsNode, k crypto.PrivKey, ref path.P
181 Value: ref.String(),
182 }, nil
183 }
184 +
185 +func keylookup(k string, n *core.IpfsNode) (crypto.PrivKey, error) {
186 +
187 + res, err := n.GetKey(k)
188 + if res != nil {
189 + return res, nil
190 + }
191 +
192 + keys, err := n.Repo.Keystore().List()
193 + if err != nil {
194 + return nil, err
195 + }
196 +
197 + for _, key := range keys {
198 + privKey, err := n.Repo.Keystore().Get(key)
199 + if err != nil {
200 + return nil, err
201 + }
202 +
203 + pubKey := privKey.GetPublic()
204 +
205 + pid, err := peer.IDFromPublicKey(pubKey)
206 + if err != nil {
207 + return nil, err
208 + }
209 +
210 + if pid.Pretty() == k {
211 + return privKey, nil
212 + }
213 + }
214 +
215 + return nil, fmt.Errorf("no key by the given name or PeerID was found")
216 +}