@@ -29,6 +29,7 @@ import (
2929
3030 mbase "github.com/multiformats/go-multibase"
3131 mh "github.com/multiformats/go-multihash"
32+ strbinary "github.com/multiformats/go-multihash/strbinary"
3233)
3334
3435// UnsupportedVersionString just holds an error message
@@ -133,18 +134,18 @@ var CodecToStr = map[uint64]string{
133134// compatibility with the plain-multihash format used used in IPFS.
134135// NewCidV1 should be used preferentially.
135136func NewCidV0 (mhash mh.Multihash ) Cid {
136- return Cid {string ( mhash )}
137+ return Cid {mhash . Binary ( )}
137138}
138139
139140// NewCidV1 returns a new Cid using the given multicodec-packed
140141// content type.
141142func NewCidV1 (codecType uint64 , mhash mh.Multihash ) Cid {
142- hashlen := len (mhash )
143+ hashlen := len (mhash . Binary () )
143144 // two 8 bytes (max) numbers plus hash
144145 buf := make ([]byte , 2 * binary .MaxVarintLen64 + hashlen )
145146 n := binary .PutUvarint (buf , 1 )
146147 n += binary .PutUvarint (buf [n :], codecType )
147- cn := copy (buf [n :], mhash )
148+ cn := copy (buf [n :], mhash . Binary () )
148149 if cn != hashlen {
149150 panic ("copy hash length is inconsistent" )
150151 }
@@ -215,7 +216,7 @@ func Decode(v string) (Cid, error) {
215216 return Nil , err
216217 }
217218
218- return NewCidV0 (hash ), nil
219+ return NewCidV0 (hash . Cast () ), nil
219220 }
220221
221222 _ , data , err := mbase .Decode (v )
@@ -277,7 +278,7 @@ func Cast(data []byte) (Cid, error) {
277278 return Nil , err
278279 }
279280
280- return NewCidV0 (h ), nil
281+ return NewCidV0 (h . Cast () ), nil
281282 }
282283
283284 vers , n := binary .Uvarint (data )
@@ -316,8 +317,7 @@ func (c Cid) Type() uint64 {
316317 if c .Version () == 0 {
317318 return DagProtobuf
318319 }
319- _ , n := uvarint (c .str )
320- codec , _ := uvarint (c .str [n :])
320+ codec , _ := strbinary .Uvarint (c .str [1 :])
321321 return codec
322322}
323323
@@ -358,18 +358,16 @@ func (c Cid) StringOfBase(base mbase.Encoding) (string, error) {
358358
359359// Hash returns the multihash contained by a Cid.
360360func (c Cid ) Hash () mh.Multihash {
361- bytes := c .Bytes ()
362-
363361 if c .Version () == 0 {
364- return mh .Multihash ( bytes )
362+ return mh .FromBinary ( c . str )
365363 }
366364
367- // skip version length
368- _ , n1 := binary . Uvarint ( bytes )
369- // skip codec length
370- _ , n2 := binary . Uvarint ( bytes [ n1 :])
365+ // Skip 1 byte for the version prefix
366+ i := 1
367+ // Skip the Codec prefix
368+ i += strbinary . UvarintLen ( c . str [ i :])
371369
372- return mh .Multihash ( bytes [ n1 + n2 :])
370+ return mh .FromBinary ( c . str [ i :])
373371}
374372
375373// Bytes returns the byte representation of a Cid.
@@ -438,10 +436,10 @@ func (c Cid) Loggable() map[string]interface{} {
438436
439437// Prefix builds and returns a Prefix out of a Cid.
440438func (c Cid ) Prefix () Prefix {
441- dec , _ := mh . Decode ( c .Hash ()) // assuming we got a valid multiaddr, this will not error
439+ mhType , mhLength , _ := c .Hash (). Parts ()
442440 return Prefix {
443- MhType : dec . Code ,
444- MhLength : dec . Length ,
441+ MhType : mhType ,
442+ MhLength : mhLength ,
445443 Version : c .Version (),
446444 Codec : c .Type (),
447445 }
@@ -463,7 +461,7 @@ type Prefix struct {
463461// Sum uses the information in a prefix to perform a multihash.Sum()
464462// and return a newly constructed Cid with the resulting multihash.
465463func (p Prefix ) Sum (data []byte ) (Cid , error ) {
466- hash , err := mh .Sum ( data , p .MhType , p .MhLength )
464+ hash , err := mh.Builder { Type : p .MhType , Length : p .MhLength }. Sum ( data )
467465 if err != nil {
468466 return Nil , err
469467 }
0 commit comments