@cryptotaxi247 / kubo / commits / b4ad66eda

add --ipns-base to publish and subs commands add sharness tests for --ipns-base in name publish and subs commands

add --ipns-base to publish and subs commands add sharness tests for --ipns-base in name publish and subs commands

Petar Maymounkov committed Aug 17, 2020 at 09:46 UTC b4ad66eda42c9da24926d955c39ac242ac9b74e1
6 files changed +64 -66
core/commands/keybase/keybase.go
+2
@@ -5,6 +5,8 @@ import (
5 mbase "github.com/multiformats/go-multibase"
6 )
7
8 +const KeyFormatOptionName = "ipns-base"
9 +
10 type KeyEncoder struct {
11 baseEnc *mbase.Encoder
12 }
core/commands/keystore.go
+10 -47
@@ -20,7 +20,6 @@ import (
20 options "github.com/ipfs/interface-go-ipfs-core/options"
21 "github.com/libp2p/go-libp2p-core/crypto"
22 peer "github.com/libp2p/go-libp2p-core/peer"
23 - mbase "github.com/multiformats/go-multibase"
23 )
24
25 var KeyCmd = &cmds.Command{
@@ -72,7 +71,6 @@ const (
71 keyStoreAlgorithmDefault = options.RSAKey
72 keyStoreTypeOptionName = "type"
73 keyStoreSizeOptionName = "size"
75 - keyFormatOptionName = "ipns-base"
74 oldKeyOptionName = "oldkey"
75 )
76
@@ -83,7 +81,7 @@ var keyGenCmd = &cmds.Command{
81 Options: []cmds.Option{
82 cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault(keyStoreAlgorithmDefault),
83 cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
86 - cmds.StringOption(keyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
84 + cmds.StringOption(kb.KeyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
85 },
86 Arguments: []cmds.Argument{
87 cmds.StringArg("name", true, false, "name of key to create"),
@@ -110,7 +108,7 @@ var keyGenCmd = &cmds.Command{
108 if sizefound {
109 opts = append(opts, options.Key.Size(size))
110 }
113 - keyEnc, err := kb.KeyEncoderFromString(req.Options[keyFormatOptionName].(string))
111 + keyEnc, err := kb.KeyEncoderFromString(req.Options[kb.KeyFormatOptionName].(string))
112 if err != nil {
113 return err
114 }
@@ -225,7 +223,7 @@ var keyImportCmd = &cmds.Command{
223 Tagline: "Import a key and prints imported key id",
224 },
225 Options: []cmds.Option{
228 - cmds.StringOption(keyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
226 + cmds.StringOption(kb.KeyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
227 },
228 Arguments: []cmds.Argument{
229 cmds.StringArg("name", true, false, "name to associate with key in keychain"),
@@ -238,7 +236,7 @@ var keyImportCmd = &cmds.Command{
236 return fmt.Errorf("cannot import key with name 'self'")
237 }
238
241 - keyEnc, err := kb.KeyEncoderFromString(req.Options[keyFormatOptionName].(string))
239 + keyEnc, err := kb.KeyEncoderFromString(req.Options[kb.KeyFormatOptionName].(string))
240 if err != nil {
241 return err
242 }
@@ -305,10 +303,10 @@ var keyListCmd = &cmds.Command{
303 },
304 Options: []cmds.Option{
305 cmds.BoolOption("l", "Show extra information about keys."),
308 - cmds.StringOption(keyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
306 + cmds.StringOption(kb.KeyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
307 },
308 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
311 - keyEnc, err := kb.KeyEncoderFromString(req.Options[keyFormatOptionName].(string))
309 + keyEnc, err := kb.KeyEncoderFromString(req.Options[kb.KeyFormatOptionName].(string))
310 if err != nil {
311 return err
312 }
@@ -354,14 +352,14 @@ var keyRenameCmd = &cmds.Command{
352 },
353 Options: []cmds.Option{
354 cmds.BoolOption(keyStoreForceOptionName, "f", "Allow to overwrite an existing key."),
357 - cmds.StringOption(keyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
355 + cmds.StringOption(kb.KeyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
356 },
357 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
358 api, err := cmdenv.GetApi(env, req)
359 if err != nil {
360 return err
361 }
364 - keyEnc, err := kb.KeyEncoderFromString(req.Options[keyFormatOptionName].(string))
362 + keyEnc, err := kb.KeyEncoderFromString(req.Options[kb.KeyFormatOptionName].(string))
363 if err != nil {
364 return err
365 }
@@ -404,14 +402,14 @@ var keyRmCmd = &cmds.Command{
402 },
403 Options: []cmds.Option{
404 cmds.BoolOption("l", "Show extra information about keys."),
407 - cmds.StringOption(keyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
405 + cmds.StringOption(kb.KeyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
406 },
407 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
408 api, err := cmdenv.GetApi(env, req)
409 if err != nil {
410 return err
411 }
414 - keyEnc, err := kb.KeyEncoderFromString(req.Options[keyFormatOptionName].(string))
412 + keyEnc, err := kb.KeyEncoderFromString(req.Options[kb.KeyFormatOptionName].(string))
413 if err != nil {
414 return err
415 }
@@ -538,41 +536,6 @@ func doRotate(out io.Writer, repoRoot string, oldKey string, algorithm string, n
536 return nil
537 }
538
541 -func verifyIDFormatLabel(formatLabel string) error {
542 - switch formatLabel {
543 - case "b58mh", "v0":
544 - return nil
545 - default:
546 - _, err := mbase.EncoderByName(formatLabel)
547 - return err
548 - }
549 -}
550 -
551 -func keyEncoderFromString(formatLabel string) (keyEncoder, error) {
552 - switch formatLabel {
553 - case "b58mh", "v0":
554 - return keyEncoder{}, nil
555 - default:
556 - if enc, err := mbase.EncoderByName(formatLabel); err != nil {
557 - return keyEncoder{}, err
558 - } else {
559 - return keyEncoder{&enc}, nil
560 - }
561 - }
562 -}
563 -
564 -func (enc keyEncoder) FormatID(id peer.ID) string {
565 - if enc.baseEnc == nil {
566 - //nolint deprecated
567 - return peer.IDB58Encode(id)
568 - }
569 - if s, err := peer.ToCid(id).StringOfBase(enc.baseEnc.Encoding()); err != nil {
570 - panic(err)
571 - } else {
572 - return s
573 - }
574 -}
575 -
539 func keyOutputListEncoders() cmds.EncoderFunc {
540 return cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, list *KeyOutputList) error {
541 withID, _ := req.Options["l"].(bool)
core/commands/name/ipnsps.go
+12 -3
@@ -5,10 +5,11 @@ import (
5 "io"
6 "strings"
7
8 - "github.com/ipfs/go-ipfs-cmds"
8 + cmds "github.com/ipfs/go-ipfs-cmds"
9 "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10 + kb "github.com/ipfs/go-ipfs/core/commands/keybase"
11 "github.com/libp2p/go-libp2p-core/peer"
11 - "github.com/libp2p/go-libp2p-record"
12 + record "github.com/libp2p/go-libp2p-record"
13 )
14
15 type ipnsPubsubState struct {
@@ -72,7 +73,15 @@ var ipnspsSubsCmd = &cmds.Command{
73 Helptext: cmds.HelpText{
74 Tagline: "Show current name subscriptions",
75 },
76 + Options: []cmds.Option{
77 + cmds.StringOption(kb.KeyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
78 + },
79 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
80 + keyEnc, err := kb.KeyEncoderFromString(req.Options[kb.KeyFormatOptionName].(string))
81 + if err != nil {
82 + return err
83 + }
84 +
85 n, err := cmdenv.GetNode(env)
86 if err != nil {
87 return err
@@ -93,7 +102,7 @@ var ipnspsSubsCmd = &cmds.Command{
102 log.Errorf("ipns key not a valid peer ID: %s", err)
103 continue
104 }
96 - paths = append(paths, "/ipns/"+peer.Encode(pid))
105 + paths = append(paths, "/ipns/"+keyEnc.FormatID(pid))
106 }
107
108 return cmds.EmitOnce(res, &stringList{paths})
core/commands/name/publish.go
+14 -1
@@ -9,9 +9,11 @@ import (
9 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
11 cmds "github.com/ipfs/go-ipfs-cmds"
12 + kb "github.com/ipfs/go-ipfs/core/commands/keybase"
13 iface "github.com/ipfs/interface-go-ipfs-core"
14 options "github.com/ipfs/interface-go-ipfs-core/options"
15 path "github.com/ipfs/interface-go-ipfs-core/path"
16 + peer "github.com/libp2p/go-libp2p-core/peer"
17 )
18
19 var (
@@ -81,12 +83,17 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
83 cmds.StringOption(ttlOptionName, "Time duration this record should be cached for. Uses the same syntax as the lifetime option. (caution: experimental)"),
84 cmds.StringOption(keyOptionName, "k", "Name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'.").WithDefault("self"),
85 cmds.BoolOption(quieterOptionName, "Q", "Write only final hash."),
86 + cmds.StringOption(kb.KeyFormatOptionName, "", "Encoding used for keys: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("base36"),
87 },
88 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
89 api, err := cmdenv.GetApi(env, req)
90 if err != nil {
91 return err
92 }
93 + keyEnc, err := kb.KeyEncoderFromString(req.Options[kb.KeyFormatOptionName].(string))
94 + if err != nil {
95 + return err
96 + }
97
98 allowOffline, _ := req.Options[allowOfflineOptionName].(bool)
99 kname, _ := req.Options[keyOptionName].(string)
@@ -129,8 +136,14 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
136 return err
137 }
138
139 + // parse path, extract cid, re-base cid, reconstruct path
140 + pid, err := peer.Decode(out.Name())
141 + if err != nil {
142 + return err
143 + }
144 +
145 return cmds.EmitOnce(res, &IpnsEntry{
133 - Name: out.Name(),
146 + Name: keyEnc.FormatID(pid),
147 Value: out.Value().String(),
148 })
149 },
test/sharness/t0100-name.sh
+10 -5
@@ -116,14 +116,19 @@ test_name_with_self() {
116 '
117
118 test_expect_success "'ipfs name publish --allow-offline --key=<peer-id> <hash>' succeeds" '
119 - ipfs name publish --allow-offline --key=${B58MH_ID} "/ipfs/$HASH_WELCOME_DOCS" >b58mh_published_id &&
120 - ipfs name publish --allow-offline --key=${B36CID_ID} "/ipfs/$HASH_WELCOME_DOCS" >base36_published_id
119 + ipfs name publish --allow-offline --key=${B58MH_ID} "/ipfs/$HASH_WELCOME_DOCS" >b58mh_published_id_base36 &&
120 + ipfs name publish --allow-offline --key=${B36CID_ID} "/ipfs/$HASH_WELCOME_DOCS" >base36_published_id_base36 &&
121 + ipfs name publish --allow-offline --key=${B58MH_ID} --ipns-base=b58mh "/ipfs/$HASH_WELCOME_DOCS" >b58mh_published_id_b58mh &&
122 + ipfs name publish --allow-offline --key=${B36CID_ID} --ipns-base=b58mh "/ipfs/$HASH_WELCOME_DOCS" >base36_published_id_b58mh
123 '
124
125 test_expect_success "publish an explicit node ID as two key in B58MH and B36CID, name looks good" '
124 - echo "Published to ${B36CID_ID}: /ipfs/$HASH_WELCOME_DOCS" >expected_published_id &&
125 - test_cmp expected_published_id b58mh_published_id &&
126 - test_cmp expected_published_id base36_published_id
126 + echo "Published to ${B36CID_ID}: /ipfs/$HASH_WELCOME_DOCS" >expected_published_id_base36 &&
127 + echo "Published to ${B58MH_ID}: /ipfs/$HASH_WELCOME_DOCS" >expected_published_id_b58mh &&
128 + test_cmp expected_published_id_base36 b58mh_published_id_base36 &&
129 + test_cmp expected_published_id_base36 base36_published_id_base36 &&
130 + test_cmp expected_published_id_b58mh b58mh_published_id_b58mh &&
131 + test_cmp expected_published_id_b58mh base36_published_id_b58mh
132 '
133
134 test_expect_success "'ipfs name resolve' succeeds" '
test/sharness/t0183-namesys-pubsub.sh
+16 -10
@@ -13,7 +13,8 @@ test_expect_success 'init iptb' '
13 startup_cluster $NUM_NODES --enable-namesys-pubsub
14
15 test_expect_success 'peer ids' '
16 - PEERID_0=$(iptb attr get 0 id)
16 + PEERID_0_BASE36=$(ipfsi 0 key list --ipns-base=base36 -l | grep self | head -n 1 | cut -d " " -f1) &&
17 + PEERID_0_B58MH=$(ipfsi 0 key list --ipns-base=b58mh -l | grep self | head -n 1 | cut -d " " -f1)
18 '
19
20 test_expect_success 'check namesys pubsub state' '
@@ -28,17 +29,22 @@ test_expect_success 'check namesys pubsub state' '
29
30 # These commands are *expected* to fail. We haven't published anything yet.
31 test_expect_success 'subscribe nodes to the publisher topic' '
31 - ipfsi 1 name resolve /ipns/$PEERID_0 --timeout=1s;
32 - ipfsi 2 name resolve /ipns/$PEERID_0 --timeout=1s;
32 + ipfsi 1 name resolve /ipns/$PEERID_0_BASE36 --timeout=1s;
33 + ipfsi 2 name resolve /ipns/$PEERID_0_BASE36 --timeout=1s;
34 true
35 '
36
37 test_expect_success 'check subscriptions' '
37 - echo /ipns/$PEERID_0 > expected &&
38 + echo /ipns/$PEERID_0_BASE36 > expected_base36 &&
39 + echo /ipns/$PEERID_0_B58MH > expected_b58mh &&
40 ipfsi 1 name pubsub subs > subs1 &&
41 ipfsi 2 name pubsub subs > subs2 &&
40 - test_cmp expected subs1 &&
41 - test_cmp expected subs2
42 + ipfsi 1 name pubsub subs --ipns-base=b58mh > subs1_b58mh &&
43 + ipfsi 2 name pubsub subs --ipns-base=b58mh > subs2_b58mh &&
44 + test_cmp expected_base36 subs1 &&
45 + test_cmp expected_base36 subs2 &&
46 + test_cmp expected_b58mh subs1_b58mh &&
47 + test_cmp expected_b58mh subs2_b58mh
48 '
49
50 test_expect_success 'add an object on publisher node' '
@@ -56,15 +62,15 @@ test_expect_success 'wait for the flood' '
62
63 test_expect_success 'resolve name in subscriber nodes' '
64 echo "/ipfs/$HASH_FILE" > expected &&
59 - ipfsi 1 name resolve /ipns/$PEERID_0 > name1 &&
60 - ipfsi 2 name resolve /ipns/$PEERID_0 > name2 &&
65 + ipfsi 1 name resolve /ipns/$PEERID_0_BASE36 > name1 &&
66 + ipfsi 2 name resolve /ipns/$PEERID_0_BASE36 > name2 &&
67 test_cmp expected name1 &&
68 test_cmp expected name2
69 '
70
71 test_expect_success 'cancel subscriptions to the publisher topic' '
66 - ipfsi 1 name pubsub cancel /ipns/$PEERID_0 &&
67 - ipfsi 2 name pubsub cancel /ipns/$PEERID_0
72 + ipfsi 1 name pubsub cancel /ipns/$PEERID_0_BASE36 &&
73 + ipfsi 2 name pubsub cancel /ipns/$PEERID_0_BASE36
74 '
75
76 test_expect_success 'check subscriptions' '