Skip to content

Commit eca2e5e

Browse files
authored
Merge pull request ipfs/interface-go-ipfs-core#65 from ipfs/petar/namefmt
Add ID formatting functions, used by various IPFS cli commands This commit was moved from ipfs/interface-go-ipfs-core@b935dfe
2 parents 82e9a39 + 91b27a6 commit eca2e5e

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

coreapi/iface/idfmt.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package iface
2+
3+
import (
4+
peer "github.com/libp2p/go-libp2p-core/peer"
5+
mbase "github.com/multiformats/go-multibase"
6+
)
7+
8+
func FormatKeyID(id peer.ID) string {
9+
if s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil {
10+
panic(err)
11+
} else {
12+
return s
13+
}
14+
}
15+
16+
// FormatKey formats the given IPNS key in a canonical way.
17+
func FormatKey(key Key) string {
18+
return FormatKeyID(key.ID())
19+
}

coreapi/iface/tests/key.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
"strings"
66
"testing"
77

8-
"github.com/ipfs/interface-go-ipfs-core"
8+
cid "github.com/ipfs/go-cid"
9+
coreiface "github.com/ipfs/interface-go-ipfs-core"
10+
iface "github.com/ipfs/interface-go-ipfs-core"
911
opt "github.com/ipfs/interface-go-ipfs-core/options"
12+
mbase "github.com/multiformats/go-multibase"
1013
)
1114

1215
func (tp *TestSuite) TestKey(t *testing.T) {
@@ -64,8 +67,8 @@ func (tp *TestSuite) TestListSelf(t *testing.T) {
6467
t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name())
6568
}
6669

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

@@ -134,9 +137,30 @@ func (tp *TestSuite) TestGenerate(t *testing.T) {
134137
t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
135138
}
136139

137-
if !strings.HasPrefix(k.Path().String(), "/ipns/Qm") {
138-
t.Errorf("expected the key to be prefixed with '/ipns/Qm', got '%s'", k.Path().String())
140+
verifyIPNSPath(t, k.Path().String())
141+
}
142+
143+
func verifyIPNSPath(t *testing.T, p string) bool {
144+
t.Helper()
145+
if !strings.HasPrefix(p, "/ipns/") {
146+
t.Errorf("path %q does not look like an IPNS path", p)
147+
return false
148+
}
149+
k := p[len("/ipns/"):]
150+
c, err := cid.Decode(k)
151+
if err != nil {
152+
t.Errorf("failed to decode IPNS key %q (%v)", k, err)
153+
return false
154+
}
155+
b36, err := c.StringOfBase(mbase.Base36)
156+
if err != nil {
157+
t.Fatalf("cid cannot format itself in b36")
158+
return false
159+
}
160+
if b36 != k {
161+
t.Errorf("IPNS key is not base36")
139162
}
163+
return true
140164
}
141165

142166
func (tp *TestSuite) TestGenerateSize(t *testing.T) {
@@ -157,9 +181,7 @@ func (tp *TestSuite) TestGenerateSize(t *testing.T) {
157181
t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
158182
}
159183

160-
if !strings.HasPrefix(k.Path().String(), "/ipns/Qm") {
161-
t.Errorf("expected the key to be prefixed with '/ipns/Qm', got '%s'", k.Path().String())
162-
}
184+
verifyIPNSPath(t, k.Path().String())
163185
}
164186

165187
func (tp *TestSuite) TestGenerateType(t *testing.T) {
@@ -256,15 +278,8 @@ func (tp *TestSuite) TestList(t *testing.T) {
256278
return
257279
}
258280

259-
if !strings.HasPrefix(l[0].Path().String(), "/ipns/Qm") {
260-
t.Fatalf("expected key 0 to be prefixed with '/ipns/Qm', got '%s'", l[0].Name())
261-
return
262-
}
263-
264-
if !strings.HasPrefix(l[1].Path().String(), "/ipns/Qm") {
265-
t.Fatalf("expected key 1 to be prefixed with '/ipns/Qm', got '%s'", l[1].Name())
266-
return
267-
}
281+
verifyIPNSPath(t, l[0].Path().String())
282+
verifyIPNSPath(t, l[1].Path().String())
268283
}
269284

270285
func (tp *TestSuite) TestRename(t *testing.T) {

coreapi/iface/tests/name.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package tests
22

33
import (
44
"context"
5-
path "github.com/ipfs/interface-go-ipfs-core/path"
65
"io"
76
"math/rand"
87
gopath "path"
98
"testing"
109
"time"
1110

12-
"github.com/ipfs/go-ipfs-files"
13-
ipath "github.com/ipfs/go-path"
11+
path "github.com/ipfs/interface-go-ipfs-core/path"
12+
13+
files "github.com/ipfs/go-ipfs-files"
1414

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

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

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

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

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

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

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

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

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

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

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

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

262262
if e.Value().String() != p.String() {

0 commit comments

Comments
 (0)