diff --git a/cid.go b/cid.go index fb6496e..72e822a 100644 --- a/cid.go +++ b/cid.go @@ -29,6 +29,7 @@ import ( mbase "github.com/multiformats/go-multibase" mh "github.com/multiformats/go-multihash" + strbinary "github.com/multiformats/go-multihash/strbinary" ) // UnsupportedVersionString just holds an error message @@ -133,18 +134,18 @@ var CodecToStr = map[uint64]string{ // compatibility with the plain-multihash format used used in IPFS. // NewCidV1 should be used preferentially. func NewCidV0(mhash mh.Multihash) Cid { - return Cid{string(mhash)} + return Cid{mhash.Binary()} } // NewCidV1 returns a new Cid using the given multicodec-packed // content type. func NewCidV1(codecType uint64, mhash mh.Multihash) Cid { - hashlen := len(mhash) + hashlen := len(mhash.Binary()) // two 8 bytes (max) numbers plus hash buf := make([]byte, 2*binary.MaxVarintLen64+hashlen) n := binary.PutUvarint(buf, 1) n += binary.PutUvarint(buf[n:], codecType) - cn := copy(buf[n:], mhash) + cn := copy(buf[n:], mhash.Binary()) if cn != hashlen { panic("copy hash length is inconsistent") } @@ -291,12 +292,12 @@ func Cast(data []byte) (Cid, error) { } rest := data[n+cn:] - h, err := mh.Cast(rest) + _, err := mh.FromBinary(string(rest)) if err != nil { return Nil, err } - return Cid{string(data[0 : n+cn+len(h)])}, nil + return Cid{string(data)}, nil } // Version returns the Cid version. @@ -312,8 +313,7 @@ func (c Cid) Type() uint64 { if c.Version() == 0 { return DagProtobuf } - _, n := uvarint(c.str) - codec, _ := uvarint(c.str[n:]) + codec, _ := strbinary.Uvarint(c.str[1:]) return codec } @@ -368,18 +368,18 @@ func (c Cid) Encode(base mbase.Encoder) string { // Hash returns the multihash contained by a Cid. func (c Cid) Hash() mh.Multihash { - bytes := c.Bytes() - if c.Version() == 0 { - return mh.Multihash(bytes) + h, _ := mh.FromBinary(c.str) + return h } - // skip version length - _, n1 := binary.Uvarint(bytes) - // skip codec length - _, n2 := binary.Uvarint(bytes[n1:]) + // Skip 1 byte for the version prefix + i := 1 + // Skip the Codec prefix + i += strbinary.UvarintLen(c.str[i:]) - return mh.Multihash(bytes[n1+n2:]) + h, _ := mh.FromBinary(c.str[i:]) + return h } // Bytes returns the byte representation of a Cid. @@ -448,10 +448,10 @@ func (c Cid) Loggable() map[string]interface{} { // Prefix builds and returns a Prefix out of a Cid. func (c Cid) Prefix() Prefix { - dec, _ := mh.Decode(c.Hash()) // assuming we got a valid multiaddr, this will not error + mhType, digest := c.Hash().Parts() return Prefix{ - MhType: dec.Code, - MhLength: dec.Length, + MhType: mhType, + MhLength: len(digest), Version: c.Version(), Codec: c.Type(), } diff --git a/cid_test.go b/cid_test.go index 1b1e9c4..5151439 100644 --- a/cid_test.go +++ b/cid_test.go @@ -1,7 +1,6 @@ package cid import ( - "bytes" "encoding/json" "fmt" "math/rand" @@ -46,7 +45,7 @@ func assertEqual(t *testing.T, a, b Cid) { t.Fatal("mismatch on version") } - if !bytes.Equal(a.Hash(), b.Hash()) { + if a.Hash() != b.Hash() { t.Fatal("multihash mismatch") } } diff --git a/package.json b/package.json index 8b821c4..e209910 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ "gxDependencies": [ { "author": "whyrusleeping", - "hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8", + "hash": "QmbdgmS5VjypczPKycTRH9GnyKt6WQLsQERyLUzaJECHh6", "name": "go-multihash", - "version": "1.0.8" + "version": "2.0.0" }, { "author": "whyrusleeping", diff --git a/varint.go b/varint.go deleted file mode 100644 index 391c1f4..0000000 --- a/varint.go +++ /dev/null @@ -1,34 +0,0 @@ -package cid - -// Version of varint function that work with a string rather than -// []byte to avoid unnecessary allocation - -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license as given at https://golang.org/LICENSE - -// uvarint decodes a uint64 from buf and returns that value and the -// number of characters read (> 0). If an error occurred, the value is 0 -// and the number of bytes n is <= 0 meaning: -// -// n == 0: buf too small -// n < 0: value larger than 64 bits (overflow) -// and -n is the number of bytes read -// -func uvarint(buf string) (uint64, int) { - var x uint64 - var s uint - // we have a binary string so we can't use a range loope - for i := 0; i < len(buf); i++ { - b := buf[i] - if b < 0x80 { - if i > 9 || i == 9 && b > 1 { - return 0, -(i + 1) // overflow - } - return x | uint64(b)<