feat: add flag to ipfs key and list to output keys in b36/CIDv1 (#7531)
* add flag to "ipfs key gen" to output keys in b36 CIDv1 * add flag to "ipfs key list" to output keys in b36 CIDv1 * add and modify corresponding sharness tests
Petar Maymounkov committed
Jul 13, 2020 at 09:08 UTC
b3e5ffc41ae4ef46402ff38be21c66912b59bc42
6 files changed
+112
-15
core/commands/keystore.go
+43
-4
@@ -5,10 +5,11 @@ import (
5
"io"
6
"text/tabwriter"
7
8
- cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9
-
8
cmds "github.com/ipfs/go-ipfs-cmds"
9
+ cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
options "github.com/ipfs/interface-go-ipfs-core/options"
11
+ peer "github.com/libp2p/go-libp2p-core/peer"
12
+ mbase "github.com/multiformats/go-multibase"
13
)
14
15
var KeyCmd = &cmds.Command{
@@ -56,6 +57,7 @@ type KeyRenameOutput struct {
57
const (
58
keyStoreTypeOptionName = "type"
59
keyStoreSizeOptionName = "size"
60
+ keyFormatOptionName = "format"
61
)
62
63
var keyGenCmd = &cmds.Command{
@@ -65,6 +67,7 @@ var keyGenCmd = &cmds.Command{
67
Options: []cmds.Option{
68
cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault("rsa"),
69
cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
70
+ cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b58mh"),
71
},
72
Arguments: []cmds.Argument{
73
cmds.StringArg("name", true, false, "name of key to create"),
@@ -91,6 +94,9 @@ var keyGenCmd = &cmds.Command{
94
if sizefound {
95
opts = append(opts, options.Key.Size(size))
96
}
97
+ if err = verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil {
98
+ return err
99
+ }
100
101
key, err := api.Key().Generate(req.Context, name, opts...)
102
@@ -100,7 +106,7 @@ var keyGenCmd = &cmds.Command{
106
107
return cmds.EmitOnce(res, &KeyOutput{
108
Name: name,
103
- Id: key.ID().Pretty(),
109
+ Id: formatID(key.ID(), req.Options[keyFormatOptionName].(string)),
110
})
111
},
112
Encoders: cmds.EncoderMap{
@@ -112,14 +118,44 @@ var keyGenCmd = &cmds.Command{
118
Type: KeyOutput{},
119
}
120
121
+func verifyFormatLabel(formatLabel string) error {
122
+ switch formatLabel {
123
+ case "b58mh":
124
+ return nil
125
+ case "b36cid":
126
+ return nil
127
+ }
128
+ return fmt.Errorf("invalid output format option")
129
+}
130
+
131
+func formatID(id peer.ID, formatLabel string) string {
132
+ switch formatLabel {
133
+ case "b58mh":
134
+ return id.Pretty()
135
+ case "b36cid":
136
+ if s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil {
137
+ panic(err)
138
+ } else {
139
+ return s
140
+ }
141
+ default:
142
+ panic("unreachable")
143
+ }
144
+}
145
+
146
var keyListCmd = &cmds.Command{
147
Helptext: cmds.HelpText{
148
Tagline: "List all local keypairs",
149
},
150
Options: []cmds.Option{
151
cmds.BoolOption("l", "Show extra information about keys."),
152
+ cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b58mh"),
153
},
154
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
155
+ if err := verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil {
156
+ return err
157
+ }
158
+
159
api, err := cmdenv.GetApi(env, req)
160
if err != nil {
161
return err
@@ -133,7 +169,10 @@ var keyListCmd = &cmds.Command{
169
list := make([]KeyOutput, 0, len(keys))
170
171
for _, key := range keys {
136
- list = append(list, KeyOutput{Name: key.Name(), Id: key.ID().Pretty()})
172
+ list = append(list, KeyOutput{
173
+ Name: key.Name(),
174
+ Id: formatID(key.ID(), req.Options[keyFormatOptionName].(string)),
175
+ })
176
}
177
178
return cmds.EmitOnce(res, &KeyOutputList{list})
go.mod
+1
@@ -89,6 +89,7 @@ require (
89
github.com/miekg/dns v1.1.29 // indirect
90
github.com/mitchellh/go-homedir v1.1.0
91
github.com/mr-tron/base58 v1.1.3
92
+ github.com/multiformats/go-base36 v0.1.0
93
github.com/multiformats/go-multiaddr v0.2.2
94
github.com/multiformats/go-multiaddr-dns v0.2.0
95
github.com/multiformats/go-multiaddr-net v0.1.5
test/sharness/lib/test-lib.sh
+32
@@ -451,6 +451,38 @@ test_check_peerid() {
451
}
452
}
453
454
+test_check_rsa2048_b58mh_peerid() {
455
+ peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
456
+ test "$peeridlen" = "46" || {
457
+ echo "Bad RSA2048 B58MH peerid '$1' with len '$peeridlen'"
458
+ return 1
459
+ }
460
+}
461
+
462
+test_check_ed25519_b58mh_peerid() {
463
+ peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
464
+ test "$peeridlen" = "46" || {
465
+ echo "Bad ED25519 B58MH peerid '$1' with len '$peeridlen'"
466
+ return 1
467
+ }
468
+}
469
+
470
+test_check_rsa2048_b36cid_peerid() {
471
+ peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
472
+ test "$peeridlen" = "56" || {
473
+ echo "Bad RSA2048 B36CID peerid '$1' with len '$peeridlen'"
474
+ return 1
475
+ }
476
+}
477
+
478
+test_check_ed25519_b36cid_peerid() {
479
+ peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
480
+ test "$peeridlen" = "62" || {
481
+ echo "Bad ED25519 B36CID peerid '$1' with len '$peeridlen'"
482
+ return 1
483
+ }
484
+}
485
+
486
convert_tcp_maddr() {
487
echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }'
488
}
test/sharness/t0100-name.sh
+1
-1
@@ -101,7 +101,7 @@ test_expect_failure "publish with our explicit node ID looks good" '
101
# publish with an explicit node ID as key name
102
103
test_expect_success "generate and verify a new key" '
104
- NEWID=`ipfs key gen --type=rsa --size=2048 keyname` &&
104
+ NEWID=`ipfs key gen -f=b58mh --type=rsa --size=2048 keyname` &&
105
test_check_peerid "${NEWID}"
106
'
107
test/sharness/t0160-resolve.sh
+1
-1
@@ -23,7 +23,7 @@ test_expect_success "resolve: prepare dag" '
23
24
test_expect_success "resolve: prepare keys" '
25
self_hash=$(ipfs id -f="<id>") &&
26
- alt_hash=$(ipfs key gen -t rsa alt)
26
+ alt_hash=$(ipfs key gen -f=b58mh -t rsa alt)
27
'
28
29
test_resolve_setup_name() {
test/sharness/t0165-keystore.sh
+34
-9
@@ -11,37 +11,62 @@ test_description="Test keystore commands"
11
test_init_ipfs
12
13
test_key_cmd() {
14
+# test key output format
15
+test_expect_success "create an RSA key and test B58MH multihash output" '
16
+PEERID=$(ipfs key gen -f=b58mh --type=rsa --size=2048 key_rsa) &&
17
+test_check_rsa2048_b58mh_peerid $PEERID
18
+'
19
+
20
+test_expect_success "test RSA key B36CID multihash format" '
21
+PEERID=$(ipfs key list -f=b36cid -l | grep key_rsa | head -n 1 | cut -d " " -f1) &&
22
+test_check_rsa2048_b36cid_peerid $PEERID &&
23
+ipfs key rm key_rsa
24
+'
25
+
26
+test_expect_success "create an ED25519 key and test multihash output" '
27
+PEERID=$(ipfs key gen -f=b36cid --type=ed25519 key_ed25519) &&
28
+test_check_ed25519_b36cid_peerid $PEERID
29
+'
30
+
31
+test_expect_success "test ED25519 key B36CID multihash format" '
32
+PEERID=$(ipfs key list -f=b36cid -l | grep key_ed25519 | head -n 1 | cut -d " " -f1) &&
33
+test_check_ed25519_b36cid_peerid $PEERID &&
34
+ipfs key rm key_ed25519
35
+'
36
+# end of format test
37
+
38
+
39
test_expect_success "create a new rsa key" '
15
- rsahash=$(ipfs key gen foobarsa --type=rsa --size=2048)
40
+ rsahash=$(ipfs key gen -f=b58mh foobarsa --type=rsa --size=2048)
41
'
42
43
test_expect_success "create a new ed25519 key" '
19
- edhash=$(ipfs key gen bazed --type=ed25519)
44
+ edhash=$(ipfs key gen -f=b58mh bazed --type=ed25519)
45
'
46
47
test_expect_success "both keys show up in list output" '
48
echo bazed > list_exp &&
49
echo foobarsa >> list_exp &&
50
echo self >> list_exp
26
- ipfs key list | sort > list_out &&
51
+ ipfs key list -f=b58mh | sort > list_out &&
52
test_cmp list_exp list_out
53
'
54
55
test_expect_success "key hashes show up in long list output" '
31
- ipfs key list -l | grep $edhash > /dev/null &&
32
- ipfs key list -l | grep $rsahash > /dev/null
56
+ ipfs key list -f=b58mh -l | grep $edhash > /dev/null &&
57
+ ipfs key list -f=b58mh -l | grep $rsahash > /dev/null
58
'
59
60
test_expect_success "key list -l contains self key with peerID" '
61
PeerID="$(ipfs config Identity.PeerID)"
37
- ipfs key list -l | grep "$PeerID\s\+self"
62
+ ipfs key list -f=b58mh -l | grep "$PeerID\s\+self"
63
'
64
65
test_expect_success "key rm remove a key" '
66
ipfs key rm foobarsa
67
echo bazed > list_exp &&
68
echo self >> list_exp
44
- ipfs key list | sort > list_out &&
69
+ ipfs key list -f=b58mh | sort > list_out &&
70
test_cmp list_exp list_out
71
'
72
@@ -54,12 +79,12 @@ test_key_cmd() {
79
ipfs key rename bazed fooed
80
echo fooed > list_exp &&
81
echo self >> list_exp
57
- ipfs key list | sort > list_out &&
82
+ ipfs key list -f=b58mh | sort > list_out &&
83
test_cmp list_exp list_out
84
'
85
86
test_expect_success "key rename rename key output succeeds" '
62
- key_content=$(ipfs key gen key1 --type=rsa --size=2048) &&
87
+ key_content=$(ipfs key gen -f=b58mh key1 --type=rsa --size=2048) &&
88
ipfs key rename key1 key2 >rs &&
89
echo "Key $key_content renamed to key2" >expect &&
90
test_cmp rs expect