From 0f09109d9f226bd0d6061715d81793c16890e42e Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 9 Aug 2018 02:37:09 -0400 Subject: [PATCH] Replace DecodeV2 with ExtractEncoding --- cid-fmt/main.go | 7 ++++--- cid-fmt/main_test.go | 4 ++-- cid.go | 41 +++++++++++++++++++++++++++-------------- format_test.go | 10 +++++----- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/cid-fmt/main.go b/cid-fmt/main.go index 8d7f53b..988126a 100644 --- a/cid-fmt/main.go +++ b/cid-fmt/main.go @@ -69,15 +69,16 @@ outer: } } for _, cidStr := range args[1:] { - base, cid, err := c.DecodeV2(cidStr) + cid, err := c.Decode(cidStr) if err != nil { fmt.Fprintf(os.Stdout, "!INVALID_CID!\n") errorMsg("%s: %v", cidStr, err) // Don't abort on a bad cid continue } - if newBase != -1 { - base = newBase + base := newBase + if newBase == -1 { + base, _ = c.ExtractEncoding(cidStr) } if verConv != nil { cid, err = verConv(cid) diff --git a/cid-fmt/main_test.go b/cid-fmt/main_test.go index 5161258..ce66c0a 100644 --- a/cid-fmt/main_test.go +++ b/cid-fmt/main_test.go @@ -10,7 +10,7 @@ import ( func TestCidConv(t *testing.T) { cidv0 := "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" cidv1 := "zdj7WbTaiJT1fgatdet9Ei9iDB5hdCxkbVyhyh8YTUnXMiwYi" - _, cid, err := c.DecodeV2(cidv0) + cid, err := c.Decode(cidv0) if err != nil { t.Fatal(err) } @@ -34,7 +34,7 @@ func TestCidConv(t *testing.T) { func TestBadCidConv(t *testing.T) { // this cid is a raw leaf and should not be able to convert to cidv0 cidv1 := "zb2rhhzX7uSKrtQ2ZZXFAabKiKFYZrJqKY2KE1cJ8yre2GSWZ" - _, cid, err := c.DecodeV2(cidv1) + cid, err := c.Decode(cidv1) if err != nil { t.Fatal(err) } diff --git a/cid.go b/cid.go index 8992055..337e45f 100644 --- a/cid.go +++ b/cid.go @@ -213,34 +213,47 @@ func Parse(v interface{}) (*Cid, error) { // starting with "Qm" are considered CidV0 and treated directly // as B58-encoded multihashes. func Decode(v string) (*Cid, error) { - _, cid, err := DecodeV2(v) - return cid, err -} - -// DecodeV2 is like Decide but also returns the Multibase encoding the -// Cid was encoded in. EXPERIMENTAL and interface may change at any time. -func DecodeV2(v string) (mbase.Encoding, *Cid, error) { if len(v) < 2 { - return 0, nil, ErrCidTooShort + return nil, ErrCidTooShort } if len(v) == 46 && v[:2] == "Qm" { hash, err := mh.FromB58String(v) if err != nil { - return 0, nil, err + return nil, err } - return mbase.Base58BTC, NewCidV0(hash), nil + return NewCidV0(hash), nil } - base, data, err := mbase.Decode(v) + _, data, err := mbase.Decode(v) if err != nil { - return 0, nil, err + return nil, err } - cid, err := Cast(data) + return Cast(data) +} - return base, cid, err +// Extract the encoding from a Cid. If Decode on the same string did +// not return an error neither will this function. +func ExtractEncoding(v string) (mbase.Encoding, error) { + if len(v) < 2 { + return -1, ErrCidTooShort + } + + if len(v) == 46 && v[:2] == "Qm" { + return mbase.Base58BTC, nil + } + + encoding := mbase.Encoding(v[0]) + + // check encoding is valid + _, err := mbase.NewEncoder(encoding) + if err != nil { + return -1, err + } + + return encoding, nil } func uvError(read int) error { diff --git a/format_test.go b/format_test.go index 7a92bb8..019d85c 100644 --- a/format_test.go +++ b/format_test.go @@ -55,13 +55,14 @@ func TestFmt(t *testing.T) { } func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, result string) { - base, cid, err := DecodeV2(cidStr) - if newBase != -1 { - base = newBase - } + cid, err := Decode(cidStr) if err != nil { t.Fatal(err) } + base := newBase + if newBase == -1 { + base, _ = ExtractEncoding(cidStr) + } str, err := Format(fmtStr, base, cid) if err != nil { t.Fatal(err) @@ -70,4 +71,3 @@ func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, re t.Error(fmt.Sprintf("expected: %s; but got: %s", result, str)) } } -