Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store the multihash length as an uint32. #22

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gx/lastpubver
@@ -1 +1 @@
0.7.17: QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS
0.8.0: QmRwpQU6ysjKZoTFtZ2PhajRatNVwTBR6TbfLkaGMb5xDf
31 changes: 27 additions & 4 deletions cid.go
Expand Up @@ -25,6 +25,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"strings"

mbase "github.com/multiformats/go-multibase"
Expand Down Expand Up @@ -123,6 +124,24 @@ func NewCidV1(codecType uint64, mhash mh.Multihash) *Cid {
}
}

func NewPrefixV0(mhType uint64) Prefix {
return Prefix{
MhType: mhType,
MhLength: uint32(mh.DefaultLengths[mhType]),
Version: 0,
Codec: DagProtobuf,
}
}

func NewPrefixV1(codecType uint64, mhType uint64) Prefix {
return Prefix{
MhType: mhType,
MhLength: uint32(mh.DefaultLengths[mhType]),
Version: 1,
Codec: codecType,
}
}

// Cid represents a self-describing content adressed
// identifier. It is formed by a Version, a Codec (which indicates
// a multicodec-packed content type) and a Multihash.
Expand Down Expand Up @@ -390,7 +409,7 @@ func (c *Cid) Prefix() Prefix {
dec, _ := mh.Decode(c.hash) // assuming we got a valid multiaddr, this will not error
return Prefix{
MhType: dec.Code,
MhLength: dec.Length,
MhLength: uint32(dec.Length),
Version: c.version,
Codec: c.codec,
}
Expand All @@ -404,13 +423,13 @@ type Prefix struct {
Version uint64
Codec uint64
MhType uint64
MhLength int
MhLength uint32
}

// 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.Sum(data, p.MhType, int(p.MhLength))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -461,10 +480,14 @@ func PrefixFromBytes(buf []byte) (Prefix, error) {
return Prefix{}, err
}

if mhlen > math.MaxInt32 {
return Prefix{}, errors.New("digest too long, supporting only <= 2^31-1")
}

return Prefix{
Version: vers,
Codec: codec,
MhType: mhtype,
MhLength: int(mhlen),
MhLength: uint32(mhlen),
}, nil
}
58 changes: 58 additions & 0 deletions cid_test.go
Expand Up @@ -189,6 +189,64 @@ func TestV0ErrorCases(t *testing.T) {
}
}

func TestNewPrefixV1(t *testing.T) {
data := []byte("this is some test content")

// Construct c1
prefix := NewPrefixV1(DagCBOR, mh.SHA2_256)
c1, err := prefix.Sum(data)
if err != nil {
t.Fatal(err)
}

if c1.Prefix() != prefix {
t.Fatal("prefix not preserved")
}

// Construct c2
hash, err := mh.Sum(data, mh.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}
c2 := NewCidV1(DagCBOR, hash)

if !c1.Equals(c2) {
t.Fatal("cids mismatch")
}
if c1.Prefix() != c2.Prefix() {
t.Fatal("prefixes mismatch")
}
}

func TestNewPrefixV0(t *testing.T) {
data := []byte("this is some test content")

// Construct c1
prefix := NewPrefixV0(mh.SHA2_256)
c1, err := prefix.Sum(data)
if err != nil {
t.Fatal(err)
}

if c1.Prefix() != prefix {
t.Fatal("prefix not preserved")
}

// Construct c2
hash, err := mh.Sum(data, mh.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}
c2 := NewCidV0(hash)

if !c1.Equals(c2) {
t.Fatal("cids mismatch")
}
if c1.Prefix() != c2.Prefix() {
t.Fatal("prefixes mismatch")
}
}

func TestPrefixRoundtrip(t *testing.T) {
data := []byte("this is some test content")
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -25,6 +25,6 @@
"license": "MIT",
"name": "go-cid",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.7.17"
"version": "0.8.0"
}