Skip to content

Commit

Permalink
Update to use the new string representation of a Multihash.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevina committed Aug 31, 2018
1 parent 193039c commit 78513ff
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 54 deletions.
36 changes: 18 additions & 18 deletions cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -295,12 +296,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.
Expand All @@ -316,8 +317,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
}

Expand Down Expand Up @@ -358,18 +358,18 @@ func (c Cid) StringOfBase(base mbase.Encoding) (string, error) {

// 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.
Expand Down Expand Up @@ -438,10 +438,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(),
}
Expand Down
3 changes: 1 addition & 2 deletions cid_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cid

import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
Expand Down Expand Up @@ -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")
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"gxDependencies": [
{
"author": "whyrusleeping",
"hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8",
"hash": "QmbdgmS5VjypczPKycTRH9GnyKt6WQLsQERyLUzaJECHh6",
"name": "go-multihash",
"version": "1.0.8"
"version": "2.0.0"
},
{
"author": "whyrusleeping",
Expand Down
32 changes: 0 additions & 32 deletions varint.go

This file was deleted.

0 comments on commit 78513ff

Please sign in to comment.