diff --git a/builder.go b/builder.go index a5a4806..9e22f64 100644 --- a/builder.go +++ b/builder.go @@ -34,7 +34,7 @@ func (p Prefix) WithCodec(c uint64) Builder { } func (p V0Builder) Sum(data []byte) (Cid, error) { - hash, err := mh.Sum(data, mh.SHA2_256, -1) + hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data) if err != nil { return Nil, err } @@ -53,11 +53,7 @@ func (p V0Builder) WithCodec(c uint64) Builder { } func (p V1Builder) Sum(data []byte) (Cid, error) { - mhLen := p.MhLength - if mhLen <= 0 { - mhLen = -1 - } - hash, err := mh.Sum(data, p.MhType, mhLen) + hash, err := mh.Builder{Type: p.MhType, Length: p.MhLength}.Sum(data) if err != nil { return Nil, err } diff --git a/builder_test.go b/builder_test.go index dc6f35c..b757f8b 100644 --- a/builder_test.go +++ b/builder_test.go @@ -17,7 +17,7 @@ func TestV0Builder(t *testing.T) { } // Construct c2 - hash, err := mh.Sum(data, mh.SHA2_256, -1) + hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data) if err != nil { t.Fatal(err) } @@ -42,7 +42,7 @@ func TestV1Builder(t *testing.T) { } // Construct c2 - hash, err := mh.Sum(data, mh.SHA2_256, -1) + hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data) if err != nil { t.Fatal(err) } diff --git a/cid.go b/cid.go index 2532413..aa0e795 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") } @@ -215,7 +216,7 @@ func Decode(v string) (Cid, error) { return Nil, err } - return NewCidV0(hash), nil + return NewCidV0(hash.Cast()), nil } _, data, err := mbase.Decode(v) @@ -277,7 +278,7 @@ func Cast(data []byte) (Cid, error) { return Nil, err } - return NewCidV0(h), nil + return NewCidV0(h.Cast()), nil } vers, n := binary.Uvarint(data) @@ -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 } @@ -358,18 +358,16 @@ 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) + return mh.FromBinary(c.str) } - // 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:]) + return mh.FromBinary(c.str[i:]) } // Bytes returns the byte representation of a Cid. @@ -438,10 +436,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, mhLength, _ := c.Hash().Parts() return Prefix{ - MhType: dec.Code, - MhLength: dec.Length, + MhType: mhType, + MhLength: mhLength, Version: c.Version(), Codec: c.Type(), } @@ -463,7 +461,7 @@ type Prefix struct { // Sum uses the information in a prefix to perform a multihash.Sum() // and return a newly constructed Cid with the resulting multihash. func (p Prefix) Sum(data []byte) (Cid, error) { - hash, err := mh.Sum(data, p.MhType, p.MhLength) + hash, err := mh.Builder{Type: p.MhType, Length: p.MhLength}.Sum(data) if err != nil { return Nil, err } diff --git a/cid_test.go b/cid_test.go index c363608..2ff0bf4 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") } } @@ -72,7 +71,7 @@ func TestTableForV0(t *testing.T) { } func TestBasicMarshaling(t *testing.T) { - h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4) + h, err := mh.Builder{Type: mh.SHA3}.Sum([]byte("TEST")) if err != nil { t.Fatal(err) } @@ -98,7 +97,7 @@ func TestBasicMarshaling(t *testing.T) { } func TestBasesMarshaling(t *testing.T) { - h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4) + h, err := mh.Builder{Type: mh.SHA3}.Sum([]byte("TEST")) if err != nil { t.Fatal(err) } @@ -198,7 +197,7 @@ func TestNewPrefixV1(t *testing.T) { } // Construct c2 - hash, err := mh.Sum(data, mh.SHA2_256, -1) + hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data) if err != nil { t.Fatal(err) } @@ -227,7 +226,7 @@ func TestNewPrefixV0(t *testing.T) { } // Construct c2 - hash, err := mh.Sum(data, mh.SHA2_256, -1) + hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data) if err != nil { t.Fatal(err) } @@ -243,7 +242,7 @@ func TestNewPrefixV0(t *testing.T) { func TestPrefixRoundtrip(t *testing.T) { data := []byte("this is some test content") - hash, _ := mh.Sum(data, mh.SHA2_256, -1) + hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data) c := NewCidV1(DagCBOR, hash) pref := c.Prefix() @@ -272,7 +271,7 @@ func TestPrefixRoundtrip(t *testing.T) { func Test16BytesVarint(t *testing.T) { data := []byte("this is some test content") - hash, _ := mh.Sum(data, mh.SHA2_256, -1) + hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data) c := NewCidV1(1<<63, hash) _ = c.Bytes() } @@ -296,10 +295,11 @@ func TestParse(t *testing.T) { } theHash := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n" - h, err := mh.FromB58String(theHash) + h0, err := mh.FromB58String(theHash) if err != nil { t.Fatal(err) } + h := h0.Cast() assertions := [][]interface{}{ []interface{}{NewCidV0(h), theHash}, @@ -407,7 +407,7 @@ func TestJsonRoundTrip(t *testing.T) { func BenchmarkStringV1(b *testing.B) { data := []byte("this is some test content") - hash, _ := mh.Sum(data, mh.SHA2_256, -1) + hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data) cid := NewCidV1(Raw, hash) b.ReportAllocs() diff --git a/package.json b/package.json index 8b821c4..da91e17 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ "gxDependencies": [ { "author": "whyrusleeping", - "hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8", + "hash": "QmNNAKBZusgdz5GpZh5D5RvBaSYgqfXtwB7YLsjyemkcPU", "name": "go-multihash", - "version": "1.0.8" + "version": "1.2.0" }, { "author": "whyrusleeping", diff --git a/set_test.go b/set_test.go index fa553d0..8da8483 100644 --- a/set_test.go +++ b/set_test.go @@ -15,7 +15,7 @@ func makeRandomCid(t *testing.T) Cid { t.Fatal(err) } - h, err := mh.Sum(p, mh.SHA3, 4) + h, err := mh.Builder{Type: mh.SHA3, Length: 4}.Sum(p) if err != nil { t.Fatal(err) } diff --git a/varint.go b/varint.go deleted file mode 100644 index 3503612..0000000 --- a/varint.go +++ /dev/null @@ -1,32 +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 - for i, b := range buf { - if b < 0x80 { - if i > 9 || i == 9 && b > 1 { - return 0, -(i + 1) // overflow - } - return x | uint64(b)<