Skip to content

Commit b3e5ffc

Browse files
authored
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
1 parent 231fab8 commit b3e5ffc

File tree

6 files changed

+112
-15
lines changed

6 files changed

+112
-15
lines changed

core/commands/keystore.go

+43-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"io"
66
"text/tabwriter"
77

8-
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9-
108
cmds "github.com/ipfs/go-ipfs-cmds"
9+
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
1110
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"
1213
)
1314

1415
var KeyCmd = &cmds.Command{
@@ -56,6 +57,7 @@ type KeyRenameOutput struct {
5657
const (
5758
keyStoreTypeOptionName = "type"
5859
keyStoreSizeOptionName = "size"
60+
keyFormatOptionName = "format"
5961
)
6062

6163
var keyGenCmd = &cmds.Command{
@@ -65,6 +67,7 @@ var keyGenCmd = &cmds.Command{
6567
Options: []cmds.Option{
6668
cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault("rsa"),
6769
cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
70+
cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b58mh"),
6871
},
6972
Arguments: []cmds.Argument{
7073
cmds.StringArg("name", true, false, "name of key to create"),
@@ -91,6 +94,9 @@ var keyGenCmd = &cmds.Command{
9194
if sizefound {
9295
opts = append(opts, options.Key.Size(size))
9396
}
97+
if err = verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil {
98+
return err
99+
}
94100

95101
key, err := api.Key().Generate(req.Context, name, opts...)
96102

@@ -100,7 +106,7 @@ var keyGenCmd = &cmds.Command{
100106

101107
return cmds.EmitOnce(res, &KeyOutput{
102108
Name: name,
103-
Id: key.ID().Pretty(),
109+
Id: formatID(key.ID(), req.Options[keyFormatOptionName].(string)),
104110
})
105111
},
106112
Encoders: cmds.EncoderMap{
@@ -112,14 +118,44 @@ var keyGenCmd = &cmds.Command{
112118
Type: KeyOutput{},
113119
}
114120

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+
115146
var keyListCmd = &cmds.Command{
116147
Helptext: cmds.HelpText{
117148
Tagline: "List all local keypairs",
118149
},
119150
Options: []cmds.Option{
120151
cmds.BoolOption("l", "Show extra information about keys."),
152+
cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b58mh"),
121153
},
122154
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+
123159
api, err := cmdenv.GetApi(env, req)
124160
if err != nil {
125161
return err
@@ -133,7 +169,10 @@ var keyListCmd = &cmds.Command{
133169
list := make([]KeyOutput, 0, len(keys))
134170

135171
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+
})
137176
}
138177

139178
return cmds.EmitOnce(res, &KeyOutputList{list})

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ require (
8989
github.com/miekg/dns v1.1.29 // indirect
9090
github.com/mitchellh/go-homedir v1.1.0
9191
github.com/mr-tron/base58 v1.1.3
92+
github.com/multiformats/go-base36 v0.1.0
9293
github.com/multiformats/go-multiaddr v0.2.2
9394
github.com/multiformats/go-multiaddr-dns v0.2.0
9495
github.com/multiformats/go-multiaddr-net v0.1.5

test/sharness/lib/test-lib.sh

+32
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,38 @@ test_check_peerid() {
451451
}
452452
}
453453

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+
454486
convert_tcp_maddr() {
455487
echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }'
456488
}

test/sharness/t0100-name.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test_expect_failure "publish with our explicit node ID looks good" '
101101
# publish with an explicit node ID as key name
102102

103103
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` &&
105105
test_check_peerid "${NEWID}"
106106
'
107107

test/sharness/t0160-resolve.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_expect_success "resolve: prepare dag" '
2323

2424
test_expect_success "resolve: prepare keys" '
2525
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)
2727
'
2828

2929
test_resolve_setup_name() {

test/sharness/t0165-keystore.sh

+34-9
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,62 @@ test_description="Test keystore commands"
1111
test_init_ipfs
1212

1313
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+
1439
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)
1641
'
1742

1843
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)
2045
'
2146

2247
test_expect_success "both keys show up in list output" '
2348
echo bazed > list_exp &&
2449
echo foobarsa >> list_exp &&
2550
echo self >> list_exp
26-
ipfs key list | sort > list_out &&
51+
ipfs key list -f=b58mh | sort > list_out &&
2752
test_cmp list_exp list_out
2853
'
2954

3055
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
3358
'
3459

3560
test_expect_success "key list -l contains self key with peerID" '
3661
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"
3863
'
3964

4065
test_expect_success "key rm remove a key" '
4166
ipfs key rm foobarsa
4267
echo bazed > list_exp &&
4368
echo self >> list_exp
44-
ipfs key list | sort > list_out &&
69+
ipfs key list -f=b58mh | sort > list_out &&
4570
test_cmp list_exp list_out
4671
'
4772

@@ -54,12 +79,12 @@ test_key_cmd() {
5479
ipfs key rename bazed fooed
5580
echo fooed > list_exp &&
5681
echo self >> list_exp
57-
ipfs key list | sort > list_out &&
82+
ipfs key list -f=b58mh | sort > list_out &&
5883
test_cmp list_exp list_out
5984
'
6085

6186
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) &&
6388
ipfs key rename key1 key2 >rs &&
6489
echo "Key $key_content renamed to key2" >expect &&
6590
test_cmp rs expect

0 commit comments

Comments
 (0)