Skip to content

Commit

Permalink
IPNS format keys in b36cid
Browse files Browse the repository at this point in the history
  • Loading branch information
petar committed Jul 30, 2020
1 parent b7eed86 commit 92e2133
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 239 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ jobs:
command: |
npm init -y
npm install ipfs@^0.48.1
npm install ipfs-interop@^2.0.0
npm install ipfs-interop@^2.0.1
npm install mocha-circleci-reporter@0.0.3
working_directory: ~/ipfs/go-ipfs/interop
- run:
Expand Down
33 changes: 33 additions & 0 deletions core/commands/fmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package commands

import (
"fmt"

peer "github.com/libp2p/go-libp2p-core/peer"
mbase "github.com/multiformats/go-multibase"
)

func verifyIDFormatLabel(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")
}
}
31 changes: 2 additions & 29 deletions core/commands/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
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"
mbase "github.com/multiformats/go-multibase"
)

var KeyCmd = &cmds.Command{
Expand Down Expand Up @@ -94,7 +92,7 @@ var keyGenCmd = &cmds.Command{
if sizefound {
opts = append(opts, options.Key.Size(size))
}
if err = verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil {
if err = verifyIDFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil {
return err
}

Expand All @@ -118,31 +116,6 @@ var keyGenCmd = &cmds.Command{
Type: KeyOutput{},
}

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")
}
}

var keyListCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List all local keypairs",
Expand All @@ -152,7 +125,7 @@ var keyListCmd = &cmds.Command{
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 {
if err := verifyIDFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (k *key) Name() string {

// Path returns the path of the key.
func (k *key) Path() path.Path {
return path.New(ipfspath.Join([]string{"/ipns", k.peerID.Pretty()}))
return path.New(ipfspath.Join([]string{"/ipns", coreiface.FormatKeyID(k.peerID)}))
}

// ID returns key PeerID
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.Nam
}

return &ipnsEntry{
name: pid.Pretty(),
name: coreiface.FormatKeyID(pid),
value: p,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/ipfs/go-path v0.0.7
github.com/ipfs/go-unixfs v0.2.4
github.com/ipfs/go-verifcid v0.0.1
github.com/ipfs/interface-go-ipfs-core v0.3.0
github.com/ipfs/interface-go-ipfs-core v0.4.0
github.com/ipld/go-car v0.1.0
github.com/jbenet/go-is-domain v1.0.5
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2
github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0=
github.com/ipfs/interface-go-ipfs-core v0.3.0 h1:oZdLLfh256gPGcYPURjivj/lv296GIcr8mUqZUnXOEI=
github.com/ipfs/interface-go-ipfs-core v0.3.0/go.mod h1:Tihp8zxGpUeE3Tokr94L6zWZZdkRQvG5TL6i9MuNE+s=
github.com/ipfs/interface-go-ipfs-core v0.4.0 h1:+mUiamyHIwedqP8ZgbCIwpy40oX7QcXUbo4CZOeJVJg=
github.com/ipfs/interface-go-ipfs-core v0.4.0/go.mod h1:UJBcU6iNennuI05amq3FQ7g0JHUkibHFAfhfUIy927o=
github.com/ipld/go-car v0.1.0 h1:AaIEA5ITRnFA68uMyuIPYGM2XXllxsu8sNjFJP797us=
github.com/ipld/go-car v0.1.0/go.mod h1:RCWzaUh2i4mOEkB3W45Vc+9jnS/M6Qay5ooytiBHl3g=
github.com/ipld/go-ipld-prime v0.0.2-0.20191108012745-28a82f04c785 h1:fASnkvtR+SmB2y453RxmDD3Uvd4LonVUgFGk9JoDaZs=
Expand Down
6 changes: 5 additions & 1 deletion test/sharness/lib/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,13 @@ file_size() {
$_STAT "$1"
}

# len 46: 2048-bit RSA keys, b58mh-encoded
# len 52: ED25519 keys, b58mh-encoded
# len 56: 2048-bit RSA keys, b36cid-encoded
# len 62: ED25519 keys, b36cid-encoded
test_check_peerid() {
peeridlen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
test "$peeridlen" = "46" -o "$peeridlen" = "52" -o "$peeridlen" = "62" || {
test "$peeridlen" = "46" -o "$peeridlen" = "52" -o "$peeridlen" = "56" -o "$peeridlen" = "62" || {
echo "Bad peerid '$1' with len '$peeridlen'"
return 1
}
Expand Down
Loading

0 comments on commit 92e2133

Please sign in to comment.