Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
add id and key formatting utils; format keys as b36cid by default; up…
Browse files Browse the repository at this point in the history
…date tests
  • Loading branch information
petar committed Jul 22, 2020
1 parent c1c3731 commit 9ae21a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
19 changes: 19 additions & 0 deletions idfmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package iface

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

func FormatKeyID(id peer.ID) string {
if s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil {
panic(err)
} else {
return s
}
}

// FormatKey formats the given IPNS key in a canonical way.
func FormatKey(key Key) string {
return FormatKeyID(key.ID())
}
7 changes: 4 additions & 3 deletions tests/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"strings"
"testing"

"github.com/ipfs/interface-go-ipfs-core"
coreiface "github.com/ipfs/interface-go-ipfs-core"
iface "github.com/ipfs/interface-go-ipfs-core"
opt "github.com/ipfs/interface-go-ipfs-core/options"
)

Expand Down Expand Up @@ -64,8 +65,8 @@ func (tp *TestSuite) TestListSelf(t *testing.T) {
t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name())
}

if keys[0].Path().String() != "/ipns/"+self.ID().Pretty() {
t.Errorf("expected the key to have path '/ipns/%s', got '%s'", self.ID().Pretty(), keys[0].Path().String())
if keys[0].Path().String() != "/ipns/"+coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected the key to have path '/ipns/%s', got '%s'", coreiface.FormatKeyID(self.ID()), keys[0].Path().String())
}
}

Expand Down
30 changes: 15 additions & 15 deletions tests/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package tests

import (
"context"
path "github.com/ipfs/interface-go-ipfs-core/path"
"io"
"math/rand"
gopath "path"
"testing"
"time"

"github.com/ipfs/go-ipfs-files"
ipath "github.com/ipfs/go-path"
path "github.com/ipfs/interface-go-ipfs-core/path"

files "github.com/ipfs/go-ipfs-files"

coreiface "github.com/ipfs/interface-go-ipfs-core"
opt "github.com/ipfs/interface-go-ipfs-core/options"
Expand Down Expand Up @@ -70,8 +70,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
t.Fatal(err)
}

if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
if e.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String() {
Expand Down Expand Up @@ -100,8 +100,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
t.Fatal(err)
}

if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
if e.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String()+"/test" {
Expand Down Expand Up @@ -130,8 +130,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
t.Fatal(err)
}

if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
if e.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String() {
Expand Down Expand Up @@ -160,8 +160,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
t.Fatal(err)
}

if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
if e.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String()+"/a" {
Expand Down Expand Up @@ -212,8 +212,8 @@ func (tp *TestSuite) TestBasicPublishResolveKey(t *testing.T) {
t.Fatal(err)
}

if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() {
t.Errorf("expected e.Name to equal '%s', got '%s'", e.Name(), k.Path().String())
if e.Name() != coreiface.FormatKey(k) {
t.Errorf("expected e.Name to equal %s, got '%s'", e.Name(), coreiface.FormatKey(k))
}

if e.Value().String() != p.String() {
Expand Down Expand Up @@ -255,8 +255,8 @@ func (tp *TestSuite) TestBasicPublishResolveTimeout(t *testing.T) {
t.Fatal(err)
}

if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
if e.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String() {
Expand Down

0 comments on commit 9ae21a7

Please sign in to comment.