Skip to content

Commit 70b07e9

Browse files
committed
Update to use the new string representation of a Multihash.
1 parent 193039c commit 70b07e9

File tree

7 files changed

+34
-72
lines changed

7 files changed

+34
-72
lines changed

builder.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (p Prefix) WithCodec(c uint64) Builder {
3434
}
3535

3636
func (p V0Builder) Sum(data []byte) (Cid, error) {
37-
hash, err := mh.Sum(data, mh.SHA2_256, -1)
37+
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
3838
if err != nil {
3939
return Nil, err
4040
}
@@ -53,11 +53,7 @@ func (p V0Builder) WithCodec(c uint64) Builder {
5353
}
5454

5555
func (p V1Builder) Sum(data []byte) (Cid, error) {
56-
mhLen := p.MhLength
57-
if mhLen <= 0 {
58-
mhLen = -1
59-
}
60-
hash, err := mh.Sum(data, p.MhType, mhLen)
56+
hash, err := mh.Builder{Type: p.MhType, Length: p.MhLength}.Sum(data)
6157
if err != nil {
6258
return Nil, err
6359
}

builder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestV0Builder(t *testing.T) {
1717
}
1818

1919
// Construct c2
20-
hash, err := mh.Sum(data, mh.SHA2_256, -1)
20+
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
2121
if err != nil {
2222
t.Fatal(err)
2323
}
@@ -42,7 +42,7 @@ func TestV1Builder(t *testing.T) {
4242
}
4343

4444
// Construct c2
45-
hash, err := mh.Sum(data, mh.SHA2_256, -1)
45+
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
4646
if err != nil {
4747
t.Fatal(err)
4848
}

cid.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
135136
func 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.
141142
func 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.
360360
func (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.
440438
func (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.
465463
func (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
}

cid_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cid
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"fmt"
76
"math/rand"
@@ -46,7 +45,7 @@ func assertEqual(t *testing.T, a, b Cid) {
4645
t.Fatal("mismatch on version")
4746
}
4847

49-
if !bytes.Equal(a.Hash(), b.Hash()) {
48+
if a.Hash() != b.Hash() {
5049
t.Fatal("multihash mismatch")
5150
}
5251
}
@@ -72,7 +71,7 @@ func TestTableForV0(t *testing.T) {
7271
}
7372

7473
func TestBasicMarshaling(t *testing.T) {
75-
h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4)
74+
h, err := mh.Builder{Type: mh.SHA3}.Sum([]byte("TEST"))
7675
if err != nil {
7776
t.Fatal(err)
7877
}
@@ -98,7 +97,7 @@ func TestBasicMarshaling(t *testing.T) {
9897
}
9998

10099
func TestBasesMarshaling(t *testing.T) {
101-
h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4)
100+
h, err := mh.Builder{Type: mh.SHA3}.Sum([]byte("TEST"))
102101
if err != nil {
103102
t.Fatal(err)
104103
}
@@ -198,7 +197,7 @@ func TestNewPrefixV1(t *testing.T) {
198197
}
199198

200199
// Construct c2
201-
hash, err := mh.Sum(data, mh.SHA2_256, -1)
200+
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
202201
if err != nil {
203202
t.Fatal(err)
204203
}
@@ -227,7 +226,7 @@ func TestNewPrefixV0(t *testing.T) {
227226
}
228227

229228
// Construct c2
230-
hash, err := mh.Sum(data, mh.SHA2_256, -1)
229+
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
231230
if err != nil {
232231
t.Fatal(err)
233232
}
@@ -243,7 +242,7 @@ func TestNewPrefixV0(t *testing.T) {
243242

244243
func TestPrefixRoundtrip(t *testing.T) {
245244
data := []byte("this is some test content")
246-
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
245+
hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data)
247246
c := NewCidV1(DagCBOR, hash)
248247

249248
pref := c.Prefix()
@@ -272,7 +271,7 @@ func TestPrefixRoundtrip(t *testing.T) {
272271

273272
func Test16BytesVarint(t *testing.T) {
274273
data := []byte("this is some test content")
275-
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
274+
hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data)
276275
c := NewCidV1(1<<63, hash)
277276
_ = c.Bytes()
278277
}
@@ -296,10 +295,11 @@ func TestParse(t *testing.T) {
296295
}
297296

298297
theHash := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
299-
h, err := mh.FromB58String(theHash)
298+
h0, err := mh.FromB58String(theHash)
300299
if err != nil {
301300
t.Fatal(err)
302301
}
302+
h := h0.Cast()
303303

304304
assertions := [][]interface{}{
305305
[]interface{}{NewCidV0(h), theHash},
@@ -407,7 +407,7 @@ func TestJsonRoundTrip(t *testing.T) {
407407

408408
func BenchmarkStringV1(b *testing.B) {
409409
data := []byte("this is some test content")
410-
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
410+
hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data)
411411
cid := NewCidV1(Raw, hash)
412412

413413
b.ReportAllocs()

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"gxDependencies": [
1010
{
1111
"author": "whyrusleeping",
12-
"hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8",
12+
"hash": "QmNNAKBZusgdz5GpZh5D5RvBaSYgqfXtwB7YLsjyemkcPU",
1313
"name": "go-multihash",
14-
"version": "1.0.8"
14+
"version": "1.2.0"
1515
},
1616
{
1717
"author": "whyrusleeping",

set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func makeRandomCid(t *testing.T) Cid {
1515
t.Fatal(err)
1616
}
1717

18-
h, err := mh.Sum(p, mh.SHA3, 4)
18+
h, err := mh.Builder{Type: mh.SHA3, Length: 4}.Sum(p)
1919
if err != nil {
2020
t.Fatal(err)
2121
}

varint.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)