From e7af20a91a99db3a5d0fa5acc766ea09f316835b Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Thu, 9 Jul 2020 15:18:30 -0700 Subject: [PATCH 1/7] add flag to "ipfs key gen" to output keys in b36/CIDv1, also make it default --- core/commands/keystore.go | 14 +++++++++++--- go.mod | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index f22d64afae3..ab0d3496eae 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -5,10 +5,11 @@ import ( "io" "text/tabwriter" - cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" - cmds "github.com/ipfs/go-ipfs-cmds" + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" options "github.com/ipfs/interface-go-ipfs-core/options" + peer "github.com/libp2p/go-libp2p-core/peer" + b36 "github.com/multiformats/go-base36" ) var KeyCmd = &cmds.Command{ @@ -56,6 +57,7 @@ type KeyRenameOutput struct { const ( keyStoreTypeOptionName = "type" keyStoreSizeOptionName = "size" + cidOptionName = "cid" ) var keyGenCmd = &cmds.Command{ @@ -65,6 +67,7 @@ var keyGenCmd = &cmds.Command{ Options: []cmds.Option{ cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault("rsa"), cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"), + cmds.BoolOption(cidOptionName, "c", "return a base-36 CIDv1 encoding of the key").WithDefault(true), }, Arguments: []cmds.Argument{ cmds.StringArg("name", true, false, "name of key to create"), @@ -98,9 +101,14 @@ var keyGenCmd = &cmds.Command{ return err } + id := key.ID().Pretty() + if req.Options[cidOptionName].(bool) { + id = b36.EncodeToStringLc(peer.ToCid(key.ID()).Bytes()) + } + return cmds.EmitOnce(res, &KeyOutput{ Name: name, - Id: key.ID().Pretty(), + Id: id, }) }, Encoders: cmds.EncoderMap{ diff --git a/go.mod b/go.mod index 25b5b6da722..7dac7eb6111 100644 --- a/go.mod +++ b/go.mod @@ -89,6 +89,7 @@ require ( github.com/miekg/dns v1.1.29 // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/mr-tron/base58 v1.1.3 + github.com/multiformats/go-base36 v0.1.0 github.com/multiformats/go-multiaddr v0.2.2 github.com/multiformats/go-multiaddr-dns v0.2.0 github.com/multiformats/go-multiaddr-net v0.1.5 From c423bfed5eb011af980d50084bde42a998c994e6 Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Thu, 9 Jul 2020 21:36:43 -0700 Subject: [PATCH 2/7] add cid output format flag to "ipfs key list" --- core/commands/keystore.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index ab0d3496eae..1319029cd50 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -101,14 +101,9 @@ var keyGenCmd = &cmds.Command{ return err } - id := key.ID().Pretty() - if req.Options[cidOptionName].(bool) { - id = b36.EncodeToStringLc(peer.ToCid(key.ID()).Bytes()) - } - return cmds.EmitOnce(res, &KeyOutput{ Name: name, - Id: id, + Id: formatID(key.ID(), req.Options[cidOptionName].(bool)), }) }, Encoders: cmds.EncoderMap{ @@ -120,12 +115,20 @@ var keyGenCmd = &cmds.Command{ Type: KeyOutput{}, } +func formatID(id peer.ID, useCID bool) string { + if useCID { + return b36.EncodeToStringLc(peer.ToCid(id).Bytes()) + } + return id.Pretty() +} + var keyListCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "List all local keypairs", }, Options: []cmds.Option{ cmds.BoolOption("l", "Show extra information about keys."), + cmds.BoolOption(cidOptionName, "c", "return a base-36 CIDv1 encoding of the keys").WithDefault(true), }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { api, err := cmdenv.GetApi(env, req) @@ -141,7 +144,10 @@ var keyListCmd = &cmds.Command{ list := make([]KeyOutput, 0, len(keys)) for _, key := range keys { - list = append(list, KeyOutput{Name: key.Name(), Id: key.ID().Pretty()}) + list = append(list, KeyOutput{ + Name: key.Name(), + Id: formatID(key.ID(), req.Options[cidOptionName].(bool)), + }) } return cmds.EmitOnce(res, &KeyOutputList{list}) From ef38fc5d3a95cd5cdc3e1fe8c48d3952a823b97d Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Fri, 10 Jul 2020 10:57:57 -0700 Subject: [PATCH 3/7] change flag format --- core/commands/keystore.go | 45 ++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index 1319029cd50..d39eae051b5 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -9,7 +9,7 @@ import ( cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" options "github.com/ipfs/interface-go-ipfs-core/options" peer "github.com/libp2p/go-libp2p-core/peer" - b36 "github.com/multiformats/go-base36" + mbase "github.com/multiformats/go-multibase" ) var KeyCmd = &cmds.Command{ @@ -57,7 +57,7 @@ type KeyRenameOutput struct { const ( keyStoreTypeOptionName = "type" keyStoreSizeOptionName = "size" - cidOptionName = "cid" + keyFormatOptionName = "format" ) var keyGenCmd = &cmds.Command{ @@ -67,7 +67,7 @@ var keyGenCmd = &cmds.Command{ Options: []cmds.Option{ cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault("rsa"), cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"), - cmds.BoolOption(cidOptionName, "c", "return a base-36 CIDv1 encoding of the key").WithDefault(true), + cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b36cid"), }, Arguments: []cmds.Argument{ cmds.StringArg("name", true, false, "name of key to create"), @@ -94,6 +94,9 @@ var keyGenCmd = &cmds.Command{ if sizefound { opts = append(opts, options.Key.Size(size)) } + if err = verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil { + return err + } key, err := api.Key().Generate(req.Context, name, opts...) @@ -103,7 +106,7 @@ var keyGenCmd = &cmds.Command{ return cmds.EmitOnce(res, &KeyOutput{ Name: name, - Id: formatID(key.ID(), req.Options[cidOptionName].(bool)), + Id: formatID(key.ID(), req.Options[keyFormatOptionName].(string)), }) }, Encoders: cmds.EncoderMap{ @@ -115,11 +118,29 @@ var keyGenCmd = &cmds.Command{ Type: KeyOutput{}, } -func formatID(id peer.ID, useCID bool) string { - if useCID { - return b36.EncodeToStringLc(peer.ToCid(id).Bytes()) +func verifyFormatLabel(formatLabel string) error { + switch formatLabel { + case "b58mh": + return nil + case "b36cid": + return nil + } + return fmt.Errorf("invalid output format option") +} + +func formatID(id peer.ID, formatLabel string) string { + switch formatLabel { + case "b58mh": + return id.Pretty() + case "b36cid": + if s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil { + panic(err) + } else { + return s + } + default: + panic("unreachable") } - return id.Pretty() } var keyListCmd = &cmds.Command{ @@ -128,9 +149,13 @@ var keyListCmd = &cmds.Command{ }, Options: []cmds.Option{ cmds.BoolOption("l", "Show extra information about keys."), - cmds.BoolOption(cidOptionName, "c", "return a base-36 CIDv1 encoding of the keys").WithDefault(true), + cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b36cid"), }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + if err := verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil { + return err + } + api, err := cmdenv.GetApi(env, req) if err != nil { return err @@ -146,7 +171,7 @@ var keyListCmd = &cmds.Command{ for _, key := range keys { list = append(list, KeyOutput{ Name: key.Name(), - Id: formatID(key.ID(), req.Options[cidOptionName].(bool)), + Id: formatID(key.ID(), req.Options[keyFormatOptionName].(string)), }) } From 1e029e873e077b8463b71e31429cae2ef4900f53 Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Fri, 10 Jul 2020 11:43:14 -0700 Subject: [PATCH 4/7] update sharness --- test/sharness/t0100-name.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sharness/t0100-name.sh b/test/sharness/t0100-name.sh index 96d2ce5ea82..10ee52f64ae 100755 --- a/test/sharness/t0100-name.sh +++ b/test/sharness/t0100-name.sh @@ -101,7 +101,7 @@ test_expect_failure "publish with our explicit node ID looks good" ' # publish with an explicit node ID as key name test_expect_success "generate and verify a new key" ' - NEWID=`ipfs key gen --type=rsa --size=2048 keyname` && + NEWID=`ipfs key gen -f=b58mh --type=rsa --size=2048 keyname` && test_check_peerid "${NEWID}" ' From e61a2632f56778fdfc6cda66789fafdb0c940ce3 Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Fri, 10 Jul 2020 11:54:25 -0700 Subject: [PATCH 5/7] more sharness --- test/sharness/t0160-resolve.sh | 2 +- test/sharness/t0165-keystore.sh | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/sharness/t0160-resolve.sh b/test/sharness/t0160-resolve.sh index 1524ed8d28e..6f63d6f31b3 100755 --- a/test/sharness/t0160-resolve.sh +++ b/test/sharness/t0160-resolve.sh @@ -23,7 +23,7 @@ test_expect_success "resolve: prepare dag" ' test_expect_success "resolve: prepare keys" ' self_hash=$(ipfs id -f="") && - alt_hash=$(ipfs key gen -t rsa alt) + alt_hash=$(ipfs key gen -f=b58mh -t rsa alt) ' test_resolve_setup_name() { diff --git a/test/sharness/t0165-keystore.sh b/test/sharness/t0165-keystore.sh index 617b0e2b5e3..1053a453f3e 100755 --- a/test/sharness/t0165-keystore.sh +++ b/test/sharness/t0165-keystore.sh @@ -12,36 +12,36 @@ test_init_ipfs test_key_cmd() { test_expect_success "create a new rsa key" ' - rsahash=$(ipfs key gen foobarsa --type=rsa --size=2048) + rsahash=$(ipfs key gen -f=b58mh foobarsa --type=rsa --size=2048) ' test_expect_success "create a new ed25519 key" ' - edhash=$(ipfs key gen bazed --type=ed25519) + edhash=$(ipfs key gen -f=b58mh bazed --type=ed25519) ' test_expect_success "both keys show up in list output" ' echo bazed > list_exp && echo foobarsa >> list_exp && echo self >> list_exp - ipfs key list | sort > list_out && + ipfs key list -f=b58mh | sort > list_out && test_cmp list_exp list_out ' test_expect_success "key hashes show up in long list output" ' - ipfs key list -l | grep $edhash > /dev/null && - ipfs key list -l | grep $rsahash > /dev/null + ipfs key list -f=b58mh -l | grep $edhash > /dev/null && + ipfs key list -f=b58mh -l | grep $rsahash > /dev/null ' test_expect_success "key list -l contains self key with peerID" ' PeerID="$(ipfs config Identity.PeerID)" - ipfs key list -l | grep "$PeerID\s\+self" + ipfs key list -f=b58mh -l | grep "$PeerID\s\+self" ' test_expect_success "key rm remove a key" ' ipfs key rm foobarsa echo bazed > list_exp && echo self >> list_exp - ipfs key list | sort > list_out && + ipfs key list -f=b58mh | sort > list_out && test_cmp list_exp list_out ' @@ -54,12 +54,12 @@ test_key_cmd() { ipfs key rename bazed fooed echo fooed > list_exp && echo self >> list_exp - ipfs key list | sort > list_out && + ipfs key list -f=b58mh | sort > list_out && test_cmp list_exp list_out ' test_expect_success "key rename rename key output succeeds" ' - key_content=$(ipfs key gen key1 --type=rsa --size=2048) && + key_content=$(ipfs key gen -f=b58mh key1 --type=rsa --size=2048) && ipfs key rename key1 key2 >rs && echo "Key $key_content renamed to key2" >expect && test_cmp rs expect From 15a90d1ca1e68cbc685201b191df72cf4e5e96df Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Fri, 10 Jul 2020 15:15:59 -0700 Subject: [PATCH 6/7] change default to b58mh --- core/commands/keystore.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index d39eae051b5..c9865fd93ee 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -67,7 +67,7 @@ var keyGenCmd = &cmds.Command{ Options: []cmds.Option{ cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault("rsa"), cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"), - cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b36cid"), + cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b58mh"), }, Arguments: []cmds.Argument{ cmds.StringArg("name", true, false, "name of key to create"), @@ -149,7 +149,7 @@ var keyListCmd = &cmds.Command{ }, Options: []cmds.Option{ cmds.BoolOption("l", "Show extra information about keys."), - cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b36cid"), + cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b58mh"), }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { if err := verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil { From b28a7b4908696e0fca2d4e540d7983020daedd2e Mon Sep 17 00:00:00 2001 From: Petar Maymounkov Date: Mon, 13 Jul 2020 08:17:47 -0700 Subject: [PATCH 7/7] add sharness tests for key gen format --- test/sharness/lib/test-lib.sh | 32 ++++++++++++++++++++++++++++++++ test/sharness/t0165-keystore.sh | 25 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/test/sharness/lib/test-lib.sh b/test/sharness/lib/test-lib.sh index 69766579a71..4b7fd32aa61 100644 --- a/test/sharness/lib/test-lib.sh +++ b/test/sharness/lib/test-lib.sh @@ -451,6 +451,38 @@ test_check_peerid() { } } +test_check_rsa2048_b58mh_peerid() { + peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") && + test "$peeridlen" = "46" || { + echo "Bad RSA2048 B58MH peerid '$1' with len '$peeridlen'" + return 1 + } +} + +test_check_ed25519_b58mh_peerid() { + peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") && + test "$peeridlen" = "46" || { + echo "Bad ED25519 B58MH peerid '$1' with len '$peeridlen'" + return 1 + } +} + +test_check_rsa2048_b36cid_peerid() { + peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") && + test "$peeridlen" = "56" || { + echo "Bad RSA2048 B36CID peerid '$1' with len '$peeridlen'" + return 1 + } +} + +test_check_ed25519_b36cid_peerid() { + peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") && + test "$peeridlen" = "62" || { + echo "Bad ED25519 B36CID peerid '$1' with len '$peeridlen'" + return 1 + } +} + convert_tcp_maddr() { echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }' } diff --git a/test/sharness/t0165-keystore.sh b/test/sharness/t0165-keystore.sh index 1053a453f3e..c4538c70eef 100755 --- a/test/sharness/t0165-keystore.sh +++ b/test/sharness/t0165-keystore.sh @@ -11,6 +11,31 @@ test_description="Test keystore commands" test_init_ipfs test_key_cmd() { +# test key output format +test_expect_success "create an RSA key and test B58MH multihash output" ' +PEERID=$(ipfs key gen -f=b58mh --type=rsa --size=2048 key_rsa) && +test_check_rsa2048_b58mh_peerid $PEERID +' + +test_expect_success "test RSA key B36CID multihash format" ' +PEERID=$(ipfs key list -f=b36cid -l | grep key_rsa | head -n 1 | cut -d " " -f1) && +test_check_rsa2048_b36cid_peerid $PEERID && +ipfs key rm key_rsa +' + +test_expect_success "create an ED25519 key and test multihash output" ' +PEERID=$(ipfs key gen -f=b36cid --type=ed25519 key_ed25519) && +test_check_ed25519_b36cid_peerid $PEERID +' + +test_expect_success "test ED25519 key B36CID multihash format" ' +PEERID=$(ipfs key list -f=b36cid -l | grep key_ed25519 | head -n 1 | cut -d " " -f1) && +test_check_ed25519_b36cid_peerid $PEERID && +ipfs key rm key_ed25519 +' +# end of format test + + test_expect_success "create a new rsa key" ' rsahash=$(ipfs key gen -f=b58mh foobarsa --type=rsa --size=2048) '