diff --git a/cid.go b/cid.go index 7859f75..30c1ea0 100644 --- a/cid.go +++ b/cid.go @@ -337,6 +337,18 @@ func (c *Cid) StringOfBase(base mbase.Encoding) (string, error) { } } +// Format return the string representation of a Cid +func (c *Cid) Format(base mbase.Encoder) string { + switch c.version { + case 0: + return c.hash.B58String() + case 1: + return base.Encode(c.bytesV1()) + default: + panic("not possible to reach this point") + } +} + // Hash returns the multihash contained by a Cid. func (c *Cid) Hash() mh.Multihash { return c.hash diff --git a/cid_test.go b/cid_test.go index ed690d8..8ef5d51 100644 --- a/cid_test.go +++ b/cid_test.go @@ -152,6 +152,15 @@ func TestBasesMarshaling(t *testing.T) { } assertEqual(t, cid, out2) + + prefix, err := mbase.NewEncoder(b) + if err != nil { + t.Fatal(err) + } + s2 := cid.Format(prefix) + if s != s2 { + t.Fatalf("'%s' != '%s'", s, s2) + } } }